示例#1
0
        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 new InvalidException();
                }

                if (val.Length == 0 || val.Length > 255)
                {
                    throw new InvalidException();
                }
                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 new InvalidException();
                }

                break;

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

                if (ReconnectIvlMax < 0)
                {
                    throw new InvalidException();
                }

                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 new InvalidException();
                }
                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 new InvalidException();
                }
                else
                {
                    TcpAddress.TcpAddressMask filter = new TcpAddress.TcpAddressMask();
                    filter.Resolve(filterStr, IPv4Only);
                    TcpAcceptFilters.Add(filter);
                }
                break;

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

            default:
                throw new InvalidException();
            }
        }
示例#2
0
        /// <summary>
        /// Assign the given optionValue to the specified option.
        /// </summary>
        /// <param name="option">a ZmqSocketOptions that specifies what to set</param>
        /// <param name="optionValue">an Object that is the value to set that option to</param>
        public void SetSocketOption(ZmqSocketOptions option, Object optionValue)
        {
            switch (option)
            {
            case ZmqSocketOptions.SendHighWatermark:
                SendHighWatermark = (int)optionValue;
                break;

            case ZmqSocketOptions.ReceiveHighWatermark:
                ReceiveHighWatermark = (int)optionValue;
                break;

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

            case ZmqSocketOptions.Identity:
                byte[] val;

                if (optionValue is String)
                {
                    val = Encoding.ASCII.GetBytes((String)optionValue);
                }
                else if (optionValue is byte[])
                {
                    val = (byte[])optionValue;
                }
                else
                {
                    String xMsg = String.Format("In Options.SetSocketOption(Identity, {0}) optionValue must be a String or byte-array.", optionValue == null ? "null" : optionValue.ToString());
                    throw new InvalidException(xMsg);
                }

                if (val.Length == 0 || val.Length > 255)
                {
                    String xMsg = String.Format("In Options.SetSocketOption(Identity,) optionValue yielded a byte-array of length {0}, should be 1..255.", val.Length);
                    throw new InvalidException(xMsg);
                }
                Identity = new byte[val.Length];
                val.CopyTo(Identity, 0);
                IdentitySize = (byte)Identity.Length;
                break;

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

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

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

            case ZmqSocketOptions.ReceiveBuffer:
                ReceiveBuffer = (int)optionValue;
                break;

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

            case ZmqSocketOptions.ReconnectIvl:
                ReconnectIvl = (int)optionValue;
                if (ReconnectIvl < -1)
                {
                    throw new InvalidException(String.Format("Options.SetSocketOption(ReconnectIvl, {0}) optionValue must be >= -1.", ReconnectIvl));
                }
                break;

            case ZmqSocketOptions.ReconnectIvlMax:
                ReconnectIvlMax = (int)optionValue;
                if (ReconnectIvlMax < 0)
                {
                    throw new InvalidException(String.Format("Options.SetSocketOption(ReconnectIvlMax, {0}) optionValue must be non-negative.", ReconnectIvlMax));
                }
                break;

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

            case ZmqSocketOptions.Maxmsgsize:
                MaxMessageSize = (long)optionValue;
                break;

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

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

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

            case ZmqSocketOptions.IPv4Only:
                IPv4Only = (bool)optionValue;
                break;

            case ZmqSocketOptions.TcpKeepalive:
                TcpKeepalive = (int)optionValue;
                if (TcpKeepalive != -1 && TcpKeepalive != 0 && TcpKeepalive != 1)
                {
                    throw new InvalidException(String.Format("Options.SetSocketOption(TcpKeepalive, {0}) optionValue is neither -1, 0, nor 1.", TcpKeepalive));
                }
                break;

            case ZmqSocketOptions.DelayAttachOnConnect:
                DelayAttachOnConnect = (bool)optionValue;
                break;

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

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

            case ZmqSocketOptions.TcpAcceptFilter:
                String filterStr = (String)optionValue;
                if (filterStr == null)
                {
                    TcpAcceptFilters.Clear();
                }
                else if (filterStr.Length == 0 || filterStr.Length > 255)
                {
                    throw new InvalidException(String.Format("Options.SetSocketOption(TcpAcceptFilter,{0}), optionValue has invalid length of {1) but must be 1..255", filterStr, filterStr.Length));
                }
                else
                {
                    TcpAddress.TcpAddressMask filter = new TcpAddress.TcpAddressMask();
                    filter.Resolve(filterStr, IPv4Only);
                    TcpAcceptFilters.Add(filter);
                }
                break;

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

            default:
                throw new InvalidException("Options.SetSocketOption called with invalid ZmqSocketOptions of " + option);
            }
        }