本文整理汇总了C++中zmq::socket_t类的典型用法代码示例。如果您正苦于以下问题:C++ socket_t类的具体用法?C++ socket_t怎么用?C++ socket_t使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了socket_t类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: recv
bool recv(zmq::socket_t & socket) {
clear();
while(1) {
zmq::message_t message(0);
try {
if (!socket.recv(&message, 0)) {
return false;
}
} catch (zmq::error_t error) {
//std::cout << "E: " << error.what() << std::endl;
return false;
}
char *data = reinterpret_cast<char*>(message.data());
if (message.size() == 17 && data[0] == 0) {
push_back(encode_uuid(msg_to_string(message)));
} else {
push_back(msg_to_string(message));
}
boost::int64_t more;
size_t more_size = sizeof(more);
socket.getsockopt(ZMQ_RCVMORE, &more, &more_size);
if (!more) {
break;
}
}
return true;
}
示例2: raw_sender
void
raw_sender(zmq::socket_t& s)
{
for (size_t i = 0; i < ITERS; ++i)
{
zmq::message_t msg1(ARRAY_LEN(PART1)-1);
memcpy(msg1.data(), PART1, msg1.size());
s.send(msg1, ZMQ_SNDMORE);
zmq::message_t msg2(ARRAY_LEN(PART2)-1);
memcpy(msg2.data(), PART2, msg2.size());
s.send(msg2, ZMQ_SNDMORE);
zmq::message_t msg3(ARRAY_LEN(PART3)-1);
memcpy(msg3.data(), PART3, msg3.size());
s.send(msg3);
zmq::message_t msg_res;
s.recv(&msg_res, 0);
if (i % 1000 == 0)
{
std::cout << ".";
std::cout.flush();
}
}
}
示例3: socket
void OTSocket::Listen(const OTString &strBind)
{
if (NULL != m_pSocket)
delete m_pSocket;
// m_pSocket = NULL;
m_pSocket = new zmq::socket_t(*m_pContext, ZMQ_REP); // RESPONSE socket (Request / Response.)
OT_ASSERT_MSG(NULL != m_pSocket, "OTSocket::Listen: new zmq::socket(context, ZMQ_REP)");
OTString strTemp(strBind); // In case m_strBindPath is what was passed in. (It happens.)
m_strBindPath.Set(strTemp); // In case we have to close/reopen the socket to finish a send/receive.
// ------------------------
// Configure socket to not wait at close time
//
const int linger = 0; // close immediately
m_pSocket->setsockopt (ZMQ_LINGER, &linger, sizeof (linger));
/*
int zmq_setsockopt (void *socket, int option_name, const void *option_value, size_t option_len);
Caution: All options, with the exception of ZMQ_SUBSCRIBE, ZMQ_UNSUBSCRIBE and ZMQ_LINGER, only take effect for subsequent socket bind/connects.
*/
// ------------------------
m_pSocket->bind(strBind.Get());
}
示例4: RecvMsg
/*
* return number of bytes received, negative if error, 0 for received nothing,
* which should be treated as error
*/
int32_t RecvMsg(zmq::socket_t &sock, boost::shared_array<uint8_t> &data,
bool *_more){
zmq::message_t msgt;
int nbytes;
try{
nbytes = sock.recv(&msgt);
}catch(zmq::error_t &e){
LOG(ERROR) << "RecvMsg error = " << e.what();
return -1;
}
if(nbytes == 0) return 0;
size_t len = msgt.size();
uint8_t *dataptr;
try{
dataptr = new uint8_t[len];
}catch(std::bad_alloc &e){
return -1;
}
memcpy(dataptr, msgt.data(), len);
data.reset(dataptr);
if(_more != NULL){
int more_recv;
size_t s = sizeof(int);
sock.getsockopt(ZMQ_RCVMORE, &more_recv, &s);
*_more = (more_recv == 1) ? true : false;
}
return len;
}
示例5: process_frontend
void process_frontend(zmq::socket_t& frontend, int& available_workers) {
zmq::message_t req;
frontend.recv(&req);
zmq::message_t reply(sizeof(available_workers));
memcpy(reply.data(), &available_workers, sizeof(available_workers));
frontend.send(reply);
}
示例6: handleOp
void handleOp(zmq::socket_t &socket, int opCode) {
std::string s_req, trace, line;
std::cout<<"Trace: ";
std::getline(std::cin, trace);
Request req;
Id id;
Person person;
Request_Header_Type type;
switch (opCode) {
case 1:
type = Request_Header_Type_LOOKUP;
std::cout<<"id: ";
line.clear();
std::getline(std::cin, line);
id = makeId(fromString<int>(line));
req = makeRequest(type, id, person, false, trace);
break;
case 3:
type = Request_Header_Type_REMOVE;
std::cout<<"id: ";
line.clear();
std::getline(std::cin, line);
id = makeId(fromString<int>(line));
req = makeRequest(type, id, person, false, trace);
break;
case 2:
{
type = Request_Header_Type_INSERT;
std::cout<<"id: ";
line.clear();
std::getline(std::cin, line);
id = makeId(fromString<int>(line));
line.clear();
std::cout<<"Name: ";
std::getline(std::cin, line);
std::string ph, ovr;
std::cout<<"Phone: ";
std::getline(std::cin, ph);
std::cout<<"Overwrite: (y/n) ";
std::getline(std::cin, ovr);
person = makePerson(line, ph);
req = makeRequest(type, id, person, ovr.compare("y") == 0,
trace);
break;
}
};
std::cout<<"Request: "<<requestToString(req);
TRACE(std::cout, "");
req.SerializeToString(&s_req);
zmq::message_t z_req(s_req.size());
memcpy(z_req.data(), (void *)s_req.data(), s_req.size());
socket.send(z_req);
zmq::message_t z_resp;
socket.recv(&z_resp);
std::string s_resp((char *)z_resp.data(), z_resp.size());
Response resp;
resp.ParseFromString(s_resp);
std::cout<<"Response = "<<responseToString(resp);
}
示例7: process_status_request
// capacity request
void process_status_request(zmq::socket_t& status, int& number_of_workers) {
// null request
zmq::message_t incoming_msg;
status.recv(&incoming_msg);
// send current capacity
zmq::message_t status_msg(sizeof(number_of_workers));
memcpy(status_msg.data(), &number_of_workers, sizeof(number_of_workers));
status.send(status_msg);
}
示例8: s_send
static bool
s_send (zmq::socket_t & socket, const char* buffer, unsigned length, bool zerocopy=false) {
if(zerocopy) {
zmq::message_t message((void *)buffer, length, NULL, NULL);
return socket.send (message);
}else {
zmq::message_t message(length);
memcpy (message.data(),buffer, length);
return socket.send (message);
}
}
示例9: getRequest
bool Server::getRequest (zmq::socket_t &socket, pb::RequestEnvelope &envelope) {
long long more;
size_t sz = sizeof (more);
zmq::message_t request;
socket.recv (&request);
socket.getsockopt (ZMQ_RCVMORE, &more, &sz);
bool isAnotherRequest = more != 0;
envelope.ParseFromArray (request.data(), request.size());
return isAnotherRequest;
}
示例10: SendMove
bool ServerInterface::SendMove(enum Tetromino::Move move, int pieceId, zmq::socket_t& commandSocket) const {
std::string const* moveCommandS = m_factory.CreateMoveMessage(move, pieceId);
zmq::message_t moveCommand (moveCommandS->size());
memcpy(static_cast<void*>(moveCommand.data()), moveCommandS->data(),
moveCommandS->size());
delete moveCommandS;
commandSocket.send(moveCommand);
zmq::message_t reply;
commandSocket.recv(&reply);
std::string replyS (static_cast<char const*>(reply.data()), reply.size());
return m_factory.ParseMoveReply(replyS);
}
示例11: rawSocketSend
void MessageSender::rawSocketSend( zmq::socket_t& a_socket, const std::string& a_name, const unsigned char* a_msg, size_t a_msgSize )
{
bool ok = false;
try
{
ok = a_socket.send( a_name.data(), a_name.size(), ZMQ_SNDMORE );
ok = a_socket.send( a_msg, a_msgSize );
}
catch( const zmq::error_t& ex )
{
//std::cout << __PRETTY_FUNCTION__ << " " << ex.what() << std::endl;
}
}
示例12: s_sendmore
// Sends string as 0MQ string, as multipart non-terminal
static bool
s_sendmore (zmq::socket_t & socket, const std::string & string, bool zerocopy=false) {
zmq::message_t *pmessage = NULL;
if(zerocopy) {
zmq::message_t message ((void *)string.c_str(), string.length(), NULL, NULL);
return socket.send (message, ZMQ_SNDMORE);
}else {
zmq::message_t message(string.size());
memcpy (message.data(), string.data(), string.size());
return socket.send (message, ZMQ_SNDMORE);
}
}
示例13: recv_multipart
void recv_multipart(zmq::socket_t &socket) {
int64_t more=-1;
size_t more_size = sizeof (more);
while(more) {
zmq::message_t response;
socket.recv(&response);
std::string response_str((const char *)response.data(),
response.size());
cout << response_str << endl;
socket.getsockopt(ZMQ_RCVMORE, &more, &more_size);
}
}
示例14: process_backend
// relay data from workers to frontend
void process_backend(zmq::socket_t& frontend, zmq::socket_t& backend) {
int64_t more;
size_t moresz;
moresz = sizeof(more);
while(true) {
zmq::message_t worker_msg;
backend.recv(&worker_msg);
backend.getsockopt(ZMQ_RCVMORE, &more, &moresz);
frontend.send(worker_msg, more ? ZMQ_SNDMORE : 0);
if(!more) {
break;
}
}
}
示例15: send
/** Send empty message. */
inline bool send(zmq::socket_t &s, const empty &v, int flags)
{
zmq::message_t msg(0);
IB_FIRST_PART(s.send(msg, flags));
return true;
}