I am trying to write a small application to process networking packet. To facilitate the test, I hope this app can operate in two mode: local streaming mode (using std::cin) and network streaming mode (using asio) – based on if the command line provides a port number for example. The goal is make it transparent to rest of the client class(es) who do the actual parsing of the packet, for example, they can grab a reference to the stream, event_stream, then do parsing as:
event_stream >> MsgHeader;
Assuming the class MsgHeader has a overload operator >>.
For local stream mode: I am just return &cin. For networking mode, I hope I can do something like this:
boost::asio::io_service io_s;
tcp::acceptor acc(io_s. tcp::endpoint(tcp::v4(), 1950));
tcp::iostream stream;
acc.accept(*stream.rdbuf()); // act as server, another client will write to it.
return &stream;
But I am a little lost how to make the rest of the client “unaware” of what kind stream they are deal with? Can I just declare it such as:
std::istream &in = get_event_stream();
Two possibilities:
Templatize everything on the stream type. Might be overkill, but allows flexibility for streams that don’t all adhere to a standard interface (through specialization). Different interfaces will need different specializations, but the high-level interface (the calling of the template) will be uniform at least.
Make sure all streams you use inherit from the same base class (if not, provide adaptor classes to wrap around the difference in interface), and use that. I’m not sure if a reference will work here, you might need a pointer variable instead, like this:
std::istream *in = &get_event_stream();
Then make your functions take a pointer to the base class (here
std::istream), and only use functions the base class has: