Esempio n. 1
0
        //  Create a socket of a specified type.
        public static SocketBase Create(ZmqSocketType type, Ctx parent,
                                        int tid, int sid)
        {
            SocketBase s;

            switch (type)
            {
            case ZmqSocketType.Pair:
                s = new Pair(parent, tid, sid);
                break;

            case ZmqSocketType.Pub:
                s = new Pub(parent, tid, sid);
                break;

            case ZmqSocketType.Sub:
                s = new Sub(parent, tid, sid);
                break;

            case ZmqSocketType.Req:
                s = new Req(parent, tid, sid);
                break;

            case ZmqSocketType.Rep:
                s = new Rep(parent, tid, sid);
                break;

            case ZmqSocketType.Dealer:
                s = new Dealer(parent, tid, sid);
                break;

            case ZmqSocketType.Router:
                s = new Router(parent, tid, sid);
                break;

            case ZmqSocketType.Pull:
                s = new Pull(parent, tid, sid);
                break;

            case ZmqSocketType.Push:
                s = new Push(parent, tid, sid);
                break;

            case ZmqSocketType.Xpub:
                s = new XPub(parent, tid, sid);
                break;

            case ZmqSocketType.Xsub:
                s = new XSub(parent, tid, sid);
                break;

            case ZmqSocketType.Stream:
                s = new Stream(parent, tid, sid);
                break;

            default:
                throw InvalidException.Create("type=" + type);
            }
            return(s);
        }
Esempio n. 2
0
        private NetMQSocket InitSocket(ZmqSocketType socketType, string endPoint, bool isBind, string identity)
        {
            NetMQSocket socket = GetSocket(socketType);

            if (identity != null && identity != "")
            {
                socket.Options.Identity = Encoding.UTF8.GetBytes(identity);
            }
            socket.Options.SendHighWatermark    = 10000;
            socket.Options.ReceiveHighWatermark = 10000;
            socket.Options.Linger               = TimeSpan.Zero;
            socket.Options.TcpKeepalive         = true;
            socket.Options.TcpKeepaliveIdle     = TimeSpan.FromMinutes(1);
            socket.Options.TcpKeepaliveInterval = TimeSpan.FromSeconds(10);
            if (isBind)
            {
                socket.Bind(endPoint);
            }
            else
            {
                socket.Connect(endPoint);
            }
            SetReceive(socketType, socket);
            return(socket);
        }
Esempio n. 3
0
        private NetMQSocket GetSocket(ZmqSocketType socketType)
        {
            switch (socketType)
            {
            case ZmqSocketType.Pub:
                return(new NetMQ.Sockets.PublisherSocket());

            case ZmqSocketType.Sub:
                return(new NetMQ.Sockets.SubscriberSocket());

            case ZmqSocketType.Router:
                return(new NetMQ.Sockets.RouterSocket());

            case ZmqSocketType.Dealer:
                return(new NetMQ.Sockets.DealerSocket());

            case ZmqSocketType.Rep:
                return(new NetMQ.Sockets.ResponseSocket());

            case ZmqSocketType.Req:
                return(new NetMQ.Sockets.RequestSocket());

            default:
                throw new ArgumentOutOfRangeException($"SocketType:({socketType}) out of range!");
            }
        }
Esempio n. 4
0
 public void Socket_Connect_Disconnect_Test(ZmqSocketType socketType, string endpoint)
 {
     using (var socket = ZmqContext.Current.Socket(socketType))
     {
         socket.Connect(endpoint);
         socket.Disconnect(endpoint);
     }
 }
Esempio n. 5
0
 public void Socket_Bind_Unbind_Test(ZmqSocketType socketType, string endpoint)
 {
     using (var socket = ZmqContext.Current.Socket(socketType))
     {
         socket.Bind(endpoint);
         socket.Unbind(endpoint);
     }
 }
