Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef MULTIPARTMESSAGE
- #define MULTIPARTMESSAGE
- #include <string>
- #include <vector>
- #include <zmq.h>
- class MultiPartMessage
- {
- public:
- //Constructor
- MultiPartMessage(void* &socket, const std::string &identity, const std::vector<std::string> &framesOut) : id(identity) //sends multipart message at instantiation.
- {
- if (framesOut.size() > 0)//if there are frames to send
- {
- //Prepare & send identity frame
- if (id != "NULL")
- {
- zmq_msg_t messageOut;
- int rc = zmq_msg_init_size(&messageOut, id.size());
- if (rc == -1)
- {
- std::string errStr = zmq_strerror(zmq_errno());
- std::string errConc = "MultiPartMessage: Error Initialising Message: " + errStr;
- const char* errOut = errConc.c_str();
- OutputDebugStringA(errOut);
- }
- memcpy(zmq_msg_data(&messageOut), id.data(), id.size());
- rc = zmq_msg_send(&messageOut, socket, ZMQ_SNDMORE);//part of multipart message, more to follow.
- if (rc == -1)
- {
- std::string errStr = zmq_strerror(zmq_errno());
- std::string errConc = "MultiPartMessage: Error Sending Identity Message Part: " + errStr;
- const char* errOut = errConc.c_str();
- OutputDebugStringA(errOut);
- //Release Message, when sending only necessary to release in the case of an error.
- rc = zmq_msg_close(&messageOut);
- if (rc == -1)
- {
- std::string errStr = zmq_strerror(zmq_errno());
- std::string errConc = "MultiPartMessage: Error Releasing Sent Message: " + errStr;
- const char* errOut = errConc.c_str();
- OutputDebugStringA(errOut);
- }
- }
- }//prepare & send identity frame
- //Prepare & send regular message frames. Each part of a multipart message is a separate zmq_msg_t object. Each frame (a.k.a a message part) is put into a message object. A message object is a length specified piece of data.
- for (auto frm = framesOut.cbegin(); frm < framesOut.cend(); ++frm)
- {
- zmq_msg_t messageOut;
- int rc = zmq_msg_init_size(&messageOut, frm->size());
- if (rc == -1)
- {
- std::string errStr = zmq_strerror(zmq_errno());
- std::string errConc = "MultiPartMessage: Error Initialising Message: " + errStr;
- const char* errOut = errConc.c_str();
- OutputDebugStringA(errOut);
- }
- memcpy(zmq_msg_data(&messageOut), frm->data(), frm->size());
- if (frm + 1 != framesOut.cend())
- {
- rc = zmq_msg_send(&messageOut, socket, ZMQ_SNDMORE);//part of multipart message, more to follow.
- if (rc == -1)
- {
- std::string errStr = zmq_strerror(zmq_errno());
- std::string errConc = "MultiPartMessage: Error Sending Regular Message Part: " + errStr;
- const char* errOut = errConc.c_str();
- OutputDebugStringA(errOut);
- //return 1;//error
- }
- }
- else if (frm + 1 == framesOut.cend())
- {
- rc = zmq_msg_send(&messageOut, socket, 0);//final part.
- if (rc == -1)
- {
- std::string errStr = zmq_strerror(zmq_errno());
- std::string errConc = "MultiPartMessage: Error Sending Final Message Part: " + errStr;
- const char* errOut = errConc.c_str();
- OutputDebugStringA(errOut);
- //Release Message, when sending only necessary to release in the case of an error.
- rc = zmq_msg_close(&messageOut);
- if (rc == -1)
- {
- std::string errStr = zmq_strerror(zmq_errno());
- std::string errConc = "MultiPartMessage: Error Releasing Sent Message: " + errStr;
- const char* errOut = errConc.c_str();
- OutputDebugStringA(errOut);
- }
- }
- }
- }//if there are frames to send
- }//for loop
- }//send constructor
- //Constructor
- MultiPartMessage(void* &socket) //blocking receive of new message at instantiation.
- {
- id = "blank";//assign default
- id_size = 0;//assign default
- //Determine socket type
- size_t socketType;
- size_t socketType_size = sizeof(socketType);
- int rc = zmq_getsockopt(socket, ZMQ_TYPE, &socketType, &socketType_size);
- if (rc == -1)
- {
- std::string errStr = zmq_strerror(zmq_errno());
- std::string errConc = "MultiPartMessage: Error Getting Socket Type: " + errStr;
- const char* errOut = errConc.c_str();
- OutputDebugStringA(errOut);
- }
- //Determine handling of first message frame
- bool firstFrame(false);
- if (socketType == ZMQ_ROUTER)
- {
- firstFrame = true;
- }
- //Receive multipart message
- while (1)
- {
- //Initialise message object
- zmq_msg_t messageIn;
- int rc = zmq_msg_init(&messageIn);
- if (rc == -1)
- {
- std::string errStr = zmq_strerror(zmq_errno());
- std::string errConc = "MultiPartMessage: Error Initialising Message: " + errStr;
- const char* errOut = errConc.c_str();
- OutputDebugStringA(errOut);
- }
- //Receive message part
- rc = zmq_msg_recv(&messageIn, socket, 0);
- if (rc == -1)
- {
- std::string errStr = zmq_strerror(zmq_errno());
- std::string errConc = "MultiPartMessage: Error Receiving Message Part: " + errStr;
- const char* errOut = errConc.c_str();
- OutputDebugStringA(errOut);
- }
- //Cast the received message data blob to a string for storage. A char type object is one byte of memory in size and holds the binary representation of one character in eight bits.
- //Char is suitable for storing the raw binary data received off a network socket or a data blob (binary large object).
- if (!firstFrame)//Routine handling of non-identity message frames
- {
- //Put into the frames vector
- framesIn.push_back(std::string(static_cast<char*>(zmq_msg_data(&messageIn)), zmq_msg_size(&messageIn)));
- }
- else if (firstFrame)//Router socket will prepend identity to the multipart message, handle this message part differently
- {
- id = std::string(static_cast<char*>(zmq_msg_data(&messageIn)), zmq_msg_size(&messageIn));
- id_size = zmq_msg_size(&messageIn);//size in bytes
- firstFrame = false;//Handle subsequent message parts differently
- }
- //Detect final message part
- if (!zmq_msg_more(&messageIn))
- {
- zmq_msg_close(&messageIn);//discard message part
- break;//Final message frame
- }
- else
- zmq_msg_close(&messageIn);//discard message part
- }
- }//receive constructor
- //Copy constructor
- MultiPartMessage(const MultiPartMessage &orig) :framesIn(orig.framesIn), id(orig.id), id_size(orig.id_size){};
- //Copy assignment
- MultiPartMessage& operator=(const MultiPartMessage& that)
- {
- if (this != &that)
- {
- //Release any resources; eg. those acquired in the constructor.
- //Take over resources from 'that'.
- framesIn = that.framesIn;
- id = that.id;
- id_size = that.id_size;
- }
- return *this;
- }
- //Move constructor
- MultiPartMessage(MultiPartMessage &&orig) /*noexcept NOT VS2013*/ : framesIn(orig.framesIn), id(orig.id), id_size(orig.id_size)
- {
- //Steals resources from the source instance.C++ Primer.p534.
- //leave orig in a state in which it is harmless to call the destructor. C++ Primer. p534.
- }
- //Move assignment constructor
- MultiPartMessage& operator=(MultiPartMessage &&that) /*noexcept NOT VS2013*/
- {
- //Direct test for self assignment.
- if (this != &that)
- {
- //Take over resources from 'that'. Steals resources from the source instance. C++ Primer. p534.
- framesIn = that.framesIn;
- id = that.id;
- id_size = that.id_size;
- //leave 'that' in a destructible state
- }
- return *this;
- }
- //Destructor
- ~MultiPartMessage(){}
- //Operator
- bool operator==(const MultiPartMessage &anotherMPM) const
- {
- return (framesIn == anotherMPM.framesIn &&
- id == anotherMPM.id &&
- id_size == anotherMPM.id_size);
- }
- //Getters
- std::string getIdentity() const
- {
- return id;
- }
- size_t getIdentitySize() const
- {
- return id_size;
- }
- size_t getNumberOfFrames() const
- {
- return framesIn.size();
- }
- std::string getFrameElement(int frmElem) const
- {
- return framesIn.at(frmElem);
- }
- private:
- std::vector <std::string> framesIn;
- std::string id;
- size_t id_size;
- };
- #endif//header guard
Advertisement
Add Comment
Please, Sign In to add comment