public async Task <FrameData> QueryAsyncInternal(byte[] titleBytes, byte[] contentBytes, byte[] extentionBytes = null, bool throwIfErrorResponseCode = false) { if (titleBytes == null) { throw new ArgumentNullException("titleBytes"); } if (titleBytes.Length > 65535) { throw new ArgumentOutOfRangeException("titleBytes"); } _isSyncQuery = false; int messageId = GetMessageId(); var messageByteCount = FrameFormat.ComputeFrameByteCount(extentionBytes, titleBytes, contentBytes); var sendBuffer = _transport.SendBufferCache.Get(messageByteCount); FrameFormat.FillFrame(sendBuffer, extentionBytes, titleBytes, contentBytes, 0, messageId); _asyncQueryContext.Reset(messageId); CheckConnection(); //if (NetworkSettings.ServerTcpSendMode == TcpSendMode.Async) // _transport.SendAsync(sendBuffer, messageByteCount); //else _transport.Send(sendBuffer, messageByteCount); _transport.SendBufferCache.Cache(sendBuffer); var receiveData = await _asyncQueryContext.WaitForResult(messageId, NetworkSettings.ReceiveTimeout); if (receiveData == null) { _transport.Close(); throw new Exception("queryAsync failed, time is out"); } if (throwIfErrorResponseCode) { var stateCode = receiveData.StateCode; if (stateCode != (byte)ResponseCode.OK) { throw new Exception("queryAsync failed,error code:" + stateCode); } } return(receiveData); }
internal void ConstructCurrentMessage(byte[] titleBytes, byte[] contentBytes, byte[] extentionBytes = null, bool throwIfErrorResponseCode = true) { MessageId++; if (MessageId >= 100000000) { MessageId = 1; } MessageByteCount = FrameFormat.ComputeFrameByteCount(extentionBytes, titleBytes, contentBytes); SendBuffer = _sendBufferCache.Get(MessageByteCount); FrameFormat.FillFrame(SendBuffer, extentionBytes, titleBytes, contentBytes, 0, MessageId); ThrowIfErrorResponseCode = throwIfErrorResponseCode; }
private FrameData QueryInternal(byte[] titleBytes, byte[] contentBytes, byte[] extentionBytes, bool throwIfErrorResponseCode) { if (titleBytes == null) { throw new ArgumentNullException("titleBytes"); } if (titleBytes.Length > 65535) { throw new ArgumentOutOfRangeException("titleBytes"); } int messageId = GenerateMessageId(); var messageByteCount = FrameFormat.ComputeFrameByteCount(extentionBytes, titleBytes, contentBytes); var sendBuffer = _transport.SendBufferCache.Get(messageByteCount); FrameFormat.FillFrame(sendBuffer, extentionBytes, titleBytes, contentBytes, 0, messageId); TransportKeepAlive(); _syncQueryContext.Reset(messageId); _transport.Send(sendBuffer, messageByteCount); _transport.SendBufferCache.Cache(sendBuffer); var receiveData = _syncQueryContext.WaitForResult(messageId, NetworkSettings.ReceiveTimeout).Result; if (receiveData == null) { _transport.Close(); throw new RpcException("query failed,time is out"); } if (throwIfErrorResponseCode) { var stateCode = receiveData.StateCode; if (stateCode != (byte)ResponseCode.OK) { throw new RpcException("query failed,error code:" + stateCode); } } return(receiveData); }
private static async void ProcessReceive(TcpTransport serverTransport, FrameData frameData, IMessageProcessor messageProcessor) { ResponseBase response = null; if (frameData.TitleBytes == null || frameData.TitleBytes.Length == 0) { response = new ErrorResponse((byte)ResponseCode.SERVICE_TITLE_ERROR); } if (messageProcessor == null) { response = new ErrorResponse((byte)ResponseCode.SERVICE_NOT_FOUND); } try { if (response == null) { if (NetworkSettings.ServerProcessMode == CommunicationMode.Sync) { var responseTask = messageProcessor.Process(frameData); responseTask.Wait(); response = responseTask.Result; } else { response = await messageProcessor.Process(frameData); } } } catch { response = new ErrorResponse((byte)ResponseCode.SERVER_INTERNAL_ERROR); } var responseExtention = response.HeaderExtentionBytes ?? FrameFormat.EmptyBytes; var responseContent = response.ContentBytes ?? FrameFormat.EmptyBytes; var responseCode = response.Code; try { var messageByteCount = FrameFormat.ComputeFrameByteCount(responseExtention, FrameFormat.EmptyBytes, responseContent); var sendBuffer = serverTransport.SendBufferCache.Get(messageByteCount); FrameFormat.FillFrame(sendBuffer, responseExtention, FrameFormat.EmptyBytes, responseContent, responseCode, frameData.MessageId); //if (NetworkSettings.ServerTcpSendMode == TcpSendMode.Async) //{ // serverTransport.SendAsync(sendBuffer, messageByteCount); //} //else //{ serverTransport.Send(sendBuffer, messageByteCount); //} serverTransport.SendBufferCache.Cache(sendBuffer); } catch { serverTransport.Close(); } }