예제 #1
0
        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();
            }
        }
예제 #2
0
 public void Send(Message message)
 {
     try
     {
         TcpWorks.SendObjectOnce(message, Client);
     }
     catch (Exception e)
     {
         MessageBox.Show(e.Message);
     }
 }
예제 #3
0
        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);
        }
예제 #4
0
        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();
            }
        }
예제 #5
0
        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");
        }
예제 #6
0
        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();
        }