libtnet是一个用c++编写的高性能网络库,它在设计上面主要参考tornado,为服务端网络编程提供简洁而高效的接口,非常易于使用。
一个简单的Echo例子:
void onConnEvent(const ConnectionPtr_t& conn, ConnEvent event, const void* context) { switch(event) { case Conn_ReadEvent: { const StackBuffer* buffer = static_cast(context); conn->send(string(buffer->buffer, buffer->count)); } break; default: break; } } int main() { TcpServer s; s.listen(Address(11181), std::bind(&onConnEvent, _1, _2, _3)); s.start(); return 0; }
启动shell进行测试
root@tnet:~# telnet 127.0.0.1 11181 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. hello world hello world