private void TryChangeNick(string nick, Human human, TcpClient client) { if (_humans.Any(hum => hum.Nick == nick)) { // AddMessageToQueue(new Message("System", // DateTime.Now, // Message.PackType.Post, // "Nick is already occupied")); #region TcpWorks.SendObjectOnce(new Message("System", DateTime.Now, Message.PackType.Post, "Nick is already occupied"), client); #endregion } else { human.Nick = nick; // AddMessageToQueue(new Message("System", // DateTime.Now, // Message.PackType.Nick, // nick)); #region TcpWorks.SendObjectOnce(new Message("System", DateTime.Now, Message.PackType.Nick, nick), client); #endregion UpdateRoomBroadcast(); } }
public void Send(Message message) { try { TcpWorks.SendObjectOnce(message, Client); } catch (Exception e) { MessageBox.Show(e.Message); } }
private bool PerformHandshake() { TcpWorks.SendObjectOnce("handshake", Client); var answer = (string)TcpWorks.ReciveObjectOnce(Client); if (answer == "handshake") { return(true); } MessageBox.Show("Handshake didn't performed."); return(false); }
public TCPClient(string server, string port) { int servPort; if (!int.TryParse(port, out servPort)) { servPort = TcpWorks.DefaultPort; } Client = new TcpClient(server, servPort); if (!PerformHandshake()) { return; } TcpWorks.ReciveMessagesLoop(Client, PerformRecivedMessage); }
private void BroadcastMessages() { while (true) { NewMessegeEvent.Reset(); Message mess; _messages.TryDequeue(out mess); if (mess != null) { foreach (var human in _humans) { TcpWorks.SendObjectOnce(mess, human.Client); } } NewMessegeEvent.WaitOne(); } }
private void ReplyToHandshake(TcpClient client) { string handshake; try { handshake = (string)TcpWorks.ReciveObjectOnce(client); Console.WriteLine("Handshake recived"); } catch (Exception e) { Console.WriteLine(e.Message); return; } TcpWorks.SendObjectOnce(handshake, client); Console.WriteLine("Handshake replyed"); }
private void LetClientJoin(IAsyncResult ar) { Console.WriteLine("Handling new client"); var client = _listener.EndAcceptTcpClient(ar); ReplyToHandshake(client); var nick = GenerateNick(); _humans.AddLast(new Human(client, nick)); Console.WriteLine("New client added"); TcpWorks.SendObjectOnce(new Message(nick, DateTime.Now, Message.PackType.Nick, nick), client); Console.WriteLine("New client name \"" + nick + "\" sended"); UpdateRoomBroadcast(); TcpWorks.ReciveMessagesLoop(client, DealWithRecieved); Console.WriteLine("Now waiting for " + nick + " messages."); WaitForClient(); }