From 06af94e94f6c3313bc1561aaaba1b0a3a0f99a8c Mon Sep 17 00:00:00 2001 From: flashwave Date: Thu, 16 Feb 2023 22:25:41 +0100 Subject: [PATCH] Renamed 'Sessions' to 'Connections' --- SharpChat/ChatCommandContext.cs | 10 +-- .../{ChatUserSession.cs => ChatConnection.cs} | 26 +++--- SharpChat/ChatContext.cs | 30 +++---- SharpChat/ChatPacketHandlerContext.cs | 6 +- SharpChat/ChatUser.cs | 39 +++++---- SharpChat/Commands/KickBanCommand.cs | 2 +- SharpChat/Commands/ShutdownRestartCommand.cs | 6 +- SharpChat/Packet/AuthSuccessPacket.cs | 6 +- SharpChat/PacketHandlers/AuthHandler.cs | 50 +++++------ SharpChat/PacketHandlers/PingHandler.cs | 6 +- .../PacketHandlers/SendMessageHandler.cs | 6 +- SharpChat/SockChatServer.cs | 82 +++++++++---------- 12 files changed, 134 insertions(+), 135 deletions(-) rename SharpChat/{ChatUserSession.cs => ChatConnection.cs} (68%) diff --git a/SharpChat/ChatCommandContext.cs b/SharpChat/ChatCommandContext.cs index fb0fa0f..2ac1c37 100644 --- a/SharpChat/ChatCommandContext.cs +++ b/SharpChat/ChatCommandContext.cs @@ -7,14 +7,14 @@ namespace SharpChat { public string[] Args { get; } public ChatContext Chat { get; } public ChatUser User { get; } - public ChatUserSession Session { get; } + public ChatConnection Connection { get; } public ChatChannel Channel { get; } public ChatCommandContext( string text, ChatContext chat, ChatUser user, - ChatUserSession session, + ChatConnection connection, ChatChannel channel ) { if(text == null) @@ -22,7 +22,7 @@ namespace SharpChat { Chat = chat ?? throw new ArgumentNullException(nameof(chat)); User = user ?? throw new ArgumentNullException(nameof(user)); - Session = session ?? throw new ArgumentNullException(nameof(session)); + Connection = connection ?? throw new ArgumentNullException(nameof(connection)); Channel = channel ?? throw new ArgumentNullException(nameof(channel)); string[] parts = text[1..].Split(' '); @@ -35,14 +35,14 @@ namespace SharpChat { string[] args, ChatContext chat, ChatUser user, - ChatUserSession session, + ChatConnection connection, ChatChannel channel ) { Name = name ?? throw new ArgumentNullException(nameof(name)); Args = args ?? throw new ArgumentNullException(nameof(args)); Chat = chat ?? throw new ArgumentNullException(nameof(chat)); User = user ?? throw new ArgumentNullException(nameof(user)); - Session = session ?? throw new ArgumentNullException(nameof(session)); + Connection = connection ?? throw new ArgumentNullException(nameof(connection)); Channel = channel ?? throw new ArgumentNullException(nameof(channel)); } diff --git a/SharpChat/ChatUserSession.cs b/SharpChat/ChatConnection.cs similarity index 68% rename from SharpChat/ChatUserSession.cs rename to SharpChat/ChatConnection.cs index d4724f4..9d5e6c4 100644 --- a/SharpChat/ChatUserSession.cs +++ b/SharpChat/ChatConnection.cs @@ -4,8 +4,8 @@ using System.Collections.Generic; using System.Net; namespace SharpChat { - public class ChatUserSession : IDisposable { - public const int ID_LENGTH = 32; + public class ChatConnection : IDisposable { + public const int ID_LENGTH = 20; #if DEBUG public static TimeSpan SessionTimeOut { get; } = TimeSpan.FromMinutes(1); @@ -13,7 +13,7 @@ namespace SharpChat { public static TimeSpan SessionTimeOut { get; } = TimeSpan.FromMinutes(5); #endif - public IWebSocketConnection Connection { get; } + public IWebSocketConnection Socket { get; } public string Id { get; private set; } public bool IsDisposed { get; private set; } @@ -27,11 +27,11 @@ namespace SharpChat { public IPAddress RemoteAddress { get { if(_RemoteAddress == null) { - if((Connection.ConnectionInfo.ClientIpAddress == "127.0.0.1" || Connection.ConnectionInfo.ClientIpAddress == "::1") - && Connection.ConnectionInfo.Headers.ContainsKey("X-Real-IP")) - _RemoteAddress = IPAddress.Parse(Connection.ConnectionInfo.Headers["X-Real-IP"]); + if((Socket.ConnectionInfo.ClientIpAddress == "127.0.0.1" || Socket.ConnectionInfo.ClientIpAddress == "::1") + && Socket.ConnectionInfo.Headers.ContainsKey("X-Real-IP")) + _RemoteAddress = IPAddress.Parse(Socket.ConnectionInfo.Headers["X-Real-IP"]); else - _RemoteAddress = IPAddress.Parse(Connection.ConnectionInfo.ClientIpAddress); + _RemoteAddress = IPAddress.Parse(Socket.ConnectionInfo.ClientIpAddress); } return _RemoteAddress; @@ -39,13 +39,13 @@ namespace SharpChat { } } - public ChatUserSession(IWebSocketConnection ws) { - Connection = ws; + public ChatConnection(IWebSocketConnection sock) { + Socket = sock; Id = RNG.SecureRandomString(ID_LENGTH); } public void Send(IServerPacket packet) { - if(!Connection.IsAvailable) + if(!Socket.IsAvailable) return; IEnumerable data = packet.Pack(); @@ -53,7 +53,7 @@ namespace SharpChat { if(data != null) foreach(string line in data) if(!string.IsNullOrWhiteSpace(line)) - Connection.Send(line); + Socket.Send(line); } public void BumpPing() { @@ -67,7 +67,7 @@ namespace SharpChat { CloseCode = 1012; } - ~ChatUserSession() { + ~ChatConnection() { DoDispose(); } @@ -81,7 +81,7 @@ namespace SharpChat { return; IsDisposed = true; - Connection.Close(CloseCode); + Socket.Close(CloseCode); } public override int GetHashCode() { diff --git a/SharpChat/ChatContext.cs b/SharpChat/ChatContext.cs index 6929c42..eede29f 100644 --- a/SharpChat/ChatContext.cs +++ b/SharpChat/ChatContext.cs @@ -11,8 +11,8 @@ namespace SharpChat { public HashSet Channels { get; } = new(); public readonly object ChannelsAccess = new(); - public HashSet Sessions { get; } = new(); - public readonly object SessionsAccess = new(); + public HashSet Connections { get; } = new(); + public readonly object ConnectionsAccess = new(); public HashSet Users { get; } = new(); public readonly object UsersAccess = new(); @@ -27,21 +27,21 @@ namespace SharpChat { public void Update() { lock(UsersAccess) foreach(ChatUser user in Users) { - IEnumerable timedOut = user.GetDeadSessions(); + IEnumerable timedOut = user.GetDeadConnections(); - foreach(ChatUserSession sess in timedOut) { - user.RemoveSession(sess); - sess.Dispose(); - Logger.Write($"Nuked session {sess.Id} from {user.Username} (timeout)"); + foreach(ChatConnection conn in timedOut) { + user.RemoveConnection(conn); + conn.Dispose(); + Logger.Write($"Nuked session {conn.Id} from {user.Username} (timeout)"); } - if(!user.HasSessions) + if(!user.HasConnections) UserLeave(null, user, UserDisconnectReason.TimeOut); } } - public ChatUserSession GetSession(IWebSocketConnection conn) { - return Sessions.FirstOrDefault(s => s.Connection == conn); + public ChatConnection GetConnection(IWebSocketConnection sock) { + return Connections.FirstOrDefault(s => s.Socket == sock); } public void BanUser(ChatUser user, TimeSpan duration, UserDisconnectReason reason = UserDisconnectReason.Kicked) { @@ -54,21 +54,21 @@ namespace SharpChat { UserLeave(user.Channel, user, reason); } - public void HandleJoin(ChatUser user, ChatChannel chan, ChatUserSession sess, int maxMsgLength) { + public void HandleJoin(ChatUser user, ChatChannel chan, ChatConnection conn, int maxMsgLength) { lock(EventsAccess) { if(!chan.HasUser(user)) { chan.Send(new UserConnectPacket(DateTimeOffset.Now, user)); Events.AddEvent(new UserConnectEvent(DateTimeOffset.Now, user, chan)); } - sess.Send(new AuthSuccessPacket(user, chan, sess, maxMsgLength)); - sess.Send(new ContextUsersPacket(chan.GetUsers(new[] { user }))); + conn.Send(new AuthSuccessPacket(user, chan, conn, maxMsgLength)); + conn.Send(new ContextUsersPacket(chan.GetUsers(new[] { user }))); foreach(IChatEvent msg in Events.GetTargetEventLog(chan.Name)) - sess.Send(new ContextMessagePacket(msg)); + conn.Send(new ContextMessagePacket(msg)); lock(ChannelsAccess) - sess.Send(new ContextChannelsPacket(Channels.Where(c => c.Rank <= user.Rank))); + conn.Send(new ContextChannelsPacket(Channels.Where(c => c.Rank <= user.Rank))); if(!chan.HasUser(user)) chan.UserJoin(user); diff --git a/SharpChat/ChatPacketHandlerContext.cs b/SharpChat/ChatPacketHandlerContext.cs index 4661017..eb8eda3 100644 --- a/SharpChat/ChatPacketHandlerContext.cs +++ b/SharpChat/ChatPacketHandlerContext.cs @@ -4,16 +4,16 @@ namespace SharpChat { public class ChatPacketHandlerContext { public string Text { get; } public ChatContext Chat { get; } - public ChatUserSession Session { get; } + public ChatConnection Connection { get; } public ChatPacketHandlerContext( string text, ChatContext chat, - ChatUserSession session + ChatConnection connection ) { Text = text ?? throw new ArgumentNullException(nameof(text)); Chat = chat ?? throw new ArgumentNullException(nameof(chat)); - Session = session ?? throw new ArgumentNullException(nameof(session)); + Connection = connection ?? throw new ArgumentNullException(nameof(connection)); } public bool CheckPacketId(string packetId) { diff --git a/SharpChat/ChatUser.cs b/SharpChat/ChatUser.cs index 677e49d..7828789 100644 --- a/SharpChat/ChatUser.cs +++ b/SharpChat/ChatUser.cs @@ -83,7 +83,7 @@ namespace SharpChat { public class ChatUser : BasicUser, IPacketTarget { public DateTimeOffset SilencedUntil { get; set; } - private readonly List Sessions = new(); + private readonly List Connections = new(); private readonly List Channels = new(); public readonly ChatRateLimiter RateLimiter = new(); @@ -98,14 +98,13 @@ namespace SharpChat { public bool IsSilenced => DateTimeOffset.UtcNow - SilencedUntil <= TimeSpan.Zero; - public bool HasSessions => Sessions.Where(c => !c.HasTimedOut && !c.IsDisposed).Any(); + public bool HasConnections => Connections.Where(c => !c.HasTimedOut && !c.IsDisposed).Any(); - public int SessionCount => Sessions.Where(c => !c.HasTimedOut && !c.IsDisposed).Count(); + public int ConnectionCount => Connections.Where(c => !c.HasTimedOut && !c.IsDisposed).Count(); - public IEnumerable RemoteAddresses => Sessions.Select(c => c.RemoteAddress); + public IEnumerable RemoteAddresses => Connections.Select(c => c.RemoteAddress); - public ChatUser() { - } + public ChatUser() {} public ChatUser(MisuzuAuthInfo auth) { UserId = auth.UserId; @@ -127,14 +126,14 @@ namespace SharpChat { } public void Send(IServerPacket packet) { - foreach(ChatUserSession conn in Sessions) + foreach(ChatConnection conn in Connections) conn.Send(packet); } public void Close() { - foreach(ChatUserSession conn in Sessions) + foreach(ChatConnection conn in Connections) conn.Dispose(); - Sessions.Clear(); + Connections.Clear(); } public void ForceChannel(ChatChannel chan = null) { @@ -166,25 +165,25 @@ namespace SharpChat { return Channels.ToList(); } - public void AddSession(ChatUserSession sess) { - if(sess == null) + public void AddConnection(ChatConnection conn) { + if(conn == null) return; - sess.User = this; + conn.User = this; - Sessions.Add(sess); + Connections.Add(conn); } - public void RemoveSession(ChatUserSession sess) { - if(sess == null) + public void RemoveConnection(ChatConnection conn) { + if(conn == null) return; - if(!sess.IsDisposed) // this could be possible - sess.User = null; + if(!conn.IsDisposed) // this could be possible + conn.User = null; - Sessions.Remove(sess); + Connections.Remove(conn); } - public IEnumerable GetDeadSessions() { - return Sessions.Where(x => x.HasTimedOut || x.IsDisposed).ToList(); + public IEnumerable GetDeadConnections() { + return Connections.Where(x => x.HasTimedOut || x.IsDisposed).ToList(); } public bool NameEquals(string name) { diff --git a/SharpChat/Commands/KickBanCommand.cs b/SharpChat/Commands/KickBanCommand.cs index 1b7708a..5e611ed 100644 --- a/SharpChat/Commands/KickBanCommand.cs +++ b/SharpChat/Commands/KickBanCommand.cs @@ -72,7 +72,7 @@ namespace SharpChat.Commands { await Misuzu.CreateBanAsync( banUser.UserId.ToString(), banUser.RemoteAddresses.First().ToString(), - ctx.User.UserId.ToString(), ctx.Session.RemoteAddress.ToString(), + ctx.User.UserId.ToString(), ctx.Connection.RemoteAddress.ToString(), duration, banReason ); diff --git a/SharpChat/Commands/ShutdownRestartCommand.cs b/SharpChat/Commands/ShutdownRestartCommand.cs index fb43b71..75a28e0 100644 --- a/SharpChat/Commands/ShutdownRestartCommand.cs +++ b/SharpChat/Commands/ShutdownRestartCommand.cs @@ -27,9 +27,9 @@ namespace SharpChat.Commands { return; if(ctx.NameEquals("restart")) - lock(ctx.Chat.SessionsAccess) - foreach(ChatUserSession sess in ctx.Chat.Sessions) - sess.PrepareForRestart(); + lock(ctx.Chat.ConnectionsAccess) + foreach(ChatConnection conn in ctx.Chat.Connections) + conn.PrepareForRestart(); ctx.Chat.Update(); WaitHandle?.Set(); diff --git a/SharpChat/Packet/AuthSuccessPacket.cs b/SharpChat/Packet/AuthSuccessPacket.cs index 3b07454..e614f54 100644 --- a/SharpChat/Packet/AuthSuccessPacket.cs +++ b/SharpChat/Packet/AuthSuccessPacket.cs @@ -6,18 +6,18 @@ namespace SharpChat.Packet { public class AuthSuccessPacket : ServerPacket { public ChatUser User { get; private set; } public ChatChannel Channel { get; private set; } - public ChatUserSession Session { get; private set; } + public ChatConnection Connection { get; private set; } public int MaxMessageLength { get; private set; } public AuthSuccessPacket( ChatUser user, ChatChannel channel, - ChatUserSession sess, + ChatConnection connection, int maxMsgLength ) { User = user ?? throw new ArgumentNullException(nameof(user)); Channel = channel ?? throw new ArgumentNullException(nameof(channel)); - Session = sess ?? throw new ArgumentNullException(nameof(channel)); + Connection = connection ?? throw new ArgumentNullException(nameof(connection)); MaxMessageLength = maxMsgLength; } diff --git a/SharpChat/PacketHandlers/AuthHandler.cs b/SharpChat/PacketHandlers/AuthHandler.cs index f3193e9..58e5e59 100644 --- a/SharpChat/PacketHandlers/AuthHandler.cs +++ b/SharpChat/PacketHandlers/AuthHandler.cs @@ -35,15 +35,15 @@ namespace SharpChat.PacketHandlers { string authMethod = args.ElementAtOrDefault(1); if(string.IsNullOrWhiteSpace(authMethod)) { - ctx.Session.Send(new AuthFailPacket(AuthFailReason.AuthInvalid)); - ctx.Session.Dispose(); + ctx.Connection.Send(new AuthFailPacket(AuthFailReason.AuthInvalid)); + ctx.Connection.Dispose(); return; } string authToken = args.ElementAtOrDefault(2); if(string.IsNullOrWhiteSpace(authToken)) { - ctx.Session.Send(new AuthFailPacket(AuthFailReason.AuthInvalid)); - ctx.Session.Dispose(); + ctx.Connection.Send(new AuthFailPacket(AuthFailReason.AuthInvalid)); + ctx.Connection.Dispose(); return; } @@ -55,14 +55,14 @@ namespace SharpChat.PacketHandlers { Task.Run(async () => { MisuzuAuthInfo fai; - string ipAddr = ctx.Session.RemoteAddress.ToString(); + string ipAddr = ctx.Connection.RemoteAddress.ToString(); try { fai = await Misuzu.AuthVerifyAsync(authMethod, authToken, ipAddr); } catch(Exception ex) { - Logger.Write($"<{ctx.Session.Id}> Failed to authenticate: {ex}"); - ctx.Session.Send(new AuthFailPacket(AuthFailReason.AuthInvalid)); - ctx.Session.Dispose(); + Logger.Write($"<{ctx.Connection.Id}> Failed to authenticate: {ex}"); + ctx.Connection.Send(new AuthFailPacket(AuthFailReason.AuthInvalid)); + ctx.Connection.Dispose(); #if DEBUG throw; #else @@ -71,9 +71,9 @@ namespace SharpChat.PacketHandlers { } if(!fai.Success) { - Logger.Debug($"<{ctx.Session.Id}> Auth fail: {fai.Reason}"); - ctx.Session.Send(new AuthFailPacket(AuthFailReason.AuthInvalid)); - ctx.Session.Dispose(); + Logger.Debug($"<{ctx.Connection.Id}> Auth fail: {fai.Reason}"); + ctx.Connection.Send(new AuthFailPacket(AuthFailReason.AuthInvalid)); + ctx.Connection.Dispose(); return; } @@ -81,9 +81,9 @@ namespace SharpChat.PacketHandlers { try { fbi = await Misuzu.CheckBanAsync(fai.UserId.ToString(), ipAddr); } catch(Exception ex) { - Logger.Write($"<{ctx.Session.Id}> Failed auth ban check: {ex}"); - ctx.Session.Send(new AuthFailPacket(AuthFailReason.AuthInvalid)); - ctx.Session.Dispose(); + Logger.Write($"<{ctx.Connection.Id}> Failed auth ban check: {ex}"); + ctx.Connection.Send(new AuthFailPacket(AuthFailReason.AuthInvalid)); + ctx.Connection.Dispose(); #if DEBUG throw; #else @@ -92,9 +92,9 @@ namespace SharpChat.PacketHandlers { } if(fbi.IsBanned && !fbi.HasExpired) { - Logger.Write($"<{ctx.Session.Id}> User is banned."); - ctx.Session.Send(new AuthFailPacket(AuthFailReason.Banned, fbi)); - ctx.Session.Dispose(); + Logger.Write($"<{ctx.Connection.Id}> User is banned."); + ctx.Connection.Send(new AuthFailPacket(AuthFailReason.Banned, fbi)); + ctx.Connection.Dispose(); return; } @@ -109,28 +109,28 @@ namespace SharpChat.PacketHandlers { } // Enforce a maximum amount of connections per user - if(aUser.SessionCount >= MaxConnections) { - ctx.Session.Send(new AuthFailPacket(AuthFailReason.MaxSessions)); - ctx.Session.Dispose(); + if(aUser.ConnectionCount >= MaxConnections) { + ctx.Connection.Send(new AuthFailPacket(AuthFailReason.MaxSessions)); + ctx.Connection.Dispose(); return; } // Bumping the ping to prevent upgrading - ctx.Session.BumpPing(); + ctx.Connection.BumpPing(); - aUser.AddSession(ctx.Session); + aUser.AddConnection(ctx.Connection); - ctx.Session.Send(new LegacyCommandResponse(LCR.WELCOME, false, $"Welcome to Flashii Chat, {aUser.Username}!")); + ctx.Connection.Send(new LegacyCommandResponse(LCR.WELCOME, false, $"Welcome to Flashii Chat, {aUser.Username}!")); if(File.Exists("welcome.txt")) { IEnumerable lines = File.ReadAllLines("welcome.txt").Where(x => !string.IsNullOrWhiteSpace(x)); string line = lines.ElementAtOrDefault(RNG.Next(lines.Count())); if(!string.IsNullOrWhiteSpace(line)) - ctx.Session.Send(new LegacyCommandResponse(LCR.WELCOME, false, line)); + ctx.Connection.Send(new LegacyCommandResponse(LCR.WELCOME, false, line)); } - ctx.Chat.HandleJoin(aUser, DefaultChannel, ctx.Session, MaxMessageLength); + ctx.Chat.HandleJoin(aUser, DefaultChannel, ctx.Connection, MaxMessageLength); } }).Wait(); } diff --git a/SharpChat/PacketHandlers/PingHandler.cs b/SharpChat/PacketHandlers/PingHandler.cs index 13bb6df..49d0f9a 100644 --- a/SharpChat/PacketHandlers/PingHandler.cs +++ b/SharpChat/PacketHandlers/PingHandler.cs @@ -26,15 +26,15 @@ namespace SharpChat.PacketHandlers { if(!int.TryParse(parts.FirstOrDefault(), out int pTime)) return; - ctx.Session.BumpPing(); - ctx.Session.Send(new PongPacket(ctx.Session.LastPing)); + ctx.Connection.BumpPing(); + ctx.Connection.Send(new PongPacket(ctx.Connection.LastPing)); lock(BumpAccess) { if(LastBump < DateTimeOffset.UtcNow - BumpInterval) { (string, string)[] bumpList; lock(ctx.Chat.UsersAccess) bumpList = ctx.Chat.Users - .Where(u => u.HasSessions && u.Status == ChatUserStatus.Online) + .Where(u => u.HasConnections && u.Status == ChatUserStatus.Online) .Select(u => (u.UserId.ToString(), u.RemoteAddresses.FirstOrDefault()?.ToString() ?? string.Empty)) .ToArray(); diff --git a/SharpChat/PacketHandlers/SendMessageHandler.cs b/SharpChat/PacketHandlers/SendMessageHandler.cs index af29630..6842b08 100644 --- a/SharpChat/PacketHandlers/SendMessageHandler.cs +++ b/SharpChat/PacketHandlers/SendMessageHandler.cs @@ -31,7 +31,7 @@ namespace SharpChat.PacketHandlers { public void Handle(ChatPacketHandlerContext ctx) { string[] args = ctx.SplitText(3); - ChatUser user = ctx.Session.User; + ChatUser user = ctx.Connection.User; // No longer concats everything after index 1 with \t, no previous implementation did that either string messageText = args.ElementAtOrDefault(2); @@ -61,13 +61,13 @@ namespace SharpChat.PacketHandlers { messageText = messageText.Trim(); #if DEBUG - Logger.Write($"<{ctx.Session.Id} {user.Username}> {messageText}"); + Logger.Write($"<{ctx.Connection.Id} {user.Username}> {messageText}"); #endif IChatMessage message = null; if(messageText.StartsWith("/")) { - ChatCommandContext context = new(messageText, ctx.Chat, user, ctx.Session, channel); + ChatCommandContext context = new(messageText, ctx.Chat, user, ctx.Connection, channel); IChatCommand command = null; diff --git a/SharpChat/SockChatServer.cs b/SharpChat/SockChatServer.cs index 4c50127..e4c3b64 100644 --- a/SharpChat/SockChatServer.cs +++ b/SharpChat/SockChatServer.cs @@ -129,32 +129,32 @@ namespace SharpChat { Logger.Write("Listening..."); } - private void OnOpen(IWebSocketConnection conn) { - Logger.Write($"Connection opened from {conn.ConnectionInfo.ClientIpAddress}:{conn.ConnectionInfo.ClientPort}"); + private void OnOpen(IWebSocketConnection sock) { + Logger.Write($"Connection opened from {sock.ConnectionInfo.ClientIpAddress}:{sock.ConnectionInfo.ClientPort}"); - lock(Context.SessionsAccess) { - if(!Context.Sessions.Any(x => x.Connection == conn)) - Context.Sessions.Add(new ChatUserSession(conn)); + lock(Context.ConnectionsAccess) { + if(!Context.Connections.Any(x => x.Socket == sock)) + Context.Connections.Add(new ChatConnection(sock)); } Context.Update(); } - private void OnClose(IWebSocketConnection conn) { - Logger.Write($"Connection closed from {conn.ConnectionInfo.ClientIpAddress}:{conn.ConnectionInfo.ClientPort}"); + private void OnClose(IWebSocketConnection sock) { + Logger.Write($"Connection closed from {sock.ConnectionInfo.ClientIpAddress}:{sock.ConnectionInfo.ClientPort}"); - ChatUserSession sess; - lock(Context.SessionsAccess) - sess = Context.GetSession(conn); + ChatConnection conn; + lock(Context.ConnectionsAccess) + conn = Context.GetConnection(sock); // Remove connection from user - if(sess?.User != null) { + if(conn?.User != null) { // RemoveConnection sets conn.User to null so we must grab a local copy. - ChatUser user = sess.User; + ChatUser user = conn.User; - user.RemoveSession(sess); + user.RemoveConnection(conn); - if(!user.HasSessions) + if(!user.HasConnections) Context.UserLeave(null, user); } @@ -162,59 +162,59 @@ namespace SharpChat { Context.Update(); // Remove connection from server - lock(Context.SessionsAccess) - Context.Sessions.Remove(sess); + lock(Context.ConnectionsAccess) + Context.Connections.Remove(conn); - sess?.Dispose(); + conn?.Dispose(); } - private void OnError(IWebSocketConnection conn, Exception ex) { - string sessId; - lock(Context.SessionsAccess) { - ChatUserSession sess = Context.GetSession(conn); - sessId = sess?.Id ?? new string('0', ChatUserSession.ID_LENGTH); + private void OnError(IWebSocketConnection sock, Exception ex) { + string connId; + lock(Context.ConnectionsAccess) { + ChatConnection conn = Context.GetConnection(sock); + connId = conn?.Id ?? new string('0', ChatConnection.ID_LENGTH); } - Logger.Write($"[{sessId} {conn.ConnectionInfo.ClientIpAddress}] {ex}"); + Logger.Write($"[{connId} {sock.ConnectionInfo.ClientIpAddress}] {ex}"); Context.Update(); } - private void OnMessage(IWebSocketConnection conn, string msg) { + private void OnMessage(IWebSocketConnection sock, string msg) { Context.Update(); - ChatUserSession sess; - lock(Context.SessionsAccess) - sess = Context.GetSession(conn); + ChatConnection conn; + lock(Context.ConnectionsAccess) + conn = Context.GetConnection(sock); - if(sess == null) { - conn.Close(); + if(conn == null) { + sock.Close(); return; } // this doesn't affect non-authed connections????? - if(sess.User is not null && sess.User.HasFloodProtection) { - sess.User.RateLimiter.AddTimePoint(); + if(conn.User is not null && conn.User.HasFloodProtection) { + conn.User.RateLimiter.AddTimePoint(); - if(sess.User.RateLimiter.State == ChatRateLimitState.Kick) { + if(conn.User.RateLimiter.State == ChatRateLimitState.Kick) { Task.Run(async () => { TimeSpan duration = TimeSpan.FromSeconds(FloodKickLength); await Misuzu.CreateBanAsync( - sess.User.UserId.ToString(), sess.RemoteAddress.ToString(), + conn.User.UserId.ToString(), conn.RemoteAddress.ToString(), string.Empty, "::1", duration, "Kicked from chat for flood protection." ); - Context.BanUser(sess.User, duration, UserDisconnectReason.Flood); + Context.BanUser(conn.User, duration, UserDisconnectReason.Flood); }).Wait(); return; - } else if(sess.User.RateLimiter.State == ChatRateLimitState.Warning) - sess.User.Send(new FloodWarningPacket()); + } else if(conn.User.RateLimiter.State == ChatRateLimitState.Warning) + conn.User.Send(new FloodWarningPacket()); } - ChatPacketHandlerContext context = new(msg, Context, sess); - IChatPacketHandler handler = sess.User is null + ChatPacketHandlerContext context = new(msg, Context, conn); + IChatPacketHandler handler = conn.User is null ? GuestHandlers.FirstOrDefault(h => h.IsMatch(context)) : AuthedHandlers.FirstOrDefault(h => h.IsMatch(context)); @@ -235,9 +235,9 @@ namespace SharpChat { return; IsDisposed = true; - lock(Context.SessionsAccess) - foreach(ChatUserSession sess in Context.Sessions) - sess.Dispose(); + lock(Context.ConnectionsAccess) + foreach(ChatConnection conn in Context.Connections) + conn.Dispose(); Server?.Dispose(); HttpClient?.Dispose();