-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathipc.h
208 lines (157 loc) · 3.75 KB
/
ipc.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#ifndef IPC_H
#define IPC_H
#include <string>
#include <boost/thread.hpp>
#include <boost/uuid/uuid.hpp> // uuid class
#include <boost/uuid/uuid_generators.hpp> // generators
#include <boost/uuid/uuid_io.hpp> // streaming operators etc.
#include <boost/lexical_cast.hpp>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
using namespace std;
class Ipc {
public:
Ipc(int pid) {
string guid = boost::lexical_cast<std::string>(boost::uuids::random_generator()());
char pairbuf[128];
snprintf(pairbuf, 127, "webtail_%d_%s", pid, guid.c_str());
pair = string(pairbuf);
data_fd = -1;
}
Ipc(const char *pair) {
this->pair = pair;
data_fd = -1;
}
const char *getPair() {
return pair.c_str();
}
void send(const char *message) {
if(!connect()) {
return;
}
uint nlen = htonl(strlen(message));
int written = write(data_fd, &nlen, 4);
if(written < 4) {
perror("write error");
return;
}
uint n=0;
while(n<strlen(message)) {
int written = write(data_fd, &message[n], strlen(message)-n);
if(written < 0) {
perror("write error");
return;
}
n += written;
}
}
string receive() {
if(!connect()) {
return "";
}
uint nlen;
if(read(data_fd,&nlen,4) < 4) {
perror("read buffer length");
return "";
}
uint len = ntohl(nlen);
int rc;
char buf[len+1];
uint n=0;
while ( n<len && (rc=read(data_fd, &buf[n], len-n)) > 0) {
n += rc;
}
if (rc == -1) {
perror("read");
return "";
}
buf[len] = 0;
return string(buf);
}
static bool testme() {
Ipc conn(::getpid());
boost::thread echo_thread(Ipc::echoer, conn.getPair());
conn.send("Hello!");
sleep(2);
conn.send("stop");
echo_thread.join();
return true;
}
private:
static void echoer(const char *id) {
Ipc conn(id);
string msg;
do {
msg = conn.receive();
if(msg.empty()) {
break;
}
string echomsg = "ECHO:" + string(msg);
conn.send(echomsg.c_str());
} while(strcmp(msg.c_str(), "stop") != 0);
}
bool connect() {
if(data_fd > 0) {
return true;
}
static int socket_fd;
while(true) {
sleep(5);
if ( (socket_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket error");
return false;
}
// First try and connect
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, pair.c_str(), sizeof(addr.sun_path)-1);
if (::connect(socket_fd, (struct sockaddr*)&addr, sizeof(addr)) >= 0) {
data_fd = socket_fd;
cout << "Thread: " << boost::this_thread::get_id() << " connected";
return true; // connected
}
if(errno != ECONNREFUSED && errno != ENOENT) {
perror("connect error");
return false;
}
// If otherside isn't listening, listen ourselves
close(socket_fd);
if ( (socket_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket error");
return false;
}
// unlink(pair.c_str());
if (bind(socket_fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
if(errno==EADDRINUSE) {
continue; // Something already binded
}
perror("bind error");
return false;
}
if (listen(socket_fd, 5) == -1) {
if(errno==EADDRINUSE || errno==EINVAL) {
close(socket_fd);
continue; // Something already listened
}
perror("listen error");
return false;
}
cout << "Thread: " << boost::this_thread::get_id() << " in accept";
int connect_fd;
if ( (connect_fd = accept(socket_fd, NULL, NULL)) == -1) {
perror("accept error");
close(socket_fd);
continue;
}
cout << "Thread: " << boost::this_thread::get_id() << " accept ok";
data_fd = connect_fd;
return true; // connected
}
return false;
}
int data_fd;
string pair;
};
#endif