我是靠谱客的博主 害怕早晨,这篇文章主要介绍C/C++编程:libfiber例子学习channel_cpphttp,现在分享给大家,希望可以做个参考。

channel_cpp

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <acl-lib/acl_cpp/lib_acl.hpp> #include <acl-lib/fiber/libfiber.hpp> #include <zconf.h> struct A{ char buf[256]; }; class fiber_consumer : public acl::fiber{ public: explicit fiber_consumer(int n, acl::channel<int>&chan1, acl::channel<A>& chan2, acl::channel<acl::string *>& chan3) :count_(n), chan1_(chan1), chan2_(chan2), chan3_(chan3){} ~fiber_consumer() override = default; protected: void run() override{ for(int i = 0; i < count_; i++){ int n; chan1_.pop(n); A a{}; chan2_.pop(a); acl::string * s; chan3_.pop(s); if (i < 1000) printf(">>read: n = %d, a = %s, s = %srn", n, a.buf, s->c_str()); delete s; } } private: int count_; acl::channel<int> & chan1_; acl::channel<A> & chan2_; acl::channel<acl::string *> &chan3_; }; class fiber_producer : public acl::fiber{ public: fiber_producer(int n, acl::channel<int>& chan1, acl::channel<A>& chan2, acl::channel<acl::string*>& chan3) : count_(n), chan1_(chan1), chan2_(chan2), chan3_(chan3) {} ~fiber_producer() override = default; protected: void run() override { for(int i = 0; i < count_; i++){ int t = count_ - i - 1; chan1_ << t; A a{}; snprintf(a.buf, sizeof(a.buf), "A-%d", t); chan2_ << a; auto * s = new acl::string; s->format("string-%d", t); chan3_ << s; } } private: int count_; acl::channel<int>& chan1_; acl::channel<A>& chan2_; acl::channel<acl::string*>& chan3_; }; int main() { int ch, n = 10; acl::acl_cpp_init(); acl::log::stdout_open(true); acl::channel<int> chan1; acl::channel<A> chan2; acl::channel<acl::string*> chan3; fiber_consumer consumer(n, chan1, chan2, chan3); consumer.start(); fiber_producer producer(n, chan1, chan2, chan3); producer.start(); acl::fiber::schedule(); return 1; }

现象:
在这里插入图片描述

分析:

在这里插入图片描述
数据是先进先出的。

http

服务器1

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "lib_acl.h" #include "acl_cpp/lib_acl.hpp" #include <stdio.h> #include <stdlib.h> #include <lib_fiber/cpp/include/fiber/http_server.hpp> int main(int argc, char *argv[]) { const char* addr = "0.0.0.0|8194"; acl::http_server server; server.on_thread_init([] { printf("---> thread-%lu on initrn", acl::thread::self()); }).on_thread_accept([] (acl::socket_stream& conn) { printf("---> thread-%lu on accept %drn", acl::thread::self(), conn.sock_handle()); return true; }).Get("/", [](acl::HttpRequest& req, acl::HttpResponse& res) { acl::string buf("hello world1!"); res.setContentLength(buf.size()); return res.write(buf.c_str(), buf.size()); }).Get("/param", [](acl::HttpRequest& req, acl::HttpResponse& res) { const char * query = req.getQueryString(); const char * name = req.getParameter("name"); const char * age = req.getParameter("age"); printf("query : [%s], name : [%s], age : [%s]", query, name, age); acl::string buf(query); res.setContentLength(buf.size()); return res.write(buf.c_str(), buf.size()); }).Get("/json", [&](acl::HttpRequest&, acl::HttpResponse& res) { acl::json json; acl::json_node& root = json.get_root(); root.add_number("code", 200) .add_text("status", "+ok") .add_child("data", json.create_node() .add_text("name", "value") .add_bool("success", true) .add_number("number", 200)); return res.write(json); }); server.run_alone(addr); return 0; }

客户端1

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "acl_cpp/lib_acl.hpp" int main(void) { acl::http_request req("127.0.0.1:8194"); acl::http_header& hdr = req.request_header(); hdr.set_url("/param") .add_param("name", "oceanstar") .add_int("age", 8); if (!req.request(nullptr, 0)) { printf("request error!rn"); return 1; } acl::string body; if (!req.get_body(body)) { printf("get_body error!rn"); return 1; } printf("%srn", body.c_str()); return 0; }

最后

以上就是害怕早晨最近收集整理的关于C/C++编程:libfiber例子学习channel_cpphttp的全部内容,更多相关C/C++编程内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(86)

评论列表共有 0 条评论

立即
投稿
返回
顶部