PhoenixZMQ  7.2.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 40 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 43 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 45 of file PZmqBackend.h.

Constructor & Destructor Documentation

◆ PZmqSocket()

PZmqSocket::PZmqSocket ( )

Default constructor of PZmqSocket.

Definition at line 116 of file PZmqBackend.cpp.

117 :p_socket(NULL)
118{
119
120}
zmq::socket_t * p_socket
ZMQ Socket.
Definition PZmqBackend.h:65

References p_socket.

◆ ~PZmqSocket()

PZmqSocket::~PZmqSocket ( )
virtual

Destructor of PZmqSocket.

Definition at line 123 of file PZmqBackend.cpp.

123 {
124 close();
125}
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 234 of file PZmqBackend.cpp.

234 {
235 if(p_socket != NULL){
236 p_socket->close();
237 delete p_socket;
238 p_socket = NULL;
239 }
240}

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

133 {
134 p_socket = pzmq_createClientSocket(context, socketParam.hostname, socketParam.port, extraParam.type, extraParam.nbBufferMessage,
135 extraParam.bufferSizeByte, extraParam.threadAffinity, extraParam.dataRate);
136#if (CPPZMQ_VERSION_MAJOR*100 + CPPZMQ_VERSION_MINOR*10 + CPPZMQ_VERSION_PATCH) >= 471
137 p_socket->set(zmq::sockopt::rcvtimeo, socketParam.recvTimeOut);
138 p_socket->set(zmq::sockopt::sndtimeo, socketParam.sendTimeOut);
139 p_socket->set(zmq::sockopt::linger, extraParam.linger); //number of ms to stop
140#else
141 p_socket->setsockopt(ZMQ_RCVTIMEO, &socketParam.recvTimeOut, sizeof(int));
142 p_socket->setsockopt(ZMQ_SNDTIMEO, &socketParam.sendTimeOut, sizeof(int));
143 p_socket->setsockopt(ZMQ_LINGER, extraParam.linger); //number of ms to stop
144#endif
145 return p_socket != NULL;
146}
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::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 154 of file PZmqBackend.cpp.

154 {
155 p_socket = pzmq_createServerSocket(context, socketParam.port, extraParam.type, extraParam.nbBufferMessage, extraParam.bufferSizeByte,
156 extraParam.threadAffinity, extraParam.dataRate);
157#if (CPPZMQ_VERSION_MAJOR*100 + CPPZMQ_VERSION_MINOR*10 + CPPZMQ_VERSION_PATCH) >= 471
158 p_socket->set(zmq::sockopt::rcvtimeo, socketParam.recvTimeOut);
159 p_socket->set(zmq::sockopt::sndtimeo, socketParam.sendTimeOut);
160 p_socket->set(zmq::sockopt::linger, extraParam.linger); //number of ms to stop
161#else
162 p_socket->setsockopt(ZMQ_RCVTIMEO, &socketParam.recvTimeOut, sizeof(int));
163 p_socket->setsockopt(ZMQ_SNDTIMEO, &socketParam.sendTimeOut, sizeof(int));
164 p_socket->setsockopt(ZMQ_LINGER, extraParam.linger); //number of ms to stop
165#endif
166 return p_socket != NULL;
167}
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::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 226 of file PZmqBackend.cpp.

226 {
227 if(p_socket != NULL){
228 return p_socket->handle() != NULL;
229 }
230 return false;
231}

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

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

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

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