Esempio n. 6
0
 internal SocketAdapter(ZmqSocketType socketType, string endPoint, bool isBind, string identity, int dataLength)
 {
     _endPoint   = endPoint;
     _socketType = socketType;
     _isBind     = isBind;
     _topic      = new List <string>();
     SetDataLength(dataLength);
     _socket = InitSocket(socketType, endPoint, isBind, identity);
 }
Esempio n. 7
0
        // Sockets
        public static SocketBase Socket(Ctx ctx, ZmqSocketType type)
        {
            if (ctx == null || !ctx.CheckTag())
            {
                throw NetMQException.Create(ErrorCode.EFAULT);
            }
            SocketBase s = ctx.CreateSocket(type);

            return(s);
        }
Esempio n. 8
0
        public ZmqSocket(ZmqContext context, ZmqSocketType type)
        {
            _context = context;

            _handle = ZmqNative.socket(_context.Handle, (int)type);

            if (_handle == IntPtr.Zero)
            {
                ZmqUtil.ThrowLastError($"Could not create ZMQ {type} socket");
            }
        }
Esempio n. 9
0
        /// <summary>
        ///  Only used to identify the socket for the Socket-Type
        ///  property in the wire protocol.
        /// </summary>
        /// <param name="socketType"></param>
        protected string GetSocketName(ZmqSocketType socketType)
        {
            switch (socketType)
            {
            case ZmqSocketType.Pair:
                return(SocketNames.Pair);

            case ZmqSocketType.Pub:
                return(SocketNames.Pub);

            case ZmqSocketType.Sub:
                return(SocketNames.Sub);

            case ZmqSocketType.Req:
                return(SocketNames.Req);

            case ZmqSocketType.Rep:
                return(SocketNames.Rep);

            case ZmqSocketType.Dealer:
                return(SocketNames.Dealer);

            case ZmqSocketType.Router:
                return(SocketNames.Router);

            case ZmqSocketType.Pull:
                return(SocketNames.Pull);

            case ZmqSocketType.Push:
                return(SocketNames.Push);

            case ZmqSocketType.Xpub:
                return(SocketNames.Xpub);

            case ZmqSocketType.Xsub:
                return(SocketNames.Xsub);

            case ZmqSocketType.Stream:
                return(SocketNames.Stream);

            case ZmqSocketType.Peer:
                return(SocketNames.Peer);

            case ZmqSocketType.Server:
                return(SocketNames.Server);

            case ZmqSocketType.Client:
                return(SocketNames.Client);

            default:
                throw new ArgumentOutOfRangeException(nameof(socketType), socketType, null);
            }
        }
Esempio n. 10
0
 private void SetReceive(ZmqSocketType socketType, NetMQSocket socket)
 {
     switch (socketType)
     {
     case ZmqSocketType.Sub:
     case ZmqSocketType.Dealer:
     case ZmqSocketType.Router:
     case ZmqSocketType.Rep:
     case ZmqSocketType.Req:
         socket.ReceiveReady += Socket_ReceiveReady;
         break;
     }
 }
Esempio n. 11
0
        public void Socket_Create_Dispose_Test(ZmqSocketType socketType)
        {
            ZmqSocket socket;

            using (socket = ZmqContext.Current.Socket(socketType))
            {
                Assert.AreEqual(ZmqContext.Current, socket.Context);

                Assert.AreEqual(socketType, socket.SocketType);
                Assert.IsFalse(socket.Handle.IsClosed);
                Assert.IsFalse(socket.Handle.IsInvalid);
            }

            Assert.IsTrue(socket.Handle.IsClosed);
        }
