5 #include "AliNetMessage.h"
8 void __freeBuffer (void *data, void *hint)
15 AliSocket::AliSocket(zmq::context_t *context,int type)
19 fSocket = new zmq::socket_t(*context,type);
22 AliSocket::~AliSocket()
27 void AliSocket::Bind(const char* endpoint)
31 fSocket->bind(endpoint);
34 void AliSocket::Connect(const char* endpoint)
38 fSocket->connect(endpoint);
41 void AliSocket::Subscribe(const char* filter)
43 fSocket->setsockopt(ZMQ_SUBSCRIBE, filter, strlen(filter) );
46 bool AliSocket::Recv(AliNetMessage *&mess, int flags)
48 zmq::message_t message;
50 if(!fSocket->recv(&message, flags))
53 int bufSize = (int)message.size();
55 // buffer will be adopted by AliNetMessage, no need to free it
56 char* buf = new char[bufSize];
57 memcpy(buf, (char*)message.data(), bufSize);
59 mess = new AliNetMessage(buf, bufSize);
64 bool AliSocket::Send(AliNetMessage &mess, int flags)
66 //NOTE: this is already done by AliNetMessage, should we do this too?
67 // send length of the message
68 int bufSize = mess.BufferSize();
70 // we need to copy it elsewhere because zmq takes ownership of the buffer data
71 char* buf = new char[bufSize];
72 memcpy(buf, (char*)mess.Buffer(), bufSize);
74 zmq::message_t message(buf, bufSize, __freeBuffer, NULL);
76 //fwrite(mess.Buffer(), sizeof(char), bufSize, stdout);
78 return fSocket->send(message, flags);