-
Notifications
You must be signed in to change notification settings - Fork 1
/
zmq_common.h
78 lines (54 loc) · 1.63 KB
/
zmq_common.h
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "hphp/runtime/base/base-includes.h"
#include "string.h"
#include <zmq.h>
namespace HPHP {
const StaticString
s_zmqcontext("ZMQContext"),
s_zmqsocket("ZMQSocket"),
s_context("__context");
// ContextData
class ContextData : public SweepableResourceData {
public:
static ContextData *GetPersistent(int64_t io_threads);
static void SetPersistent(int64_t io_threads, ContextData *context);
private:
static std::string GetHash(const char *name, int64_t io_threads);
static ContextData *GetCachedImpl(const char *name, int64_t io_threads);
static void SetCachedImpl(const char *name, int64_t io_threads, ContextData *context);
public:
ContextData(int64_t io_threads);
~ContextData();
CLASSNAME_IS("Context")
// overriding ResourceData
virtual const String& o_getClassNameHook() const { return classnameof(); }
virtual bool isInvalid() const { return m_context == nullptr; }
void *get() { return m_context; }
private:
void *m_context;
};
ContextData *get_context(Object obj);
// SocketData
class SocketData : public ResourceData {
public:
DECLARE_RESOURCE_ALLOCATION_NO_SWEEP(SocketData)
SocketData(void *context, int64_t type) {
m_socket = zmq_socket(context, type);
}
~SocketData() {
close();
}
CLASSNAME_IS("SocketData");
// overriding ResourceData
const String& o_getClassNameHook() const { return classnameof(); }
void close() {
if (!isValid())
return;
zmq_close(m_socket);
m_socket = nullptr;
}
bool isValid() { return m_socket != nullptr; }
void *get() { return m_socket; }
private:
void *m_socket;
};
} // namespace HPHP