示例#1
0
        private Object Receive(StateObject stateObject)
        {
            Object resultObject = null;

            //if return void, don't call socket receive
            if (stateObject.transferObject.getReturnType() == TransferObject.DATATYPE_VOID)
            {
                return(resultObject);
            }

            Socket client              = stateObject.workSocket;
            int    receiveLength       = 0;
            int    expectReceiveLength = 0;
            bool   firsttime           = true;

            while (true)
            {
                int bytesReceived = client.Receive(stateObject.buffer, 0, StateObject.BufferSize, SocketFlags.None);

                stateObject.memoryStream.Write(stateObject.buffer, 0, bytesReceived);
                receiveLength += bytesReceived;

                //check whether one server response is done or not
                if (bytesReceived < StateObject.BufferSize)
                {
                    break;
                }
                if (bytesReceived == StateObject.BufferSize)
                {
                    if (firsttime)
                    {
                        expectReceiveLength = TransferUtil.getExpectReceiveLength(stateObject.buffer);
                    }
                    //if expectReceiveLength equal receiveLength, the server response is done
                    if (expectReceiveLength == receiveLength)
                    {
                        break;
                    }
                }
                firsttime = false;
            }

            byte[] receiveData = stateObject.memoryStream.ToArray();
            resultObject = TransferUtil.byteArrayToReturnObject(receiveData);

            return(resultObject);
        }
        public void CheckOneReceiveDone(int receivedCount, byte[] receiveData)
        {
            receiveLength += receivedCount;

            //if finish receive one client request, set handle flag is true
            if (receivedCount < StateObject.BUFFER_SIZE)
            {
                handleFlag = true;
            }
            if (receivedCount == StateObject.BUFFER_SIZE)
            {
                if (firsttime)
                {
                    expectReceiveLength = TransferUtil.getExpectReceiveLength(receiveData);
                }
                //if expectReceiveLength equal receiveLength, the server response is done
                if (expectReceiveLength == receiveLength)
                {
                    handleFlag = true;
                }
            }

            firsttime = false;
        }