示例#1
0
        private void ListenForData()
        {
            try
            {
                socketConnection = new TcpClient("localhost", 8052);
                Byte[] bytes = new Byte[1024];
                while (true)
                {
                    using (NetworkStream stream = socketConnection.GetStream())
                    {
                        int length;
                        while ((length = stream.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            var incomingData = new byte[length];
                            Array.Copy(bytes, 0, incomingData, 0, length);
                            var message = MessagePackSerializer.Deserialize <MsgObject>(incomingData);
                            message.Print(length);

                            switch (message.type)
                            {
                            case MsgObject.Type.Quit:
                                SendMessage(MsgObject.QuitMsg());
                                throw new SocketException();

                            case MsgObject.Type.Add:
                                MessageAction(message);
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is SocketException || ex is InvalidOperationException || ex is IOException)
                {
                    Console.WriteLine("Server not found, try again");
                    ListenForData();
                    return;
                }

                throw ex;
            }
        }
示例#2
0
 void OnDestroy()
 {
     SendMessage(MsgObject.QuitMsg());
 }