Пример #1
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                var count = socket.EndReceive(ar);

                if (count <= 0)
                {
                    OnConnectionClosed();
                    return;
                }

                message.Size = message.ReadHead() + 2;
                int read = 2;

                while (read < message.Size)
                {
                    count = socket.Receive(message.Buffer, read, message.Size - read, SocketFlags.None);

                    if (count <= 0)
                    {
                        OnConnectionClosed();
                        return;
                    }

                    read += count;
                }

                ParseMessage();
                BeginReceive();
            }
            catch (Exception e)
            {
                Console.WriteLine("[Error] Can't process client message. Details: " + e.Message);
                OnConnectionClosed();
            }
        }