-
Notifications
You must be signed in to change notification settings - Fork 0
/
NanoMessage.h
119 lines (111 loc) · 2.27 KB
/
NanoMessage.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* nanomsg C++ wrapper
* \see http://nanomsg.org/
*/
#ifndef NANOMESSAGE_H_
#define NANOMESSAGE_H_
#include <string>
#include "nanomsg/nn.h"
#include "nanomsg/pubsub.h"
/**
* nanomsg C++ wrapper class
* \see http://nanomsg.org/
*/
class NanoMessage {
private:
int mFamily;
int mProtocol;
int mError;
protected:
int mSocket;
size_t mReadOffset;
int socket();
int connect(const char *address);
int bind(const char *address);
int setsockoption(int level, int option, const void *optval, size_t optlen);
void close();
void drop(int err);
public:
NanoMessage(
int protocol ///< nanomsg protocol number
);
virtual ~NanoMessage();
/// last operation error code
int error();
/// last operation error text description
std::string errorMessage();
/// nanomsg family
int family();
/// set nanomsg family
void setFamily(
int value
);
/// get nanomsg socket option
int getsockoption(
int level,
int option,
void *optval,
size_t *optlen ///< size of the buffer (see nanomsg documentation)
);
/// send message
void send(
const void *buf, ///< buffer
size_t bufsize ///< buffer size
);
/// send string message
void send(
const std::string &data ///< data to send represented as string
);
/// sync receive into buffer
int recv(
void *buf, ///< buffer
size_t bufsize ///< buffer size
);
/// sync receive message of unknown size into buffer. Call nn_freemsg() if result > 0 to free memory.
int recv(
void **buf ///< buffer
);
/// sync receive string
std::string recv();
};
/**
* Publisher or Subscriber abstract class
*/
class NanoPubSub : public NanoMessage {
public:
NanoPubSub(
int protocol ///< nanomsg protocol umber
);
~NanoPubSub();
};
/**
* Nanomsg Publisher
*/
class NanoPub : public NanoPubSub {
std::string mAddress;
public:
NanoPub(
const char *address ///< address of the socket
);
const std::string &address();
};
/**
* Nanomsg Subscriber
*/
class NanoSub : public NanoPubSub {
protected:
std::string mTopic;
public:
NanoSub(
const char *address, ///< address of the socket
void *topic, ///< topic
size_t topic_size
);
NanoSub(
const char *address, ///< address of the socket
const std::string &topic ///< topic
);
/// topic
std::string &topic();
};
#endif /* NANOMESSAGE_H_ */