Exemplo n.º 1
0
        /// <summary>
        /// Create a new SessionBase object from the given IOThread, socket, and Address.
        /// </summary>
        /// <param name="ioThread">the IOThread for this session to run on</param>
        /// <param name="connect">this flag dictates whether to connect</param>
        /// <param name="socket">the socket to contain</param>
        /// <param name="options">Options that dictate the settings of this session</param>
        /// <param name="addr">an Address that dictates the protocol and IP-address to use when connecting</param>
        public SessionBase([NotNull] IOThread ioThread, bool connect, [NotNull] SocketBase socket, [NotNull] Options options, [NotNull] Address addr)
            : base(ioThread, options)
        {
            m_ioObject = new IOObject(ioThread);

            m_connect = connect;
            m_socket = socket;
            m_ioThread = ioThread;
            m_addr = addr;

            if (options.RawSocket)
            {
                m_identitySent = true;
                m_identityReceived = true;
            }

            m_terminatingPipes = new HashSet<Pipe>();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Connect this socket to the given address.
        /// </summary>
        /// <param name="addr">a string denoting the endpoint to connect to</param>
        /// <exception cref="AddressAlreadyInUseException">The specified address is already in use.</exception>
        /// <exception cref="NetMQException">No IO thread was found.</exception>
        /// <exception cref="ProtocolNotSupportedException">the specified protocol is not supported</exception>
        /// <exception cref="ProtocolNotSupportedException">the socket type and protocol do not match</exception>
        /// <exception cref="TerminatingException">The socket has been stopped.</exception>
        /// <remarks>
        /// The supported protocols are "inproc", "ipc", "tcp", "pgm", and "epgm".
        /// If the protocol is either "pgm" or "epgm", then this socket must be of type Pub, Sub, XPub, or XSub.
        /// </remarks>
        /// <exception cref="EndpointNotFoundException">The given address was not found in the list of endpoints.</exception>
        public void Connect([NotNull] 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 == 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.
                var sndhwm = m_options.SendHighWatermark != 0 && peer.Options.ReceiveHighWatermark != 0
                    ? m_options.SendHighWatermark + peer.Options.ReceiveHighWatermark
                    : 0;

                var rcvhwm = m_options.ReceiveHighWatermark != 0 && peer.Options.SendHighWatermark != 0
                    ? m_options.ReceiveHighWatermark + peer.Options.SendHighWatermark
                    : 0;

                // The total LWM for an inproc connection should be the sum of
                // the binder's LWM and the connector's LWM.
                int sndlwm = m_options.SendLowWatermark != 0 && peer.Options.ReceiveLowWatermark != 0
                    ? m_options.SendLowWatermark + peer.Options.ReceiveLowWatermark
                    : 0;

                int rcvlwm = m_options.ReceiveLowWatermark != 0 && peer.Options.SendLowWatermark != 0
                    ? m_options.ReceiveLowWatermark + peer.Options.SendLowWatermark
                    : 0;

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

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

                // If required, send the identity of the local socket to the peer.
                if (peer.Options.RecvIdentity)
                {
                    var id = new Msg();
                    id.InitPool(m_options.IdentitySize);
                    id.Put(m_options.Identity, 0, m_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 peer to the local socket.
                if (m_options.RecvIdentity)
                {
                    var id = new Msg();
                    id.InitPool(peer.Options.IdentitySize);
                    id.Put(peer.Options.Identity, 0, peer.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.
            var ioThread = ChooseIOThread(m_options.Affinity);

            if (ioThread == null)
                throw NetMQException.Create(ErrorCode.EmptyThread);

            var paddr = new Address(protocol, address);

            // Resolve address (if needed by the protocol)
            switch (protocol)
            {
                case Address.TcpProtocol:
                    {
                        paddr.Resolved = (new TcpAddress());
                        paddr.Resolved.Resolve(address, m_options.IPv4Only);
                        break;
                    }
                case Address.IpcProtocol:
                    {
                        paddr.Resolved = (new IpcAddress());
                        paddr.Resolved.Resolve(address, true);
                        break;
                    }
                case Address.PgmProtocol:
                case 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);
                        break;
                    }
            }

            // 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 == Address.PgmProtocol || protocol == Address.EpgmProtocol;
            Pipe newPipe = null;

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

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

                // 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, newPipe);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Create a return a new session.
 /// The specific subclass of SessionBase that is created is dictated by the SocketType specified by the options argument.
 /// </summary>
 /// <param name="ioThread">the <c>IOThread</c> for this session to run in</param>
 /// <param name="connect">whether to immediately connect</param>
 /// <param name="socket">the socket to connect</param>
 /// <param name="options">an <c>Options</c> that provides the SocketType that dictates which type of session to create</param>
 /// <param name="addr">an <c>Address</c> object that specifies the protocol and address to connect to</param>
 /// <returns>the newly-created instance of whichever subclass of SessionBase is specified by the options</returns>
 /// <exception cref="InvalidException">The socket must be of the correct type.</exception>
 
 public static SessionBase Create( IOThread ioThread, bool connect,  SocketBase socket,  Options options,  Address addr)
 {
     switch (options.SocketType)
     {
         case ZmqSocketType.Req:
             return new Req.ReqSession(ioThread, connect, socket, options, addr);
         case ZmqSocketType.Dealer:
             return new Dealer.DealerSession(ioThread, connect, socket, options, addr);
         case ZmqSocketType.Rep:
             return new Rep.RepSession(ioThread, connect, socket, options, addr);
         case ZmqSocketType.Router:
             return new Router.RouterSession(ioThread, connect, socket, options, addr);
         case ZmqSocketType.Pub:
             return new Pub.PubSession(ioThread, connect, socket, options, addr);
         case ZmqSocketType.Xpub:
             return new XPub.XPubSession(ioThread, connect, socket, options, addr);
         case ZmqSocketType.Sub:
             return new Sub.SubSession(ioThread, connect, socket, options, addr);
         case ZmqSocketType.Xsub:
             return new XSub.XSubSession(ioThread, connect, socket, options, addr);
         case ZmqSocketType.Push:
             return new Push.PushSession(ioThread, connect, socket, options, addr);
         case ZmqSocketType.Pull:
             return new Pull.PullSession(ioThread, connect, socket, options, addr);
         case ZmqSocketType.Pair:
             return new Pair.PairSession(ioThread, connect, socket, options, addr);
         case ZmqSocketType.Stream:
             return new Stream.StreamSession(ioThread, connect, socket, options, addr);
         default:
             throw new InvalidException("SessionBase.Create called with invalid SocketType of " + options.SocketType);
     }
 }