private void sendSocketResponse(MpxServerSocket socket, ResponseMsg response) { var chunk = socket.GetSendChunk(); try { var frame = new WireFrame(WireFrame.SLIM_FORMAT, true, response.RequestID); var size = serialize(chunk, frame, response); var wm = new WireMsg(chunk); Binding.DumpMsg(true, response, chunk.GetBuffer(), 0, (int)chunk.Position); if (size > Binding.MaxMsgSize) { Instrumentation.ServerSerializedOverMaxMsgSizeErrorEvent.Happened(Node); throw new MessageSizeException(size, Binding.MaxMsgSize, "sendResponse(" + response.RequestID + ")"); } socket.Send(wm); stat_MsgSent(); stat_BytesSent(size); } catch { stat_Errors(); throw; } finally { socket.ReleaseSendChunk(); } }
private RequestMsg deserialize(ref WireMsg wmsg) { var chunk = wmsg.Data; chunk.Position = sizeof(int); WireFrame frame; RequestMsg result = null; var arrivalTime = Binding.StatTimeTicks; object received = null; try { try { frame = new WireFrame(chunk); wmsg.Frame = frame; received = Binding.Serializer.Deserialize(chunk); } catch { Instrumentation.ServerDeserializationErrorEvent.Happened(Node); throw; } if (received == null) { throw new ProtocolException(StringConsts.GLUE_UNEXPECTED_MSG_ERROR + "RequestMsg. Got <null>"); } result = received as RequestMsg; if (result == null) { throw new ProtocolException(StringConsts.GLUE_UNEXPECTED_MSG_ERROR + "RequestMsg"); } stat_MsgReceived(); stat_BytesReceived(chunk.Position); } catch { stat_Errors(); throw; } finally { Binding.DumpMsg(true, received as Msg, chunk.GetBuffer(), 0, (int)chunk.Position); } result.__SetArrivalTimeStampTicks(arrivalTime); return(result); }
//called asynchronously to deliver data from server private void receiveAction(MpxSocket <MpxClientTransport> socket, WireMsg wm) { if (!Running) { return; } var response = deserialize(ref wm); Glue.ClientDeliverAsyncResponse(response); }
private void processReceiveCore(SocketState state) { var readNow = state.BytesTransferred; if (state.SocketError != SocketError.Success || readNow <= 0) { Dispose(); ReleaseMemChunksAfterAsyncOperations();//socket closed/client has disconnected return; } m_ReceiveChunk.Position += readNow; var hasSize = state.HasMsgSize; if (!hasSize && m_ReceiveChunk.Position >= sizeof(int)) { state.WireMsgSize = m_ReceiveChunk.GetBuffer().ReadBEInt32(); hasSize = true; //check max msg size if (state.WireMsgSize < 1 || state.WireMsgSize > Transport.Binding.MaxMsgSize) { Instrumentation.ServerGotOverMaxMsgSizeErrorEvent.Happened(Transport.Node); Transport.Binding.WriteLog(LogSrc.Server, Log.MessageType.Error, StringConsts.GLUE_MAX_MSG_SIZE_EXCEEDED_ERROR.Args(state.WireMsgSize, Transport.Binding.MaxMsgSize, "processReceive()"), "{0}.processRecive()".Args(GetType().Name)); // This is unrecoverable error - close the channel! Dispose(); ReleaseMemChunksAfterAsyncOperations();//socket closed return; } if (state.WireMsgSize > m_ReceiveChunk.Length) //grow chunk if it is not large enough { m_ReceiveChunk.SetLength(state.WireMsgSize); } } var got = (int)m_ReceiveChunk.Position; if (hasSize && got >= state.WireMsgSize) //got all { WireMsg msg = new WireMsg(m_ReceiveChunk); msg.UpdateBufferStats(); this.InvokeReceive(msg); initiateReceive(); return; } state.SetBuffer(m_ReceiveChunk.GetBuffer(), got, (hasSize ? state.WireMsgSize : sizeof(int)) - got); continueReceive(); }
private CallSlot sendRequest(ClientEndPoint endpoint, RequestMsg request, CallOptions options) { if (m_PriorDispatchTimeoutMs != options.DispatchTimeoutMs) { m_Client.Socket.SendTimeout = options.DispatchTimeoutMs; m_PriorDispatchTimeoutMs = options.DispatchTimeoutMs; } if (m_PriorTimeoutMs != options.TimeoutMs) { m_Client.Socket.ReceiveTimeout = options.TimeoutMs; m_PriorTimeoutMs = options.TimeoutMs; } var chunk = m_Client.GetSendChunk(); try { var frame = new WireFrame(WireFrame.SLIM_FORMAT, request.OneWay, request.RequestID); var size = serialize(chunk, frame, request); var wm = new WireMsg(chunk); Binding.DumpMsg(false, request, chunk.GetBuffer(), 0, (int)chunk.Position); if (size > Binding.MaxMsgSize) { Instrumentation.ClientSerializedOverMaxMsgSizeErrorEvent.Happened(Node); throw new MessageSizeException(size, Binding.MaxMsgSize, "sendRequest(" + request.RequestID + ")"); } m_Client.Send(wm); stat_MsgSent(); stat_BytesSent(wm.BufferUsedSize); } catch { stat_Errors(); throw; } finally { m_Client.ReleaseSendChunk(); } //regardless of (request.OneWay) we return callslot anyway return(new CallSlot(endpoint, this, request, CallStatus.Dispatched, options.TimeoutMs)); }
//called asynchronously to deliver data from client socket private void socketReceiveAction(MpxSocket <MpxServerTransport> socket, WireMsg wm) { if (!Running) { return; } var request = deserialize(ref wm); request.__setServerTransport(this); request.__SetBindingSpecificContext(socket); Glue.ServerDispatchRequest(request); }
/// <summary> /// Calls ReceiveAction callback guarding for possible unhandled receive action errors /// </summary> protected void InvokeReceive(WireMsg msg) { try { m_LastIdleManagerVisit = null;//taffic came through, reset idle timestamp m_ReceiveAction(this, msg); } catch (Exception error) { Transport.Binding.WriteLog(this is MpxServerSocket ? LogSrc.Server : LogSrc.Client, Log.MessageType.Error, StringConsts.GLUE_MPX_SOCKET_RECEIVE_ACTION_ERROR + error.ToMessageWithType(), "{0}.processRecive()".Args(GetType().Name), error); } }
protected override void DoSend(WireMsg data) { if (!Active) { throw new ProtocolException(StringConsts.GLUE_MPX_SOCKET_SEND_CLOSED_ERROR, closeChannel: true); } try { data.UpdateBufferStats(); m_Client.GetStream().Write(data.Buffer, 0, data.BufferUsedSize); } catch (Exception error) { Dispose(); throw new ProtocolException(error.Message, error, closeChannel: true); } }
protected abstract void DoSend(WireMsg data);
/// <summary> /// Adds the specified socket msg to the outgoing stack. /// This is a synchronous blocking call that executes until the data is written /// into socket stack (but not necessarily delivered). If send error occurs then socket is /// marked as !Active (Disposed) /// </summary> public void Send(WireMsg data) { m_LastIdleManagerVisit = null;//taffic came through, reset idle timestamp DoSend(data); }
private void processReceiveCore(SocketState state) { var readNow = state.BytesTransferred; if (state.SocketError != SocketError.Success || readNow <=0) { Dispose(); ReleaseMemChunksAfterAsyncOperations();//socket closed/client has disconnected return; } m_ReceiveChunk.Position+=readNow; var hasSize = state.HasMsgSize; if (!hasSize && m_ReceiveChunk.Position>=sizeof(int)) { state.WireMsgSize = m_ReceiveChunk.GetBuffer().ReadBEInt32(); hasSize = true; //check max msg size if (state.WireMsgSize < 1 || state.WireMsgSize>Transport.Binding.MaxMsgSize) { Instrumentation.ServerGotOverMaxMsgSizeErrorEvent.Happened(Transport.Node); Transport.Binding.WriteLog(LogSrc.Server, Log.MessageType.Error, StringConsts.GLUE_MAX_MSG_SIZE_EXCEEDED_ERROR.Args(state.WireMsgSize, Transport.Binding.MaxMsgSize, "processReceive()"), "{0}.processRecive()".Args(GetType().Name)); // This is unrecoverable error - close the channel! Dispose(); ReleaseMemChunksAfterAsyncOperations();//socket closed return; } if (state.WireMsgSize>m_ReceiveChunk.Length) //grow chunk if it is not large enough m_ReceiveChunk.SetLength(state.WireMsgSize); } var got = (int)m_ReceiveChunk.Position; if (hasSize && got >= state.WireMsgSize) //got all { WireMsg msg = new WireMsg(m_ReceiveChunk); msg.UpdateBufferStats(); this.InvokeReceive(msg); initiateReceive(); return; } state.SetBuffer(m_ReceiveChunk.GetBuffer(), got, (hasSize ? state.WireMsgSize : sizeof(int)) - got); continueReceive(); }
protected override void DoSend(WireMsg data) { if (!Active) throw new ProtocolException(StringConsts.GLUE_MPX_SOCKET_SEND_CLOSED_ERROR, closeChannel: true); try { data.UpdateBufferStats(); m_Client.GetStream().Write(data.Buffer, 0, data.BufferUsedSize); } catch(Exception error) { Dispose(); throw new ProtocolException(error.Message, error, closeChannel: true); } }