Esempio n. 12
0
        public NetMQSocket CreateSocket(ZmqSocketType socketType)
        {
            var socketHandle = CreateHandle(socketType);

            switch (socketType)
            {
            case ZmqSocketType.Pair:
                return(new PairSocket(socketHandle));

            case ZmqSocketType.Pub:
                return(new PublisherSocket(socketHandle));

            case ZmqSocketType.Sub:
                return(new SubscriberSocket(socketHandle));

            case ZmqSocketType.Req:
                return(new RequestSocket(socketHandle));

            case ZmqSocketType.Rep:
                return(new ResponseSocket(socketHandle));

            case ZmqSocketType.Dealer:
                return(new DealerSocket(socketHandle));

            case ZmqSocketType.Router:
                return(new RouterSocket(socketHandle));

            case ZmqSocketType.Pull:
                return(new PullSocket(socketHandle));

            case ZmqSocketType.Push:
                return(new PushSocket(socketHandle));

            case ZmqSocketType.Xpub:
                return(new XPublisherSocket(socketHandle));

            case ZmqSocketType.Xsub:
                return(new XSubscriberSocket(socketHandle));

            case ZmqSocketType.Stream:
                return(new StreamSocket(socketHandle));

            default:
                throw new ArgumentOutOfRangeException("socketType");
            }
        }
Esempio n. 13
0
 //? swtich to ctor with Socket and Sender?  move setup to factory?
 public SendSocket(NetMQContext context,
                   string address,
                   Func <T, byte[]> marshaller,
                   ZmqSocketType type = ZmqSocketType.Pub,
                   bool bind          = true)
 {
     _msgSender = marshaller;
     _socket    = context.CreateSocket(type);
     if (bind)
     {
         _socket.Bind(address);
     }
     else
     {
         _socket.Connect(address);
     }
 }
Esempio n. 14
0
        public static SocketBase Create(ZmqSocketType type, [NotNull] Ctx parent, int threadId, int socketId)
        {
            switch (type)
            {
            case ZmqSocketType.Pair:
                return(new Pair(parent, threadId, socketId));

            case ZmqSocketType.Pub:
                return(new Pub(parent, threadId, socketId));

            case ZmqSocketType.Sub:
                return(new Sub(parent, threadId, socketId));

            case ZmqSocketType.Req:
                return(new Req(parent, threadId, socketId));

            case ZmqSocketType.Rep:
                return(new Rep(parent, threadId, socketId));

            case ZmqSocketType.Dealer:
                return(new Dealer(parent, threadId, socketId));

            case ZmqSocketType.Router:
                return(new Router(parent, threadId, socketId));

            case ZmqSocketType.Pull:
                return(new Pull(parent, threadId, socketId));

            case ZmqSocketType.Push:
                return(new Push(parent, threadId, socketId));

            case ZmqSocketType.Xpub:
                return(new XPub(parent, threadId, socketId));

            case ZmqSocketType.Xsub:
                return(new XSub(parent, threadId, socketId));

            case ZmqSocketType.Stream:
                return(new Stream(parent, threadId, socketId));

            default:
                throw new InvalidException("SocketBase.Create called with invalid type of " + type);
            }
        }
Esempio n. 15
0
        internal NetMQSocket(ZmqSocketType socketType, string?connectionString, DefaultAction defaultAction)
        {
            m_socketHandle    = NetMQConfig.Context.CreateSocket(socketType);
            m_netMqSelector   = new NetMQSelector();
            Options           = new SocketOptions(this);
            m_socketEventArgs = new NetMQSocketEventArgs(this);

            Options.Linger = NetMQConfig.Linger;

            if (!Strings.IsNullOrEmpty(connectionString))
            {
                var endpoints = connectionString
                                .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                                .Select(a => a.Trim())
                                .Where(a => !string.IsNullOrEmpty(a));

                foreach (var endpoint in endpoints)
                {
                    switch (endpoint[0])
                    {
                    case '@':
                        Bind(endpoint.Substring(1));
                        break;

                    case '>':
                        Connect(endpoint.Substring(1));
                        break;

                    default:
                        if (defaultAction == DefaultAction.Connect)
                        {
                            Connect(endpoint);
                        }
                        else
                        {
                            Bind(endpoint);
                        }
                        break;
                    }
                }
            }
        }
