同步操作将从 CageQ/knet 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
Simple morden c++ network library wrapper based on asio standalone version , provide simple apis to write network applications.
it uses the std::string_view api for http parser, so we need c++17 to compile it.
the library contains a simple log library to print debug log. using fmt library to accelerate the log output.
you can use "setup.sh" to install dependence lib into deps directory.
the library is headonly library, basically you can copy to you project and use it.
./setup.sh # install asio fmt lib to deps directory, no need root permissions.
cmake .
make -j4
create a tcp server , you need define a tcp session type, with the session type , you can create the listener. start it with the port. now you get a discard tcp server.
#include "knet.hpp"
class TcpSession : public TcpConnection<TcpSession> {
public:
};
TcpListener<TcpSession> listener;
listener.start(8899);
It's almost the same with the server code, you need define a tcp session first , then connect to server will create a session for you .
class TcpSession : public TcpConnection<TcpSession> {
public:
};
TcpConnector<TcpSession> connector;
connector.start();
connector.add_connection("127.0.0.1", 8899);
there are two different handler , event handler and data handler, you can bind your handlers in your session . using the bind_event_handler and bind_data_handler to get data or connection events.
class TcpSession : public TcpConnection<TcpSession >
{
public:
typedef std::shared_ptr<TcpSession> TcpSessionPtr;
TcpSession()
{
bind_data_handler(&TcpSession::process_data);
bind_event_handler([this]( TcpSessionPtr,knet::NetEvent ){
std::string msg("hello world");
this->send(msg.c_str(),msg.length());
return 0;
} );
}
uint32_t process_data(const std::string & msg , knet::MessageStatus status)
{
// trace;
dlog("received data {} ",msg);
this->send(msg.data(),msg.length());
return msg.length();
}
};
most of the time , we need a manager to manage all the sessions, so you need create a factory to handle all connections' events .
if you use your own factory, you cann't get data or events in your own session .
class MyFactory: public ConnectionFactory<TcpSession> { // TcpSession is your real session class to process your session events and data
public:
virtual void destroy(TPtr conn) {
dlog("connection factory destroy connection in my factory ");
}
virtual void handle_event(TPtr conn, knet::NetEvent evt) {
ilog("handle event in connection my factory");
}
virtual int32_t handle_data(TPtr conn, char * data, uint32_t len) {
conn->send(data,len);
return len ;
};
};
MyFactory factory;
dlog("start server");
TcpListener<TcpSession,MyFactory> listener(&factory);// you need create a factory instance and pass it to listener.
int port = 8899;
listener.start( port);
you can create one or multi EventWorker to process the connections, but we will keep one connecion's events will always be in one thread in its lifecycle.
for example it is safe to create one lua engine in your session , all net events will be called in the same thread.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。