public void Process() { try { Stream = client.GetStream(); //Gets stream InitializeUserData(); //Gets userData while (true) { try { var message = GetMessage(); //While stream is available lets read stream if (message.Length > 0) //If message is not empty { var msg = IMessageDeserializable.Parse(message); switch (msg.PostCode) { case { } i when(i >= 1 && i <= 4): { server.Notification(); server.BroadcastMessage(msg, Id); //If this is regular message then broadcast it break; } case 6: //if client updates his UserData { var userDataMessage = msg as UserDataMessage; if (userDataMessage?.Method == Method.Send) { user = new User(userDataMessage?.Sender.UserName, userDataMessage.Sender.Color); //Update UserData on server } break; } case 7: { var idMessage = msg as IDMessage; if (idMessage?.Method == Method.Get) { var sendMessage = new IDMessage(Method.Send, Id); Stream.Write(sendMessage.Serialize()); } break; } case 9: //If user Disconnecting { server.BroadcastMessage(msg, Id); //Broadcast it server.RemoveConnection(Id); //And remove connection break; } default: { continue; } } } } catch { var disconnectionMsg = new ConnectionMessage(Connection.Disconnect, user); //If there is error, disconnect this user server.BroadcastMessage(disconnectionMsg, Id); break; } } } catch (Exception e) { Console.WriteLine(e.Message); } finally { server.RemoveConnection(Id); Close(); } }
private void SendId() { var msg = new IDMessage(Method.Send, Id); Stream.Write(msg.Serialize()); }