Example #1
0
        private void InboundProc(InboundProcStartSequenceState state)
        {
            Thread.CurrentThread.Name = "ZmqTransport.InboundProc";
            _logger.DebugFormat("Starting inbound proc...");

            var inboundSocket = CreateInboundSocket(state);

            if (inboundSocket == null)
            {
                return;
            }

            using (inboundSocket)
            {
                var inputBuffer = new MutableMemoryStream();
                while (IsListening)
                {
                    if (!inboundSocket.TryReceive(inputBuffer))
                    {
                        continue;
                    }

                    DeserializeAndForwardTransportMessage(inputBuffer);
                }

                GracefullyDisconnectInboundSocket(inboundSocket, inputBuffer);
            }

            _logger.InfoFormat("InboundProc terminated");
        }
Example #2
0
        public bool TryReceive(MutableMemoryStream inputBuffer, TimeSpan?timeout = null)
        {
            int size;

            _socket.ReceiveTimeout = timeout.HasValue ? timeout.Value : _options.ReadTimeout;
            _readBuffer            = _socket.Receive(_readBuffer, TimeSpan.MaxValue, out size);

            if (size <= 0)
            {
                return(false);
            }

            inputBuffer.SetBuffer(_readBuffer, 0, size);

            return(true);
        }
Example #3
0
        private void GracefullyDisconnectInboundSocket(ZmqInboundSocket inboundSocket, MutableMemoryStream inputBuffer)
        {
            inboundSocket.Disconnect();

            while (inboundSocket.TryReceive(inputBuffer, 100.Milliseconds()))
            {
                DeserializeAndForwardTransportMessage(inputBuffer);
            }
        }
Example #4
0
        /* ===============================================================
         *                 External Serialisation Methods
         * ===============================================================
         */

        /**
         * Write out our header, and our children.
         * @param headerA the first byte of the header
         * @param headerB the second byte of the header
         * @param type the record type
         * @param children our child records
         * @param out the stream to write to
         */
        public void WriteOut(byte headerA, byte headerB, long type, Record[] children, Stream out1)
        {
            // If we have a mutable output stream, take advantage of that
            if (out1 is MutableMemoryStream)
            {
                MutableMemoryStream mout =
                    (MutableMemoryStream)out1;

                // Grab current size
                int oldSize = mout.GetBytesWritten();

                // Write out our header, less the size
                mout.Write(new byte[] { headerA, headerB });
                byte[] typeB = new byte[2];
                LittleEndian.PutShort(typeB, (short)type);
                mout.Write(typeB);
                mout.Write(new byte[4]);

                // Write out the children
                for (int i = 0; i < children.Length; i++)
                {
                    children[i].WriteOut(mout);
                }

                // Update our header with the size
                // Don't forget to knock 8 more off, since we don't include the
                //  header in the size
                int    length = mout.GetBytesWritten() - oldSize - 8;
                byte[] size   = new byte[4];
                LittleEndian.PutInt(size, 0, length);
                mout.OverWrite(size, oldSize + 4);
            }
            else
            {
                // Going to have to do it a slower way, because we have
                // to update the length come the end

                // Create a MemoryStream to hold everything in
                MemoryStream baos = new MemoryStream();

                // Write out our header, less the size
                baos.Write(new byte[] { headerA, headerB }, 0, 2);
                byte[] typeB = new byte[2];
                LittleEndian.PutShort(typeB, (short)type);
                baos.Write(typeB, 2, 2);
                baos.Write(new byte[] { 0, 0, 0, 0 }, 4, 4);

                // Write out our children
                for (int i = 0; i < children.Length; i++)
                {
                    children[i].WriteOut(baos);
                }

                // Grab the bytes back
                byte[] toWrite = baos.ToArray();

                // Update our header with the size
                // Don't forget to knock 8 more off, since we don't include the
                //  header in the size
                LittleEndian.PutInt(toWrite, 4, (toWrite.Length - 8));

                // Write out the bytes
                out1.Write(toWrite, (int)out1.Position, toWrite.Length);
            }
        }