Exemple #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);
        }
Exemple #2
0
        public void InvalidException()
        {
            var before = new InvalidException("Hello");

            Assert.AreEqual(ErrorCode.Invalid, before.ErrorCode);
            Assert.AreEqual("Hello", before.Message);
        }
Exemple #3
0
        protected override bool XSend(Msg msg, SendReceiveOptions flags)
        {
            byte[] data = msg.Data;
            // Malformed subscriptions.
            if (data.Length < 1 || (data[0] != 0 && data[0] != 1))
            {
                throw InvalidException.Create();
            }

            // Process the subscription.
            if (data[0] == 1)
            {
                if (m_subscriptions.Add(data, 1))
                {
                    m_dist.SendToAll(msg, flags);
                }
            }
            else
            {
                if (m_subscriptions.Remove(data, 1))
                {
                    m_dist.SendToAll(msg, flags);
                }
            }

            return(true);
        }
Exemple #4
0
 protected override void XSetSocketOption(ZmqSocketOptions option, Object optval)
 {
     if (option != ZmqSocketOptions.XpubVerbose)
     {
         throw InvalidException.Create();
     }
     m_verbose = (int)optval == 1;
 }
 protected override void XSetSocketOption(ZmqSocketOptions option, Object optval)
 {
     if (option != ZmqSocketOptions.RouterMandatory)
     {
         throw InvalidException.Create();
     }
     m_mandatory = (bool)optval;
 }
        public void Should_serialize_and_deserialize()
        {
            var original = new InvalidException("My Message");

            var source = new OrleansWrapperException(original, original.GetType());
            var result = source.SerializeAndDeserializeBinary();

            Assert.Equal(result.ExceptionType, source.ExceptionType);
            Assert.Equal(result.Message, source.Message);
        }
Exemple #7
0
 //  Stable/legacy context API
 public static Ctx Init(int ioThreads)
 {
     if (ioThreads >= 0)
     {
         Ctx ctx = CtxNew();
         CtxSet(ctx, ContextOption.IOThreads, ioThreads);
         return(ctx);
     }
     throw InvalidException.Create();
 }
Exemple #8
0
        public static int ZmqMsgGet(Msg msg, MsgFlags option)
        {
            switch (option)
            {
            case MsgFlags.More:
                return(msg.HasMore ? 1 : 0);

            default:
                throw InvalidException.Create();
            }
        }
        public async Task Should_wrap_non_serializable_exception()
        {
            var original = new InvalidException("My Message");

            A.CallTo(() => context.Invoke())
            .Throws(original);

            var ex = await Assert.ThrowsAnyAsync <OrleansWrapperException>(() => sut.Invoke(context));

            Assert.Equal(original.GetType(), ex.ExceptionType);
            Assert.Contains(original.Message, ex.Message);
        }
Exemple #10
0
        protected override void XSetSocketOption(ZmqSocketOptions option, Object optval)
        {
            if (option != ZmqSocketOptions.Subscribe && option != ZmqSocketOptions.Unsubscribe)
            {
                throw InvalidException.Create();
            }

            byte[] val;

            if (optval is String)
            {
                val = Encoding.ASCII.GetBytes((String)optval);
            }
            else if (optval is byte[])
            {
                val = (byte[])optval;
            }
            else
            {
                throw InvalidException.Create();
            }

            //  Create the subscription message.
            Msg msg = new Msg();

            msg.InitPool(val.Length + 1);
            if (option == ZmqSocketOptions.Subscribe)
            {
                msg.Put((byte)1);
            }
            else if (option == ZmqSocketOptions.Unsubscribe)
            {
                msg.Put((byte)0);
            }
            msg.Put(val, 1, val.Length);

            try
            {
                //  Pass it further on in the stack.
                bool isMessageSent = base.XSend(ref msg, 0);

                if (!isMessageSent)
                {
                    throw AgainException.Create();
                }
            }
            finally
            {
                msg.Close();
            }
        }
