예제 #1
0
        public override async Task DoReceiveMessageAsync()
        {
            // receive bytes
            UdpReceiveResult udpRes;

            try
            {
                udpRes = await _udpClient.ReceiveAsync().WithCancellation(CloseToken);
            }
            catch (OperationCanceledException)
            {
                // the socket has been closed
                return;
            }

            var buf = AlternativeCompositeByteBuf.CompBuffer();

            buf.WriteBytes(udpRes.Buffer.ToSByteArray());

            var localEp = (IPEndPoint)Socket.LocalEndPoint;

            RemoteEndPoint = udpRes.RemoteEndPoint;

            var dgram = new DatagramPacket(buf, localEp, RemoteEndPoint);

            Logger.Debug("Received {0}. {1} : {2}", dgram, Convenient.ToHumanReadable(udpRes.Buffer.Length), Convenient.ToString(udpRes.Buffer));

            // execute inbound pipeline
            if (Session.IsTimedOut)
            {
                return;
            }
            Session.Read(dgram);
            Session.Reset();
        }
예제 #2
0
        public override async Task SendBytesAsync(byte[] bytes, IPEndPoint senderEp, IPEndPoint receiverEp = null)
        {
            // send bytes
            await _udpClient.SendAsync(bytes, bytes.Length, receiverEp);

            Logger.Debug("Sent UDP: Sender {0} --> Recipient {1}. {2} : {3}", senderEp, receiverEp,
                         Convenient.ToHumanReadable(bytes.Length), Convenient.ToString(bytes));
        }
예제 #3
0
        public override async Task SendBytesAsync(byte[] bytes, IPEndPoint senderEp, IPEndPoint receiverEp = null)
        {
            // send bytes
            var recvEp = _tcpClient.Client.RemoteEndPoint;
            await _tcpClient.GetStream().WriteAsync(bytes, 0, bytes.Length);

            Logger.Debug("Sent TCP: Sender {0} --> Recipient {1}. {2} : {3}", senderEp, recvEp,
                         Convenient.ToHumanReadable(bytes.Length), Convenient.ToString(bytes));
        }
예제 #4
0
            public Buffer Reply(PeerAddress sender, Buffer requestBuffer, bool complete)
            {
                Console.WriteLine("{0}.", ++_counter);
                Console.WriteLine("Request from {0}.", sender);
                Console.WriteLine("Buffer Size : {0}", Convenient.ToHumanReadable(requestBuffer.Length));

                // server returns just OK if same buffer is returned
                return(requestBuffer);
            }
예제 #5
0
        protected override async Task ProcessRequestAsync(object state)
        {
            var udpRes = (UdpReceiveResult)state;

            // prepare new session
            var buf     = AlternativeCompositeByteBuf.CompBuffer();
            var session = Pipeline.CreateNewServerSession(this);

            session.TriggerActive();

            // process content
            buf.WriteBytes(udpRes.Buffer.ToSByteArray());

            var localEp  = (IPEndPoint)_udpClient.Client.LocalEndPoint;
            var remoteEp = udpRes.RemoteEndPoint;

            var dgram = new DatagramPacket(buf, localEp, remoteEp);

            Logger.Debug("Received {0}. {1} : {2}", dgram, Convenient.ToHumanReadable(udpRes.Buffer.Length),
                         Convenient.ToString(udpRes.Buffer));

            // execute inbound pipeline
            var readRes = session.Read(dgram); // resets timeout

            if (session.IsTimedOut)
            {
                session.TriggerInactive();
                return;
            }

            // execute outbound pipeline
            var writeRes = session.Write(readRes); // resets timeout

            if (session.IsTimedOut)
            {
                session.TriggerInactive();
                return;
            }

            // send back
            var bytes = ConnectionHelper.ExtractBytes(writeRes);
            await _udpClient.SendAsync(bytes, bytes.Length, remoteEp);

            NotifyWriteCompleted(); // resets timeout
            Logger.Debug("Sent {0} : {1}", Convenient.ToHumanReadable(udpRes.Buffer.Length),
                         Convenient.ToString(udpRes.Buffer));

            session.TriggerInactive();
        }
예제 #6
0
        public override async Task DoReceiveMessageAsync()
        {
            // TODO find zero-copy way, use same buffer
            // receive bytes
            var bytesRecv = new byte[256];
            var buf       = AlternativeCompositeByteBuf.CompBuffer();

            var stream     = _tcpClient.GetStream();
            var pieceCount = 0;

            do
            {
                Array.Clear(bytesRecv, 0, bytesRecv.Length);
                buf.Clear();
                int nrBytes;
                try
                {
                    nrBytes = await stream.ReadAsync(bytesRecv, 0, bytesRecv.Length).WithCancellation(CloseToken);
                }
                catch (OperationCanceledException)
                {
                    // the socket has been closed
                    return;
                }
                buf.WriteBytes(bytesRecv.ToSByteArray(), 0, nrBytes);

                var localEp = (IPEndPoint)Socket.LocalEndPoint;
                RemoteEndPoint = (IPEndPoint)Socket.RemoteEndPoint;

                var piece = new StreamPiece(buf, localEp, RemoteEndPoint);
                Logger.Debug("[{0}] Received {1}. {2} : {3}", ++pieceCount, piece, Convenient.ToHumanReadable(nrBytes), Convenient.ToString(bytesRecv));

                // execute inbound pipeline, per piece (reset session!)
                if (Session.IsTimedOut)
                {
                    return;
                }
                Session.Read(piece);
                Session.Reset();
            } while (!IsClosed && stream.DataAvailable && !Session.IsTimedOut);
        }
예제 #7
0
        protected override async Task ProcessRequestAsync(object state)
        {
            var client = (TcpClient)state;
            var stream = client.GetStream();

            object readRes;
            var    pieceCount = 0;

            // prepare new session
            var recvBuffer = new byte[256];
            var buf        = AlternativeCompositeByteBuf.CompBuffer();
            var session    = Pipeline.CreateNewServerSession(this);

            session.TriggerActive();

            // process content
            do
            {
                // TODO find zero-copy way, use same buffer
                Array.Clear(recvBuffer, 0, recvBuffer.Length);
                buf.Clear();
                var nrBytes = await stream.ReadAsync(recvBuffer, 0, recvBuffer.Length);

                buf.WriteBytes(recvBuffer.ToSByteArray(), 0, nrBytes);

                var localEp  = (IPEndPoint)client.Client.LocalEndPoint;
                var remoteEp = (IPEndPoint)client.Client.RemoteEndPoint;

                var piece = new StreamPiece(buf, localEp, remoteEp);
                Logger.Debug("[{0}] Received {1}. {2} : {3}", ++pieceCount, piece,
                             Convenient.ToHumanReadable(nrBytes), Convenient.ToString(recvBuffer));

                // execute inbound pipeline, per piece
                readRes = session.Read(piece); // resets timeout
                session.Reset();               // resets session internals
            } while (!IsClosed && stream.DataAvailable && !session.IsTimedOut);

            if (session.IsTimedOut)
            {
                session.TriggerInactive();
                return;
            }

            // execute outbound pipeline
            var writeRes = session.Write(readRes); // resets timeout

            if (session.IsTimedOut)
            {
                session.TriggerInactive();
                return;
            }

            // send back
            var bytes = ConnectionHelper.ExtractBytes(writeRes);
            await stream.WriteAsync(bytes, 0, bytes.Length);

            NotifyWriteCompleted(); // resets timeout
            Logger.Debug("Sent {0} : {1}", Convenient.ToHumanReadable(bytes.Length), Convenient.ToString(bytes));

            session.TriggerInactive();
        }