private void HandleTextFromClient(string readTextFromClient, TcpClient clientThatConnected) { if (readTextFromClient != Common.UnicodeIdentifierCharacter) { CommunicationMessage receivedCommunicationMessage = new CommunicationMessage(readTextFromClient, Common.CommunicationEventType.TextReceived); if (receivedCommunicationMessage.MessageType == (int)Common.CommunicationMessageType.Login) { AddAuthenticatedClient(receivedCommunicationMessage, clientThatConnected); } OnRaiseTextReceivedEvent(receivedCommunicationMessage); } }
public void SendToAuthenticatedClient(CommunicationMessage communicationMessage) { byte[] textMessageAsBytes; try { AuthenticatedTcpClient clientToSendTo; textMessageAsBytes = Encoding.Unicode.GetBytes(CryptographyService.EncryptForNetwork(communicationMessage.BuildCommunicationMessageText())); clientToSendTo = allAuthenticatedClients.First(AuthenticatedTcpClient => AuthenticatedTcpClient.ClientID == communicationMessage.RecipientID); clientToSendTo.tcpClient.GetStream().WriteAsync(textMessageAsBytes, 0, textMessageAsBytes.Length); Array.Clear(textMessageAsBytes, 0, textMessageAsBytes.Length); } catch (Exception ex) { string s = ex.Message; } }
private void AddAuthenticatedClient(CommunicationMessage receivedCommunicationMessage, TcpClient clientThatConnected) { if (receivedCommunicationMessage.MessageType == (int)Common.CommunicationMessageType.Login) { Common.CommunicationAuthenticationData communicationAuthenticationData; communicationAuthenticationData.ID = receivedCommunicationMessage.SenderID; communicationAuthenticationData.Authentication = receivedCommunicationMessage.SenderAuthentication; communicationAuthenticationData.Type = (Common.CommunicationClientType)receivedCommunicationMessage.SenderType; AuthenticatedTcpClient authenticatedTcpClient = new AuthenticatedTcpClient(communicationAuthenticationData); if (authenticatedTcpClient.IsAuthenticated()) { allConnectedClients.Remove(clientThatConnected); authenticatedTcpClient.tcpClient = clientThatConnected; allAuthenticatedClients.Add(authenticatedTcpClient); } } }
protected virtual void OnRaisePeerConnectedEvent(CommunicationMessage clientEventArguments) { RaiseServerConnectedEvent?.Invoke(this, clientEventArguments); }
protected virtual void OnRaiseTextReceivedEvent(CommunicationMessage clientEventArguments) { RaiseTextReceivedEvent?.Invoke(this, clientEventArguments); }
protected virtual void OnRaiseClientAuthenticatedEvent(CommunicationMessage serverEventArguments) { RaiseClientAuthenticatedEvent?.Invoke(this, serverEventArguments); }
public void SendToAllAuthenticatedClients(CommunicationMessage communicationMessage) { }