Esempio n. 16
0
        /// <summary>
        /// Create a new NetMQSocket with the given <see cref="ZmqSocketType"/>.
        /// </summary>
        /// <param name="socketType">Type of socket to create</param>
        internal NetMQSocket(ZmqSocketType socketType, string connectionString, DefaultAction defaultAction)
        {
            m_socketHandle    = NetMQConfig.Context.CreateSocket(socketType);
            m_selector        = new Selector();
            Options           = new SocketOptions(this);
            m_socketEventArgs = new NetMQSocketEventArgs(this);

            Options.Linger = NetMQConfig.Linger;

            if (!string.IsNullOrEmpty(connectionString))
            {
                var endpoints =
                    connectionString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(a => a.Trim()).Where(a => !string.IsNullOrEmpty(a));

                foreach (string endpoint in endpoints)
                {
                    if (endpoint[0] == '@')
                    {
                        Bind(endpoint.Substring(1));
                    }
                    else if (endpoint[0] == '>')
                    {
                        Connect(endpoint.Substring(1));
                    }
                    else if (defaultAction == DefaultAction.Connect)
                    {
                        Connect(endpoint);
                    }
                    else
                    {
                        Bind(endpoint);
                    }
                }
            }
        }
Esempio n. 17
0
        public SocketBase CreateSocket(ZmqSocketType type)
        {
            lock (this.m_slotSync)
            {
                if (this.m_starting)
                {
                    this.m_starting = false;
                    // Initialise the array of mailboxes. Additional three slots are for
                    // zmq_term thread and reaper thread.

                    int ios;
                    int mazmq;

                    lock (this.m_optSync)
                    {
                        mazmq = this.m_maxSockets;
                        ios   = this.m_ioThreadCount;
                    }

                    this.m_slotCount = mazmq + ios + 2;
                    this.m_slots     = new IMailbox[this.m_slotCount];
                    //alloc_Debug.Assert(slots);

                    // Initialise the infrastructure for zmq_term thread.
                    this.m_slots[Ctx.TermTid] = this.m_termMailbox;

                    // Create the reaper thread.
                    this.m_reaper = new Reaper(this, Ctx.ReaperTid);
                    //alloc_Debug.Assert(reaper);
                    this.m_slots[Ctx.ReaperTid] = this.m_reaper.Mailbox;
                    this.m_reaper.Start();

                    // Create I/O thread objects and launch them.
                    for (int i = 2; i != ios + 2; i++)
                    {
                        IOThread ioThread = new IOThread(this, i);
                        //alloc_Debug.Assert(io_thread);
                        this.m_ioThreads.Add(ioThread);
                        this.m_slots[i] = ioThread.Mailbox;
                        ioThread.Start();
                    }

                    // In the unused part of the slot array, create a list of empty slots.
                    for (int i = this.m_slotCount - 1; i >= ios + 2; i--)
                    {
                        this.m_emptySlots.Push(i);
                        this.m_slots[i] = null;
                    }
                }

                // Once zmq_term() was called, we can't create new sockets.
                if (this.m_terminating)
                {
                    string xMsg = $"Ctx.CreateSocket({type}), cannot create new socket while terminating.";
                    throw new TerminatingException(null, xMsg);
                }

                // If max_sockets limit was reached, return error.
                if (this.m_emptySlots.Count == 0)
                {
                    #if DEBUG
                    string xMsg = $"Ctx.CreateSocket({type}), max number of sockets {this.m_maxSockets} reached.";
                    throw NetMQException.Create(xMsg, ErrorCode.TooManyOpenSockets);
                    #else
                    throw NetMQException.Create(ErrorCode.TooManyOpenSockets);
                                        #endif
                }

                // Choose a slot for the socket.
                int slot = this.m_emptySlots.Pop();

                // Generate new unique socket ID.
                int socketId = Interlocked.Increment(ref Ctx.s_maxSocketId);

                // Create the socket and register its mailbox.
                SocketBase s = SocketBase.Create(type, this, slot, socketId);

                this.m_sockets.Add(s);
                this.m_slots[slot] = s.Mailbox;

                //LOG.debug("NEW Slot [" + slot + "] " + s);

                return(s);
            }
        }