Exemple #11
0
        public int Get(ContextOption option)
        {
            int rc = 0;

            if (option == ContextOption.MaxSockets)
            {
                rc = m_maxSockets;
            }
            else
            if (option == ContextOption.IOThreads)
            {
                rc = m_ioThreadCount;
            }
            else
            {
                throw InvalidException.Create("option = " + option);
            }
            return(rc);
        }
Exemple #12
0
 protected override void XSetSocketOption(ZmqSocketOptions option, Object optval)
 {
     if (option != ZmqSocketOptions.RouterMandatory && option != ZmqSocketOptions.RouterRawSocket)
     {
         throw InvalidException.Create();
     }
     if (option == ZmqSocketOptions.RouterRawSocket)
     {
         m_rawSocket = (bool)optval;
         if (m_rawSocket)
         {
             m_options.RecvIdentity = false;
             m_options.RawSocket    = true;
         }
     }
     else
     {
         m_mandatory = (bool)optval;
     }
 }
Exemple #13
0
 public void Set(ContextOption option, int optval)
 {
     if (option == ContextOption.MaxSockets && optval >= 1)
     {
         lock (m_optSync)
         {
             m_maxSockets = optval;
         }
     }
     else
     if (option == ContextOption.IOThreads && optval >= 0)
     {
         lock (m_optSync)
         {
             m_ioThreadCount = optval;
         }
     }
     else
     {
         throw InvalidException.Create("option = " + option);
     }
 }
        public bool TermEndpoint(String addr)
        {
            if (m_ctxTerminated)
            {
                throw TerminatingException.Create();
            }

            //  Check whether endpoint address passed to the function is valid.
            if (addr == null)
            {
                throw InvalidException.Create();
            }

            //  Process pending commands, if any, since there could be pending unprocessed process_own()'s
            //  (from launch_child() for example) we're asked to terminate now.
            ProcessCommands(0, false);

            if (!m_endpoints.ContainsKey(addr))
            {
                return(false);
            }

            foreach (var e in m_endpoints)
            {
                TermChild(e.Value);
            }

            m_endpoints.Clear();

            //  Find the endpoints range (if any) corresponding to the addr_ string.
            //Iterator<Entry<String, Own>> it = endpoints.entrySet().iterator();

            //while(it.hasNext()) {
            //    Entry<String, Own> e = it.next();
            //    term_child(e.getValue());
            //    it.remove();
            //}
            return(true);
        }
        public void Resolve(String name, bool ip4Only)
        {
            this.m_name = name;

            int hash = name.GetHashCode();

            if (hash < 0)
            {
                hash = -hash;
            }
            hash  = hash % 55536;
            hash += 10000;


            try
            {
                Address = new IPEndPoint(IPAddress.Loopback, hash);
            }
            catch (Exception ex)
            {
                throw InvalidException.Create(ex);
            }
        }
