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 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, out m_prefetchedMsg); if (!isMessageAvailable) { return(false); } // 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 (m_prefetchedMsg != null && m_prefetchedMsg.IsIdentity) { isMessageAvailable = m_fq.RecvPipe(pipe, out m_prefetchedMsg); if (!isMessageAvailable) { return(false); } } if (m_prefetchedMsg == null) { return(false); } Debug.Assert(pipe[0] != null); Blob identity = pipe[0].Identity; m_prefetchedId = new Msg(identity.Data); m_prefetchedId.SetFlags(MsgFlags.More); m_prefetched = true; m_identitySent = false; return(true); }
private bool FlagsReady() { m_tmpbuf.Reset(); // Store the flags from the wire into the message structure. int first = m_tmpbuf[0]; m_inProgress.SetFlags((MsgFlags)first & MsgFlags.More); NextStep(m_inProgress.Data, m_inProgress.Size, MessageReadyState); return(true); }
protected override bool XRecv(SendReceiveOptions flags, out Msg msg) { msg = null; if (m_prefetched) { if (!m_identitySent) { msg = m_prefetchedId; m_prefetchedId = null; m_identitySent = true; } else { msg = m_prefetchedMsg; m_prefetchedMsg = null; m_prefetched = false; } return(true); } Pipe[] pipe = new Pipe[1]; bool isMessageAvailable = m_fq.RecvPipe(pipe, out m_prefetchedMsg); if (!isMessageAvailable) { return(false); } else if (m_prefetchedMsg == null) { return(true); } 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 = new Msg(identity.Data, true); msg.SetFlags(MsgFlags.More); m_prefetched = true; m_identitySent = true; return(true); }
protected override bool XSend(ref Msg msg, SendReceiveOptions flags) { // If we've sent a request and we still haven't got the reply, // we can't send another request. if (m_receivingReply) { throw NetMQException.Create("Cannot send another request", ErrorCode.EFSM); } bool isMessageSent; // First part of the request is the request identity. if (m_messageBegins) { Msg bottom = new Msg(); bottom.InitEmpty(); bottom.SetFlags(MsgFlags.More); isMessageSent = base.XSend(ref bottom, 0); if (!isMessageSent) { return(false); } m_messageBegins = false; } bool more = msg.HasMore; isMessageSent = base.XSend(ref msg, flags); if (!isMessageSent) { return(false); } // If the request was fully sent, flip the FSM into reply-receiving state. else if (!more) { m_receivingReply = true; m_messageBegins = true; } return(true); }
public virtual void PushMsg(Msg msg) { // First message to receive is identity (if required). if (!m_identityReceived) { msg.SetFlags(MsgFlags.Identity); m_identityReceived = true; if (!m_options.RecvIdentity) { return; } } if (m_pipe != null && m_pipe.Write(msg)) { return; } throw AgainException.Create(); }
public virtual bool PushMsg(ref Msg msg) { // First message to receive is identity (if required). if (!m_identityReceived) { msg.SetFlags(MsgFlags.Identity); m_identityReceived = true; if (!m_options.RecvIdentity) { msg.Close(); msg.InitEmpty(); return(true); } } if (m_pipe != null && m_pipe.Write(ref msg)) { msg.InitEmpty(); return(true); } return(false); }
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, out m_prefetchedMsg); if (!isMessageAvailable) { return false; } // 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 (m_prefetchedMsg != null && m_prefetchedMsg.IsIdentity) { isMessageAvailable = m_fq.RecvPipe(pipe, out m_prefetchedMsg); if (!isMessageAvailable) { return false; } } if (m_prefetchedMsg == null) { return false; } Debug.Assert(pipe[0] != null); Blob identity = pipe[0].Identity; m_prefetchedId = new Msg(identity.Data); m_prefetchedId.SetFlags(MsgFlags.More); m_prefetched = true; m_identitySent = false; return true; }
protected override bool XSend(Msg msg, SendReceiveOptions flags) { // If we've sent a request and we still haven't got the reply, // we can't send another request. if (m_receivingReply) { throw NetMQException.Create("Cannot send another request", ErrorCode.EFSM); } bool isMessageSent; // First part of the request is the request identity. if (m_messageBegins) { Msg bottom = new Msg(); bottom.SetFlags(MsgFlags.More); isMessageSent = base.XSend(bottom, 0); if (!isMessageSent) { return false; } m_messageBegins = false; } bool more = msg.HasMore; isMessageSent = base.XSend(msg, flags); if (!isMessageSent) { return false; } // If the request was fully sent, flip the FSM into reply-receiving state. else if (!more) { m_receivingReply = true; m_messageBegins = true; } return true; }
protected override bool XRecv(SendReceiveOptions flags, out Msg msg) { msg = null; if (m_prefetched) { if (!m_identitySent) { msg = m_prefetchedId; m_prefetchedId = null; m_identitySent = true; } else { msg = m_prefetchedMsg; m_prefetchedMsg = null; m_prefetched = false; } m_moreIn = msg.HasMore; return(true); } Pipe[] pipe = new Pipe[1]; bool isMessageAvailable = m_fq.RecvPipe(pipe, out 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 != null && msg.IsIdentity) { isMessageAvailable = m_fq.RecvPipe(pipe, out msg); } if (!isMessageAvailable) { return(false); } else if (msg == null) { return(true); } 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 = msg; m_prefetched = true; Blob identity = pipe[0].Identity; msg = new Msg(identity.Data); msg.SetFlags(MsgFlags.More); m_identitySent = true; } return(true); }
/// <summary> /// Transmit the given Msg across the message-queueing system. /// If the msg fails to immediately send, then - if DontWait is specified and no SendTimeout was set /// then throw an AgainException. /// </summary> /// <param name="msg">the Msg to transmit</param> /// <param name="flags">a SendReceiveOptions: either don't specify DontWait, or set a timeout</param> public void Send(ref Msg msg, SendReceiveOptions flags) { CheckContextTerminated(); // Check whether message passed to the function is valid. if (!msg.IsInitialised) { throw new FaultException("SocketBase.Send passed an uninitialised Msg."); } // Process pending commands, if any. ProcessCommands(0, true); // Clear any user-visible flags that are set on the message. msg.ResetFlags(MsgFlags.More); // At this point we impose the flags on the message. if ((flags & SendReceiveOptions.SendMore) > 0) { msg.SetFlags(MsgFlags.More); } // Try to send the message. bool isMessageSent = XSend(ref msg, flags); if (isMessageSent) { return; } // In case of non-blocking send we'll simply propagate // the error - including EAGAIN - up the stack. bool isDontWaitSet = (flags & SendReceiveOptions.DontWait) > 0; if (isDontWaitSet || m_options.SendTimeout == 0) { #if DEBUG string xMsg; if (isDontWaitSet && m_options.SendTimeout == 0) { xMsg = "SocketBase.Send failed, and DontWait is true AND SendTimeout is 0."; } else if (isDontWaitSet) { xMsg = "SocketBase.Send failed and DontWait is specified."; } else { xMsg = "SocketBase.Send failed and no SendTimeout is specified."; } throw new AgainException(innerException: null, message: xMsg); #else throw new AgainException(innerException: null, message: "SocketBase.Send failed"); #endif } // Compute the time when the timeout should occur. // If the timeout is infinite, don't care. int timeout = m_options.SendTimeout; long end = timeout < 0 ? 0 : (Clock.NowMs() + timeout); // Oops, we couldn't send the message. Wait for the next // command, process it and try to send the message again. // If timeout is reached in the meantime, return EAGAIN. while (true) { ProcessCommands(timeout, false); isMessageSent = XSend(ref msg, flags); if (isMessageSent) { break; } if (timeout <= 0) { continue; } timeout = (int)(end - Clock.NowMs()); if (timeout <= 0) { throw new AgainException(innerException: null, message: "SocketBase.Send failed and timeout <= 0"); } } }
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, out m_prefetchedMsg); if (!isMessageAvailable) { return false; } if (m_prefetchedMsg == null) { return false; } Debug.Assert(pipe[0] != null); Debug.Assert(!m_prefetchedMsg.HasMore); Blob identity = pipe[0].Identity; m_prefetchedId = new Msg(identity.Data); m_prefetchedId.SetFlags(MsgFlags.More); m_prefetched = true; m_identitySent = false; return true; }
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 bool Send(Msg msg, SendRecieveOptions flags) { if (m_ctxTerminated) { ZError.ErrorNumber = (ErrorNumber.ETERM); return false; } // Check whether message passed to the function is valid. if (msg == null) { ZError.ErrorNumber = (ErrorNumber.EFAULT); throw new ArgumentException(); } // Process pending commands, if any. bool rc = ProcessCommands(0, true); if (!rc) return false; // Clear any user-visible flags that are set on the message. msg.ResetFlags(MsgFlags.More); // At this point we impose the flags on the message. if ((flags & SendRecieveOptions.SendMore) > 0) msg.SetFlags(MsgFlags.More); // Try to send the message. rc = XSend(msg, flags); if (rc) return true; if (!ZError.IsError(ErrorNumber.EAGAIN)) return false; // In case of non-blocking send we'll simply propagate // the error - including EAGAIN - up the stack. if ((flags & SendRecieveOptions.DontWait) > 0 || m_options.SendTimeout == 0) return false; // Compute the time when the timeout should occur. // If the timeout is infite, don't care. int timeout = m_options.SendTimeout; long end = timeout < 0 ? 0 : (Clock.NowMs() + timeout); // Oops, we couldn't send the message. Wait for the next // command, process it and try to send the message again. // If timeout is reached in the meantime, return EAGAIN. while (true) { if (!ProcessCommands(timeout, false)) return false; rc = XSend(msg, flags); if (rc) break; if (!ZError.IsError(ErrorNumber.EAGAIN)) return false; if (timeout > 0) { timeout = (int)(end - Clock.NowMs()); if (timeout <= 0) { ZError.ErrorNumber = (ErrorNumber.EAGAIN); return false; } } } return true; }
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; }
protected override bool XRecv(SendReceiveOptions flags, out Msg msg) { msg = null; if (m_prefetched) { if (!m_identitySent) { msg = m_prefetchedId; m_prefetchedId = null; m_identitySent = true; } else { msg = m_prefetchedMsg; m_prefetchedMsg = null; m_prefetched = false; } m_moreIn = msg.HasMore; return true; } Pipe[] pipe = new Pipe[1]; bool isMessageAvailable = m_fq.RecvPipe(pipe, out 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 != null && msg.IsIdentity) isMessageAvailable = m_fq.RecvPipe(pipe, out msg); if (!isMessageAvailable) { return false; } else if (msg == null) { return true; } 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 = msg; m_prefetched = true; Blob identity = pipe[0].Identity; msg = new Msg(identity.Data); msg.SetFlags(MsgFlags.More); m_identitySent = true; } return true; }
public virtual bool PushMsg(Msg msg) { // First message to receive is identity (if required). if (!m_identityReceived) { msg.SetFlags(MsgFlags.Identity); m_identityReceived = true; if (!m_options.RecvIdentity) { return true; } } if (m_pipe != null && m_pipe.Write(msg)) { return true; } ZError.ErrorNumber = (ErrorNumber.EAGAIN); return false; }
public void Send(Msg msg, SendReceiveOptions flags) { if (m_ctxTerminated) { throw TerminatingException.Create(); } // Check whether message passed to the function is valid. if (msg == null) { throw NetMQException.Create(ErrorCode.EFAULT); } // Process pending commands, if any. ProcessCommands(0, true); // Clear any user-visible flags that are set on the message. msg.ResetFlags(MsgFlags.More); // At this point we impose the flags on the message. if ((flags & SendReceiveOptions.SendMore) > 0) { msg.SetFlags(MsgFlags.More); } // Try to send the message. bool isMessageSent = XSend(msg, flags); if (isMessageSent) { return; } // In case of non-blocking send we'll simply propagate // the error - including EAGAIN - up the stack. if ((flags & SendReceiveOptions.DontWait) > 0 || m_options.SendTimeout == 0) { throw AgainException.Create(); } // Compute the time when the timeout should occur. // If the timeout is infite, don't care. int timeout = m_options.SendTimeout; long end = timeout < 0 ? 0 : (Clock.NowMs() + timeout); // Oops, we couldn't send the message. Wait for the next // command, process it and try to send the message again. // If timeout is reached in the meantime, return EAGAIN. while (true) { ProcessCommands(timeout, false); isMessageSent = XSend(msg, flags); if (isMessageSent) { break; } if (timeout > 0) { timeout = (int)(end - Clock.NowMs()); if (timeout <= 0) { throw AgainException.Create(); } } } }
protected override bool XSend(Msg msg, SendRecieveOptions flags) { // If we've sent a request and we still haven't got the reply, // we can't send another request. if (m_receivingReply) { throw new InvalidOperationException("Cannot send another request"); } bool rc; // First part of the request is the request identity. if (m_messageBegins) { Msg bottom = new Msg(); bottom.SetFlags (MsgFlags.More); rc = base.XSend (bottom, 0); if (!rc) return false; m_messageBegins = false; } bool more = msg.HasMore; rc = base.XSend (msg, flags); if (!rc) return rc; // If the request was fully sent, flip the FSM into reply-receiving state. if (!more) { m_receivingReply = true; m_messageBegins = true; } return true; }
/// <summary> /// Transmit the given Msg across the message-queueing system. /// If the msg fails to immediately send, then - if DontWait is specified and no SendTimeout was set /// then throw an AgainException. /// </summary> /// <param name="msg">the Msg to transmit</param> /// <param name="flags">a SendReceiveOptions: either don't specify DontWait, or set a timeout</param> public void Send(ref Msg msg, SendReceiveOptions flags) { CheckContextTerminated(); // Check whether message passed to the function is valid. if (!msg.IsInitialised) throw new FaultException("SocketBase.Send passed an uninitialised Msg."); // Process pending commands, if any. ProcessCommands(0, true); // Clear any user-visible flags that are set on the message. msg.ResetFlags(MsgFlags.More); // At this point we impose the flags on the message. if ((flags & SendReceiveOptions.SendMore) > 0) msg.SetFlags(MsgFlags.More); // Try to send the message. bool isMessageSent = XSend(ref msg); if (isMessageSent) return; // In case of non-blocking send we'll simply propagate // the error - including EAGAIN - up the stack. bool isDontWaitSet = (flags & SendReceiveOptions.DontWait) > 0; if (isDontWaitSet || m_options.SendTimeout == 0) { #if DEBUG string xMsg; if (isDontWaitSet && m_options.SendTimeout == 0) xMsg = "SocketBase.Send failed, and DontWait is true AND SendTimeout is 0."; else if (isDontWaitSet) xMsg = "SocketBase.Send failed and DontWait is specified."; else xMsg = "SocketBase.Send failed and no SendTimeout is specified."; throw new AgainException(innerException: null, message: xMsg); #else throw new AgainException(innerException: null, message: "SocketBase.Send failed"); #endif } // Compute the time when the timeout should occur. // If the timeout is infinite, don't care. int timeout = m_options.SendTimeout; long end = timeout < 0 ? 0 : (Clock.NowMs() + timeout); // Oops, we couldn't send the message. Wait for the next // command, process it and try to send the message again. // If timeout is reached in the meantime, return EAGAIN. while (true) { ProcessCommands(timeout, false); isMessageSent = XSend(ref msg); if (isMessageSent) break; if (timeout <= 0) continue; timeout = (int)(end - Clock.NowMs()); if (timeout <= 0) throw new AgainException(innerException: null, message: "SocketBase.Send failed and timeout <= 0"); } }
public void Receive(ref Msg msg, SendReceiveOptions options) { LastOptions = options; byte[] bytes = m_frames.Dequeue(); msg.InitGC(bytes, bytes.Length); if (m_frames.Count != 0) msg.SetFlags(MsgFlags.More); }
public void Send(Msg msg, SendReceiveOptions flags) { if (m_ctxTerminated) { throw TerminatingException.Create(); } // Check whether message passed to the function is valid. if (msg == null) { throw NetMQException.Create(ErrorCode.EFAULT); } // Process pending commands, if any. ProcessCommands(0, true); // Clear any user-visible flags that are set on the message. msg.ResetFlags(MsgFlags.More); // At this point we impose the flags on the message. if ((flags & SendReceiveOptions.SendMore) > 0) msg.SetFlags(MsgFlags.More); // Try to send the message. bool isMessageSent = XSend(msg, flags); if (isMessageSent) { return; } // In case of non-blocking send we'll simply propagate // the error - including EAGAIN - up the stack. if ((flags & SendReceiveOptions.DontWait) > 0 || m_options.SendTimeout == 0) throw AgainException.Create(); // Compute the time when the timeout should occur. // If the timeout is infite, don't care. int timeout = m_options.SendTimeout; long end = timeout < 0 ? 0 : (Clock.NowMs() + timeout); // Oops, we couldn't send the message. Wait for the next // command, process it and try to send the message again. // If timeout is reached in the meantime, return EAGAIN. while (true) { ProcessCommands(timeout, false); isMessageSent = XSend(msg, flags); if (isMessageSent) break; if (timeout > 0) { timeout = (int)(end - Clock.NowMs()); if (timeout <= 0) { throw AgainException.Create(); } } } }
public virtual bool PushMsg(ref Msg msg) { // First message to receive is identity (if required). if (!m_identityReceived) { msg.SetFlags(MsgFlags.Identity); m_identityReceived = true; if (!m_options.RecvIdentity) { msg.Close(); msg.InitEmpty(); return true; } } if (m_pipe != null && m_pipe.Write(ref msg)) { msg.InitEmpty(); return true; } return false; }
protected override bool XRecv(SendReceiveOptions flags, out Msg msg) { msg = null; if (m_prefetched) { if (!m_identitySent) { msg = m_prefetchedId; m_prefetchedId = null; m_identitySent = true; } else { msg = m_prefetchedMsg; m_prefetchedMsg = null; m_prefetched = false; } return true; } Pipe[] pipe = new Pipe[1]; bool isMessageAvailable = m_fq.RecvPipe(pipe, out m_prefetchedMsg); if (!isMessageAvailable) { return false; } else if (m_prefetchedMsg == null) { return true; } 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 = new Msg(identity.Data, true); msg.SetFlags(MsgFlags.More); m_prefetched = true; m_identitySent = true; return true; }