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 void putResponse(TcpClient client, MemoryStream ms, ResponseMsg response, SlimSerializer serializer) { var frameBegin = Consts.PACKET_DELIMITER_LENGTH; ms.Position = frameBegin; var frame = new WireFrame(WireFrame.SLIM_FORMAT, true, response.RequestID); // Write the frame var frameSize = frame.Serialize(ms); // Write the message serializer.Serialize(ms, response); var size = (int)ms.Position - frameBegin; var buffer = ms.GetBuffer(); //no stream expansion beyond this point buffer.WriteBEInt32(0, size); Binding.DumpMsg(true, response, buffer, 0, (int)ms.Position); if (size > Binding.MaxMsgSize) { Instrumentation.ServerSerializedOverMaxMsgSizeErrorEvent.Happened(Node); throw new MessageSizeException(size, Binding.MaxMsgSize, "putResponse()", serializer.BatchTypesAdded); } client.GetStream().Write(buffer, 0, (int)ms.Position); stat_MsgSent(); stat_BytesSent(size); }
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); }
private RequestMsg getRequest(TcpClient client, MemoryStream ms, SlimSerializer serializer) { var nets = client.GetStream(); var msb = ms.GetBuffer(); var frameBegin = Consts.PACKET_DELIMITER_LENGTH; SyncBinding.socketRead(nets, msb, 0, frameBegin); var size = msb.ReadBEInt32(); if (size < 1 || size > Binding.MaxMsgSize) { Instrumentation.ServerGotOverMaxMsgSizeErrorEvent.Happened(Node); // This is unrecoverable error - close the channel! throw new MessageSizeException(size, Binding.MaxMsgSize, "getRequest()", closeChannel: true); } ms.SetLength(Consts.PACKET_DELIMITER_LENGTH + size); //this may invalidate msb SyncBinding.socketRead(nets, ms.GetBuffer(), Consts.PACKET_DELIMITER_LENGTH, size); var arrivalTime = Binding.StatTimeTicks; ms.Position = Consts.PACKET_DELIMITER_LENGTH; RequestMsg result = null; WireFrame frame; try { try { frame = new WireFrame(ms); result = DoDecodeRequest(frame, ms, serializer); } catch { Instrumentation.ServerDeserializationErrorEvent.Happened(Node); throw; } } finally { Binding.DumpMsg(true, result, ms.GetBuffer(), 0, size + Consts.PACKET_DELIMITER_LENGTH); } result.__SetArrivalTimeStampTicks(arrivalTime); stat_MsgReceived(); stat_BytesReceived(size); return(result); }
private ResponseMsg getResponse() { var ms = m_MemStream; var nets = m_Client.GetStream(); var msb = ms.GetBuffer(); var frameBegin = Consts.PACKET_DELIMITER_LENGTH; SyncBinding.socketRead(nets, msb, 0, frameBegin); var size = msb.ReadBEInt32(); if (size < 1 || size > Binding.MaxMsgSize) { Instrumentation.ClientGotOverMaxMsgSizeErrorEvent.Happened(Node); // There is no recovery here - close the channel! throw new MessageSizeException(size, Binding.MaxMsgSize, "getResponse()", closeChannel: true); } ms.SetLength(frameBegin + size); //this may invalidate msb SyncBinding.socketRead(nets, ms.GetBuffer(), frameBegin, size); var arrivalTime = Binding.StatTimeTicks; ms.Position = Consts.PACKET_DELIMITER_LENGTH; ResponseMsg result = null; WireFrame frame; try { try { frame = new WireFrame(ms); result = DoDecodeResponse(frame, ms); } catch { Instrumentation.ClientDeserializationErrorEvent.Happened(Node); throw; } } finally { Binding.DumpMsg(false, result, ms.GetBuffer(), 0, size + Consts.PACKET_DELIMITER_LENGTH); } result.__SetArrivalTimeStampTicks(arrivalTime); stat_MsgReceived(); stat_BytesReceived(size); return(result); }
private int serialize(MemChunk chunk, WireFrame frame, Msg msg) { chunk.Position = sizeof(int); frame.Serialize(chunk); Binding.Serializer.Serialize(chunk, msg); var size = (int)chunk.Position; //includes 4 byte len prefix var buff = chunk.GetBuffer(); //no stream expansion beyond this point buff.WriteBEInt32(0, size); return(size); }
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)); }
/// <summary> /// Decode the ResponseMsg per fame.Format, ms.Position is set after frame /// </summary> protected virtual RequestMsg DoDecodeRequest(WireFrame frame, MemoryStream ms, ISerializer serializer) { var recv = serializer.Deserialize(ms); if (recv == null) { throw new ProtocolException(StringConsts.GLUE_UNEXPECTED_MSG_ERROR + "RequestMsg. Got <null>"); } var result = recv as RequestMsg; if (result == null) { throw new ProtocolException(StringConsts.GLUE_UNEXPECTED_MSG_ERROR + "RequestMsg"); } return(result); }
private void putRequest(RequestMsg request) { var ms = m_MemStream; var dataBegin = Consts.PACKET_DELIMITER_LENGTH; ms.Position = dataBegin; var frame = new WireFrame(WireFrame.SLIM_FORMAT, request.OneWay, request.RequestID); // Write the frame var frameSize = frame.Serialize(ms); // Write the message m_Serializer.Serialize(ms, request); var size = (int)ms.Position - dataBegin; var buffer = ms.GetBuffer(); //no stream expansion beyond this point buffer.WriteBEInt32(0, size); Binding.DumpMsg(false, request, buffer, 0, (int)ms.Position); if (size > Binding.MaxMsgSize) { Instrumentation.ClientSerializedOverMaxMsgSizeErrorEvent.Happened(Node); //do not tear the socket, however we may have added extra types to Serializer typereg that server never received //so in thatcase we do close the channel throw new MessageSizeException(size, Binding.MaxMsgSize, "putRequest(" + request.RequestID + ")", m_Serializer.BatchTypesAdded); } m_Client.GetStream().Write(buffer, 0, (int)ms.Position); stat_MsgSent(); stat_BytesSent(size); }
public void EchoResponse_SerializeDeserialize() { var frm1 = new WireFrame(FrameType.EchoResponse, 123, false, FID.Generate()); Assert.IsTrue( frm1.OneWay ); Assert.AreEqual( WireFrame.FRAME_LENGTH, frm1.Length ); var ms = new MemoryStream(); Assert.AreEqual(WireFrame.FRAME_LENGTH, frm1.Serialize(ms)); ms.Position = 0; var frm2 = new WireFrame(ms); Assert.AreEqual( frm1.Type, frm2.Type ); Assert.AreEqual( frm1.RequestID, frm2.RequestID ); Assert.AreEqual( frm1.OneWay, frm2.OneWay ); Assert.AreEqual( frm1.Length, frm2.Length ); Assert.AreEqual( frm1.Format, frm2.Format ); Assert.AreEqual( frm1.HeadersContent, frm2.HeadersContent ); Assert.IsTrue( frm2.OneWay ); }
public void Glue_SerializeDeserialize_WithHeadersWithLatinText() { var hdr = "<a><remote name='zzz'/></a>";//Latin only chars var frm1 = new WireFrame(123, false, FID.Generate(), hdr); var utfLen = WireFrame.HEADERS_ENCODING.GetByteCount( hdr ); Assert.IsTrue( utfLen == hdr.Length); Assert.AreEqual( WireFrame.FRAME_LENGTH + hdr.Length, frm1.Length ); var ms = new MemoryStream(); Assert.AreEqual(WireFrame.FRAME_LENGTH + hdr.Length, frm1.Serialize(ms)); ms.Position = 0; var frm2 = new WireFrame(ms); Assert.AreEqual( frm1.Type, frm2.Type ); Assert.AreEqual( frm1.RequestID, frm2.RequestID ); Assert.AreEqual( frm1.OneWay, frm2.OneWay ); Assert.AreEqual( frm1.Length, frm2.Length ); Assert.AreEqual( frm1.Format, frm2.Format ); Assert.AreEqual( frm1.HeadersContent, frm2.HeadersContent ); Assert.IsFalse( frm2.OneWay ); Assert.AreEqual( "zzz", frm2.Headers["remote"].AttrByName("name").Value); }
public void Glue_SerializeDeserialize_WithHeadersWithChineseText() { var hdr = "<a><remote name='久有归天愿'/></a>"; var frm1 = new WireFrame(123, false, FID.Generate(), hdr); var utfLen = WireFrame.HEADERS_ENCODING.GetByteCount( hdr ); Assert.IsTrue( utfLen > hdr.Length); Console.WriteLine("{0} has {1} byte len and {2} char len".Args(hdr, utfLen, hdr.Length) ); Assert.AreEqual( WireFrame.FRAME_LENGTH + utfLen, frm1.Length ); var ms = new MemoryStream(); Assert.AreEqual(WireFrame.FRAME_LENGTH + utfLen, frm1.Serialize(ms)); ms.Position = 0; var frm2 = new WireFrame(ms); Assert.AreEqual( frm1.Type, frm2.Type ); Assert.AreEqual( frm1.RequestID, frm2.RequestID ); Assert.AreEqual( frm1.OneWay, frm2.OneWay ); Assert.AreEqual( frm1.Length, frm2.Length ); Assert.AreEqual( frm1.Format, frm2.Format ); Assert.AreEqual( frm1.HeadersContent, frm2.HeadersContent ); Assert.IsFalse( frm2.OneWay ); Assert.AreEqual( "久有归天愿", frm2.Headers["remote"].AttrByName("name").Value); }