forked from kikidong/avbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.cpp
66 lines (60 loc) · 1.71 KB
/
input.cpp
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
#include <boost/signals2.hpp>
#include <boost/asio.hpp>
#if !defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
#include <boost/thread.hpp>
#endif
#include <boost/stringencodings.hpp>
#include "input.hpp"
static boost::signals2::signal< void(std::string)> input_got_one_line;
#if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
static void inputread(const boost::system::error_code & ec, std::size_t length,
std::shared_ptr<boost::asio::posix::stream_descriptor> stdin,
std::shared_ptr<boost::asio::streambuf> inputbuffer)
{
std::istream input(inputbuffer.get());
std::string line;
std::getline(input,line);
input_got_one_line(line);
boost::asio::async_read_until(
*stdin, *inputbuffer, '\n',
boost::bind(inputread , _1,_2, stdin, inputbuffer)
);
}
#else
// workarround windows that can use posix stream for stdin
static void input_thread(boost::asio::io_service & io_service)
{
while (!boost::this_thread::interruption_requested() && !std::cin.eof())
{
std::string line;
std::getline(std::cin, line);
io_service.post([line]{
input_got_one_line(ansi_utf8(line));
});
}
}
#endif
void start_stdinput(boost::asio::io_service & io_service)
{
#if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
std::shared_ptr<boost::asio::posix::stream_descriptor> stdin(
new boost::asio::posix::stream_descriptor( io_service, 0 )
);
std::shared_ptr<boost::asio::streambuf> inputbuffer(
new boost::asio::streambuf
);
boost::asio::async_read_until(
*stdin, *inputbuffer, '\n',
boost::bind(inputread , _1, _2, stdin, inputbuffer)
);
#else
boost::thread(
boost::bind(input_thread, boost::ref(io_service))
);
#endif
}
void connect_stdinput(boost::function<void(std::string)> cb)
{
input_got_one_line.connect(cb);
}