Beispiel #1
0
        public TcpConnecter(IOThread ioThread,
												SessionBase session, Options options,
												Address addr, bool delayedStart)
            : base(ioThread, options)
        {
            m_ioObject = new IOObject(ioThread);
            m_addr = addr;
            m_handle = null;
            m_handleValid = false;
            m_delayedStart = delayedStart;
            m_timerStarted = false;
            m_session = session;
            m_currentReconnectIvl = m_options.ReconnectIvl;

            Debug.Assert(m_addr != null);
            m_endpoint = m_addr.ToString();
            m_socket = session.Socket;
        }
Beispiel #2
0
        protected void SendAttach(SessionBase destination,
		                            IEngine engine, bool incSeqnum)
        {
            if (incSeqnum)
                destination.IncSeqnum ();

            Command cmd = new Command(destination, CommandType.Attach, engine);
            SendCommand (cmd);
        }
Beispiel #3
0
 protected void SendAttach(SessionBase destination, IEngine engine)
 {
     SendAttach(destination, engine, true);
 }
Beispiel #4
0
        public StreamEngine(Socket fd, Options options, String endpoint)
        {
            m_handle = fd;
            //      inbuf = null;
            m_insize = 0;
            m_inputError = false;
            //        outbuf = null;
            m_outsize = 0;
            m_handshaking = true;
            m_session = null;
            m_options = options;
            m_plugged = false;
            m_endpoint = endpoint;
            m_socket = null;
            m_encoder = null;
            m_decoder = null;

            //  Put the socket into non-blocking mode.
            Utils.UnblockSocket(m_handle);

            //  Set the socket buffer limits for the underlying socket.
            if (m_options.SendBuffer != 0)
            {
                m_handle.SendBufferSize = m_options.SendBuffer;
            }
            if (m_options.ReceiveBuffer != 0)
            {
                m_handle.ReceiveBufferSize = m_options.ReceiveBuffer;
            }
        }
Beispiel #5
0
        private void Unplug()
        {
            Debug.Assert(m_plugged);
            m_plugged = false;

            //  Cancel all fd subscriptions.
            if (!m_inputError)
                m_ioObject.RmFd(m_handle);

            //  Disconnect from I/O threads poller object.
            m_ioObject.Unplug();

            //  Disconnect from session object.
            if (m_encoder != null)
                m_encoder.SetMsgSource(null);
            if (m_decoder != null)
                m_decoder.SetMsgSink(null);
            m_session = null;
        }
Beispiel #6
0
        public void Plug(IOThread ioThread,
		                 SessionBase session)
        {
            Debug.Assert(!m_plugged);
            m_plugged = true;

            //  Connect to session object.
            Debug.Assert(m_session == null);
            Debug.Assert(session != null);
            m_session = session;
            m_socket = m_session.Socket;

            m_ioObject = new IOObject(null);
            m_ioObject.SetHandler(this);
            //  Connect to I/O threads poller object.
            m_ioObject.Plug(ioThread);
            m_ioObject.AddFd(m_handle);

            //  Send the 'length' and 'flags' fields of the identity message.
            //  The 'length' field is encoded in the long format.

            m_greetingOutputBuffer[m_outsize++] = ((byte) 0xff);
            m_greetingOutputBuffer.PutLong((long) m_options.IdentitySize + 1, 1);
            m_outsize += 8;
            m_greetingOutputBuffer[m_outsize++] = ((byte) 0x7f);

            m_outpos = new ByteArraySegment(m_greetingOutputBuffer);

            m_ioObject.SetPollin(m_handle);
            m_ioObject.SetPollout(m_handle);

            //  Flush all the data that may have been already received downstream.
            InEvent();
        }
Beispiel #7
0
        private void Error()
        {
            Debug.Assert(m_session != null);
            //m_socket.EventDisconnected(m_endpoint, m_handle);
            m_session.Detach();

            //  Cancel all fd subscriptions.
            m_ioObject.RmFd(m_handle);

            //  Disconnect from I/O threads poller object.
            m_ioObject.Unplug();

            //  Disconnect from session object.
            if (m_decoder != null)
                m_decoder.SetMsgSink(null);

            m_session = null;

            Destroy();
        }
Beispiel #8
0
        public void Plug(IOThread ioThread, SessionBase session)
        {
            m_session = session;

            m_socket = session.Socket;

            m_ioObject = new IOObject(null);
            m_ioObject.SetHandler(this);
            m_ioObject.Plug(ioThread);
            m_ioObject.AddFd(m_handle);
            m_ioObject.SetPollin(m_handle);

            DropSubscriptions();

            // push message to the session because there is no identity message with pgm
            session.PushMsg(new Msg());
        }
        public void Plug(IOThread ioThread, SessionBase session)
        {
            m_encoder.SetMsgSource(session);

            AddFd(m_socket);
            SetPollout(m_socket);

            // get the first message from the session because we don't want to send identities
            session.PullMsg();
        }
