当前位置: 首页 > news >正文

一次sougo workflow库的使用过程

安装就是常规的make install

tutorial http_echoserver实现一下,在macos上实现


cmakelist.txt

cmake_minimum_required(VERSION 3.6)set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Release")project(mainLANGUAGES C CXX
)set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin) #放在本项目下bin文件夹下
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_COMPILER g++) # cmake 找包
find_package(workflow REQUIRED CONFIG HINTS ..)
# 来自
include_directories(${OPENSSL_INCLUDE_DIR} ${WORKFLOW_INCLUDE_DIR})
# 找到workflow就有这些变量,不用管
link_directories(${WORKFLOW_LIB_DIR})# 源码目录
AUX_SOURCE_DIRECTORY(. SRC)#  目前只针对macos端
if (APPLE)set(WORKFLOW_LIB workflow pthread )
endif ()add_executable(main ${SRC})
target_link_libraries(main ${WORKFLOW_LIB})

main.cpp

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include "workflow/HttpMessage.h"
#include "workflow/HttpUtil.h"
#include "workflow/WFServer.h"
#include "workflow/WFHttpServer.h"
#include "workflow/WFFacilities.h"void process(WFHttpTask *server_task)
{protocol::HttpRequest *req = server_task->get_req();protocol::HttpResponse *resp = server_task->get_resp();long long seq = server_task->get_task_seq();protocol::HttpHeaderCursor cursor(req);std::string name;std::string value;char buf[8192];int len;/* Set response message body. */resp->append_output_body_nocopy("<html>", 6);len = snprintf(buf, 8192, "<p>%s %s %s</p>", req->get_method(),req->get_request_uri(), req->get_http_version());resp->append_output_body(buf, len);while (cursor.next(name, value)){len = snprintf(buf, 8192, "<p>%s: %s</p>", name.c_str(), value.c_str());resp->append_output_body(buf, len);}resp->append_output_body_nocopy("</html>", 7);/* Set status line if you like. */resp->set_http_version("HTTP/1.1");resp->set_status_code("200");resp->set_reason_phrase("OK");resp->add_header_pair("Content-Type", "text/html");resp->add_header_pair("Server", "Sogou WFHttpServer");if (seq == 9) /* no more than 10 requests on the same connection. */resp->add_header_pair("Connection", "close");/* print some log */char addrstr[128];struct sockaddr_storage addr;socklen_t l = sizeof addr;unsigned short port = 0;server_task->get_peer_addr((struct sockaddr *)&addr, &l);if (addr.ss_family == AF_INET){struct sockaddr_in *sin = (struct sockaddr_in *)&addr;inet_ntop(AF_INET, &sin->sin_addr, addrstr, 128);port = ntohs(sin->sin_port);}else if (addr.ss_family == AF_INET6){struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr;inet_ntop(AF_INET6, &sin6->sin6_addr, addrstr, 128);port = ntohs(sin6->sin6_port);}elsestrcpy(addrstr, "Unknown");fprintf(stderr, "Peer address: %s:%d, seq: %lld.\n",addrstr, port, seq);
}static WFFacilities::WaitGroup wait_group(1);void sig_handler(int signo)
{wait_group.done();
}int main(int argc, char *argv[])
{unsigned short port;if (argc != 2){fprintf(stderr, "USAGE: %s <port>\n", argv[0]);exit(1);}signal(SIGINT, sig_handler);WFHttpServer server(process);port = atoi(argv[1]);if (server.start(port) == 0){wait_group.wait();server.stop();}else{perror("Cannot start server");exit(1);}return 0;
}

run.sh

cmake -B b
cmake --build b 
./bin/main 9999 &
curl localhost:9999
killall main
rm -rf b

文件目录结构

MacBook :: Programming/myf » ls 
CMakeLists.txt b              bin            main.cpp       run.sh
http://www.lryc.cn/news/227118.html

相关文章:

  • macOS Big Sur(macos11版本)
  • 泛微E-Office信息泄露漏洞复现
  • -bash: sudo: command not found的解决方法
  • CMOS介绍
  • 《软件工程与计算》期末考试真题范例及答案
  • springboot高校全流程考勤系统-计算机毕设 附源码 27637
  • 大二第四周总结——用原生js封装一个分页器
  • 智能AI系统ChatGPT系统源码+支持GPT4.0+支持ai绘画(Midjourney)/支持OpenAI GPT全模型+国内AI全模型
  • ARM Linux 基础学习 / 系统相关,文件系统,文件属性
  • nginx https 如何将部分路径转移到 http
  • 算法通关村第八关-白银挑战二叉树的深度和高度问题
  • 使用LogBack替换Log4j
  • 财务报告是什么
  • SOME/IP 协议介绍(四)RPC协议规范
  • PostgreSQL基础入门
  • Django 密码管理:安全实践与技术深入
  • 说说你对React Router的理解?常用的Router组件有哪些?
  • “可一学院”新课程《区块链企业应用》正式上线
  • Springboot---整合对象储存服务MinIO
  • HDRP图形入门:HDRP渲染管线depth翻转
  • uniapp——项目02
  • Go语言的Json序列化与反序列化、Goto语法、Tcp Socket通信
  • gitlab-ce-12.3.5 挖矿病毒及解决方案
  • 每日一题(LeetCode)----数组--移除元素(四)
  • 421. 数组中两个数的最大异或值/字典树【leetcode】
  • C++(20):自定义类型实现基于范围的for循环
  • Linux常用命令:find、grep、vim、cat、less、more
  • Oracle导入,注意事项
  • 【数据结构】入队序列出队序列问题(以21年408真题举例)
  • 在ant构建脚本中调用maven的命令