private void HeaderReceivedCallBack(IAsyncResult result)
        {
            ITransferContext context = (ITransferContext)result.AsyncState;
            int receive = context.Listener.EndReceive(result);

            if (receive > 0)
            {
                context.IncomingHeader(receive);
            }

            if (context.DeserialPacketSize > 0)
            {
                int buffersize = (context.DeserialPacketSize < context.BufferSize) ? (int)context.DeserialPacketSize : context.BufferSize;
                context.Listener.BeginReceive(context.HeaderBuffer, 0, buffersize, SocketFlags.None, HeaderReceivedCallBack, context);
            }
            else
            {
                TransferOperation request = new TransferOperation(context.Transfer, MessagePart.Header, DirectionType.Receive);
                request.Resolve(context.DeserialPacket);

                if (!context.ReceiveMessage &&
                    !context.SendMessage)
                {
                    context.Close = true;
                }

                context.HeaderReceivedNotice.Set();
                HeaderReceived.Execute(this);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// The DealHeaderReceived.
        /// </summary>
        /// <param name="context">The context<see cref="ITransferContext"/>.</param>
        public void DealHeaderReceived(ITransferContext context)
        {
            if (context.BlockSize > 0)
            {
                int buffersize = (context.BlockSize < context.BufferSize) ? (int)context.BlockSize : context.BufferSize;
                context.Listener.BeginReceive(context.HeaderBuffer, 0, buffersize, SocketFlags.None, HeaderReceivedCallback, context);
            }
            else
            {
                TransferOperation request = new TransferOperation(context.Transfer, MessagePart.Header, DirectionType.Receive);
                request.Resolve(context);

                context.HeaderReceivedNotice.Set();

                try
                {
                    HeaderReceived.Execute(context);
                }
                catch (Exception ex)
                {
                    Echo(ex.Message);
                    CloseClient(context.Id);
                }
            }
        }