Esempio n. 18
0
 // Sockets
 public static SocketBase Socket(Ctx ctx, ZmqSocketType type)
 {
     if (ctx == null || !ctx.CheckTag())
     {
         throw NetMQException.Create(ErrorCode.EFAULT);
     }
     SocketBase s = ctx.CreateSocket(type);
     return s;
 }
Esempio n. 19
0
File: ZMQ.cs Progetto: knocte/netmq
 // Sockets
 public static SocketBase Socket(Ctx ctx, ZmqSocketType type)
 {
     if (ctx == null || !ctx.CheckTag())
     {
         throw new InvalidOperationException();
     }
     SocketBase s = ctx.CreateSocket(type);
     return s;
 }
Esempio n. 20
0
        private SocketBase CreateHandle(ZmqSocketType socketType)
        {
            m_ctx.CheckDisposed();

            return(m_ctx.CreateSocket(socketType));
        }
Esempio n. 21
0
        //  Create a socket of a specified type.
        public static SocketBase Create(ZmqSocketType type, Ctx parent,
                                                                                                                                        int tid, int sid)
        {
            SocketBase s;
            switch (type)
            {

                case ZmqSocketType.Pair:
                    s = new Pair(parent, tid, sid);
                    break;
                case ZmqSocketType.Pub:
                    s = new Pub(parent, tid, sid);
                    break;
                case ZmqSocketType.Sub:
                    s = new Sub(parent, tid, sid);
                    break;
                case ZmqSocketType.Req:
                    s = new Req(parent, tid, sid);
                    break;
                case ZmqSocketType.Rep:
                    s = new Rep(parent, tid, sid);
                    break;
                case ZmqSocketType.Dealer:
                    s = new Dealer(parent, tid, sid);
                    break;
                case ZmqSocketType.Router:
                    s = new Router(parent, tid, sid);
                    break;
                case ZmqSocketType.Pull:
                    s = new Pull(parent, tid, sid);
                    break;
                case ZmqSocketType.Push:
                    s = new Push(parent, tid, sid);
                    break;

                case ZmqSocketType.Xpub:
                    s = new XPub(parent, tid, sid);
                    break;

                case ZmqSocketType.Xsub:
                    s = new XSub(parent, tid, sid);
                    break;
                case ZmqSocketType.Stream:
                    s = new Stream(parent, tid, sid);
                    break;
                default:
                    throw InvalidException.Create("type=" + type);
            }
            return s;
        }
Esempio n. 22
0
 public static SocketBase Create(ZmqSocketType type, [NotNull] Ctx parent, int threadId, int socketId)
 {
     switch (type)
     {
         case ZmqSocketType.Pair:
             return new Pair(parent, threadId, socketId);
         case ZmqSocketType.Pub:
             return new Pub(parent, threadId, socketId);
         case ZmqSocketType.Sub:
             return new Sub(parent, threadId, socketId);
         case ZmqSocketType.Req:
             return new Req(parent, threadId, socketId);
         case ZmqSocketType.Rep:
             return new Rep(parent, threadId, socketId);
         case ZmqSocketType.Dealer:
             return new Dealer(parent, threadId, socketId);
         case ZmqSocketType.Router:
             return new Router(parent, threadId, socketId);
         case ZmqSocketType.Pull:
             return new Pull(parent, threadId, socketId);
         case ZmqSocketType.Push:
             return new Push(parent, threadId, socketId);
         case ZmqSocketType.Xpub:
             return new XPub(parent, threadId, socketId);
         case ZmqSocketType.Xsub:
             return new XSub(parent, threadId, socketId);
         case ZmqSocketType.Stream:
             return new Stream(parent, threadId, socketId);
         default:
             throw new InvalidException("SocketBase.Create called with invalid type of " + type);
     }
 }
