-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryStreamService.h
375 lines (316 loc) · 13.2 KB
/
MemoryStreamService.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
* MemoryStreamService.h
*
* Created on: Nov 16, 2013
* Author: janoc
*/
#ifndef MEMORYSTREAMSERVICE_H_
#define MEMORYSTREAMSERVICE_H_
#include <cstddef>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/asio/error.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/thread/mutex.hpp>
class memory_stream_impl
{
public:
memory_stream_impl() :
m_read_data_available(false),
m_write_data_available(false)
{};
virtual ~memory_stream_impl() {};
virtual void destroy() {}
template <typename MutableBufferSequence>
std::size_t read_some(const MutableBufferSequence& buffers, boost::system::error_code& ec)
{
// wait for new data to arrive
boost::unique_lock<boost::mutex> lock(m_read_mutex);
while(!m_read_data_available)
m_cond_read.wait(lock);
char *buf_pos = &m_read_buffer[0];
std::size_t remains = m_read_buffer.size();
std::size_t read = 0;
// fill the buffers
typename MutableBufferSequence::const_iterator it;
for(it = buffers.begin(); it != buffers.end(); ++it)
{
std::size_t b_size = boost::asio::buffer_size(*it);
if(b_size < remains)
{
// fill as much as possible
boost::asio::buffer_copy((*it), boost::asio::mutable_buffer((void *)buf_pos, b_size));
read += b_size;
buf_pos += b_size;
remains -= b_size;
} else
{
// fill the remaining bit, we will block next time because the buffer is now empty
boost::asio::buffer_copy((*it), boost::asio::mutable_buffer((void *)buf_pos, remains));
read += remains;
remains = 0;
m_read_data_available = false;
break;
}
}
m_read_buffer.erase(0, read);
ec = boost::system::error_code();
return read;
}
/// Write the given data to the stream.
template<typename ConstBufferSequence>
std::size_t write_some(const ConstBufferSequence& buffers,
boost::system::error_code& ec)
{
std::size_t written = 0;
// copy buffers into our write buffer
{
boost::lock_guard<boost::mutex> lock(m_write_mutex);
typename ConstBufferSequence::const_iterator it;
for(it = buffers.begin(); it != buffers.end(); ++it)
{
unsigned char *buf = boost::asio::buffer_cast<unsigned char*>(*it);
std::size_t len = boost::asio::buffer_size(*it);
m_write_buffer.insert(m_write_buffer.end(), buf, buf + len);
written += len;
}
m_write_data_available = true;
}
// signal whoever may be waiting for data
m_cond_write.notify_one();
ec = boost::system::error_code();
return written;
}
void push_read_buffer(const std::string &data)
{
// fill buffer
{
boost::lock_guard<boost::mutex> lock(m_read_mutex);
m_read_buffer.insert(m_read_buffer.end(), data.begin(), data.end());
m_read_data_available = true;
}
// signal whoever may be waiting for data
m_cond_read.notify_one();
}
std::string pop_write_buffer()
{
boost::unique_lock<boost::mutex> lock(m_write_mutex);
// block until we have something to send out
while(!m_write_data_available)
m_cond_write.wait(lock);
std::string tmp = m_write_buffer;
m_write_buffer.clear();
m_write_data_available = false;
return tmp;
}
private:
bool m_read_data_available;
bool m_write_data_available;
boost::mutex m_read_mutex;
boost::mutex m_write_mutex;
boost::condition_variable m_cond_read;
boost::condition_variable m_cond_write;
std::string m_read_buffer;
std::string m_write_buffer;
};
template <typename Service>
class basic_memory_stream
: public boost::asio::basic_io_object<Service>
{
public:
explicit basic_memory_stream(boost::asio::io_service &io_service) : boost::asio::basic_io_object<Service>(io_service) {};
void pushReadBuffer(const std::string &data)
{
this->get_service().push_read_buffer(this->get_implementation(), data);
}
std::string popWriteBuffer(void)
{
return this->get_service().pop_write_buffer(this->get_implementation());
}
template <typename MutableBufferSequence>
std::size_t read_some(const MutableBufferSequence& buffers,
boost::system::error_code& ec)
{
return this->get_service().read_some(this->get_implementation(), buffers, ec);
}
template <typename MutableBufferSequence, typename ReadHandler>
void async_read_some(const MutableBufferSequence& buffers,
BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
{
// If you get an error on the following line it means that your handler does
// not meet the documented type requirements for a ReadHandler.
//BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
this->get_service().async_read_some(this->get_implementation(),
buffers, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
}
template<typename ConstBufferSequence, typename WriteHandler>
void async_write_some(const ConstBufferSequence& buffers,
BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
{
// If you get an error on the following line it means that your handler does
// not meet the documented type requirements for a WriteHandler.
//BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
this->get_service().async_write_some(this->get_implementation(),
buffers, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
}
};
template <typename MemoryStreamImplementation = memory_stream_impl>
class memory_stream_service
: public boost::asio::io_service::service
{
public:
/*
* mandatory io_service API
*/
static boost::asio::io_service::id id;
explicit memory_stream_service(boost::asio::io_service &io_service) :
boost::asio::io_service::service(io_service),
async_work_(new boost::asio::io_service::work(async_io_service_)),
async_thread_(boost::bind(&boost::asio::io_service::run, &async_io_service_))
{
}
~memory_stream_service()
{
async_work_.reset();
async_io_service_.stop();
async_thread_.join();
}
typedef boost::shared_ptr<MemoryStreamImplementation> implementation_type;
void construct(implementation_type &impl)
{
impl.reset(new MemoryStreamImplementation);
}
void destroy(implementation_type &impl)
{
impl->destroy();
impl.reset();
}
/*
* sync and async read API
*/
template <typename MutableBufferSequence>
std::size_t read_some(implementation_type& impl,
const MutableBufferSequence& buffers, boost::system::error_code& ec)
{
return impl->read_some(buffers, ec);
}
// async read operation functor
template<typename ReadHandler, typename MutableBufferSequence>
class read_operation
{
public:
read_operation(implementation_type &impl,
boost::asio::io_service &io_service,
const MutableBufferSequence& buffers,
ReadHandler handler) :
impl_(impl),
io_service_(io_service),
work_(io_service),
buffers_(buffers), handler_(handler)
{
}
void operator()() const
{
implementation_type impl = impl_.lock();
if (impl)
{
boost::system::error_code ec;
std::size_t transferred = impl->read_some(buffers_, ec);
this->io_service_.post(boost::asio::detail::bind_handler(handler_, ec, transferred));
}
else
{
this->io_service_.post(boost::asio::detail::bind_handler(handler_,
boost::asio::error::operation_aborted, 0));
}
}
private:
boost::weak_ptr<memory_stream_impl> impl_;
boost::asio::io_service &io_service_;
boost::asio::io_service::work work_;
const MutableBufferSequence buffers_;
ReadHandler handler_;
};
template <typename MutableBufferSequence, typename ReadHandler>
void async_read_some(implementation_type& impl,
const MutableBufferSequence& buffers,
BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
{
this->async_io_service_.post(read_operation<ReadHandler, MutableBufferSequence>(impl, this->get_io_service(), buffers, handler));
}
/*
* sync and async write API
*/
template<typename ConstBufferSequence>
std::size_t write_some(implementation_type& impl,
const ConstBufferSequence& buffers,
boost::system::error_code& ec)
{
return impl->write_some(buffers, ec);
}
// async write operation functor
template<typename WriteHandler, typename ConstBufferSequence>
class write_operation
{
public:
write_operation(implementation_type &impl,
boost::asio::io_service &io_service,
const ConstBufferSequence& buffers,
WriteHandler handler) :
impl_(impl),
io_service_(io_service),
work_(io_service),
buffers_(buffers), handler_(handler)
{
}
void operator()() const
{
implementation_type impl = impl_.lock();
if (impl)
{
boost::system::error_code ec;
std::size_t transferred = impl->write_some(buffers_, ec);
this->io_service_.post(boost::asio::detail::bind_handler(handler_, ec, transferred));
}
else
{
this->io_service_.post(boost::asio::detail::bind_handler(handler_,
boost::asio::error::operation_aborted, 0));
}
}
private:
boost::weak_ptr<memory_stream_impl> impl_;
boost::asio::io_service &io_service_;
boost::asio::io_service::work work_;
const ConstBufferSequence buffers_;
WriteHandler handler_;
};
template<typename ConstBufferSequence, typename WriteHandler>
void async_write_some(implementation_type& impl,
const ConstBufferSequence& buffers,
BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
{
this->async_io_service_.post(write_operation<WriteHandler, ConstBufferSequence>(impl, this->get_io_service(), buffers, handler));
}
// test api, dummy data source/sink
void push_read_buffer(implementation_type& impl, const std::string &data)
{
impl->push_read_buffer(data);
}
std::string pop_write_buffer(implementation_type& impl)
{
return impl->pop_write_buffer();
}
private:
void shutdown_service()
{
}
boost::asio::io_service async_io_service_;
boost::scoped_ptr<boost::asio::io_service::work> async_work_;
boost::thread async_thread_;
};
template <typename MemoryStreamImplementation>
boost::asio::io_service::id memory_stream_service<MemoryStreamImplementation>::id;
typedef basic_memory_stream<memory_stream_service<memory_stream_impl> > MemoryStream;
#endif /* MEMORYSTREAMSERVICE_H_ */