Пример #1
0
        private void Receive()
        {
            //if handleFlag is true, execute
            if (handleFlag)
            {
                byte[] receiveData = mStateObject.memoryStream.ToArray();
                if (TransferUtil.isServerCall(receiveData[0]))
                {
                    //it is server call
                    ExecuteDelegate executeDelegate = delegate() {
                        handleServerCall(receiveData);
                    };
                    executeDelegate.BeginInvoke(null, null);

                    //ThreadPool.QueueUserWorkItem(handleServerCall, receiveData);
                }
                else
                {
                    //it is server response
                    receiveQueue.Enqueue(receiveData);
                }

                handleFlag          = false;
                firsttime           = true;
                expectReceiveLength = 0;
                receiveLength       = 0;

                //reset memory stream
                mStateObject.memoryStream.Position = 0;
                mStateObject.memoryStream.SetLength(0);
            }
            Socket socket = mStateObject.workSocket;

            socket.BeginReceive(mStateObject.buffer, 0, StateObject.BUFFER_SIZE, SocketFlags.None, new AsyncCallback(ReceiveCallback), mStateObject);
        }