/// <summary> /// Listens for incoming messages on the <see cref="NetworkStream" />. /// Fires a <see cref="MessageReceived" /> event when a new <see cref="IMessage" /> has been received. /// </summary> /// <param name="clientUserId">The Id of the user the NetworkStream is linked to.</param> /// <param name="tcpClient">The stream between the Client and the Server.</param> public void ReceiveMessages(int clientUserId, TcpClient tcpClient) { using (NetworkStream networkStream = tcpClient.GetStream()) { try { while (true) { MessageIdentifier messageIdentifier = MessageIdentifierSerialiser.DeserialiseMessageIdentifier(networkStream); IMessageSerialiser serialiser = SerialiserFactory.GetSerialiser(messageIdentifier); IMessage message = serialiser.Deserialise(networkStream); OnMessageReceived(new MessageEventArgs(message)); } } catch (IOException) { Log.Info("Detected client disconnection, notifying Server of ClientDisconnection."); IMessage message = new ClientDisconnection(clientUserId); OnMessageReceived(new MessageEventArgs(message)); } } }
private IMessage GetConnectionIMessage() { MessageIdentifier messageIdentifier = MessageIdentifierSerialiser.DeserialiseMessageIdentifier(serverConnection.GetStream()); IMessageSerialiser serialiser = SerialiserFactory.GetSerialiser(messageIdentifier); return(serialiser.Deserialise(serverConnection.GetStream())); }
private static LoginRequest GetLoginRequest(TcpClient tcpClient) { MessageIdentifier messageIdentifier = MessageIdentifierSerialiser.DeserialiseMessageIdentifier(tcpClient.GetStream()); IMessageSerialiser serialiser = SerialiserFactory.GetSerialiser(messageIdentifier); var loginRequest = (LoginRequest)serialiser.Deserialise(tcpClient.GetStream()); return(loginRequest); }