private static void ProcessServerJoinRequest(Tuple <Transmission, Connection> transmission) { Console.WriteLine($"{transmission.Item2.ConnectionID} (Connection): Processing Join Request"); var connection = transmission.Item2; var userID = transmission.Item1.SenderID.ToByteArray(); // Update connection User data // TODO: Set name if already exists in DB connection.User = new User(userID, (new Guid(userID.Skip(16).Take(16).ToArray())).ToString()); // Check to see if the User is currently connected on any other Connections var firstConnection = !ServerConnections.USER_CONNECTIONS.ContainsKey(userID); // Add Connection to relevant collections ServerConnections.Add(connection); // Send join message if this is the first Connection for this User if (firstConnection) { ServerTransmissionHandler.Send(userID, $"-------------------User: {connection.User.Name} has joined--------------------"); } // Update Connection to show succesful join connection.Joined = true; // Send success Response ServerTransmissionHandler.SendResponse(connection, Errors.Error.NoError); // Send success ServerMessage ServerTransmissionHandler.Send(connection, "SERVER: Join Successful"); // Send user count ServerMessage ServerTransmissionHandler.Send(connection, $"SERVER: {ServerConnections.USER_CONNECTIONS.Keys.Count} users currently online"); }
// Remove connection from all collections public static void Remove(Connection connection) { var userID = connection.User.ID; var connectionID = connection.ConnectionID; Connection tempConnection; ConcurrentDictionary <Guid, Connection> tempUser; // Remove the connection from the list of all connections CONNECTIONS.TryRemove(connectionID, out tempConnection); // If there is no user associated with the connection it should not be in any other collections if (userID != null) { // Remove the connection from the list of connections for this user in USER_CONNECTIONS USER_CONNECTIONS[userID].TryRemove(connectionID, out tempConnection); // Check to see if this was the last connection for this user if (USER_CONNECTIONS[userID].IsEmpty) { // If this was the last connection for this user remove the user from USER_CONNECTIONS USER_CONNECTIONS.TryRemove(connection.User.ID, out tempUser); // Send diconnect message ServerTransmissionHandler.Send(connection.User.ID, $"------------------User: {connection.User.Name} disconnected ------------------"); // Log disconnect in console Console.WriteLine($"{connection.User.Name} (User) Disconnected"); } } }
private static void LookForConnections() { var tcpListener = new TcpListener(LOCAL_ADDRESS, PORT); try { tcpListener.Start(1000); //UICONTROLLER.Display(new Message(SERVER_USER, "Starting up...")); while (true) { try { var connection = new Connection(tcpListener.AcceptTcpClient()); Console.WriteLine($"{connection.ConnectionID} (Connection): Connection Established"); connection.SessionKey = Encryption.DiffieHellman.GetSharedKey(connection.TCPClient); Console.WriteLine($"{connection.ConnectionID} (Connection): Session Key Established"); ServerConnections.Add(connection); new Task(() => { ServerTransmissionHandler.RecieveFrom(connection); }).Start(); } catch (Exception e) { Console.WriteLine("Connection failed, Error: {0}", e); } } } catch (SocketException e) { Console.WriteLine("SocketException: {0}", e); } finally { tcpListener.Stop(); //UICONTROLLER.Display(new Message(SERVER_USER, "Shutting down...")); } }
private static void ProcessChatJoinRequest(Tuple <Transmission, Connection> transmission) { var userID = transmission.Item1.SenderID.ToByteArray(); var chatID = new Guid(transmission.Item1.Request.ChatJoinRequest.ChatID.ToByteArray()); // TODO: Permissions if (!(ServerConnections.CHAT_USERS.ContainsKey(chatID) && ServerConnections.CHAT_USERS[chatID].ContainsKey(userID))) { ServerConnections.AddToChat(userID, chatID); ServerTransmissionHandler.SendResponse(transmission.Item2, Errors.Error.NoError); Console.WriteLine($"{transmission.Item2.User.Name} (User): Joined {chatID} (Chat)"); } //TODO: Error processing }
public static void StartServer() { //UICONTROLLER.Display += (x) => { System.Console.WriteLine($"<{x.Author.Name}>: {x._message}"); }; //System.Console.WriteLine(Dns.GetHostName()); Task processTransmisisons = new Task(() => { ServerTransmissionHandler.OnTransmissionRecieved(); }); processTransmisisons.Start(); Task lookingForConenctions = new Task(LookForConnections); lookingForConenctions.Start(); lookingForConenctions.Wait(); processTransmisisons.Wait(); foreach (var connection in ServerConnections.CONNECTIONS) { ServerConnections.Remove(connection.Value); } }
private static void Rebroadcast(Tuple <Transmission, Connection> transmission) { Console.WriteLine($"{transmission.Item2.ConnectionID} (Connection): Processing Rebroadcast to {transmission.Item1.Message.ChatID} (Chat)"); ServerTransmissionHandler.Send(transmission.Item1.Message.ChatID, transmission.Item1); }