Exemple #16
0
        public void Resolve(string name, bool ip4Only)
        {
            m_network = name;

            int delimiter = name.LastIndexOf(':');

            if (delimiter < 0)
            {
                throw InvalidException.Create();
            }

            //  Separate the address/port.
            String addrStr = name.Substring(0, delimiter);
            String portStr = name.Substring(delimiter + 1);

            if (addrStr.Contains(";"))
            {
                int    semiColonDelimiter = addrStr.IndexOf(";");
                string interfaceIP        = addrStr.Substring(0, semiColonDelimiter);
                addrStr = addrStr.Substring(semiColonDelimiter + 1);

                InterfaceAddress = IPAddress.Parse(interfaceIP);
            }
            else
            {
                InterfaceAddress = null;
            }

            //  Remove square brackets around the address, if any.
            if (addrStr.Length >= 2 && addrStr[0] == '[' &&
                addrStr[addrStr.Length - 1] == ']')
            {
                addrStr = addrStr.Substring(1, addrStr.Length - 2);
            }

            int port;

            //  Allow 0 specifically, to detect invalid port error in atoi if not
            if (portStr.Equals("*") || portStr.Equals("0"))
            {
                //  Resolve wildcard to 0 to allow autoselection of port
                port = 0;
            }
            else
            {
                //  Parse the port number (0 is not a valid port).
                port = Convert.ToInt32(portStr);
                if (port == 0)
                {
                    throw InvalidException.Create();
                }
            }

            IPEndPoint addrNet = null;

            if (addrStr.Equals("*"))
            {
                addrStr = "0.0.0.0";
            }

            IPAddress ipAddress;

            if (!IPAddress.TryParse(addrStr, out ipAddress))
            {
                throw InvalidException.Create();
            }

            addrNet = new IPEndPoint(ipAddress, port);

            Address = addrNet;
        }
        public Object GetSocketOption(ZmqSocketOptions option)
        {
            switch (option)
            {
            case ZmqSocketOptions.SendHighWatermark:
                return(SendHighWatermark);

            case ZmqSocketOptions.ReceivevHighWatermark:
                return(ReceiveHighWatermark);

            case ZmqSocketOptions.Affinity:
                return(Affinity);

            case ZmqSocketOptions.Identity:
                return(Identity);

            case ZmqSocketOptions.Rate:
                return(Rate);

            case ZmqSocketOptions.RecoveryIvl:
                return(RecoveryIvl);

            case ZmqSocketOptions.SendBuffer:
                return(SendBuffer);

            case ZmqSocketOptions.ReceivevBuffer:
                return(ReceiveBuffer);

            case ZmqSocketOptions.Type:
                return(SocketType);

            case ZmqSocketOptions.Linger:
                return(Linger);

            case ZmqSocketOptions.ReconnectIvl:
                return(ReconnectIvl);

            case ZmqSocketOptions.ReconnectIvlMax:
                return(ReconnectIvlMax);

            case ZmqSocketOptions.Backlog:
                return(Backlog);

            case ZmqSocketOptions.Maxmsgsize:
                return(Maxmsgsize);

            case ZmqSocketOptions.MulticastHops:
                return(MulticastHops);

            case ZmqSocketOptions.ReceiveTimeout:
                return(ReceiveTimeout);

            case ZmqSocketOptions.SendTimeout:
                return(SendTimeout);

            case ZmqSocketOptions.IPv4Only:
                return(IPv4Only);

            case ZmqSocketOptions.TcpKeepalive:
                return(TcpKeepalive);

            case ZmqSocketOptions.DelayAttachOnConnect:
                return(DelayAttachOnConnect);

            case ZmqSocketOptions.TcpKeepaliveCnt:
                // not supported
                return(0);

            case ZmqSocketOptions.TcpKeepaliveIdle:
                return(TcpKeepaliveIdle);

            case ZmqSocketOptions.TcpKeepaliveIntvl:
                return(TcpKeepaliveIntvl);

            case ZmqSocketOptions.LastEndpoint:
                return(LastEndpoint);

            case ZmqSocketOptions.Endian:
                return(Endian);

            default:
                throw InvalidException.Create();
            }
        }
        public void SetSocketOption(ZmqSocketOptions option, Object optval)
        {
            switch (option)
            {
            case ZmqSocketOptions.SendHighWatermark:
                SendHighWatermark = (int)optval;
                break;

            case ZmqSocketOptions.ReceivevHighWatermark:
                ReceiveHighWatermark = (int)optval;
                break;

            case ZmqSocketOptions.Affinity:
                Affinity = (long)optval;
                break;

            case ZmqSocketOptions.Identity:
                byte[] val;

                if (optval is String)
                {
                    val = Encoding.ASCII.GetBytes((String)optval);
                }
                else if (optval is byte[])
                {
                    val = (byte[])optval;
                }
                else
                {
                    throw InvalidException.Create();
                }

                if (val.Length == 0 || val.Length > 255)
                {
                    throw InvalidException.Create();
                }
                Identity = new byte[val.Length];
                val.CopyTo(Identity, 0);
                IdentitySize = (byte)Identity.Length;
                break;

            case ZmqSocketOptions.Rate:
                Rate = (int)optval;
                break;

            case ZmqSocketOptions.RecoveryIvl:
                RecoveryIvl = (int)optval;
                break;

            case ZmqSocketOptions.SendBuffer:
                SendBuffer = (int)optval;
                break;

            case ZmqSocketOptions.ReceivevBuffer:
                ReceiveBuffer = (int)optval;
                break;

            case ZmqSocketOptions.Linger:
                Linger = (int)optval;
                break;

            case ZmqSocketOptions.ReconnectIvl:
                ReconnectIvl = (int)optval;

                if (ReconnectIvl < -1)
                {
                    throw InvalidException.Create();
                }

                break;

            case ZmqSocketOptions.ReconnectIvlMax:
                ReconnectIvlMax = (int)optval;

                if (ReconnectIvlMax < 0)
                {
                    throw InvalidException.Create();
                }

                break;

            case ZmqSocketOptions.Backlog:
                Backlog = (int)optval;
                break;

            case ZmqSocketOptions.Maxmsgsize:
                Maxmsgsize = (long)optval;
                break;

            case ZmqSocketOptions.MulticastHops:
                MulticastHops = (int)optval;
                break;

            case ZmqSocketOptions.ReceiveTimeout:
                ReceiveTimeout = (int)optval;
                break;

            case ZmqSocketOptions.SendTimeout:
                SendTimeout = (int)optval;
                break;

            case ZmqSocketOptions.IPv4Only:

                IPv4Only = (bool)optval;

                break;

            case ZmqSocketOptions.TcpKeepalive:

                TcpKeepalive = (int)optval;
                if (TcpKeepalive != -1 && TcpKeepalive != 0 && TcpKeepalive != 1)
                {
                    throw InvalidException.Create();
                }
                break;

            case ZmqSocketOptions.DelayAttachOnConnect:

                DelayAttachOnConnect = (bool)optval;

                break;

            case ZmqSocketOptions.TcpKeepaliveCnt:
                // not supported
                break;

            case ZmqSocketOptions.TcpKeepaliveIdle:
                TcpKeepaliveIdle = (int)optval;
                break;

            case ZmqSocketOptions.TcpKeepaliveIntvl:
                TcpKeepaliveIntvl = (int)optval;
                break;

            case ZmqSocketOptions.TcpAcceptFilter:
                String filterStr = (String)optval;
                if (filterStr == null)
                {
                    TcpAcceptFilters.Clear();
                }
                else if (filterStr.Length == 0 || filterStr.Length > 255)
                {
                    throw InvalidException.Create();
                }
                else
                {
                    TcpAddress.TcpAddressMask filter = new TcpAddress.TcpAddressMask();
                    filter.Resolve(filterStr, IPv4Only);
                    TcpAcceptFilters.Add(filter);
                }
                break;

            case ZmqSocketOptions.Endian:
                Endian = (Endianness)optval;
                break;

            default:
                throw InvalidException.Create();
            }
        }