Esempio n. 23
0
        private SocketBase CreateHandle(ZmqSocketType socketType)
        {
            m_ctx.CheckDisposed();

            return m_ctx.CreateSocket(socketType);
        }
Esempio n. 24
0
File: Ctx.cs Progetto: awb99/netmq
        /// <summary>
        /// Create and return a new socket of the given type, and initialize this Ctx if this is the first one.
        /// </summary>
        /// <param name="type">the type of socket to create</param>
        /// <returns>the newly-created socket</returns>
        /// <exception cref="TerminatingException">Cannot create new socket while terminating.</exception>
        /// <exception cref="NetMQException">Maximum number of sockets reached.</exception>
        /// <exception cref="TerminatingException">The context (Ctx) must not be already terminating.</exception>
        
        public SocketBase CreateSocket(ZmqSocketType type)
        {
            lock (m_slotSync)
            {
                if (m_starting)
                {
                    m_starting = false;
                    // Initialise the array of mailboxes. Additional three slots are for
                    // zmq_term thread and reaper thread.

                    int ios;
                    int mazmq;

                    lock (m_optSync)
                    {
                        mazmq = m_maxSockets;
                        ios = m_ioThreadCount;
                    }
                    m_slotCount = mazmq + ios + 2;
                    m_slots = new IMailbox[m_slotCount];
                    //alloc_Debug.Assert(slots);

                    // Initialise the infrastructure for zmq_term thread.
                    m_slots[TermTid] = m_termMailbox;

                    // Create the reaper thread.
                    m_reaper = new Reaper(this, ReaperTid);
                    //alloc_Debug.Assert(reaper);
                    m_slots[ReaperTid] = m_reaper.Mailbox;
                    m_reaper.Start();

                    // Create I/O thread objects and launch them.
                    for (int i = 2; i != ios + 2; i++)
                    {
                        var ioThread = new IOThread(this, i);
                        //alloc_Debug.Assert(io_thread);
                        m_ioThreads.Add(ioThread);
                        m_slots[i] = ioThread.Mailbox;
                        ioThread.Start();
                    }

                    // In the unused part of the slot array, create a list of empty slots.
                    for (int i = m_slotCount - 1; i >= ios + 2; i--)
                    {
                        m_emptySlots.Push(i);
                        m_slots[i] = null;
                    }
                }

                // Once zmq_term() was called, we can't create new sockets.
                if (m_terminating)
                {
                    string xMsg = string.Format("Ctx.CreateSocket({0}), cannot create new socket while terminating.", type);
                    throw new TerminatingException(innerException: null, message: xMsg);
                }

                // If max_sockets limit was reached, return error.
                if (m_emptySlots.Count == 0)
                {
#if DEBUG
                    string xMsg = string.Format("Ctx.CreateSocket({0}), max number of sockets {1} reached.", type, m_maxSockets);
                    throw NetMQException.Create(xMsg, ErrorCode.TooManyOpenSockets);
#else
                    throw NetMQException.Create(ErrorCode.TooManyOpenSockets);
#endif
                }

                // Choose a slot for the socket.
                int slot = m_emptySlots.Pop();

                // Generate new unique socket ID.
                int socketId = Interlocked.Increment(ref s_maxSocketId);

                // Create the socket and register its mailbox.
                SocketBase s = SocketBase.Create(type, this, slot, socketId);

                m_sockets.Add(s);
                m_slots[slot] = s.Mailbox;

                //LOG.debug("NEW Slot [" + slot + "] " + s);

                return s;
            }
        }
