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); }
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); } //if receiveQueue is empty, wait a while, until server response come byte[] receiveData = (byte[])receiveQueue.Dequeue(timeout); if (receiveData != null) { resultObject = TransferUtil.byteArrayToReturnObject(receiveData); } else { Logging.LogError("receive timeout return null"); } return(resultObject); }