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(val.Length + 1); if (option == ZmqSocketOptions.Subscribe) msg.Put((byte)1); else if (option == ZmqSocketOptions.Unsubscribe) msg.Put((byte)0); msg.Put (val,1); // Pass it further on in the stack. base.XSend (msg, 0); }
protected override bool XSetSocketOption(ZmqSocketOptions option, Object optval) { if (option != ZmqSocketOptions.Subscribe && option != ZmqSocketOptions.Unsubscribe) { ZError.ErrorNumber = (ErrorNumber.EINVAL); return false; } byte[] val; if (optval is String) val = Encoding.ASCII.GetBytes ((String)optval); else if (optval is byte[]) val = (byte[]) optval; else throw new ArgumentException(); // Create the subscription message. Msg msg = new Msg(val.Length + 1); if (option == ZmqSocketOptions.Subscribe) msg.Put((byte)1); else if (option == ZmqSocketOptions.Unsubscribe) msg.Put((byte)0); msg.Put (val,1); // Pass it further on in the stack. bool rc = base.XSend (msg, 0); return rc; }
protected override bool XHasIn() { // We may already have a message pre-fetched. if (m_prefetched) { return(true); } // Try to read the next message. // The message, if read, is kept in the pre-fetch buffer. Pipe[] pipe = new Pipe[1]; bool isMessageAvailable = m_fq.RecvPipe(pipe, ref m_prefetchedMsg); if (!isMessageAvailable) { return(false); } Debug.Assert(pipe[0] != null); Debug.Assert(!m_prefetchedMsg.HasMore); Blob identity = pipe[0].Identity; m_prefetchedId = new Msg(); m_prefetchedId.InitPool(identity.Size); m_prefetchedId.Put(identity.Data, 0, identity.Size); m_prefetchedId.SetFlags(MsgFlags.More); m_prefetched = true; m_identitySent = false; return(true); }
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(val.Length + 1); if (option == ZmqSocketOptions.Subscribe) { msg.Put((byte)1); } else if (option == ZmqSocketOptions.Unsubscribe) { msg.Put((byte)0); } msg.Put(val, 1); // Pass it further on in the stack. bool isMessageSent = base.XSend(msg, 0); if (!isMessageSent) { throw AgainException.Create(); } }
static XSub() { s_sendSubscription = (data, size, arg) => { Pipe pipe = (Pipe) arg; // Create the subsctription message. Msg msg = new Msg(size + 1); msg.Put((byte)1); msg.Put(data,1, size); // Send it to the pipe. bool sent = pipe.Write (msg); // If we reached the SNDHWM, and thus cannot send the subscription, drop // the subscription message instead. This matches the behaviour of // zmq_setsockopt(ZMQ_SUBSCRIBE, ...), which also drops subscriptions // when the SNDHWM is reached. if (!sent) msg.Close (); }; }
public static void Send(SocketBase s, byte[] buf, int len, SendReceiveOptions flags) { if (s == null || !s.CheckTag()) { throw NetMQException.Create(ErrorCode.EFAULT); } Msg msg = new Msg(len); msg.Put(buf, 0, len); SendMsg(s, msg, flags); }
static XSub() { s_sendSubscription = (data, size, arg) => { Pipe pipe = (Pipe)arg; // Create the subsctription message. Msg msg = new Msg(size + 1); msg.Put((byte)1); msg.Put(data, 1, size); // Send it to the pipe. bool sent = pipe.Write(msg); // If we reached the SNDHWM, and thus cannot send the subscription, drop // the subscription message instead. This matches the behaviour of // zmq_setsockopt(ZMQ_SUBSCRIBE, ...), which also drops subscriptions // when the SNDHWM is reached. if (!sent) { msg.Close(); } }; }
protected override bool XHasIn() { // If we are in the middle of reading the messages, there are // definitely more parts available. if (m_moreIn) { return(true); } // We may already have a message pre-fetched. if (m_prefetched) { return(true); } // Try to read the next message. // The message, if read, is kept in the pre-fetch buffer. Pipe[] pipe = new Pipe[1]; bool isMessageAvailable = m_fq.RecvPipe(pipe, ref m_prefetchedMsg); // It's possible that we receive peer's identity. That happens // after reconnection. The current implementation assumes that // the peer always uses the same identity. // TODO: handle the situation when the peer changes its identity. while (isMessageAvailable && m_prefetchedMsg.IsIdentity) { isMessageAvailable = m_fq.RecvPipe(pipe, ref m_prefetchedMsg); } if (!isMessageAvailable) { return(false); } Debug.Assert(pipe[0] != null); Blob identity = pipe[0].Identity; m_prefetchedId = new Msg(); m_prefetchedId.InitPool(identity.Size); m_prefetchedId.Put(identity.Data, 0, identity.Size); m_prefetchedId.SetFlags(MsgFlags.More); m_prefetched = true; m_identitySent = false; return(true); }
protected override bool XRecv(SendReceiveOptions flags, ref Msg msg) { // If there is at least one if (m_pending.Count == 0) { return(false); } msg.Close(); Blob first = m_pending.Dequeue(); msg.InitPool(first.Size); msg.Put(first.Data, 0, first.Size); return(true); }
protected override bool XRecv(SendReceiveOptions flags, ref Msg msg) { if (m_prefetched) { if (!m_identitySent) { msg.Move(ref m_prefetchedId); m_identitySent = true; } else { msg.Move(ref m_prefetchedMsg); m_prefetched = false; } return(true); } Pipe[] pipe = new Pipe[1]; bool isMessageAvailable = m_fq.RecvPipe(pipe, ref m_prefetchedMsg); if (!isMessageAvailable) { return(false); } Debug.Assert(pipe[0] != null); Debug.Assert(!m_prefetchedMsg.HasMore); // We have received a frame with TCP data. // Rather than sendig this frame, we keep it in prefetched // buffer and send a frame with peer's ID. Blob identity = pipe[0].Identity; msg.InitPool(identity.Size); msg.Put(identity.Data, 0, identity.Size); msg.SetFlags(MsgFlags.More); m_prefetched = true; m_identitySent = true; return(true); }
public virtual bool PullMsg(ref Msg msg) { // First message to send is identity if (!m_identitySent) { msg.InitPool(m_options.IdentitySize); msg.Put(m_options.Identity, 0, m_options.IdentitySize); m_identitySent = true; m_incompleteIn = false; return(true); } if (m_pipe == null || !m_pipe.Read(ref msg)) { return(false); } m_incompleteIn = msg.HasMore; return(true); }
public virtual Msg PullMsg() { Msg msg; // First message to send is identity if (!m_identitySent) { msg = new Msg(m_options.IdentitySize); msg.Put(m_options.Identity, 0, m_options.IdentitySize); m_identitySent = true; m_incompleteIn = false; return(msg); } if (m_pipe == null || (msg = m_pipe.Read()) == null) { return(null); } m_incompleteIn = msg.HasMore; return(msg); }
public void PushMsg(Msg msg) { Debug.Assert(m_options.SocketType == ZmqSocketType.Pub || m_options.SocketType == ZmqSocketType.Xpub); // The first message is identity. // Let the session process it. m_session.PushMsg(msg); // Inject the subscription message so that the ZMQ 2.x peer // receives our messages. msg = new Msg(1); msg.Put((byte)1); m_session.PushMsg(msg); m_session.Flush(); // Once we have injected the subscription message, we can // Divert the message flow back to the session. Debug.Assert(m_decoder != null); m_decoder.SetMsgSink(m_session); }
public bool PushMsg(Msg msg) { Debug.Assert(m_options.SocketType == ZmqSocketType.Pub || m_options.SocketType == ZmqSocketType.Xpub); // The first message is identity. // Let the session process it. bool rc = m_session.PushMsg(msg); Debug.Assert(rc); // Inject the subscription message so that the ZMQ 2.x peer // receives our messages. msg = new Msg(1); msg.Put((byte) 1); rc = m_session.PushMsg(msg); m_session.Flush(); // Once we have injected the subscription message, we can // Divert the message flow back to the session. Debug.Assert(m_decoder != null); m_decoder.SetMsgSink(m_session); return rc; }
protected override bool XRecv(SendReceiveOptions flags, ref Msg msg) { if (m_prefetched) { if (!m_identitySent) { msg.Move(ref m_prefetchedId); m_identitySent = true; } else { msg.Move(ref m_prefetchedMsg); m_prefetched = false; } m_moreIn = msg.HasMore; return(true); } Pipe[] pipe = new Pipe[1]; bool isMessageAvailable = m_fq.RecvPipe(pipe, ref msg); // It's possible that we receive peer's identity. That happens // after reconnection. The current implementation assumes that // the peer always uses the same identity. // TODO: handle the situation when the peer changes its identity. while (isMessageAvailable && msg.IsIdentity) { isMessageAvailable = m_fq.RecvPipe(pipe, ref msg); } if (!isMessageAvailable) { return(false); } Debug.Assert(pipe[0] != null); // If we are in the middle of reading a message, just return the next part. if (m_moreIn) { m_moreIn = msg.HasMore; } else { // We are at the beginning of a message. // Keep the message part we have in the prefetch buffer // and return the ID of the peer instead. m_prefetchedMsg.Move(ref msg); m_prefetched = true; Blob identity = pipe[0].Identity; msg.InitPool(identity.Size); msg.Put(identity.Data, 0, identity.Size); msg.SetFlags(MsgFlags.More); m_identitySent = true; } return(true); }
public virtual Msg PullMsg() { Msg msg; // First message to send is identity if (!m_identitySent) { msg = new Msg(m_options.IdentitySize); msg.Put(m_options.Identity, 0, m_options.IdentitySize); m_identitySent = true; m_incompleteIn = false; return msg; } if (m_pipe == null || (msg = m_pipe.Read()) == null) { return null; } m_incompleteIn = msg.HasMore; return msg; }
public void Connect([NotNull] string addr) { CheckContextTerminated(); // Process pending commands, if any. ProcessCommands(0, false); string address; string protocol; DecodeAddress(addr, out address, out protocol); CheckProtocol(protocol); if (protocol.Equals(Address.InProcProtocol)) { // TODO: inproc connect is specific with respect to creating pipes // as there's no 'reconnect' functionality implemented. Once that // is in place we should follow generic pipe creation algorithm. // Find the peer endpoint. Ctx.Endpoint peer = FindEndpoint(addr); // The total HWM for an inproc connection should be the sum of // the binder's HWM and the connector's HWM. int sndhwm; int rcvhwm; if (m_options.SendHighWatermark == 0 || peer.Options.ReceiveHighWatermark == 0) { sndhwm = 0; } else { sndhwm = m_options.SendHighWatermark + peer.Options.ReceiveHighWatermark; } if (m_options.ReceiveHighWatermark == 0 || peer.Options.SendHighWatermark == 0) { rcvhwm = 0; } else { rcvhwm = m_options.ReceiveHighWatermark + peer.Options.SendHighWatermark; } // Create a bi-directional pipe to connect the peers. ZObject[] parents = { this, peer.Socket }; int[] highWaterMarks = { sndhwm, rcvhwm }; bool[] delays = { m_options.DelayOnDisconnect, m_options.DelayOnClose }; Pipe[] pipes = Pipe.PipePair(parents, highWaterMarks, delays); // Attach local end of the pipe to this socket object. AttachPipe(pipes[0]); // If required, send the identity of the peer to the local socket. if (peer.Options.RecvIdentity) { var id = new Msg(); id.InitPool(peer.Options.IdentitySize); id.Put(peer.Options.Identity, 0, peer.Options.IdentitySize); id.SetFlags(MsgFlags.Identity); bool written = pipes[0].Write(ref id); Debug.Assert(written); pipes[0].Flush(); } // If required, send the identity of the local socket to the peer. if (m_options.RecvIdentity) { var id = new Msg(); id.InitPool(m_options.IdentitySize); id.Put(m_options.Identity, 0, m_options.IdentitySize); id.SetFlags(MsgFlags.Identity); bool written = pipes[1].Write(ref id); Debug.Assert(written); pipes[1].Flush(); } // Attach remote end of the pipe to the peer socket. Note that peer's // seqnum was incremented in find_endpoint function. We don't need it // increased here. SendBind(peer.Socket, pipes[1], false); // Save last endpoint URI m_options.LastEndpoint = addr; // remember inproc connections for disconnect m_inprocs.Add(addr, pipes[0]); return; } // Choose the I/O thread to run the session in. IOThread ioThread = ChooseIOThread(m_options.Affinity); if (ioThread == null) { throw NetMQException.Create("Empty IO Thread", ErrorCode.EmptyThread); } var paddr = new Address(protocol, address); // Resolve address (if needed by the protocol) if (protocol.Equals(Address.TcpProtocol)) { paddr.Resolved = (new TcpAddress()); paddr.Resolved.Resolve( address, m_options.IPv4Only); } else if (protocol.Equals(Address.IpcProtocol)) { paddr.Resolved = (new IpcAddress()); paddr.Resolved.Resolve(address, true); } else if (protocol.Equals(Address.PgmProtocol) || protocol.Equals(Address.EpgmProtocol)) { if (m_options.SocketType == ZmqSocketType.Sub || m_options.SocketType == ZmqSocketType.Xsub) { Bind(addr); return; } paddr.Resolved = new PgmAddress(); paddr.Resolved.Resolve(address, m_options.IPv4Only); } // Create session. SessionBase session = SessionBase.Create(ioThread, true, this, m_options, paddr); Debug.Assert(session != null); // PGM does not support subscription forwarding; ask for all data to be // sent to this pipe. bool icanhasall = protocol.Equals(Address.PgmProtocol) || protocol.Equals(Address.EpgmProtocol); if (!m_options.DelayAttachOnConnect || icanhasall) { // Create a bi-directional pipe. ZObject[] parents = { this, session }; int[] hwms = { m_options.SendHighWatermark, m_options.ReceiveHighWatermark }; bool[] delays = { m_options.DelayOnDisconnect, m_options.DelayOnClose }; Pipe[] pipes = Pipe.PipePair(parents, hwms, delays); // Attach local end of the pipe to the socket object. AttachPipe(pipes[0], icanhasall); // Attach remote end of the pipe to the session object later on. session.AttachPipe(pipes[1]); } // Save last endpoint URI m_options.LastEndpoint = paddr.ToString(); AddEndpoint(addr, session); }
public static int Send(SocketBase s, byte[] buf, int len, SendRecieveOptions flags) { if (s == null || !s.CheckTag()) { throw new InvalidOperationException(); } Msg msg = new Msg(len); msg.Put(buf, 0, len); int rc = SendMsg(s, msg, flags); if (rc < 0) { return -1; } return rc; }
public void Connect(String addr) { if (m_ctxTerminated) { throw TerminatingException.Create(); } // Process pending commands, if any. ProcessCommands(0, false); string address; string protocol; DecodeAddress(addr, out address, out protocol); CheckProtocol(protocol); if (protocol.Equals(Address.InProcProtocol)) { // TODO: inproc connect is specific with respect to creating pipes // as there's no 'reconnect' functionality implemented. Once that // is in place we should follow generic pipe creation algorithm. // Find the peer endpoint. Ctx.Endpoint peer = FindEndpoint(addr); // The total HWM for an inproc connection should be the sum of // the binder's HWM and the connector's HWM. int sndhwm; int rcvhwm; if (m_options.SendHighWatermark == 0 || peer.Options.ReceiveHighWatermark == 0) sndhwm = 0; else sndhwm = m_options.SendHighWatermark + peer.Options.ReceiveHighWatermark; if (m_options.ReceiveHighWatermark == 0 || peer.Options.SendHighWatermark == 0) rcvhwm = 0; else rcvhwm = m_options.ReceiveHighWatermark + peer.Options.SendHighWatermark; // Create a bi-directional pipe to connect the peers. ZObject[] parents = { this, peer.Socket }; int[] hwms = { sndhwm, rcvhwm }; bool[] delays = { m_options.DelayOnDisconnect, m_options.DelayOnClose }; Pipe[] pipes = Pipe.PipePair(parents, hwms, delays); // Attach local end of the pipe to this socket object. AttachPipe(pipes[0]); // If required, send the identity of the peer to the local socket. if (peer.Options.RecvIdentity) { Msg id = new Msg(peer.Options.IdentitySize); id.Put(peer.Options.Identity, 0, peer.Options.IdentitySize); id.SetFlags(MsgFlags.Identity); bool written = pipes[0].Write(id); Debug.Assert(written); pipes[0].Flush(); } // If required, send the identity of the local socket to the peer. if (m_options.RecvIdentity) { Msg id = new Msg(m_options.IdentitySize); id.Put(m_options.Identity, 0, m_options.IdentitySize); id.SetFlags(MsgFlags.Identity); bool written = pipes[1].Write(id); Debug.Assert(written); pipes[1].Flush(); } // Attach remote end of the pipe to the peer socket. Note that peer's // seqnum was incremented in find_endpoint function. We don't need it // increased here. SendBind(peer.Socket, pipes[1], false); // Save last endpoint URI m_options.LastEndpoint = addr; return; } // Choose the I/O thread to run the session in. IOThread ioThread = ChooseIOThread(m_options.Affinity); if (ioThread == null) { throw NetMQException.Create("Empty IO Thread", ErrorCode.EMTHREAD); } Address paddr = new Address(protocol, address); // Resolve address (if needed by the protocol) if (protocol.Equals(Address.TcpProtocol)) { paddr.Resolved = (new TcpAddress()); paddr.Resolved.Resolve( address, m_options.IPv4Only); } else if (protocol.Equals(Address.IpcProtocol)) { paddr.Resolved = (new IpcAddress()); paddr.Resolved.Resolve(address, true); } else if (protocol.Equals(Address.PgmProtocol) || protocol.Equals(Address.EpgmProtocol)) { if (m_options.SocketType == ZmqSocketType.Sub || m_options.SocketType == ZmqSocketType.Xsub) { Bind(addr); return; } paddr.Resolved = new PgmAddress(); paddr.Resolved.Resolve(address, m_options.IPv4Only); } // Create session. SessionBase session = SessionBase.Create(ioThread, true, this, m_options, paddr); Debug.Assert(session != null); // PGM does not support subscription forwarding; ask for all data to be // sent to this pipe. bool icanhasall = protocol.Equals(Address.PgmProtocol) || protocol.Equals(Address.EpgmProtocol); if (!m_options.DelayAttachOnConnect || icanhasall) { // Create a bi-directional pipe. ZObject[] parents = { this, session }; int[] hwms = { m_options.SendHighWatermark, m_options.ReceiveHighWatermark }; bool[] delays = { m_options.DelayOnDisconnect, m_options.DelayOnClose }; Pipe[] pipes = Pipe.PipePair(parents, hwms, delays); // Attach local end of the pipe to the socket object. AttachPipe(pipes[0], icanhasall); // Attach remote end of the pipe to the session object later on. session.AttachPipe(pipes[1]); } // Save last endpoint URI m_options.LastEndpoint = paddr.ToString(); AddEndpoint(addr, session); return; }
public virtual bool PullMsg(ref Msg msg) { // First message to send is identity if (!m_identitySent) { msg.InitPool(m_options.IdentitySize); msg.Put(m_options.Identity, 0, m_options.IdentitySize); m_identitySent = true; m_incompleteIn = false; return true; } if (m_pipe == null || !m_pipe.Read(ref msg)) { return false; } m_incompleteIn = msg.HasMore; return true; }