Exemple #19
0
        public static SessionBase Create(IOThread ioThread, bool connect,
                                         SocketBase socket, Options options, Address addr)
        {
            SessionBase s;

            switch (options.SocketType)
            {
            case ZmqSocketType.Req:
                s = new Req.ReqSession(ioThread, connect,
                                       socket, options, addr);
                break;

            case ZmqSocketType.Dealer:
                s = new Dealer.DealerSession(ioThread, connect,
                                             socket, options, addr);
                break;

            case ZmqSocketType.Rep:
                s = new Rep.RepSession(ioThread, connect,
                                       socket, options, addr);
                break;

            case ZmqSocketType.Router:
                s = new Router.RouterSession(ioThread, connect,
                                             socket, options, addr);
                break;

            case ZmqSocketType.Pub:
                s = new Pub.PubSession(ioThread, connect,
                                       socket, options, addr);
                break;

            case ZmqSocketType.Xpub:
                s = new XPub.XPubSession(ioThread, connect,
                                         socket, options, addr);
                break;

            case ZmqSocketType.Sub:
                s = new Sub.SubSession(ioThread, connect,
                                       socket, options, addr);
                break;

            case ZmqSocketType.Xsub:
                s = new XSub.XSubSession(ioThread, connect,
                                         socket, options, addr);
                break;

            case ZmqSocketType.Push:
                s = new Push.PushSession(ioThread, connect,
                                         socket, options, addr);
                break;

            case ZmqSocketType.Pull:
                s = new Pull.PullSession(ioThread, connect,
                                         socket, options, addr);
                break;

            case ZmqSocketType.Pair:
                s = new Pair.PairSession(ioThread, connect,
                                         socket, options, addr);
                break;

            case ZmqSocketType.Stream:
                s = new Stream.StreamSession(ioThread, connect, socket, options, addr);
                break;

            default:
                throw InvalidException.Create("type=" + options.SocketType);
            }
            return(s);
        }
 //  The default implementation assumes there are no specific socket
 //  options for the particular socket type. If not so, overload this
 //  method.
 protected virtual void XSetSocketOption(ZmqSocketOptions option, Object optval)
 {
     throw InvalidException.Create();
 }
        public void Resolve(String name, bool ip4Only)
        {
            //  Find the ':' at end that separates address from the port number.
            int delimiter = name.LastIndexOf(':');

            if (delimiter < 0)
            {
                throw InvalidException.Create();
            }

            //  Separate the address/port.
            String addrStr = name.Substring(0, delimiter);
            String portStr = name.Substring(delimiter + 1);

            //  Remove square brackets around the address, if any.
            if (addrStr.Length >= 2 && addrStr[0] == '[' &&
                addrStr[addrStr.Length - 1] == ']')
            {
                addrStr = addrStr.Substring(1, addrStr.Length - 2);
            }

            int port;

            //  Allow 0 specifically, to detect invalid port error in atoi if not
            if (portStr.Equals("*") || portStr.Equals("0"))
            {
                //  Resolve wildcard to 0 to allow autoselection of port
                port = 0;
            }
            else
            {
                //  Parse the port number (0 is not a valid port).
                port = Convert.ToInt32(portStr);
                if (port == 0)
                {
                    throw InvalidException.Create();
                }
            }

            IPEndPoint addrNet = null;

            if (addrStr.Equals("*"))
            {
                addrStr = "0.0.0.0";
            }

            IPAddress ipAddress;

            if (!IPAddress.TryParse(addrStr, out ipAddress))
            {
                ipAddress = Dns.GetHostEntry(addrStr).AddressList.FirstOrDefault();

                if (ipAddress == null)
                {
                    throw InvalidException.Create(string.Format("Unable to find an IP address for {0}", name));
                }
            }

            addrNet = new IPEndPoint(ipAddress, port);

            //if (addr_net == null) {
            //    throw new ArgumentException(name_);
            //}

            Address = addrNet;
        }
