PhoenixZMQ  8.1.3
Library which integrates zeromq use
Loading...
Searching...
No Matches
PZmqSocket Class Reference

PhoenixSocket API bridge to ZMQ socket. More...

#include <PZmqBackend.h>

Public Types

typedef zmq::message_t Message
 Define the type of message used by the PAbstractSocketManager.
 
typedef PZmqParam Param
 Define the type of extra parameters which can be used to create a Socket used by the PAbstractSocketManager.
 

Public Member Functions

void close ()
 Close the socket.
 
bool createClientSocket (zmq::context_t &context, const PSocketParam &socketParam, const Param &extraParam)
 Create a client socket.
 
bool createServerSocket (zmq::context_t &context, const PSocketParam &socketParam, const Param &extraParam)
 Create a server socket.
 
bool isConnected () const
 Check if the socket is connected.
 
 PZmqSocket ()
 Default constructor of PZmqSocket.
 
template<typename T>
PRecvStatus::PRecvStatus recvData (T &data, PRecvFlag::PRecvFlag flag=PRecvFlag::BLOCK)
 Recieved data with the socket.
 
PRecvStatus::PRecvStatus recvMsg (Message &msg, PRecvFlag::PRecvFlag flag=PRecvFlag::BLOCK)
 Receive data with the socket.
 
template<typename T>
PSendStatus::PSendStatus sendData (const T &data, PSendFlag::PSendFlag flag=PSendFlag::BLOCK)
 Send data with the socket.
 
PSendStatus::PSendStatus sendMsg (Message &msg, PSendFlag::PSendFlag flag=PSendFlag::BLOCK)
 Send data with the socket.
 
virtual ~PZmqSocket ()
 Destructor of PZmqSocket.
 

Private Attributes

zmq::socket_t * p_socket
 ZMQ Socket.
 

Detailed Description

PhoenixSocket API bridge to ZMQ socket.

Definition at line 42 of file PZmqBackend.h.

Member Typedef Documentation

◆ Message

typedef zmq::message_t PZmqSocket::Message

Define the type of message used by the PAbstractSocketManager.

Definition at line 45 of file PZmqBackend.h.

◆ Param

Define the type of extra parameters which can be used to create a Socket used by the PAbstractSocketManager.

Definition at line 47 of file PZmqBackend.h.

Constructor & Destructor Documentation

◆ PZmqSocket()

PZmqSocket::PZmqSocket ( )

Default constructor of PZmqSocket.

Definition at line 118 of file PZmqBackend.cpp.

119 :p_socket(NULL)
120{
121
122}
zmq::socket_t * p_socket
ZMQ Socket.
Definition PZmqBackend.h:67

References p_socket.

◆ ~PZmqSocket()

PZmqSocket::~PZmqSocket ( )
virtual

Destructor of PZmqSocket.

Definition at line 125 of file PZmqBackend.cpp.

125 {
126 close();
127}
void close()
Close the socket.

References close().

+ Here is the call graph for this function:

Member Function Documentation

◆ close()

void PZmqSocket::close ( )

Close the socket.

Definition at line 240 of file PZmqBackend.cpp.

240 {
241 if(p_socket != NULL){
242 p_socket->close();
243 delete p_socket;
244 p_socket = NULL;
245 }
246}

References p_socket.

Referenced by ~PZmqSocket().

+ Here is the caller graph for this function:

◆ createClientSocket()

bool PZmqSocket::createClientSocket ( zmq::context_t & context,
const PSocketParam & socketParam,
const Param & extraParam )

Create a client socket.

Parameters
context: zmq context where to create socket
socketParam: parameters of the server (hostname, port), the client has to connect to
extraParam: extra customisable parameters for the creation of the socket (depends on the backend)
Returns
true if the socket has been created, false otherwise

Definition at line 135 of file PZmqBackend.cpp.

135 {
136 p_socket = pzmq_createClientSocket(context, socketParam.hostname, socketParam.port, extraParam.type, extraParam.nbBufferMessage,
137 extraParam.bufferSizeByte, extraParam.threadAffinity, extraParam.dataRate, extraParam.immediate);
138#if (CPPZMQ_VERSION_MAJOR*100 + CPPZMQ_VERSION_MINOR*10 + CPPZMQ_VERSION_PATCH) >= 471
139 p_socket->set(zmq::sockopt::rcvtimeo, socketParam.recvTimeOut);
140 p_socket->set(zmq::sockopt::sndtimeo, socketParam.sendTimeOut);
141 p_socket->set(zmq::sockopt::linger, extraParam.linger); //number of ms to stop
142 p_socket->set(zmq::sockopt::immediate, extraParam.immediate); //EAGAIN if no peer connected
143#else
144 p_socket->setsockopt(ZMQ_RCVTIMEO, &socketParam.recvTimeOut, sizeof(int));
145 p_socket->setsockopt(ZMQ_SNDTIMEO, &socketParam.sendTimeOut, sizeof(int));
146 p_socket->setsockopt(ZMQ_LINGER, &extraParam.linger, sizeof(int)); //number of ms to stop
147 p_socket->setsockopt(ZMQ_IMMEDIATE, &extraParam.immediate, sizeof(int)); //EAGAIN if no peer connected
148#endif
149 return p_socket != NULL;
150}
zmq::socket_t * pzmq_createClientSocket(zmq::context_t &context, int type, const std::string &address, size_t port, int immediate)
Create a client socket to be used by the SocketManagerZMQ.

