Ejemplo n.º 1
0
        private void ReceiveMessage()
        {
            while (receiveMessage)
            {
                try
                {
                    var message = GetMessage();

                    if (message == null || message.Length <= 0)
                    {
                        continue;
                    }
                    var msg = IMessageDeserializable.Parse(message); //Lets get it
                    ParseMessage(msg);                               //And parse
                }
                catch (ThreadInterruptedException) { return; }
                catch (System.IO.IOException) { return; }       //If Thread was interrupted or something
                catch (Exception e)
                {
                    Console.WriteLine("Can't receive message: " + e.Message);   //If something went wrong
                }
            }
        }
Ejemplo n.º 2
0
        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();
            }
        }