Beispiel #10
0
        public void Connect(String addr)
        {
            CheckContextTerminated();

            //  Process pending commands, if any.
            ProcessCommands(0, false);


            string address;
            string protocol;

            DecodeAddress(addr, out address, out protocol);

            CheckProtocol(protocol);

            if (protocol.Equals(Address.InProcProtocol))
            {
                //  TODO: inproc connect is specific with respect to creating pipes
                //  as there's no 'reconnect' functionality implemented. Once that
                //  is in place we should follow generic pipe creation algorithm.

                //  Find the peer endpoint.
                Ctx.Endpoint peer = FindEndpoint(addr);

                // The total HWM for an inproc connection should be the sum of
                // the binder's HWM and the connector's HWM.
                int sndhwm;
                int rcvhwm;
                if (m_options.SendHighWatermark == 0 || peer.Options.ReceiveHighWatermark == 0)
                {
                    sndhwm = 0;
                }
                else
                {
                    sndhwm = m_options.SendHighWatermark + peer.Options.ReceiveHighWatermark;
                }
                if (m_options.ReceiveHighWatermark == 0 || peer.Options.SendHighWatermark == 0)
                {
                    rcvhwm = 0;
                }
                else
                {
                    rcvhwm = m_options.ReceiveHighWatermark + peer.Options.SendHighWatermark;
                }

                //  Create a bi-directional pipe to connect the peers.
                ZObject[] parents = { this, peer.Socket };
                int[]     hwms    = { sndhwm, rcvhwm };
                bool[]    delays  = { m_options.DelayOnDisconnect, m_options.DelayOnClose };
                Pipe[]    pipes   = Pipe.PipePair(parents, hwms, delays);

                //  Attach local end of the pipe to this socket object.
                AttachPipe(pipes[0]);

                //  If required, send the identity of the peer to the local socket.
                if (peer.Options.RecvIdentity)
                {
                    Msg id = new Msg();
                    id.InitPool(peer.Options.IdentitySize);
                    id.Put(peer.Options.Identity, 0, peer.Options.IdentitySize);
                    id.SetFlags(MsgFlags.Identity);
                    bool written = pipes[0].Write(ref id);
                    Debug.Assert(written);
                    pipes[0].Flush();
                }

                //  If required, send the identity of the local socket to the peer.
                if (m_options.RecvIdentity)
                {
                    Msg id = new Msg();
                    id.InitPool(m_options.IdentitySize);
                    id.Put(m_options.Identity, 0, m_options.IdentitySize);
                    id.SetFlags(MsgFlags.Identity);
                    bool written = pipes[1].Write(ref id);
                    Debug.Assert(written);
                    pipes[1].Flush();
                }

                //  Attach remote end of the pipe to the peer socket. Note that peer's
                //  seqnum was incremented in find_endpoint function. We don't need it
                //  increased here.
                SendBind(peer.Socket, pipes[1], false);

                // Save last endpoint URI
                m_options.LastEndpoint = addr;

                // remember inproc connections for disconnect
                m_inprocs.Add(addr, pipes[0]);

                return;
            }

            //  Choose the I/O thread to run the session in.
            IOThread ioThread = ChooseIOThread(m_options.Affinity);

            if (ioThread == null)
            {
                throw NetMQException.Create("Empty IO Thread", ErrorCode.EmptyThread);
            }
            Address paddr = new Address(protocol, address);

            //  Resolve address (if needed by the protocol)
            if (protocol.Equals(Address.TcpProtocol))
            {
                paddr.Resolved = (new TcpAddress());
                paddr.Resolved.Resolve(
                    address, m_options.IPv4Only);
            }
            else if (protocol.Equals(Address.IpcProtocol))
            {
                paddr.Resolved = (new IpcAddress());
                paddr.Resolved.Resolve(address, true);
            }
            else if (protocol.Equals(Address.PgmProtocol) || protocol.Equals(Address.EpgmProtocol))
            {
                if (m_options.SocketType == ZmqSocketType.Sub || m_options.SocketType == ZmqSocketType.Xsub)
                {
                    Bind(addr);
                    return;
                }

                paddr.Resolved = new PgmAddress();
                paddr.Resolved.Resolve(address, m_options.IPv4Only);
            }

            //  Create session.
            SessionBase session = SessionBase.Create(ioThread, true, this, m_options, paddr);

            Debug.Assert(session != null);

            //  PGM does not support subscription forwarding; ask for all data to be
            //  sent to this pipe.
            bool icanhasall = protocol.Equals(Address.PgmProtocol) || protocol.Equals(Address.EpgmProtocol);

            if (!m_options.DelayAttachOnConnect || icanhasall)
            {
                //  Create a bi-directional pipe.
                ZObject[] parents = { this, session };
                int[]     hwms    = { m_options.SendHighWatermark, m_options.ReceiveHighWatermark };
                bool[]    delays  = { m_options.DelayOnDisconnect, m_options.DelayOnClose };
                Pipe[]    pipes   = Pipe.PipePair(parents, hwms, delays);

                //  Attach local end of the pipe to the socket object.
                AttachPipe(pipes[0], icanhasall);

                //  Attach remote end of the pipe to the session object later on.
                session.AttachPipe(pipes[1]);
            }

            // Save last endpoint URI
            m_options.LastEndpoint = paddr.ToString();

            AddEndpoint(addr, session);
            return;
        }