References PZmqParam::bufferSizeByte, PZmqParam::dataRate, PZmqParam::immediate, PZmqParam::linger, PZmqParam::nbBufferMessage, p_socket, pzmq_createClientSocket(), PZmqParam::threadAffinity, and PZmqParam::type.

Referenced by PZmqSocketGenerator::createClientSocket().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ createServerSocket()

bool PZmqSocket::createServerSocket ( zmq::context_t & context,
const PSocketParam & socketParam,
const Param & extraParam )

Create a server socket.

Parameters
context: zmq context where to create socket
socketParam: parameters of the server (hostname, port), the client has to connect to
extraParam: extra customisable parameters for the creation of the socket (depends on the backend)
Returns
true if the socket has been created, false otherwise

Definition at line 158 of file PZmqBackend.cpp.

158 {
159 p_socket = pzmq_createServerSocket(context, socketParam.port, extraParam.type, extraParam.nbBufferMessage, extraParam.bufferSizeByte,
160 extraParam.threadAffinity, extraParam.dataRate);
161#if (CPPZMQ_VERSION_MAJOR*100 + CPPZMQ_VERSION_MINOR*10 + CPPZMQ_VERSION_PATCH) >= 471
162 p_socket->set(zmq::sockopt::rcvtimeo, socketParam.recvTimeOut);
163 p_socket->set(zmq::sockopt::sndtimeo, socketParam.sendTimeOut);
164 p_socket->set(zmq::sockopt::linger, extraParam.linger); //number of ms to stop
165 p_socket->set(zmq::sockopt::immediate, extraParam.immediate); //EAGAIN if no peer connected
166#else
167 p_socket->setsockopt(ZMQ_RCVTIMEO, &socketParam.recvTimeOut, sizeof(int));
168 p_socket->setsockopt(ZMQ_SNDTIMEO, &socketParam.sendTimeOut, sizeof(int));
169 p_socket->setsockopt(ZMQ_LINGER, &extraParam.linger, sizeof(int)); //number of ms to stop
170 p_socket->setsockopt(ZMQ_IMMEDIATE, &extraParam.immediate, sizeof(int)); //EAGAIN if no peer connected
171#endif
172 return p_socket != NULL;
173}
zmq::socket_t * pzmq_createServerSocket(zmq::context_t &context, int type, size_t port)
Create a server socket to be used by the SocketManagerZMQ.

References PZmqParam::bufferSizeByte, PZmqParam::dataRate, PZmqParam::immediate, PZmqParam::linger, PZmqParam::nbBufferMessage, p_socket, pzmq_createServerSocket(), PZmqParam::threadAffinity, and PZmqParam::type.

Referenced by PZmqSocketGenerator::createServerSocket().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isConnected()

bool PZmqSocket::isConnected ( ) const

Check if the socket is connected.

Returns
true if the socket is connected, false otherwise

Definition at line 232 of file PZmqBackend.cpp.

232 {
233 if(p_socket != NULL){
234 return p_socket->handle() != NULL;
235 }
236 return false;
237}

References p_socket.

◆ recvData()

template<typename T>
PRecvStatus::PRecvStatus PZmqSocket::recvData ( T & data,
PRecvFlag::PRecvFlag flag = PRecvFlag::BLOCK )

Recieved data with the socket.

Parameters
[out]data: data to be recieved with the socket
flag: recieving flag (BLOCK, NON_BLOCK)
Returns
status of the recv

Definition at line 71 of file PZmqBackend_impl.h.

71 {
72 PRecvStatus::PRecvStatus recvStatus = PRecvStatus::OK;
73 Message msg;
74 recvStatus = recvMsg(msg, flag);
75 if(recvStatus == PRecvStatus::OK){
76 //If the message is empty we cannot initialise the given data, so, this is an error
77 if(msg.size() != 0lu){
78 DataStreamIter iter = (DataStreamIter)msg.data();
79 if(!data_message_load<T>(iter, data)){
80 return PRecvStatus::CANNOT_DESERIALIZE_DATA;
81 }
82 }else{
83 return PRecvStatus::NO_MESSAGE_RECEIVED;
84 }
85 }
86
87 return recvStatus;
88}
PRecvStatus::PRecvStatus recvMsg(Message &msg, PRecvFlag::PRecvFlag flag=PRecvFlag::BLOCK)
Receive data with the socket.
zmq::message_t Message
Define the type of message used by the PAbstractSocketManager.
Definition PZmqBackend.h:45

