private void InviteUsersToGroup(LoggedClient loggedClient, BaseMessage baseMessage) { try { StatusNotifier inviteMessage = null; try { inviteMessage = StatusNotifier.Deserialize(baseMessage); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } if (inviteMessage != null) { if (AreUsersReadyToJoinGroup(inviteMessage)) { CreateGroupAndInviteClients(loggedClient, inviteMessage); } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } }
public void HandleRBigIntRequest(LoggedClient loggedClient, RBigInt rBigInt) { try { rBigInt.Username = loggedClient.Username; var serizlizedRBigInt = MessagesSerializer.Serialize(rBigInt); var baseMessage = new BaseMessage(MessageType.RBigInt, serizlizedRBigInt); var serizedBaseMessage = MessagesSerializer.Serialize(baseMessage); int clientsCount = loggedClients.Count; for (int i = 0; i < clientsCount; ++i) { var client = loggedClients[i]; try { client.SendMessage(serizedBaseMessage); } catch (Exception ex) { Console.WriteLine("Wrong R"); System.Diagnostics.Debug.WriteLine(ex.Message); } } Console.WriteLine("R sended"); } catch { } }
private void CreateGroupAndInviteClients(LoggedClient loggedClient, StatusNotifier inviteMessage) { lock (syncRootGroupsInvites) { var groupClientsList = new List <LoggedClient>() { loggedClient }; var invitedUsernames = inviteMessage.Usernames; for (int i = 0; i < invitedUsernames.Count; ++i) { var client = loggedClients.Where(x => x.Username == invitedUsernames[i]).FirstOrDefault(); if (client != null) { groupClientsList.Add(client); } } if (groupClientsList.Count > 1) { var group = new ClientsGroupInvite(groupClientsList); group.GroupInviteTimeout += Group_GroupInviteTimeout; group.GroupCreated += Group_GroupCreated; clientsGroupInvites.Add(group); } } }
private void HandleInviteResponse(LoggedClient loggedClient, BaseMessage baseMessage) { try { InviteResponse inviteResponse = null; try { inviteResponse = InviteResponse.Deserialize(baseMessage); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } if (inviteResponse != null) { var inviteGroup = GetClientInviteGroup(loggedClient); if (inviteGroup != null) { inviteGroup.AddInviteResponse(loggedClient.Username, inviteResponse); } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } }
private ClientsGroupInvite GetClientInviteGroup(LoggedClient loggedClient) { for (int i = 0; i < clientsGroupInvites.Count; ++i) { var clients = clientsGroupInvites[i].LoggedClients; var client = clients.Where(x => x.Username == loggedClient.Username).FirstOrDefault(); if (client != null) { return(clientsGroupInvites[i]); } } return(null); }
private void LogOutClient(LoggedClient loggedClient) { if (loggedClient != null) { Console.WriteLine("client not null..."); try { loggedClients.Remove(loggedClient); Console.WriteLine("Client logged out"); } catch (Exception ex) { Console.WriteLine("Client not logged out..."); System.Diagnostics.Debug.WriteLine(ex.Message); } } }
private void HandleMessage(LoggedClient loggedClient, BaseMessage baseMessage) { try { var allGroups = clientsGroups; if (allGroups != null) { var clientGroup = allGroups.Where(x => x.GroupContainsUser(loggedClient)).FirstOrDefault(); if (clientGroup != null && baseMessage != null) { clientGroup.HandleMessage(loggedClient, baseMessage); } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } }
public void HandleDHBigIntRequest(LoggedClient loggedClient, DHBigInt dhBigInt) { try { int clientIndex = loggedClients.IndexOf(loggedClient); dhBigInt.Username = loggedClient.Username; var serializedDHBigInt = MessagesSerializer.Serialize(dhBigInt); var baseMessage = new BaseMessage(MessageType.DHBigInt, serializedDHBigInt); var seriazlizedBaseMessage = MessagesSerializer.Serialize(baseMessage); if (clientIndex == 0) { loggedClients[loggedClients.Count - 1].SendMessage(seriazlizedBaseMessage); } else { loggedClients[clientIndex - 1].SendMessage(seriazlizedBaseMessage); } } catch { } }
private void HandleDHBigIntMessage(LoggedClient loggedClient, BaseMessage baseMessage) { try { var allGroups = clientsGroups; if (allGroups != null) { var clientGroup = allGroups.Where(x => x.GroupContainsUser(loggedClient)).FirstOrDefault(); var dhBigInt = DHBigInt.Deserialize(baseMessage); if (clientGroup != null && dhBigInt != null) { clientGroup.HandleDHBigIntRequest(loggedClient, dhBigInt); } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } }
private LoggedClient AddClient(string username, byte[] AESKey, TcpClient tcpClient) { lock (syncRootClients) { var clients = loggedClients; if (clients != null) { var clientExist = clients.Where(c => c.Username == username).FirstOrDefault(); if (clientExist == null) { var loggedClient = new LoggedClient(username, tcpClient, AESKey); loggedClient.MessageReceived += LoggedClient_MessageReceived; loggedClients.Add(loggedClient); NotifyNewClientHasConnected(loggedClient.Username); return(loggedClient); } } return(null); } }
private void HandleClientRequest(object obj) { Console.WriteLine("Client connected!"); var tcpClient = (TcpClient)obj; var clientStream = tcpClient.GetStream(); var reader = new StreamReader(clientStream); var writer = new StreamWriter(clientStream); LoggedClient loggedClient = null; try { var registerMessage = GetRegisterMessage(reader); var AESKey = AESWrapper.GetRandomBytes(); var registerResponse = GetRegisterResponse(registerMessage, AESKey); writer.WriteLine(registerResponse); writer.Flush(); Console.WriteLine("Client registered!"); loggedClient = AuthorizeUser(tcpClient, AESKey); while (running) { Console.WriteLine("While true..."); var input = reader.ReadLine(); if (input == null) { throw new ArgumentNullException("Null input"); } loggedClient.HandleIncomingRequests(input); } } catch (Exception ex) { Console.WriteLine(ex.Message); System.Diagnostics.Debug.WriteLine(ex.Message); } Console.WriteLine("logging out..."); LogOutClient(loggedClient); }
public void HandleMessage(LoggedClient loggedClient, BaseMessage message) { try { var serizedBaseMessage = MessagesSerializer.Serialize(message); int clientsCount = loggedClients.Count; for (int i = 0; i < clientsCount; ++i) { var client = loggedClients[i]; try { client.SendMessage(serizedBaseMessage); } catch (Exception ex) { Console.WriteLine("Couldn't send to client"); System.Diagnostics.Debug.WriteLine(ex.Message); } } Console.WriteLine("R sended"); } catch { } }
public bool GroupContainsUser(LoggedClient loggedClient) { return(loggedClients.Contains(loggedClient)); }