lim跨平臺(tái)網(wǎng)絡(luò)通信框架
lim 是一套輕量級(jí)的高性能通信框架,基于 C/C++ 語(yǔ)言開(kāi)發(fā),采用全異步通信模式,內(nèi)部集成了 HTTP、HTTPS、WebSocket 通信協(xié)議實(shí)現(xiàn),目前支持 Windows 和 Linux 平臺(tái)。
示例代碼:
#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>
namespace lim {
class HttpServer: public HttpFullRequestSession {
public:
HttpServer(SocketChannel &channel, BootstrapConfig &config): HttpFullRequestSession(channel, config) {
RegistHandleRouter("POST", "/test", std::bind(&HttpsServer::PostTestHandle, this, std::placeholders::_1));
}
virtual ~HttpsServer() = default;
private:
bool PostTestHandle(Message &request) {
HttpFullResponse http_response(200, "OK", "HTTP/1.1");
int length = 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); //發(fā)送完畢關(guān)閉連接
});
return true;
}
};
}
using namespace lim;
int main() {
Logger *logger = Logger::GetLogger("demo");
SocketChannel::InitEnviroment();
//服務(wù)監(jiān)聽(tīng)器&處理線程池
EventLoop server_event_loop;
ExecuteThread server_execute_thread;
//客戶端連接監(jiān)聽(tīng)器&處理線程池
EventLoopGroup worker_event_loop_group;
ExecuteThreadGroup worke_execute_thread_group;
HttpBootstrapConfig config(worker_event_loop_group, worke_execute_thread_group, server_event_loop, server_execute_thread);
//設(shè)置處理超時(shí)時(shí)間
config.SetTimeout(30 * 1000);
//異?;氐艉瘮?shù)
config.SetLoggerCallback([&](LoggerLevel level, const std::string &message) {
TRACE_ERROR(logger, "%s", message.c_str());
});
Bootstrap strap = Bootstrap(config);
strap.Bind<ServerChannelSession<HttpServer>>("0.0.0.0", 8095);
while (1) {
std::this_thread::sleep_for(std::chrono::milliseconds(1000 * 5));
}
return 0;
}
評(píng)論
圖片
表情