References recvMsg().

+ Here is the call graph for this function:

◆ recvMsg()

PRecvStatus::PRecvStatus PZmqSocket::recvMsg ( Message & msg,
PRecvFlag::PRecvFlag flag = PRecvFlag::BLOCK )

Receive data with the socket.

Parameters
msg: message to be received with the socket
flag: receiving flag (BLOCK, NON_BLOCK)
Returns
status of the recv

Definition at line 205 of file PZmqBackend.cpp.

205 {
206 try {
207 zmq::recv_result_t result = p_socket->recv(msg, convertToRecvFlag(flag));
208 return checkRecvStatus(result);
209 }catch(const zmq::error_t& e){
210 if(e.num() == ENOTSOCK){
211 std::cerr << "ZMQ error in recv: " << e.what() << std::endl;
212 return PRecvStatus::BROKEN_SOCKET;
213 }
214 else if(e.num() == ENOTSUP || e.num() == EFSM || e.num() == ETERM || e.num() == EINVAL){
215 std::cerr << "ZMQ error in recv: " << e.what() << std::endl;
216 return PRecvStatus::BROKEN_BACKEND;
217 }
218 else if(e.num() == EINTR){
219 std::cerr << "ZMQ error in recv: " << e.what() << std::endl;
220 return PRecvStatus::SIGNAL_INTERRUPTION;
221 }
222 else{
223 std::cerr << "Unknown ZMQ error in recv: " << e.what() << std::endl;
224 return PRecvStatus::BROKEN_BACKEND;
225 }
226 }
227}
zmq::recv_flags convertToRecvFlag(PRecvFlag::PRecvFlag flag)
Convert a recv flag into zmq flag.
PRecvStatus::PRecvStatus checkRecvStatus(zmq::recv_result_t res)
Check the recv result and convert it into PRecvStatus.

References checkRecvStatus(), convertToRecvFlag(), and p_socket.

Referenced by recvData().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sendData()

template<typename T>
PSendStatus::PSendStatus PZmqSocket::sendData ( const T & data,
PSendFlag::PSendFlag flag = PSendFlag::BLOCK )

Send data with the socket.

Parameters
data: data to be sent with the socket
flag: sending flag (BLOCK, NON_BLOCK)
Returns
status of the send

Definition at line 53 of file PZmqBackend_impl.h.

53 {
54 size_t dataSize(data_size<T>(data));
55 Message msg;
56 msg.rebuild(dataSize);
57 DataStreamIter iter = (DataStreamIter)msg.data();
58 if(data_message_save<T>(iter, data)){ //Save the message
59 return sendMsg(msg, flag);
60 }else{
61 return PSendStatus::CANNOT_SERIALIZE_DATA;
62 }
63}
PSendStatus::PSendStatus sendMsg(Message &msg, PSendFlag::PSendFlag flag=PSendFlag::BLOCK)
Send data with the socket.

References sendMsg().

+ Here is the call graph for this function:

◆ sendMsg()

PSendStatus::PSendStatus PZmqSocket::sendMsg ( Message & msg,
PSendFlag::PSendFlag flag = PSendFlag::BLOCK )

Send data with the socket.

Parameters
msg: message to be sent with the socket
flag: sending flag (BLOCK, NON_BLOCK)
Returns
status of the send

Definition at line 180 of file PZmqBackend.cpp.

180 {
181 try {
182 zmq::send_result_t result = p_socket->send(msg, convertToSendFlag(flag));
183 return checkSendStatus(result);
184 }catch(const zmq::error_t& e) {
185 if(e.num() == ENOTSOCK){
186 return PSendStatus::BROKEN_SOCKET;
187 }else if(e.num() == ENOTSUP || e.num() == EFSM || e.num() == ETERM || e.num() == EINVAL){
188 return PSendStatus::BROKEN_BACKEND;
189 }else if(e.num() == EINTR){
190 return PSendStatus::SIGNAL_INTERRUPTION;
191 }else if(e.num() == EHOSTUNREACH){
192 return PSendStatus::NO_ROUTE_TO_RECEIVER;
193 }else{
194 std::cerr << "Unknown ZMQ error in send: " << e.what() << std::endl;
195 return PSendStatus::BROKEN_BACKEND;
196 }
197 }
198}
zmq::send_flags convertToSendFlag(PSendFlag::PSendFlag flag)
Convert a send flag into zmq flag.
PSendStatus::PSendStatus checkSendStatus(zmq::send_result_t res)
Check the send result and convert it into PSendStatus.

References checkSendStatus(), convertToSendFlag(), and p_socket.

Referenced by sendData().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ p_socket

zmq::socket_t* PZmqSocket::p_socket
private

ZMQ Socket.

Definition at line 67 of file PZmqBackend.h.

Referenced by close(), createClientSocket(), createServerSocket(), isConnected(), PZmqSocket(), recvMsg(), and sendMsg().


The documentation for this class was generated from the following files: