forked from chronolaw/cpp_study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZmq.hpp
More file actions
63 lines (49 loc) · 1.26 KB
/
Zmq.hpp
File metadata and controls
63 lines (49 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (c) 2020 by Chrono
#ifndef _ZMQ_HPP
#define _ZMQ_HPP
#include "cpplang.hpp"
// /usr/include/zmq.hpp
#include <zmq.hpp>
BEGIN_NAMESPACE(cpp_study)
using zmq_context_type = zmq::context_t;
using zmq_socket_type = zmq::socket_t;
using zmq_message_type = zmq::message_t;
template<int thread_num = 1>
class ZmqContext final
{
#if 0
public:
using zmq_context_type = zmq::context_t;
using zmq_socket_type = zmq::socket_t;
using zmq_message_type = zmq::message_t;
#endif
public:
ZmqContext() = default;
~ZmqContext() = default;
public:
static
zmq_context_type& context()
{
static zmq_context_type ctx(thread_num);
return ctx;
}
public:
static
zmq_socket_type recv_sock(int hwm = 1000, int linger = 10)
{
zmq_socket_type sock(context(), ZMQ_PULL);
sock.setsockopt(ZMQ_RCVHWM, hwm);
sock.setsockopt(ZMQ_LINGER, linger); // wait for 10ms
return sock;
}
static
zmq_socket_type send_sock(int hwm = 1000, int linger = 10)
{
zmq_socket_type sock(context(), ZMQ_PUSH);
sock.setsockopt(ZMQ_SNDHWM, hwm);
sock.setsockopt(ZMQ_LINGER, linger); // wait for 10ms
return sock;
}
};
END_NAMESPACE(cpp_study)
#endif //_ZMQ_HPP