/// <summary> /// Receives data from the socket with flags set. /// </summary> /// <param name="flags"></param> /// <returns>The received data, or null if no data was received.</returns> public byte[] Receive(ReceiveFlags flags) { Contract.Requires(!Disposed); byte[] data; if (TryReceive(out data, flags) == true) { return(data); } else { return(null); } }
/// <summary> /// Attempts to receive data from the socket with flags set. /// </summary> /// <param name="data"></param> /// <param name="flags"></param> /// <returns>True if data was received; otherwise, false, and <paramref name="data"/> will be an empty array.</returns> public bool TryReceive(out byte[] data, ReceiveFlags flags) { Contract.Requires(!Disposed); Contract.Ensures(Contract.ValueAtReturn(out data) != null); if (C.zmq_msg_init(m_msg) != 0) { throw ZeroMQException.CurrentError(); } int rc = C.zmq_recv(m_sock, m_msg, (int)flags); if (rc == 0) { data = new byte[C.zmq_msg_size(m_msg)]; Marshal.Copy(C.zmq_msg_data(m_msg), data, 0, data.Length); } else { data = new byte[0]; } C.zmq_msg_close(m_msg); if (rc == 0) { return(true); } else if (C.zmq_errno() == C.EAGAIN) { return(false); } else { throw ZeroMQException.CurrentError(); } }
/// <summary> /// Attempts to receive data from the socket with flags set. /// </summary> /// <param name="data"></param> /// <param name="flags"></param> /// <returns>True if data was received; otherwise, false, and <paramref name="data"/> will be an empty array.</returns> public bool TryReceive( out byte[] data, ReceiveFlags flags ) { Contract.Requires( !Disposed ); Contract.Ensures( Contract.ValueAtReturn( out data ) != null ); if( C.zmq_msg_init( m_msg ) != 0 ) { throw ZeroMQException.CurrentError(); } int rc = C.zmq_recv( m_sock, m_msg, (int)flags ); if( rc == 0 ) { data = new byte[C.zmq_msg_size( m_msg )]; Marshal.Copy( C.zmq_msg_data( m_msg ), data, 0, data.Length ); } else { data = new byte[0]; } C.zmq_msg_close( m_msg ); if( rc == 0 ) { return true; } else if( C.zmq_errno() == C.EAGAIN ) { return false; } else { throw ZeroMQException.CurrentError(); } }
/// <summary> /// Receives data from the socket with flags set. /// </summary> /// <param name="flags"></param> /// <returns>The received data, or null if no data was received.</returns> public byte[] Receive( ReceiveFlags flags ) { Contract.Requires( !Disposed ); byte[] data; if( TryReceive( out data, flags ) == true ) { return data; } else { return null; } }