示例#1
0
        void OnReceive(IAsyncResult asyncResult)
        {
            ReceiveState state = (ReceiveState)asyncResult.AsyncState;

            try
            {
                int bytesReceived = state.ClientSocket.EndReceive(asyncResult);
                if (bytesReceived == 0)
                {
                    // Client closing the socket
                    state.ClientSocket.Close();
                }
                else
                {
                    if (bytesReceived < state.Count)
                    {
                        state.ClientSocket.BeginReceive(state.Buffer, state.Offset + bytesReceived, state.Count - bytesReceived, SocketFlags.None, OnReceive, state);
                    }
                    else
                    {
                        if (state.ReceivingLength)
                        {
                            int length = Formatting.BytesToSize(state.Buffer, state.Offset);
                            Console.WriteLine("Length: {0}", length);
                            state.ReceivingLength = false;
                            state.Buffer          = new byte[length];
                            state.Count           = length;
                            state.ClientSocket.BeginReceive(state.Buffer, 0, length, SocketFlags.None, OnReceive, state);
                        }
                        else
                        {
                            Debugging.PrintBytes(state.Buffer, state.Count);
                            byte[] lengthBytes = new byte[4];
                            byte[] response    = this.dispatcher.DispatchOperation(state.Buffer, state.Offset, state.Count);
                            Formatting.SizeToBytes(response.Length, lengthBytes, 0);
                            state.ClientSocket.Send(lengthBytes);
                            state.ClientSocket.Send(response);

                            ReceiveState receiveState = new ReceiveState
                            {
                                ClientSocket    = state.ClientSocket,
                                Buffer          = new byte[4],
                                Offset          = 0,
                                Count           = 4,
                                ReceivingLength = true,
                            };
                            state.ClientSocket.BeginReceive(receiveState.Buffer, 0, 4, SocketFlags.None, OnReceive, receiveState);
                        }
                    }
                }
            }
            catch (ObjectDisposedException)
            {
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
        }
        void OnReceive(IAsyncResult asyncResult)
        {
            SendReceiveState state = (SendReceiveState)asyncResult.AsyncState;

            try
            {
                int bytesReceived = state.ClientSocket.EndReceive(asyncResult);
                if (bytesReceived == 0)
                {
                    // Client closing the socket
                    state.ClientSocket.Close();
                }
                else
                {
                    if (bytesReceived < state.Count)
                    {
                        state.SendReceiveOffset += bytesReceived;
                        state.ClientSocket.BeginReceive(state.Buffer, state.SendReceiveOffset, state.Count - state.SendReceiveOffset, SocketFlags.None, OnReceive, state);
                    }
                    else
                    {
                        switch (state.ClientState)
                        {
                        case ClientState.ReceivingLength:
                            Console.WriteLine("Received {0} bytes", state.Count);
                            this.AddReceivedBytes(state);
                            int length = Formatting.BytesToSize(state.Buffer, 0);
                            Console.WriteLine("Length: {0}", length);
                            state.ClientState       = ClientState.ReceivingData;
                            state.Buffer            = new byte[length];
                            state.SendReceiveOffset = 0;
                            state.Count             = length;
                            state.ClientSocket.BeginReceive(state.Buffer, 0, length, SocketFlags.None, OnReceive, state);
                            break;

                        case ClientState.ReceivingData:
                            Console.WriteLine("Received {0} bytes", state.Count);
                            this.AddReceivedBytes(state);
                            state.ClientState       = ClientState.SendingLength;
                            state.SendReceiveOffset = 0;
                            state.Data   = state.Buffer;
                            state.Buffer = new byte[4];
                            Formatting.SizeToBytes(state.Count, state.Buffer, 0);
                            state.Count = 4;
                            state.ClientSocket.BeginSend(state.Buffer, 0, 4, SocketFlags.None, OnSend, state);
                            break;
                        }
                    }
                }
            }
            catch (ObjectDisposedException)
            {
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
        }
        ArraySegment <byte> ReadData()
        {
            // 4 bytes length
            byte[] preambleBytes = this.SocketReceiveBytes(4, false);
            if (preambleBytes == null)
            {
                return(new ArraySegment <byte>());
            }

            int dataLength = Formatting.BytesToSize(preambleBytes, 0);

            byte[] data = this.SocketReceiveBytes(dataLength);
            this.bufferManager.ReturnBuffer(preambleBytes);
            return(new ArraySegment <byte>(data, 0, dataLength));
        }
            bool CompleteDrainLength(IAsyncResult result)
            {
                this.lengthBytes = channel.EndSocketReceiveBytes(result);
                if (this.lengthBytes == null)
                {
                    this.buffer = new ArraySegment <byte>();
                    return(true);
                }

                this.dataLength = Formatting.BytesToSize(this.lengthBytes, 0);

                IAsyncResult readDataResult = channel.BeginSocketReceiveBytes(this.dataLength, OnReadData, this);

                if (!readDataResult.CompletedSynchronously)
                {
                    return(false);
                }

                return(CompleteReadData(result));
            }