lim是一套轻量级的高性能通信框架,基于C/C++语言开发,采用全异步通信模式,内部集成了HTTP、HTTPS、WebSocket通信协议实现,目前支持Windows和Linux平台。
示例代码:
#include<lim/base/logger.h>#include<lim/base/bootstrap.h>#include<lim/base/server_channel_session.h>#include<lim/http/http_bootstrap_config.h>#include<lim/http/http_response_session.h>namespacelim{ classHttpServer:publicHttpFullRequestSession{ public: HttpServer(SocketChannel&channel,BootstrapConfig&config):HttpFullRequestSession(channel,config){ RegistHandleRouter("POST","/test",std::bind(&HttpsServer::PostTestHandle,this,std::placeholders::_1)); } virtual~HttpsServer()=default; private: boolPostTestHandle(Message&request){ HttpFullResponsehttp_response(200,"OK","HTTP/1.1"); intlength=http_response.Content().Content().WriteBytes("{\"aa\":8}",strlen("{\"aa\":8}")); http_response.Headers().SetHeaderValue("Connection","close"); http_response.Headers().SetHeaderValue("Content-Type","application/json"); http_response.Headers().SetHeaderValue("Content-Length",std::to_string(length)); WriteHttpResponse(http_response,[&]{ Signal(ExecuteEvent::KILL_EVENT);//发送完毕关闭连接 }); returntrue; } };}usingnamespacelim;intmain(){ Logger*logger=Logger::GetLogger("demo"); SocketChannel::InitEnviroment(); //服务监听器&处理线程池 EventLoopserver_event_loop; ExecuteThreadserver_execute_thread; //客户端连接监听器&处理线程池 EventLoopGroupworker_event_loop_group; ExecuteThreadGroupworke_execute_thread_group; HttpBootstrapConfigconfig(worker_event_loop_group,worke_execute_thread_group,server_event_loop,server_execute_thread); //设置处理超时时间 config.SetTimeout(30*1000); //异常回掉函数 config.SetLoggerCallback([&](LoggerLevellevel,conststd::string&message){ TRACE_ERROR(logger,"%s",message.c_str()); }); Bootstrapstrap=Bootstrap(config); strap.Bind<ServerChannelSession<HttpServer>>("0.0.0.0",8095); while(1){ std::this_thread::sleep_for(std::chrono::milliseconds(1000*5)); } return0;}
评论