Esempio n. 25
0
 private void InitLinks(NetMQContext ctx, ConcurrentDictionary <string, Link> links, QueueLinks queue, ZmqSocketType socketType)
 {
     foreach (LinkAddress sa in queue.Links)
     {
         log.InfoFormat("Connecting to {0}({1})", sa.Name, sa.Address);
         Link link = new Link
         {
             service = sa.Name,
             address = sa.Address,
             context = ctx,
             socket  = ctx.CreateSocket(socketType),
         };
         link.socket.Options.Identity = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString());
         link.Connect();
         links[sa.Name] = link;
     }
 }
Esempio n. 26
0
 public static ZmqSocket Socket(this ZmqContext context, ZmqSocketType socketType)
 {
     return(new ZmqSocket(context, socketType));
 }
Esempio n. 27
0
        public SocketBase CreateSocket(ZmqSocketType type)
        {
            SocketBase s = null;
            lock (m_slotSync)
            {
                if (m_starting)
                {

                    m_starting = false;
                    //  Initialise the array of mailboxes. Additional three slots are for
                    //  zmq_term thread and reaper thread.

                    int ios;
                    int mazmq;

                    lock (m_optSync)
                    {
                        mazmq = m_maxSockets;
                        ios = m_ioThreadCount;
                    }
                    m_slotCount = mazmq + ios + 2;
                    m_slots = new Mailbox[m_slotCount];
                    //alloc_Debug.Assert(slots);

                    //  Initialise the infrastructure for zmq_term thread.
                    m_slots[TermTid] = m_termMailbox;

                    //  Create the reaper thread.
                    m_reaper = new Reaper(this, ReaperTid);
                    //alloc_Debug.Assert(reaper);
                    m_slots[ReaperTid] = m_reaper.Mailbox;
                    m_reaper.Start();

                    //  Create I/O thread objects and launch them.
                    for (int i = 2; i != ios + 2; i++)
                    {
                        IOThread ioThread = new IOThread(this, i);
                        //alloc_Debug.Assert(io_thread);
                        m_ioThreads.Add(ioThread);
                        m_slots[i] = ioThread.Mailbox;
                        ioThread.Start();
                    }

                    //  In the unused part of the slot array, create a list of empty slots.
                    for (int i = (int)m_slotCount - 1;
                         i >= (int)ios + 2; i--)
                    {
                        m_emptySlots.Push(i);
                        m_slots[i] = null;
                    }

                }

                //  Once zmq_term() was called, we can't create new sockets.
                if (m_terminating)
                {
                    ZError.ErrorNumber = ErrorNumber.ETERM;
                    return null;
                }

                //  If max_sockets limit was reached, return error.
                if (m_emptySlots.Count == 0)
                {
                    ZError.ErrorNumber = ErrorNumber.EMFILE;
                    return null;
                }

                //  Choose a slot for the socket.
                int slot = m_emptySlots.Pop();

                //  Generate new unique socket ID.
                int sid = s_maxSocketId.IncrementAndGet();

                //  Create the socket and register its mailbox.
                s = SocketBase.Create(type, this, slot, sid);
                if (s == null)
                {
                    m_emptySlots.Push(slot);
                    return null;
                }
                m_sockets.Add(s);
                m_slots[slot] = s.Mailbox;

                //LOG.debug("NEW Slot [" + slot + "] " + s);
            }

            return s;
        }
Esempio n. 28
0
        public NetMQSocket CreateSocket(ZmqSocketType socketType)
        {
            var socketHandle = CreateHandle(socketType);

            switch (socketType)
            {
                case ZmqSocketType.Pair:
                    return new PairSocket(socketHandle);
                case ZmqSocketType.Pub:
                    return new PublisherSocket(socketHandle);
                case ZmqSocketType.Sub:
                    return new SubscriberSocket(socketHandle);
                case ZmqSocketType.Req:
                    return new RequestSocket(socketHandle);
                case ZmqSocketType.Rep:
                    return new ResponseSocket(socketHandle);
                case ZmqSocketType.Dealer:
                    return new DealerSocket(socketHandle);
                case ZmqSocketType.Router:
                    return new RouterSocket(socketHandle);
                case ZmqSocketType.Pull:
                    return new PullSocket(socketHandle);
                case ZmqSocketType.Push:
                    return new PushSocket(socketHandle);
                case ZmqSocketType.Xpub:
                    return new XPublisherSocket(socketHandle);
                case ZmqSocketType.Xsub:
                    return new XSubscriberSocket(socketHandle);
                case ZmqSocketType.Stream:
                    return new StreamSocket(socketHandle);
                default:
                    throw new ArgumentOutOfRangeException("socketType");
            }
        }