Exemple #22
0
        public void TermEndpoint(String addr)
        {
            if (m_ctxTerminated)
            {
                throw TerminatingException.Create();
            }

            //  Check whether endpoint address passed to the function is valid.
            if (addr == null)
            {
                throw InvalidException.Create();
            }

            //  Process pending commands, if any, since there could be pending unprocessed process_own()'s
            //  (from launch_child() for example) we're asked to terminate now.
            ProcessCommands(0, false);

            string protocol;
            string address;

            DecodeAddress(addr, out address, out protocol);

            CheckProtocol(protocol);

            if (protocol == Address.InProcProtocol)
            {
                try
                {
                    UnregisterEndpoint(addr, this);
                    return;
                }
                catch (NetMQException ex)
                {
                    if (ex.ErrorCode != ErrorCode.ENOENT)
                    {
                        throw;
                    }
                }

                Pipe pipe;

                if (m_inprocs.TryGetValue(addr, out pipe))
                {
                    pipe.Terminate(true);
                    m_inprocs.Remove(addr);
                }
                else
                {
                    throw NetMQException.Create(ErrorCode.ENOENT);
                }
            }
            else
            {
                Own endpoint;

                if (m_endpoints.TryGetValue(addr, out endpoint))
                {
                    TermChild(endpoint);

                    m_endpoints.Remove(addr);
                }
                else
                {
                    throw NetMQException.Create(ErrorCode.ENOENT);
                }
            }
        }