JeroMQ
调用签名状态:
/**
* Receive a message.
*
* @return the message received, as an array of bytes; null on error.
*/
public final byte[] recv()
{
return recv(0);
}
/**
* Receive a message.
*
* @param flags
* the flags to apply to the receive operation.
* @return the message received, as an array of bytes; null on error.
*/
public final byte[] recv(int flags)
{
zmq.Msg msg = base.recv(flags);
if (msg != null) {
return msg.data();
}
mayRaise();
return null;
}
/**
* Receive a message in to a specified buffer.
*
* @param buffer
* byte[] to copy zmq message payload in to.
* @param offset
* offset in buffer to write data
* @param len
* max bytes to write to buffer.
* If len is smaller than the incoming message size,
* the message will be truncated.
* @param flags
* the flags to apply to the receive operation.
* @return the number of bytes read, -1 on error
*/
public final int recv(byte[] buffer, int offset, int len, int flags)
{
zmq.Msg msg = base.recv(flags);
if (msg != null) {
return msg.getBytes(0, buffer, offset, len);
}
return -1;
}
您的代码使用第二个:
public final byte[] recv( int flags ){...}
您的代码为其注入硬编码0
的flags
参数的整数值。
接下来,API 中
的含义更好地记录为:int flags
flags参数是下面定义的标志的组合:指定
操作应该在非阻塞模式下执行。如果指定的套接字上没有可用的消息,则该函数将失败并设置为。
ZMQ_NOBLOCK
zmq_recv()
errno
EAGAIN
谨慎起见,可以使用DONTWAIT
flag inside JeroMQ
,但从下往上很难找到其他一些有意义的选项。
所以最后,
让我们阅读 ZeroMQ 定义
和识别的标志集:
// ------------------------------------------------- // Socket options.
#define ZMQ_HWM 1
#define ZMQ_SWAP 3
#define ZMQ_AFFINITY 4
#define ZMQ_IDENTITY 5
#define ZMQ_SUBSCRIBE 6
#define ZMQ_UNSUBSCRIBE 7
#define ZMQ_RATE 8
#define ZMQ_RECOVERY_IVL 9
#define ZMQ_MCAST_LOOP 10
#define ZMQ_SNDBUF 11
#define ZMQ_RCVBUF 12
#define ZMQ_RCVMORE 13
#define ZMQ_FD 14
#define ZMQ_EVENTS 15
#define ZMQ_TYPE 16
#define ZMQ_LINGER 17
#define ZMQ_RECONNECT_IVL 18
#define ZMQ_BACKLOG 19
#define ZMQ_RECOVERY_IVL_MSEC 20 /* opt. recovery time, reconcile in 3.x */
#define ZMQ_RECONNECT_IVL_MAX 21
// ------------------------------------------------- // Send/recv options.
#define ZMQ_NOBLOCK 1 // <<<<<<<<<<<<<<<<<<<<<<<<<<- THIS ONE IS NEEDED
#define ZMQ_SNDMORE 2
// ------------------------------------------------- // I/O Multplexing options.
#define ZMQ_POLLIN 1
#define ZMQ_POLLOUT 2
#define ZMQ_POLLERR 4
// ------------------------------------------------- // Device types.
#define ZMQ_STREAMER 1
#define ZMQ_FORWARDER 2
#define ZMQ_QUEUE 3
结语:
如果使用.recv( 0 )
您的代码将 BLOCK,直到socket
-instance 确实.recv()
从任何(可能存在于不久或更远的将来)兼容的.connect()
-ed 远程端(REQ
套接字等)接收到本地端的任何东西。
如果使用.recv( 1 )
您的代码不会阻塞,但应该正确处理两种可能的返回状态{ <msg.data()_Value> | (EXC, NULL) }
并咨询 ZeroMQerrno
详细信息,以根据信号情境上下文采取行动。