2 * Asynchronous IO buffer adapter
4 * This library is under no license, hence,
5 * you can do whatever you wanna do on this
8 * Created on: Jul 5, 2010
16 * Here is a illustration on you use this lib,
17 * this lib requires you to define a Routine that
18 * flush the data from buffer and do what you wanna
19 * ----------------------------------------------------------------------
20 void Routine(nSyncBuf_s & buf, size_t size)
22 cout<<string(*buf, size);
26 int main(int argc, char ** argv)
28 nSyncBuf_s buf(Routine, true);
29 cerr<<"[ImportFileList]:"<<ImportFileList(buf, argv[1])<<endl;
32 * ----------------------------------------------------------------------
33 nSyncBuf_s buf(coutRoutine, true):
34 "nSyncBuf_s" is a class that contains the buffer
35 and linked to boost::asio for asynchronous stream.
36 "Routine" is the Routine that handles the data.
37 You can pass the second parameter as "true" if the
38 next run of "Routine" would depend on the accomplishment
39 of the last run, thus to say, read and process sequentially,
40 or "false" if "Routine"s are absolutely independent.
41 PLEASE NOTE THE, for passing "true", it's your duty to
42 "release" when the previous Routine was done. You can
43 see "buf.release();" after "cout<<string(*buf, size);".
44 Deadlock would occur if you forget to release when "true";
45 cout<<string(*buf, size):
46 This demonstrated the way to visit the buffer, "*buf"
47 as well as "buf.ptr()" would return the foremost
48 pointer of buffer. "size" indicates the length of
49 effective data in buffer.
50 ImportFileList(buf, argv[1]):
51 "argv[1]" should be a filename that contains filenames,
52 one row each filename.
53 InsertFile(buf, argv[1]):
54 "argv[1]" should be a single filename pending to read.
55 Please notice that, for multiple files, the would be no
56 EOF at the end of each file, thus, the data of next file
57 (if available) would append tightly at the very end of
64 #if defined(__CYGWIN__)
65 # define _WIN32_WINNT 0x0501
66 # define __USE_W32_SOCKETS
67 # undef BOOST_POSIX_API
68 # define BOOST_WINDOWS_API
70 #include <boost/asio.hpp>
71 #define BOOST_PROCESS_WINDOWS_USE_NAMED_PIPE
72 #include <boost/process.hpp>
73 #include <boost/bind.hpp>
76 #include <boost/thread/mutex.hpp>
81 using namespace boost
;
82 namespace bp
= ::boost::process
;
83 namespace ba
= ::boost::asio
;
86 typedef buf_t
* nSyncBufAry_t
;
89 typedef void(*_ConsumeBufferRoutine_f
)(nSyncBuf_s
&, size_t);
95 explicit nSyncBuf_s(_ConsumeBufferRoutine_f
, bool);
98 void lock() __attribute__((always_inline
));
99 void unlock() __attribute__((always_inline
));
102 nSyncBufAry_t
operator *();
105 friend int ImportFileList(nSyncBuf_s
&, char *);
106 friend int InsertFile(nSyncBuf_s
&, char *);
107 friend void nSync(nSyncBuf_s
&);
108 friend bp::child
StartChild(nSyncBuf_s
&);
109 friend void nSyncRead(nSyncBuf_s
&);
110 friend void endRead(nSyncBuf_s
&, const boost::system::error_code
&, size_t);
113 vector
<string
> fnList
;
114 nSyncBufAry_t buffer
;
117 _ConsumeBufferRoutine_f Routine
;
120 //ASIO stream connectors
121 ba::io_service io_service
;
122 #if defined(BOOST_POSIX_API)
123 ba::posix::stream_descriptor in
;
124 #elif defined(BOOST_WINDOWS_API)
125 ba::windows::stream_handle in
;
127 #error Unsupported platform.
131 int ImportFileList(nSyncBuf_s
&, char *) __attribute__((warn_unused_result
));
132 int InsertFile(nSyncBuf_s
&, char *) __attribute__((warn_unused_result
));
133 void nSync(nSyncBuf_s
&);
135 #endif /* _NSYNC_H_AQUA */