// TODO: logging public static async Task Ban(SausageConnection user) { try { if (!(user.Socket.Connected || MainSocket.Connected)) { return; } // user exists if (ConnectedUsers.Any(x => x.UserInfo.Guid == user.UserInfo.Guid)) { Blacklisted.Add(user.Ip.Address); PacketFormat packet = new PacketFormat(PacketOption.UserBanned) { Guid = user.UserInfo.Guid, Content = "Place-holder reason" }; Log(packet); await Task.Delay(1000); // delay for waiting on the client to recieve a message user.Disconnect(); UiCtx.Send(x => ConnectedUsers.Remove(user)); } else { MessageBox.Show("User not found", "Ban result"); } } catch (ArgumentNullException e) { MessageBox.Show($"User returned null {e}", "Exception Caught"); } }
// TODO: Add user list public static void OnUserConnect(IAsyncResult ar) { SausageConnection user; try { user = new SausageConnection(MainSocket.EndAccept(ar)); } catch (SocketException ex) { Close(); return; } catch (ObjectDisposedException ex) { return; } if (!Blacklisted.Any(x => x == user.Ip.Address)) { UiCtx.Send(x => ConnectedUsers.Add(user)); UiCtx.Send(x => Vm.ConnectedUsers = SortUsersList()); UiCtx.Send(x => Vm.Messages.Add(new ServerMessage($"{user} has connected"))); UiCtx.Send(x => Mw.AddTextToDebugBox($"User connected on {user.Ip}\n")); // global packet for all the users to know the user has joined PacketFormat GlobalPacket = new PacketFormat(PacketOption.UserConnected) { Guid = user.UserInfo.Guid, NewName = user.UserInfo.Name }; // local packet for the user (who joined) to get his GUID PacketFormat LocalPacket = new PacketFormat(PacketOption.GetGuid) { Guid = user.UserInfo.Guid, UsersList = UsersDictionary.ToArray() }; UsersDictionary.Add(user.UserInfo); user.SendAsync(LocalPacket); Log(GlobalPacket, user); } else { // doesn't log if the user is blacklisted user.Disconnect(); } MainSocket.BeginAccept(OnUserConnect, null); }