Ejemplo n.º 1
0
        private void putResponse(TcpClient client, MemoryStream ms, ResponseMsg response, SlimSerializer serializer)
        {
            var frameBegin = Consts.PACKET_DELIMITER_LENGTH;

            ms.Position = frameBegin;

            var frame = new WireFrame(Binding.FrameFormat, true, response.RequestID);

            // Write the frame
            var frameSize = frame.Serialize(ms);

            // Write the message
            DoEncodeResponse(ms, response, serializer);

            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);
        }
Ejemplo n.º 2
0
        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();
            }
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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(App.Instrumentation, 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(App.Instrumentation, 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);
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
        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(App.Instrumentation, 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));
        }
Ejemplo n.º 8
0
        /// <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);
        }
Ejemplo n.º 9
0
        private void putRequest(RequestMsg request)
        {
            var ms        = m_MemStream;
            var dataBegin = Consts.PACKET_DELIMITER_LENGTH;

            ms.Position = dataBegin;

            var frame = new WireFrame(Binding.FrameFormat, request.OneWay, request.RequestID);

            // Write the frame
            var frameSize = frame.Serialize(ms);

            // Write the message
            DoEncodeRequest(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(App.Instrumentation, Node);
                //do not tear the socket, however we may have added extra types to Serializer typereg that server never received
                //so in that case 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);
        }