Esempio n. 29
0
        public SocketBase CreateSocket(ZmqSocketType type)
        {
            SocketBase s = null;

            lock (m_slotSync)
            {
                if (m_starting)
                {
                    m_starting = false;
                    //  Initialise the array of mailboxes. Additional three slots are for
                    //  zmq_term thread and reaper thread.

                    int ios;
                    int mazmq;

                    lock (m_optSync)
                    {
                        mazmq = m_maxSockets;
                        ios   = m_ioThreadCount;
                    }
                    m_slotCount = mazmq + ios + 2;
                    m_slots     = new Mailbox[m_slotCount];
                    //alloc_Debug.Assert(slots);

                    //  Initialise the infrastructure for zmq_term thread.
                    m_slots[TermTid] = m_termMailbox;

                    //  Create the reaper thread.
                    m_reaper = new Reaper(this, ReaperTid);
                    //alloc_Debug.Assert(reaper);
                    m_slots[ReaperTid] = m_reaper.Mailbox;
                    m_reaper.Start();

                    //  Create I/O thread objects and launch them.
                    for (int i = 2; i != ios + 2; i++)
                    {
                        IOThread ioThread = new IOThread(this, i);
                        //alloc_Debug.Assert(io_thread);
                        m_ioThreads.Add(ioThread);
                        m_slots[i] = ioThread.Mailbox;
                        ioThread.Start();
                    }

                    //  In the unused part of the slot array, create a list of empty slots.
                    for (int i = (int)m_slotCount - 1;
                         i >= (int)ios + 2; i--)
                    {
                        m_emptySlots.Push(i);
                        m_slots[i] = null;
                    }
                }

                //  Once zmq_term() was called, we can't create new sockets.
                if (m_terminating)
                {
                    throw TerminatingException.Create();
                }

                //  If max_sockets limit was reached, return error.
                if (m_emptySlots.Count == 0)
                {
                    throw NetMQException.Create(ErrorCode.EMFILE);
                }

                //  Choose a slot for the socket.
                int slot = m_emptySlots.Pop();

                //  Generate new unique socket ID.
                int sid = Interlocked.Increment(ref s_maxSocketId);

                //  Create the socket and register its mailbox.
                s = SocketBase.Create(type, this, slot, sid);
                if (s == null)
                {
                    m_emptySlots.Push(slot);
                    return(null);
                }
                m_sockets.Add(s);
                m_slots[slot] = s.Mailbox;

                //LOG.debug("NEW Slot [" + slot + "] " + s);
            }

            return(s);
        }
Esempio n. 30
0
 /// <summary>
 /// Creates a thread socket of type <paramref name="socketType"/>
 /// </summary>
 /// <param name="socketType"></param>
 protected internal ThreadSafeSocket(ZmqSocketType socketType)
 {
     m_socketHandle = NetMQConfig.Context.CreateSocket(socketType);
     Options        = new ThreadSafeSocketOptions(m_socketHandle);
 }
Esempio n. 31
0
 public ZmqSocket(ZmqContext context, ZmqSocketType socketType)
 {
     Context    = context ?? throw new ArgumentNullException(nameof(context));
     SocketType = socketType;
     Handle     = LibZmq.zmq_socket(context.Handle, socketType).ThrowIfLastError();
 }