概述
channel_cpp
#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
#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
#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++编程:libfiber例子学习channel_cpphttp所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复