Ejemplo n.º 1
0
        private async Task Listen()
        {
            var framer = new LengthPrefixMessageFramer(IncomingMessageArrived);

            while (_connected)
            {
                var size = 0;
                try
                {
                    size = await _connection.ReceiveAsync(_receiveBuffer, SocketFlags.None).ConfigureAwait(false);
                }
                catch (SocketException ex)
                {
                    if (ex.SocketErrorCode == SocketError.NotConnected)
                    {
                        _connected = false;
                    }
                    return;
                }
                var data = new ArraySegment <byte>(_receiveBuffer.Array, 0, size);
                framer.UnFrameData(data);
            }
        }
Ejemplo n.º 2
0
 private async Task SendMessage(TcpPackage package)
 {
     var data   = package.AsArraySegment();
     var framed = LengthPrefixMessageFramer.FrameData(data);
     await _connection.SendAsync(framed, SocketFlags.None).ConfigureAwait(false);
 }