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
|
#include <msgpack.hpp>
#include <iostream>
#include <stdexcept>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
class Server {
public:
Server(int sock) : m_sock(sock) { }
~Server() { }
typedef std::auto_ptr<msgpack::zone> auto_zone;
void socket_readable()
{
m_pac.reserve_buffer(1024);
ssize_t count =
read(m_sock, m_pac.buffer(), m_pac.buffer_capacity());
if(count < 0) {
if(errno == EAGAIN || errno == EINTR) {
return;
} else {
throw std::runtime_error(strerror(errno));
}
} else if(count == 0) {
throw std::runtime_error("connection closed");
}
m_pac.buffer_consumed(count);
while(m_pac.execute()) {
msgpack::object msg = m_pac.data();
auto_zone life( m_pac.release_zone() );
m_pac.reset();
process_message(msg, life);
}
}
private:
void process_message(msgpack::object msg, auto_zone& life)
{
std::cout << "message reached: " << msg << std::endl;
}
private:
int m_sock;
msgpack::unpacker m_pac;
};
static void* run_server(void* arg)
try {
Server* srv = reinterpret_cast<Server*>(arg);
while(true) {
srv->socket_readable();
}
return NULL;
} catch (std::exception& e) {
std::cerr << "error while processing client packet: "
<< e.what() << std::endl;
return NULL;
} catch (...) {
std::cerr << "error while processing client packet: "
<< "unknown error" << std::endl;
return NULL;
}
struct fwriter {
fwriter(int fd) : m_fp( fdopen(fd, "w") ) { }
void write(const char* buf, size_t buflen)
{
size_t count = fwrite(buf, buflen, 1, m_fp);
if(count < 1) {
std::cout << buflen << std::endl;
std::cout << count << std::endl;
throw std::runtime_error(strerror(errno));
}
}
void flush() { fflush(m_fp); }
void close() { fclose(m_fp); }
private:
FILE* m_fp;
};
int main(void)
{
int pair[2];
pipe(pair);
// run server thread
Server srv(pair[0]);
pthread_t thread;
pthread_create(&thread, NULL,
run_server, reinterpret_cast<void*>(&srv));
// client thread:
fwriter writer(pair[1]);
typedef msgpack::type::tuple<std::string, std::string, std::string> put_t;
typedef msgpack::type::tuple<std::string, std::string> get_t;
put_t req1("put", "apple", "red");
put_t req2("put", "lemon", "yellow");
get_t req3("get", "apple");
msgpack::pack(writer, req1);
msgpack::pack(writer, req2);
msgpack::pack(writer, req3);
writer.flush();
writer.close();
pthread_join(thread, NULL);
}
|