Thin wrapper of boost.asio for creating applications based on asynchronous TCP sockets.
- Based on asio library and therefore gained the same cross-platform ability
- C++ 11 compatible
- Deliberately avoid using
std::function<>to reduce overhead of callbacks - Presents extremely simple interfaces supporting two styles of session definition:
| Session Defined with Class | Session Defined with Functions |
class Server: public Session{
public:
virtual void onConnect (TCPsession& session)override{
doRead(session);
}
virtual void onDisconnect (TCPsession& session)override{
std::cout << session.getRemoteIP()
<< ':' << session.getRemotePort()
<< " disconnected" << std::endl;
}
virtual void onError(TCPsession& session,
const std::string& err,
ErrorType)override{
std::cout << err << std::endl;
}
virtual ~TimeoutServer(){}
private:
void doRead(TCPsession& session){
session.asyncRead('\0',
[](TCPsession& session_){
std::cout << session_.getMessage() << std::endl;
doRead(session_);
});
}
};
int main(){
asio::io_context io_context;
unsigned short port = 1701;
Service<> srv(port);
srv.start([]{return std::make_shared();});
Worker::run();
} |
void doRead(TCPsession& session){
session.asyncRead('\0',
[](TCPsession& session_){
std::cout << session_.getMessage() << std::endl;
doRead(session_);
});
}
int main(){
asio::io_context io_context;
auto session = SessionBuilder<>().onConnect([](TCPsession& session){
doRead(session);
}).onDisconnect([](TCPsession& session){
std::cout << session.getRemoteIP()
<< ':' << session.getRemotePort()
<< " disconnected" << std::endl;
}).onError([](TCPsession& session, const std::string& err, ErrorType){
std::cout << err << std::endl;
}).buildFactory();
unsigned short port = 1701;
Service<> srv(port);
srv.start(session);
Worker::run();
} |
- Echo client and server, messages terminated with
'\0': echoNulTer - Echo client and server, messages with header indicating the length: echoHeader
- Echo client and server, session defined by chain of callbacks: builder
- Timeout server: timeout
TCPsession encapsulated operations to be applied on the socket, including read, write, close, stat etc. This class should not be instantiated, but is passed to user-defined callbacks by reference.
-
Read
lengthbytes of data from socket, and invokecallbackupon finish. The signature ofCBshould bevoid (TCPsession&) -
Read data from socket until
tokenis met, then invokecallback. The signature ofCBshould bevoid (TCPsession&) -
Read data from socket until
tokenis met, then invokecallback. The signature ofCBshould bevoid (TCPsession&) -
Read data from socket until
completionConditionreturns 0, then invokecallback. The signature ofCBshould bevoid (TCPsession&) -
Write
messageto socket, and invokecallbackupon finish. The signature ofCBshould bevoid (TCPsession&).messagewon't be copied, so this reference must stay valid untilcallbackis called. -
Write content in
bufsto socket, and invokecallbackupon finish. The signature ofCBshould bevoid (TCPsession&).SharedBufferwill manage underlying memory, so no special care need to be paid on lifetime. -
Invoke
callbackaftertimeout. The signature ofCBshould bevoid (const std::error_code&). -
Close session after
timeout -
Invoke
callbackand close session aftertimeout -
Close the session gracefully
-
Get the received message after
asyncRead() -
Read
lengthbytes of the received message afterasyncRead() -
Get the IP address of the remote host
-
Get the port of the remote host