lim 正在参加 2021 年度 OSC 中国开源项目评选,请投票支持!
lim 在 2021 年度 OSC 中国开源项目评选 中已获得 {{ projectVoteCount }} 票,请投票支持!
2021 年度 OSC 中国开源项目评选 正在火热进行中,快来投票支持你喜欢的开源项目!
2021 年度 OSC 中国开源项目评选 >>> 中场回顾
lim 获得 2021 年度 OSC 中国开源项目评选「最佳人气项目」 !
授权协议 未知
开发语言 C/C++
操作系统 跨平台
软件类型 开源软件
开源组织
地区 国产
投 递 者 limzhoujx
适用人群 未知
收录时间 2019-09-25

软件简介

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>

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); //发送完毕关闭连接
      });
      return true;
    }
  };
}

using namespace lim;
int main() {
  Logger *logger = Logger::GetLogger("demo");
  SocketChannel::InitEnviroment();
  
  //服务监听器&处理线程池
  EventLoop server_event_loop;
  ExecuteThread server_execute_thread;
  
  //客户端连接监听器&处理线程池
  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);
  //设置处理超时时间
  config.SetTimeout(30 * 1000); 
  //异常回掉函数
  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;
}

 

展开阅读全文

代码

的 Gitee 指数为
超过 的项目

评论

点击引领话题📣 发布并加入讨论🔥
暂无内容
发表了博客
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
发表了问答
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
暂无内容
0 评论
22 收藏
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部