PhoenixZMQ  6.0.0
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 38 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 41 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 43 of file PZmqBackend.h.

Constructor & Destructor Documentation

◆ PZmqSocket()

PZmqSocket::PZmqSocket ( )

Default constructor of PZmqSocket.

Definition at line 121 of file PZmqBackend.cpp.

122:p_socket(nullptr)
123{
124
125}
zmq::socket_t * p_socket
ZMQ Socket.
Definition PZmqBackend.h:63

References p_socket.

◆ ~PZmqSocket()

PZmqSocket::~PZmqSocket ( )
virtual

Destructor of PZmqSocket.

Definition at line 128 of file PZmqBackend.cpp.

128 {
129 close();
130}
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 239 of file PZmqBackend.cpp.

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

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 138 of file PZmqBackend.cpp.

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

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

Referenced by PZmqBackend::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 157 of file PZmqBackend.cpp.

157 {
158 p_socket = pzmq_createServerSocket(context, socketParam.port, extraParam.type, extraParam.nbBufferMessage, extraParam.bufferSizeByte,
159 extraParam.threadAffinity, extraParam.dataRate);
160#if (CPPZMQ_VERSION_MAJOR*100 + CPPZMQ_VERSION_MINOR*10 + CPPZMQ_VERSION_PATCH) >= 471
161 p_socket->set(zmq::sockopt::rcvtimeo, socketParam.recvTimeOut);
162 p_socket->set(zmq::sockopt::sndtimeo, socketParam.sendTimeOut);
163#else
164 p_socket->setsockopt(ZMQ_RCVTIMEO, &socketParam.recvTimeOut, sizeof(int));
165 p_socket->setsockopt(ZMQ_SNDTIMEO, &socketParam.sendTimeOut, sizeof(int));
166#endif
167 return p_socket != nullptr;
168}
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::nbBufferMessage, p_socket, pzmq_createServerSocket(), PZmqParam::threadAffinity, and PZmqParam::type.

Referenced by PZmqBackend::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 231 of file PZmqBackend.cpp.

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

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:41

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 206 of file PZmqBackend.cpp.

206 {
207 try {
208 zmq::recv_result_t result = p_socket->recv(msg, convertToRecvFlag(flag));
209 return checkRecvStatus(result);
210 } catch(const zmq::error_t& e)
211 {
212 if(e.num() == ENOTSOCK){
213 return PRecvStatus::BROKEN_SOCKET;
214 }
215 else if(e.num() == ENOTSUP || e.num() == EFSM || e.num() == ETERM || e.num() == EINVAL){
216 return PRecvStatus::BROKEN_BACKEND;
217 }
218 else if(e.num() == EINTR){
219 return PRecvStatus::SIGNAL_INTERRUPTION;
220 }
221 else{
222 std::cerr << "Unknown ZMQ error in recv: " << e.what() << std::endl;
223 return PRecvStatus::BROKEN_BACKEND;
224 }
225 }
226}
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 175 of file PZmqBackend.cpp.

175 {
176 try {
177 zmq::send_result_t result = p_socket->send(msg, convertToSendFlag(flag));
178 return checkSendStatus(result);
179 }
180 catch(const zmq::error_t& e) {
181
182 if(e.num() == ENOTSOCK){
183 return PSendStatus::BROKEN_SOCKET;
184 }
185 else if(e.num() == ENOTSUP || e.num() == EFSM || e.num() == ETERM || e.num() == EINVAL){
186 return PSendStatus::BROKEN_BACKEND;
187 }
188 else if(e.num() == EINTR){
189 return PSendStatus::SIGNAL_INTERRUPTION;
190 }
191 else if(e.num() == EHOSTUNREACH){
192 return PSendStatus::NO_ROUTE_TO_RECEIVER;
193 }
194 else{
195 std::cerr << "Unknown ZMQ error in send: " << e.what() << std::endl;
196 return PSendStatus::BROKEN_BACKEND;
197 }
198 }
199}
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 63 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: