/// <summary> /// Set the specified option on this socket - which must be either a SubScribe or an Unsubscribe. /// </summary> /// <param name="option">which option to set</param> /// <param name="optionValue">the value to set the option to</param> /// <returns><c>true</c> if successful</returns> /// <exception cref="InvalidException">optionValue must be a String or a byte-array.</exception> protected override bool XSetSocketOption(ZmqSocketOption option, object optionValue) { // Only subscribe/unsubscribe options are supported if (option != ZmqSocketOption.Subscribe && option != ZmqSocketOption.Unsubscribe) return false; byte[] topic; if (optionValue is string) topic = Encoding.ASCII.GetBytes((string)optionValue); else if (optionValue is byte[]) topic = (byte[])optionValue; else throw new InvalidException(string.Format("In Sub.XSetSocketOption({0},{1}), optionValue must be either a string or a byte-array.", option, (optionValue == null ? "null" : optionValue.ToString()))); // Create the subscription message. var msg = new Msg(); msg.InitPool(topic.Length + 1); msg.Put(option == ZmqSocketOption.Subscribe ? (byte)1 : (byte)0); msg.Put(topic, 1, topic.Length); try { // Pass it further on in the stack. var isMessageSent = base.XSend(ref msg); if (!isMessageSent) throw new Exception(string.Format("in Sub.XSetSocketOption({0}, {1}), XSend returned false.", option, optionValue)); } finally { msg.Close(); } return true; }
/// <summary> /// Set the specified option on this socket - which must be either a SubScribe or an Unsubscribe. /// </summary> /// <param name="option">which option to set</param> /// <param name="optionValue">the value to set the option to</param> /// <returns><c>true</c> if successful</returns> /// <exception cref="InvalidException">optionValue must be a String or a byte-array.</exception> /// <exception cref="AgainException">XSend must return true.</exception> protected override bool XSetSocketOption(ZmqSocketOption option, object optionValue) { // Only subscribe/unsubscribe options are supported if (option != ZmqSocketOption.Subscribe && option != ZmqSocketOption.Unsubscribe) return false; byte[] topic; if (optionValue is string) topic = Encoding.ASCII.GetBytes((string)optionValue); else if (optionValue is byte[]) topic = (byte[])optionValue; else throw new InvalidException(string.Format("In Sub.XSetSocketOption({0},{1}), optionValue must be either a string or a byte-array.", option, (optionValue == null ? "null" : optionValue.ToString()))); // Create the subscription message. var msg = new Msg(); msg.InitPool(topic.Length + 1); msg.Put(option == ZmqSocketOption.Subscribe ? (byte)1 : (byte)0); msg.Put(topic, 1, topic.Length); try { // Pass it further on in the stack. bool isMessageSent = base.XSend(ref msg); if (!isMessageSent) { string xMsg = string.Format("in Sub.XSetSocketOption({0}, {1}), XSend returned false.", option, optionValue); // TODO: should we change the exception that is thrown here as AgainException is obsolete? #pragma warning disable 618 throw new AgainException(innerException: null, message: xMsg); #pragma warning restore 618 } } finally { msg.Close(); } return true; }
/// <summary> /// Assign the given integer value to the specified <see cref="ZmqSocketOption"/>. /// </summary> /// <param name="option">a ZmqSocketOption that specifies what to set</param> /// <param name="value">an integer that is the value to set that option to</param> /// <exception cref="TerminatingException">The socket has been stopped.</exception> /// <exception cref="ObjectDisposedException">This object is already disposed.</exception> internal void SetSocketOption(ZmqSocketOption option, int value) { m_socketHandle.CheckDisposed(); m_socketHandle.SetSocketOption(option, value); }
/// <summary> /// Get the 64-bit integer-value of the specified <see cref="ZmqSocketOption"/>. /// </summary> /// <param name="option">a ZmqSocketOption that specifies what to get</param> /// <returns>a long that is the value of that option</returns> /// <exception cref="TerminatingException">The socket has been stopped.</exception> internal long GetSocketOptionLong(ZmqSocketOption option) { return(GetSocketOptionX <long>(option)); }
/// <summary> /// Set the specified option on this socket. /// </summary> /// <param name="option">which option to set</param> /// <param name="optionValue">the value to set the option to</param> /// <returns><c>true</c> if successful</returns> /// <exception cref="InvalidException">optionValue must be a byte-array.</exception> protected override bool XSetSocketOption(ZmqSocketOption option, object optionValue) { switch (option) { case ZmqSocketOption.XpubVerbose: { this.m_verbose = (bool)optionValue; return(true); } case ZmqSocketOption.XPublisherManual: { this.m_manual = (bool)optionValue; return(true); } case ZmqSocketOption.XPublisherBroadcast: { this.m_broadcastEnabled = (bool)optionValue; return(true); } case ZmqSocketOption.Identity: { if (this.m_manual && this.m_lastPipe != null) { byte[] val; if (optionValue is string) { val = Encoding.ASCII.GetBytes((string)optionValue); } else if (optionValue is byte[]) { val = (byte[])optionValue; } else { throw new InvalidException($"In XPub.XSetSocketOption(Identity, {optionValue?.ToString() ?? "null"}) optionValue must be a string or byte-array."); } if (val.Length == 0 || val.Length > 255) { throw new InvalidException($"In XPub.XSetSocketOption(Identity,) optionValue yielded a byte-array of length {val.Length}, should be 1..255."); } this.m_lastPipe.Identity = val; this.m_options.Identity = val; } return(true); } case ZmqSocketOption.Subscribe: { if (this.m_manual && this.m_lastPipe != null) { byte[] subscription = optionValue as byte[] ?? Encoding.ASCII.GetBytes((string)optionValue); this.m_subscriptions.Add(subscription, 0, subscription.Length, this.m_lastPipe); this.m_lastPipe = null; return(true); } break; } case ZmqSocketOption.Unsubscribe: { if (this.m_manual && this.m_lastPipe != null) { byte[] subscription = optionValue as byte[] ?? Encoding.ASCII.GetBytes((string)optionValue); this.m_subscriptions.Remove(subscription, 0, subscription.Length, this.m_lastPipe); this.m_lastPipe = null; return(true); } break; } case ZmqSocketOption.XPublisherWelcomeMessage: { this.m_welcomeMessage.Close(); if (optionValue != null) { byte[] bytes = optionValue as byte[]; if (bytes == null) { throw new InvalidException($"In XPub.XSetSocketOption({option},{optionValue}), optionValue must be a byte-array."); } byte[] welcomeBytes = new byte[bytes.Length]; bytes.CopyTo(welcomeBytes, 0); this.m_welcomeMessage.InitGC(welcomeBytes, welcomeBytes.Length); } else { this.m_welcomeMessage.InitEmpty(); } return(true); } } return(false); }
/// <summary> /// Return the value of the specified option as an Object. /// </summary> /// <param name="option">which option to get</param> /// <returns>the value of the option</returns> /// <exception cref="TerminatingException">The socket has already been stopped.</exception> /// <remarks> /// If the Handle option is specified, then return the handle of the contained mailbox. /// If the Events option is specified, then process any outstanding commands, and return -1 if that throws a TerminatingException. /// then return a PollEvents that is the bitwise-OR of the PollEvents.PollOut and PollEvents.PollIn flags. /// </remarks> public object GetSocketOptionX(ZmqSocketOption option) { CheckContextTerminated(); if (option == ZmqSocketOption.ReceiveMore) { return m_rcvMore; } if (option == ZmqSocketOption.Handle) { return m_mailbox.Handle; } if (option == ZmqSocketOption.Events) { try { ProcessCommands(0, false); } catch (TerminatingException) { return -1; } PollEvents val = 0; if (HasOut()) val |= PollEvents.PollOut; if (HasIn()) val |= PollEvents.PollIn; return val; } // If the socket type doesn't support the option, pass it to // the generic option parser. return m_options.GetSocketOption(option); }
/// <summary> /// Set the specified socket option. /// </summary> /// <param name="option">which option to set</param> /// <param name="optionValue">the value to set the option to</param> /// <exception cref="TerminatingException">The socket has been stopped.</exception> public void SetSocketOption(ZmqSocketOption option, object optionValue) { CheckContextTerminated(); // First, check whether specific socket type overloads the option. if (!XSetSocketOption(option, optionValue)) { // If the socket type doesn't support the option, pass it to // the generic option parser. m_options.SetSocketOption(option, optionValue); } }
/// <summary> /// Get the value of the specified option. /// </summary> /// <param name="option">a ZmqSocketOption that specifies what to get</param> /// <returns>an Object that is the value of that option</returns> /// <exception cref="InvalidException">A valid option must be specified.</exception> public object GetSocketOption(ZmqSocketOption option) { switch (option) { case ZmqSocketOption.SendHighWatermark: return SendHighWatermark; case ZmqSocketOption.ReceiveHighWatermark: return ReceiveHighWatermark; case ZmqSocketOption.SendLowWatermark: return SendLowWatermark; case ZmqSocketOption.ReceiveLowWatermark: return ReceiveLowWatermark; case ZmqSocketOption.Affinity: return Affinity; case ZmqSocketOption.Identity: return Identity; case ZmqSocketOption.Rate: return Rate; case ZmqSocketOption.RecoveryIvl: return RecoveryIvl; case ZmqSocketOption.SendBuffer: return SendBuffer; case ZmqSocketOption.ReceiveBuffer: return ReceiveBuffer; case ZmqSocketOption.Type: return SocketType; case ZmqSocketOption.Linger: return Linger; case ZmqSocketOption.ReconnectIvl: return ReconnectIvl; case ZmqSocketOption.ReconnectIvlMax: return ReconnectIvlMax; case ZmqSocketOption.Backlog: return Backlog; case ZmqSocketOption.MaxMessageSize: return MaxMessageSize; case ZmqSocketOption.MulticastHops: return MulticastHops; case ZmqSocketOption.SendTimeout: return SendTimeout; case ZmqSocketOption.IPv4Only: return IPv4Only; case ZmqSocketOption.TcpKeepalive: return TcpKeepalive; case ZmqSocketOption.DelayAttachOnConnect: return DelayAttachOnConnect; case ZmqSocketOption.TcpKeepaliveIdle: return TcpKeepaliveIdle; case ZmqSocketOption.TcpKeepaliveIntvl: return TcpKeepaliveIntvl; case ZmqSocketOption.LastEndpoint: return LastEndpoint; case ZmqSocketOption.Endian: return Endian; case ZmqSocketOption.DisableTimeWait: return DisableTimeWait; default: throw new InvalidException("GetSocketOption called with invalid ZmqSocketOption of " + option); } }
/// <summary> /// Set the specified option on this socket. /// </summary> /// <param name="option">which option to set</param> /// <param name="optionValue">the value to set the option to</param> /// <returns><c>true</c> if successful</returns> /// <exception cref="InvalidException">optionValue must be a byte-array.</exception> protected override bool XSetSocketOption(ZmqSocketOption option, object optionValue) { switch (option) { case ZmqSocketOption.XpubVerbose: { m_verbose = (bool)optionValue; return(true); } case ZmqSocketOption.XPublisherManual: { m_manual = (bool)optionValue; return(true); } case ZmqSocketOption.XPublisherBroadcast: { m_broadcastEnabled = (bool)optionValue; return(true); } case ZmqSocketOption.Subscribe: { if (m_manual && m_lastPipe != null) { var subscription = optionValue as byte[] ?? Encoding.ASCII.GetBytes((string)optionValue); m_subscriptions.Add(subscription, 0, subscription.Length, m_lastPipe); m_lastPipe = null; return(true); } break; } case ZmqSocketOption.Unsubscribe: { if (m_manual && m_lastPipe != null) { var subscription = optionValue as byte[] ?? Encoding.ASCII.GetBytes((string)optionValue); m_subscriptions.Remove(subscription, 0, subscription.Length, m_lastPipe); m_lastPipe = null; return(true); } break; } case ZmqSocketOption.XPublisherWelcomeMessage: { m_welcomeMessage.Close(); if (optionValue != null) { var bytes = optionValue as byte[]; if (bytes == null) { throw new InvalidException(string.Format("In XPub.XSetSocketOption({0},{1}), optionValue must be a byte-array.", option, optionValue)); } var welcomeBytes = new byte[bytes.Length]; bytes.CopyTo(welcomeBytes, 0); m_welcomeMessage.InitGC(welcomeBytes, welcomeBytes.Length); } else { m_welcomeMessage.InitEmpty(); } return(true); } } return(false); }
/// <summary> /// Assign the given Object value to the specified <see cref="ZmqSocketOption"/>. /// </summary> /// <param name="option">a ZmqSocketOption that specifies what to set</param> /// <param name="value">an object that is the value to set that option to</param> /// <exception cref="TerminatingException">The socket has been stopped.</exception> /// <exception cref="ObjectDisposedException">This object is already disposed.</exception> internal void SetSocketOption(ZmqSocketOption option, object value) { m_socket.CheckDisposed(); m_socket.SetSocketOption(option, value); }
/// <summary> /// Get the value of the specified option. /// </summary> /// <param name="option">a ZmqSocketOption that specifies what to get</param> /// <returns>an Object that is the value of that option</returns> /// <exception cref="InvalidException">A valid option must be specified.</exception> public object GetSocketOption(ZmqSocketOption option) { switch (option) { case ZmqSocketOption.SendHighWatermark: return(SendHighWatermark); case ZmqSocketOption.ReceiveHighWatermark: return(ReceiveHighWatermark); case ZmqSocketOption.SendLowWatermark: return(SendLowWatermark); case ZmqSocketOption.ReceiveLowWatermark: return(ReceiveLowWatermark); case ZmqSocketOption.Affinity: return(Affinity); case ZmqSocketOption.Identity: return(Identity); case ZmqSocketOption.Rate: return(Rate); case ZmqSocketOption.RecoveryIvl: return(RecoveryIvl); case ZmqSocketOption.SendBuffer: return(SendBuffer); case ZmqSocketOption.ReceiveBuffer: return(ReceiveBuffer); case ZmqSocketOption.Type: return(SocketType); case ZmqSocketOption.Linger: return(Linger); case ZmqSocketOption.ReconnectIvl: return(ReconnectIvl); case ZmqSocketOption.ReconnectIvlMax: return(ReconnectIvlMax); case ZmqSocketOption.Backlog: return(Backlog); case ZmqSocketOption.MaxMessageSize: return(MaxMessageSize); case ZmqSocketOption.MulticastHops: return(MulticastHops); case ZmqSocketOption.SendTimeout: return(SendTimeout); case ZmqSocketOption.IPv4Only: return(IPv4Only); case ZmqSocketOption.TcpKeepalive: return(TcpKeepalive); case ZmqSocketOption.DelayAttachOnConnect: return(DelayAttachOnConnect); case ZmqSocketOption.TcpKeepaliveIdle: return(TcpKeepaliveIdle); case ZmqSocketOption.TcpKeepaliveIntvl: return(TcpKeepaliveIntvl); case ZmqSocketOption.LastEndpoint: return(LastEndpoint); case ZmqSocketOption.Endian: return(Endian); case ZmqSocketOption.DisableTimeWait: return(DisableTimeWait); default: throw new InvalidException("GetSocketOption called with invalid ZmqSocketOption of " + option); } }
/// <summary> /// Assign the given optionValue to the specified option. /// </summary> /// <param name="option">a ZmqSocketOption that specifies what to set</param> /// <param name="optionValue">an Object that is the value to set that option to</param> /// <exception cref="InvalidException">The option and optionValue must be valid.</exception> public void SetSocketOption(ZmqSocketOption option, object optionValue) { switch (option) { case ZmqSocketOption.SendHighWatermark: SendHighWatermark = (int)optionValue; break; case ZmqSocketOption.ReceiveHighWatermark: ReceiveHighWatermark = (int)optionValue; break; case ZmqSocketOption.SendLowWatermark: SendLowWatermark = (int)optionValue; break; case ZmqSocketOption.ReceiveLowWatermark: ReceiveLowWatermark = (int)optionValue; break; case ZmqSocketOption.Affinity: Affinity = (long)optionValue; break; case ZmqSocketOption.Identity: byte[] val; if (optionValue is string) { val = Encoding.ASCII.GetBytes((string)optionValue); } else if (optionValue is byte[]) { val = (byte[])optionValue; } else { throw new InvalidException($"In Options.SetSocketOption(Identity, {optionValue?.ToString() ?? "null"}) optionValue must be a string or byte-array."); } if (val.Length == 0 || val.Length > 255) { throw new InvalidException($"In Options.SetSocketOption(Identity,) optionValue yielded a byte-array of length {val.Length}, should be 1..255."); } Identity = new byte[val.Length]; val.CopyTo(Identity, 0); IdentitySize = (byte)Identity.Length; break; case ZmqSocketOption.Rate: Rate = (int)optionValue; break; case ZmqSocketOption.RecoveryIvl: RecoveryIvl = (int)optionValue; break; case ZmqSocketOption.SendBuffer: SendBuffer = (int)optionValue; break; case ZmqSocketOption.ReceiveBuffer: ReceiveBuffer = (int)optionValue; break; case ZmqSocketOption.Linger: Linger = (int)optionValue; break; case ZmqSocketOption.ReconnectIvl: var reconnectIvl = (int)optionValue; if (reconnectIvl < -1) { throw new InvalidException($"Options.SetSocketOption(ReconnectIvl, {reconnectIvl}) optionValue must be >= -1."); } ReconnectIvl = reconnectIvl; break; case ZmqSocketOption.ReconnectIvlMax: var reconnectIvlMax = (int)optionValue; if (reconnectIvlMax < 0) { throw new InvalidException($"Options.SetSocketOption(ReconnectIvlMax, {reconnectIvlMax}) optionValue must be non-negative."); } ReconnectIvlMax = reconnectIvlMax; break; case ZmqSocketOption.Backlog: Backlog = (int)optionValue; break; case ZmqSocketOption.MaxMessageSize: MaxMessageSize = (long)optionValue; break; case ZmqSocketOption.MulticastHops: MulticastHops = (int)optionValue; break; case ZmqSocketOption.SendTimeout: SendTimeout = (int)optionValue; break; case ZmqSocketOption.IPv4Only: IPv4Only = (bool)optionValue; break; case ZmqSocketOption.TcpKeepalive: var tcpKeepalive = (int)optionValue; if (tcpKeepalive != -1 && tcpKeepalive != 0 && tcpKeepalive != 1) { throw new InvalidException($"Options.SetSocketOption(TcpKeepalive, {tcpKeepalive}) optionValue is neither -1, 0, nor 1."); } TcpKeepalive = tcpKeepalive; break; case ZmqSocketOption.DelayAttachOnConnect: DelayAttachOnConnect = (bool)optionValue; break; case ZmqSocketOption.TcpKeepaliveIdle: TcpKeepaliveIdle = (int)optionValue; break; case ZmqSocketOption.TcpKeepaliveIntvl: TcpKeepaliveIntvl = (int)optionValue; break; case ZmqSocketOption.Endian: Endian = (Endianness)optionValue; break; case ZmqSocketOption.DisableTimeWait: DisableTimeWait = (bool)optionValue; break; default: throw new InvalidException("Options.SetSocketOption called with invalid ZmqSocketOption of " + option); } }
/// <summary> /// Set the specified option on this socket. /// </summary> /// <param name="option">which option to set</param> /// <param name="optionValue">the value to set the option to</param> /// <returns><c>true</c> if successful</returns> /// <exception cref="InvalidException">optionValue must be a byte-array.</exception> protected override bool XSetSocketOption(ZmqSocketOption option, object?optionValue) { T Get <T>() => optionValue is T v ? v : throw new ArgumentException($"Option {option} value must be of type {typeof(T).Name}."); switch (option) { case ZmqSocketOption.XpubVerbose: { m_verbose = Get <bool>(); return(true); } case ZmqSocketOption.XPublisherManual: { m_manual = Get <bool>(); return(true); } case ZmqSocketOption.XPublisherBroadcast: { m_broadcastEnabled = Get <bool>(); return(true); } case ZmqSocketOption.Identity: { if (m_manual && m_lastPipe != null) { byte[] val; if (optionValue is string) { val = Encoding.ASCII.GetBytes((string)optionValue); } else if (optionValue is byte[]) { val = (byte[])optionValue; } else { throw new InvalidException($"In XPub.XSetSocketOption(Identity, {optionValue?.ToString() ?? "null"}) optionValue must be a string or byte-array."); } if (val.Length == 0 || val.Length > 255) { throw new InvalidException($"In XPub.XSetSocketOption(Identity,) optionValue yielded a byte-array of length {val.Length}, should be 1..255."); } m_lastPipe.Identity = val; m_options.Identity = val; } return(true); } case ZmqSocketOption.Subscribe: { if (m_manual && m_lastPipe != null) { var subscription = optionValue as byte[] ?? Encoding.ASCII.GetBytes(Get <string>()); m_subscriptions.Add(subscription, m_lastPipe); m_lastPipe = null; return(true); } break; } case ZmqSocketOption.Unsubscribe: { if (m_manual && m_lastPipe != null) { var subscription = optionValue as byte[] ?? Encoding.ASCII.GetBytes(Get <string>()); m_subscriptions.Remove(subscription, m_lastPipe); m_lastPipe = null; return(true); } break; } case ZmqSocketOption.XPublisherWelcomeMessage: { m_welcomeMessage.Close(); if (optionValue != null) { var bytes = optionValue as byte[]; if (bytes == null) { throw new InvalidException($"In XPub.XSetSocketOption({option},{optionValue}), optionValue must be a byte-array."); } var welcomeBytes = new byte[bytes.Length]; bytes.CopyTo(welcomeBytes, 0); m_welcomeMessage.InitGC(welcomeBytes, welcomeBytes.Length); } else { m_welcomeMessage.InitEmpty(); } return(true); } } return(false); }
/// <summary> /// Get the value of the specified option. /// </summary> /// <param name="option">a ZmqSocketOption that specifies what to get</param> /// <returns>an Object that is the value of that option</returns> /// <exception cref="InvalidException">A valid option must be specified.</exception> public object?GetSocketOption(ZmqSocketOption option) { switch (option) { case ZmqSocketOption.SendHighWatermark: return(SendHighWatermark); case ZmqSocketOption.ReceiveHighWatermark: return(ReceiveHighWatermark); case ZmqSocketOption.SendLowWatermark: return(SendLowWatermark); case ZmqSocketOption.ReceiveLowWatermark: return(ReceiveLowWatermark); case ZmqSocketOption.Affinity: return(Affinity); case ZmqSocketOption.Identity: return(Identity); case ZmqSocketOption.Rate: return(Rate); case ZmqSocketOption.RecoveryIvl: return(RecoveryIvl); case ZmqSocketOption.SendBuffer: return(SendBuffer); case ZmqSocketOption.ReceiveBuffer: return(ReceiveBuffer); case ZmqSocketOption.Type: return(SocketType); case ZmqSocketOption.Linger: return(Linger); case ZmqSocketOption.ReconnectIvl: return(ReconnectIvl); case ZmqSocketOption.ReconnectIvlMax: return(ReconnectIvlMax); case ZmqSocketOption.Backlog: return(Backlog); case ZmqSocketOption.MaxMessageSize: return(MaxMessageSize); case ZmqSocketOption.MulticastHops: return(MulticastHops); case ZmqSocketOption.SendTimeout: return(SendTimeout); case ZmqSocketOption.IPv4Only: return(IPv4Only); case ZmqSocketOption.TcpKeepalive: return(TcpKeepalive); case ZmqSocketOption.DelayAttachOnConnect: return(DelayAttachOnConnect); case ZmqSocketOption.TcpKeepaliveIdle: return(TcpKeepaliveIdle); case ZmqSocketOption.TcpKeepaliveIntvl: return(TcpKeepaliveIntvl); case ZmqSocketOption.LastEndpoint: return(LastEndpoint); case ZmqSocketOption.Endian: return(Endian); case ZmqSocketOption.DisableTimeWait: return(DisableTimeWait); case ZmqSocketOption.LastPeerRoutingId: return(LastPeerRoutingId); case ZmqSocketOption.HeartbeatInterval: return(HeartbeatInterval); case ZmqSocketOption.HeartbeatTtl: return(HeartbeatTtl * 100); case ZmqSocketOption.HeartbeatTimeout: if (HeartbeatTimeout == -1) { return(HeartbeatInterval); } return(HeartbeatTimeout); case ZmqSocketOption.CurveServer: return(Mechanism == MechanismType.Curve && AsServer); case ZmqSocketOption.CurvePublicKey: return(CurvePublicKey); case ZmqSocketOption.CurveSecretKey: return(CurveSecretKey); case ZmqSocketOption.CurveServerKey: return(CurveServerKey); default: throw new InvalidException("GetSocketOption called with invalid ZmqSocketOption of " + option); } }
/// <summary> /// Assign the given optionValue to the specified option. /// </summary> /// <param name="option">a ZmqSocketOption that specifies what to set</param> /// <param name="optionValue">an Object that is the value to set that option to</param> /// <exception cref="InvalidException">The option and optionValue must be valid.</exception> public void SetSocketOption(ZmqSocketOption option, object?optionValue) { T Get <T>() => optionValue is T v ? v : throw new ArgumentException($"Value for option {option} have type {typeof(T).Name}.", nameof(optionValue)); switch (option) { case ZmqSocketOption.SendHighWatermark: SendHighWatermark = Get <int>(); break; case ZmqSocketOption.ReceiveHighWatermark: ReceiveHighWatermark = Get <int>(); break; case ZmqSocketOption.SendLowWatermark: SendLowWatermark = Get <int>(); break; case ZmqSocketOption.ReceiveLowWatermark: ReceiveLowWatermark = Get <int>(); break; case ZmqSocketOption.Affinity: Affinity = Get <long>(); break; case ZmqSocketOption.Identity: byte[] val; if (optionValue is string) { val = Encoding.ASCII.GetBytes((string)optionValue); } else if (optionValue is byte[]) { val = (byte[])optionValue; } else { throw new InvalidException($"In Options.SetSocketOption(Identity, {optionValue?.ToString() ?? "null"}) optionValue must be a string or byte-array."); } if (val.Length == 0 || val.Length > 255) { throw new InvalidException($"In Options.SetSocketOption(Identity,) optionValue yielded a byte-array of length {val.Length}, should be 1..255."); } Identity = new byte[val.Length]; val.CopyTo(Identity, 0); break; case ZmqSocketOption.Rate: Rate = Get <int>(); break; case ZmqSocketOption.RecoveryIvl: RecoveryIvl = Get <int>(); break; case ZmqSocketOption.SendBuffer: SendBuffer = Get <int>(); break; case ZmqSocketOption.ReceiveBuffer: ReceiveBuffer = Get <int>(); break; case ZmqSocketOption.Linger: Linger = Get <int>(); break; case ZmqSocketOption.ReconnectIvl: var reconnectIvl = Get <int>(); if (reconnectIvl < -1) { throw new InvalidException($"Options.SetSocketOption(ReconnectIvl, {reconnectIvl}) optionValue must be >= -1."); } ReconnectIvl = reconnectIvl; break; case ZmqSocketOption.ReconnectIvlMax: var reconnectIvlMax = Get <int>(); if (reconnectIvlMax < 0) { throw new InvalidException($"Options.SetSocketOption(ReconnectIvlMax, {reconnectIvlMax}) optionValue must be non-negative."); } ReconnectIvlMax = reconnectIvlMax; break; case ZmqSocketOption.Backlog: Backlog = Get <int>(); break; case ZmqSocketOption.MaxMessageSize: MaxMessageSize = Get <long>(); break; case ZmqSocketOption.MulticastHops: MulticastHops = Get <int>(); break; case ZmqSocketOption.SendTimeout: SendTimeout = Get <int>(); break; case ZmqSocketOption.IPv4Only: IPv4Only = Get <bool>(); break; case ZmqSocketOption.TcpKeepalive: var tcpKeepalive = Get <int>(); if (tcpKeepalive != -1 && tcpKeepalive != 0 && tcpKeepalive != 1) { throw new InvalidException($"Options.SetSocketOption(TcpKeepalive, {tcpKeepalive}) optionValue is neither -1, 0, nor 1."); } TcpKeepalive = tcpKeepalive; break; case ZmqSocketOption.DelayAttachOnConnect: DelayAttachOnConnect = Get <bool>(); break; case ZmqSocketOption.TcpKeepaliveIdle: TcpKeepaliveIdle = Get <int>(); break; case ZmqSocketOption.TcpKeepaliveIntvl: TcpKeepaliveIntvl = Get <int>(); break; case ZmqSocketOption.Endian: Endian = Get <Endianness>(); break; case ZmqSocketOption.DisableTimeWait: DisableTimeWait = Get <bool>(); break; case ZmqSocketOption.PgmMaxTransportServiceDataUnitLength: PgmMaxTransportServiceDataUnitLength = Get <int>(); break; case ZmqSocketOption.HeartbeatInterval: HeartbeatInterval = Get <int>(); break; case ZmqSocketOption.HeartbeatTtl: // Convert this to deciseconds from milliseconds HeartbeatTtl = Get <int>(); HeartbeatTtl /= 100; break; case ZmqSocketOption.HeartbeatTimeout: HeartbeatTimeout = Get <int>(); break; case ZmqSocketOption.CurveServer: AsServer = Get <bool>(); Mechanism = AsServer ? MechanismType.Curve : MechanismType.Null; break; case ZmqSocketOption.CurvePublicKey: { var key = Get <byte[]>(); if (key.Length != 32) { throw new InvalidException("Curve key size must be 32 bytes"); } Mechanism = MechanismType.Curve; Buffer.BlockCopy(key, 0, CurvePublicKey, 0, 32); break; } case ZmqSocketOption.CurveSecretKey: { var key = Get <byte[]>(); if (key.Length != 32) { throw new InvalidException("Curve key size must be 32 bytes"); } Mechanism = MechanismType.Curve; Buffer.BlockCopy(key, 0, CurveSecretKey, 0, 32); break; } case ZmqSocketOption.CurveServerKey: { var key = Get <byte[]>(); if (key.Length != 32) { throw new InvalidException("Curve key size must be 32 bytes"); } Mechanism = MechanismType.Curve; Buffer.BlockCopy(key, 0, CurveServerKey, 0, 32); break; } case ZmqSocketOption.HelloMessage: { if (optionValue == null) { HelloMsg = null; } else { var helloMsg = Get <byte[]>(); HelloMsg = new byte[helloMsg.Length]; Buffer.BlockCopy(helloMsg, 0, HelloMsg, 0, helloMsg.Length); } break; } case ZmqSocketOption.Relaxed: { Relaxed = Get <bool>(); break; } case ZmqSocketOption.Correlate: { Correlate = Get <bool>(); break; } default: throw new InvalidException("Options.SetSocketOption called with invalid ZmqSocketOption of " + option); } }
protected override bool XSetSocketOption(ZmqSocketOption option, object optval) { switch (option) { case ZmqSocketOption.RouterRawSocket: m_rawSocket = (bool)optval; if (m_rawSocket) { m_options.RecvIdentity = false; m_options.RawSocket = true; } return true; case ZmqSocketOption.RouterMandatory: m_mandatory = (bool)optval; return true; } return false; }
/// <summary> /// Assign the given optionValue to the specified option. /// </summary> /// <param name="option">a ZmqSocketOption that specifies what to set</param> /// <param name="optionValue">an Object that is the value to set that option to</param> /// <exception cref="InvalidException">The option and optionValue must be valid.</exception> public void SetSocketOption(ZmqSocketOption option, object optionValue) { switch (option) { case ZmqSocketOption.SendHighWatermark: SendHighWatermark = (int)optionValue; break; case ZmqSocketOption.ReceiveHighWatermark: ReceiveHighWatermark = (int)optionValue; break; case ZmqSocketOption.SendLowWatermark: SendLowWatermark = (int)optionValue; break; case ZmqSocketOption.ReceiveLowWatermark: ReceiveLowWatermark = (int)optionValue; break; case ZmqSocketOption.Affinity: Affinity = (long)optionValue; break; case ZmqSocketOption.Identity: byte[] val; if (optionValue is string) val = Encoding.ASCII.GetBytes((string)optionValue); else if (optionValue is byte[]) val = (byte[])optionValue; else throw new InvalidException($"In Options.SetSocketOption(Identity, {optionValue?.ToString() ?? "null"}) optionValue must be a string or byte-array."); if (val.Length == 0 || val.Length > 255) throw new InvalidException($"In Options.SetSocketOption(Identity,) optionValue yielded a byte-array of length {val.Length}, should be 1..255."); Identity = new byte[val.Length]; val.CopyTo(Identity, 0); IdentitySize = (byte)Identity.Length; break; case ZmqSocketOption.Rate: Rate = (int)optionValue; break; case ZmqSocketOption.RecoveryIvl: RecoveryIvl = (int)optionValue; break; case ZmqSocketOption.SendBuffer: SendBuffer = (int)optionValue; break; case ZmqSocketOption.ReceiveBuffer: ReceiveBuffer = (int)optionValue; break; case ZmqSocketOption.Linger: Linger = (int)optionValue; break; case ZmqSocketOption.ReconnectIvl: var reconnectIvl = (int)optionValue; if (reconnectIvl < -1) throw new InvalidException($"Options.SetSocketOption(ReconnectIvl, {reconnectIvl}) optionValue must be >= -1."); ReconnectIvl = reconnectIvl; break; case ZmqSocketOption.ReconnectIvlMax: var reconnectIvlMax = (int)optionValue; if (reconnectIvlMax < 0) throw new InvalidException($"Options.SetSocketOption(ReconnectIvlMax, {reconnectIvlMax}) optionValue must be non-negative."); ReconnectIvlMax = reconnectIvlMax; break; case ZmqSocketOption.Backlog: Backlog = (int)optionValue; break; case ZmqSocketOption.MaxMessageSize: MaxMessageSize = (long)optionValue; break; case ZmqSocketOption.MulticastHops: MulticastHops = (int)optionValue; break; case ZmqSocketOption.SendTimeout: SendTimeout = (int)optionValue; break; case ZmqSocketOption.IPv4Only: IPv4Only = (bool)optionValue; break; case ZmqSocketOption.TcpKeepalive: var tcpKeepalive = (int)optionValue; if (tcpKeepalive != -1 && tcpKeepalive != 0 && tcpKeepalive != 1) throw new InvalidException($"Options.SetSocketOption(TcpKeepalive, {tcpKeepalive}) optionValue is neither -1, 0, nor 1."); TcpKeepalive = tcpKeepalive; break; case ZmqSocketOption.DelayAttachOnConnect: DelayAttachOnConnect = (bool)optionValue; break; case ZmqSocketOption.TcpKeepaliveIdle: TcpKeepaliveIdle = (int)optionValue; break; case ZmqSocketOption.TcpKeepaliveIntvl: TcpKeepaliveIntvl = (int)optionValue; break; case ZmqSocketOption.Endian: Endian = (Endianness)optionValue; break; case ZmqSocketOption.DisableTimeWait: DisableTimeWait = (bool)optionValue; break; default: throw new InvalidException("Options.SetSocketOption called with invalid ZmqSocketOption of " + option); } }
/// <summary> /// Assign the given TimeSpan to the specified <see cref="ZmqSocketOption"/>. /// </summary> /// <param name="option">a ZmqSocketOption that specifies what to set</param> /// <param name="value">a TimeSpan that is the value to set that option to</param> /// <exception cref="TerminatingException">The socket has been stopped.</exception> internal void SetSocketOptionTimeSpan(ZmqSocketOption option, TimeSpan value) { SetSocketOption(option, (int)value.TotalMilliseconds); }
/// <summary> /// The default implementation assumes there are no specific socket /// options for the particular socket type. If not so, overload this /// method. /// </summary> /// <param name="option">a ZmqSocketOption specifying which option to set</param> /// <param name="optionValue">an Object that is the value to set the option to</param> protected virtual bool XSetSocketOption(ZmqSocketOption option, [CanBeNull] object optionValue) { return false; }
/// <summary> /// Get the integer-value of the specified <see cref="ZmqSocketOption"/>. /// </summary> /// <param name="option">a ZmqSocketOption that specifies what to get</param> /// <returns>an integer that is the value of that option</returns> /// <exception cref="TerminatingException">The socket has been stopped.</exception> /// <exception cref="ObjectDisposedException">This object is already disposed.</exception> internal int GetSocketOption(ZmqSocketOption option) { m_socketHandle.CheckDisposed(); return(m_socketHandle.GetSocketOption(option)); }
/// <summary> /// Return the integer-value of the specified option. /// </summary> /// <param name="option">which option to get</param> /// <returns>the value of the specified option, or -1 if error</returns> /// <exception cref="TerminatingException">The socket has been stopped.</exception> /// <remarks> /// If the ReceiveMore option is specified, then 1 is returned if it is true, 0 if it is false. /// If the Events option is specified, then process any outstanding commands, and return -1 if that throws a TerminatingException. /// then return an integer that is the bitwise-OR of the PollEvents.PollOut and PollEvents.PollIn flags. /// Otherwise, cast the specified option value to an integer and return it. /// </remarks> public int GetSocketOption(ZmqSocketOption option) { CheckContextTerminated(); if (option == ZmqSocketOption.ReceiveMore) { return m_rcvMore ? 1 : 0; } if (option == ZmqSocketOption.Events) { try { ProcessCommands(0, false); } catch (TerminatingException) { return -1; } PollEvents val = 0; if (HasOut()) val |= PollEvents.PollOut; if (HasIn()) val |= PollEvents.PollIn; return (int)val; } return (int)GetSocketOptionX(option); }
/// <summary> /// Get the (generically-typed) value of the specified <see cref="ZmqSocketOption"/>. /// </summary> /// <param name="option">a ZmqSocketOption that specifies what to get</param> /// <returns>an object of the given type, that is the value of that option</returns> /// <exception cref="TerminatingException">The socket has been stopped.</exception> /// <exception cref="ObjectDisposedException">This object is already disposed.</exception> internal T GetSocketOptionX <T>(ZmqSocketOption option) { m_socketHandle.CheckDisposed(); return((T)m_socketHandle.GetSocketOptionX(option)); }
/// <summary> /// Set the specified option on this socket. /// </summary> /// <param name="option">which option to set</param> /// <param name="optionValue">the value to set the option to</param> /// <returns><c>true</c> if successful</returns> /// <exception cref="InvalidException">optionValue must be a byte-array.</exception> protected override bool XSetSocketOption(ZmqSocketOption option, object optionValue) { switch (option) { case ZmqSocketOption.XpubVerbose: { m_verbose = (bool)optionValue; return true; } case ZmqSocketOption.XPublisherManual: { m_manual = (bool)optionValue; return true; } case ZmqSocketOption.XPublisherBroadcast: { m_broadcastEnabled = (bool)optionValue; return true; } case ZmqSocketOption.Subscribe: { if (m_manual && m_lastPipe != null) { var subscription = optionValue as byte[] ?? Encoding.ASCII.GetBytes((string)optionValue); m_subscriptions.Add(subscription, 0, subscription.Length, m_lastPipe); m_lastPipe = null; return true; } break; } case ZmqSocketOption.Unsubscribe: { if (m_manual && m_lastPipe != null) { var subscription = optionValue as byte[] ?? Encoding.ASCII.GetBytes((string)optionValue); m_subscriptions.Remove(subscription, 0, subscription.Length, m_lastPipe); m_lastPipe = null; return true; } break; } case ZmqSocketOption.XPublisherWelcomeMessage: { m_welcomeMessage.Close(); if (optionValue != null) { var bytes = optionValue as byte[]; if (bytes == null) throw new InvalidException(string.Format("In XPub.XSetSocketOption({0},{1}), optionValue must be a byte-array.", option, optionValue)); var welcomeBytes = new byte[bytes.Length]; bytes.CopyTo(welcomeBytes, 0); m_welcomeMessage.InitGC(welcomeBytes, welcomeBytes.Length); } else { m_welcomeMessage.InitEmpty(); } return true; } } return false; }
/// <summary> /// Get the <see cref="TimeSpan"/> value of the specified ZmqSocketOption. /// </summary> /// <param name="option">a ZmqSocketOption that specifies what to get</param> /// <returns>a TimeSpan that is the value of that option</returns> /// <exception cref="TerminatingException">The socket has been stopped.</exception> internal TimeSpan GetSocketOptionTimeSpan(ZmqSocketOption option) { return(TimeSpan.FromMilliseconds(GetSocketOption(option))); }