Exemple #1
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();
        }