mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-12-02 11:57:43 +08:00
add fiber test examples.
This commit is contained in:
parent
dd0bcc2c07
commit
c98290445b
2
lib_acl/samples/xml2/Makefile
Normal file
2
lib_acl/samples/xml2/Makefile
Normal file
@ -0,0 +1,2 @@
|
||||
include ../Makefile.in
|
||||
PROG = xml
|
112
lib_acl/samples/xml2/main.c
Normal file
112
lib_acl/samples/xml2/main.c
Normal file
@ -0,0 +1,112 @@
|
||||
#include "lib_acl.h"
|
||||
|
||||
static double stamp_sub(const struct timeval *from, const struct timeval *sub_by)
|
||||
{
|
||||
struct timeval res;
|
||||
|
||||
memcpy(&res, from, sizeof(struct timeval));
|
||||
|
||||
res.tv_usec -= sub_by->tv_usec;
|
||||
if (res.tv_usec < 0) {
|
||||
--res.tv_sec;
|
||||
res.tv_usec += 1000000;
|
||||
}
|
||||
res.tv_sec -= sub_by->tv_sec;
|
||||
|
||||
return (res.tv_sec * 1000.0 + res.tv_usec/1000.0);
|
||||
}
|
||||
|
||||
static void usage(const char* proc)
|
||||
{
|
||||
printf("usage: %s -h[help] -m[use_memslice] -c cache_count -f filepath\r\n", proc);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
char buf[8192], filepath[256];
|
||||
int ret, n;
|
||||
ACL_VSTREAM* fp;
|
||||
ACL_XML* xml;
|
||||
struct timeval begin, end;
|
||||
double spent;
|
||||
int ch, use_slice = 0, cache_count = 1000;
|
||||
|
||||
filepath[0] = 0;
|
||||
while ((ch = getopt(argc, argv, "hmc:f:")) > 0)
|
||||
{
|
||||
switch (ch)
|
||||
{
|
||||
case 'h':
|
||||
usage(argv[0]);
|
||||
return 0;
|
||||
case 'm':
|
||||
use_slice = 1;
|
||||
break;
|
||||
case 'c':
|
||||
cache_count = atoi(optarg);
|
||||
if (cache_count <= 0)
|
||||
cache_count = 1000;
|
||||
break;
|
||||
case 'f':
|
||||
snprintf(filepath, sizeof(filepath), "%s", optarg);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (use_slice)
|
||||
acl_mem_slice_init(8, 1024, 100000,
|
||||
ACL_SLICE_FLAG_GC2 |
|
||||
ACL_SLICE_FLAG_RTGC_OFF |
|
||||
ACL_SLICE_FLAG_LP64_ALIGN);
|
||||
|
||||
if (filepath[0] == 0)
|
||||
{
|
||||
usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
xml = acl_xml_alloc();
|
||||
if (cache_count > 0)
|
||||
acl_xml_cache(xml, cache_count);
|
||||
|
||||
fp = acl_vstream_fopen(filepath, O_RDONLY, 0600, 8192);
|
||||
if (fp == NULL)
|
||||
{
|
||||
printf("open file %s error %s\r\n",
|
||||
filepath, acl_last_serror());
|
||||
acl_xml_free(xml);
|
||||
return 1;
|
||||
}
|
||||
|
||||
gettimeofday(&begin, NULL);
|
||||
n = 0;
|
||||
ACL_METER_TIME("------begin------");
|
||||
while (1)
|
||||
{
|
||||
ret = acl_vstream_fgets(fp, buf, sizeof(buf) - 1);
|
||||
if (ret == ACL_VSTREAM_EOF)
|
||||
break;
|
||||
buf[ret] = 0;
|
||||
acl_xml_parse(xml, buf);
|
||||
if (++n % 10000 == 0)
|
||||
{
|
||||
printf("line: %d\r\n", n);
|
||||
ACL_METER_TIME("-------ok------");
|
||||
}
|
||||
if (n % cache_count == 0)
|
||||
{
|
||||
printf("reset xml, line: %d\r\n", n);
|
||||
acl_xml_reset(xml);
|
||||
}
|
||||
}
|
||||
|
||||
gettimeofday(&end, NULL);
|
||||
spent = stamp_sub(&end, &begin);
|
||||
printf("\r\ntotal spent: %0.2f ms\r\n", spent);
|
||||
|
||||
acl_xml_free(xml);
|
||||
acl_vstream_fclose(fp);
|
||||
return 0;
|
||||
}
|
@ -2,8 +2,8 @@
|
||||
本网络协程库的协程部分是基于 Russ Cox (golang 的协程作者) 在 2005 年实现的 libtask,libtask 实现了协程编程的基本原型,lib_fiber 一方面使协程编程接口更加简单易用(用户可以直接调用 acl_fiber_create 创建协程),另一方面 lib_fiber 实现了线程安全的协程库,通过给每个线程一个独立的协程调度器,从而方便用户使用多核,此外,lib_fiber 还增加了基于协程的信号量、协程局部变量等功能。
|
||||
本网络协程库的异步 IO 事件部分基于 redis 中的 event 模块改造而来,增加了延迟删除、句柄缓存等功能。
|
||||
|
||||
下面是一个简单使用网络协程库编写的一个简单的高并发服务器:
|
||||
|
||||
**示例一**
|
||||
下面是一个简单使用网络协程库编写的一个**简单的高并发服务器**:
|
||||
```c++
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -217,8 +217,210 @@ int main(int argc, char *argv[])
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
**示例二**
|
||||
上面的例子中因为使用了系统原生的网络 API,所以感觉代码有些臃肿,下面的例子使用 acl 库中提供的网络 API,显得更为简单些:
|
||||
```c++
|
||||
#include "stdafx.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
class fiber_client : public acl::fiber
|
||||
{
|
||||
public:
|
||||
fiber_client(acl::socket_stream* conn) : conn_(conn) {}
|
||||
|
||||
protected:
|
||||
// @override
|
||||
void run(void)
|
||||
{
|
||||
printf("fiber-%d-%d running\r\n", get_id(), acl::fiber::self());
|
||||
|
||||
char buf[8192];
|
||||
while (true)
|
||||
{
|
||||
int ret = conn_->read(buf, sizeof(buf), false);
|
||||
if (ret == -1)
|
||||
break;
|
||||
if (conn_->write(buf, ret) == -1)
|
||||
break;
|
||||
}
|
||||
|
||||
delete conn_;
|
||||
delete this;
|
||||
}
|
||||
|
||||
private:
|
||||
acl::socket_stream* conn_;
|
||||
|
||||
~fiber_client(void) {}
|
||||
};
|
||||
|
||||
class fiber_server : public acl::fiber
|
||||
{
|
||||
public:
|
||||
fiber_server(acl::server_socket& ss) : ss_(ss) {}
|
||||
~fiber_server(void) {}
|
||||
|
||||
protected:
|
||||
// @override
|
||||
void run(void)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
acl::socket_stream* conn = ss_.accept();
|
||||
if (conn == NULL)
|
||||
{
|
||||
printf("accept error %s\r\n", acl::last_serror());
|
||||
break;
|
||||
}
|
||||
|
||||
printf("accept ok, fd: %d\r\n", conn->sock_handle());
|
||||
// create one fiber for one connection
|
||||
fiber_client* fc = new fiber_client(conn);
|
||||
// start the fiber
|
||||
fc->start();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
acl::server_socket& ss_;
|
||||
};
|
||||
|
||||
static void usage(const char* procname)
|
||||
{
|
||||
printf("usage: %s -h [help] -s listen_addr\r\n", procname);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int ch;
|
||||
|
||||
acl::acl_cpp_init();
|
||||
acl::string addr("127.0.0.1:9006");
|
||||
acl::log::stdout_open(true);
|
||||
|
||||
while ((ch = getopt(argc, argv, "hs:")) > 0)
|
||||
{
|
||||
switch (ch)
|
||||
{
|
||||
case 'h':
|
||||
usage(argv[0]);
|
||||
return 0;
|
||||
case 's':
|
||||
addr = optarg;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
acl::server_socket ss;
|
||||
if (ss.open(addr) == false)
|
||||
{
|
||||
printf("listen %s error %s\r\n", addr.c_str(), acl::last_serror());
|
||||
return 1;
|
||||
}
|
||||
printf("listen %s ok\r\n", addr.c_str());
|
||||
|
||||
fiber_server fs(ss);
|
||||
fs.start(); // start listen fiber
|
||||
|
||||
acl::fiber::schedule(); // start fiber schedule
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
**示例三**
|
||||
如果使用C++11的特性,则示例二会更为简单,如下:
|
||||
```c++
|
||||
#include "stdafx.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static void fiber_client(acl::socket_stream* conn)
|
||||
{
|
||||
printf("fiber-%d running\r\n", acl::fiber::self());
|
||||
|
||||
char buf[8192];
|
||||
while (true)
|
||||
{
|
||||
int ret = conn->read(buf, sizeof(buf), false);
|
||||
if (ret == -1)
|
||||
break;
|
||||
if (conn->write(buf, ret) == -1)
|
||||
break;
|
||||
}
|
||||
|
||||
delete conn;
|
||||
}
|
||||
|
||||
static void fiber_server(acl::server_socket& ss)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
acl::socket_stream* conn = ss.accept();
|
||||
if (conn == NULL)
|
||||
{
|
||||
printf("accept error %s\r\n", acl::last_serror());
|
||||
break;
|
||||
}
|
||||
|
||||
printf("accept ok, fd: %d\r\n", conn->sock_handle());
|
||||
|
||||
go[=] {
|
||||
fiber_client(conn);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
static void usage(const char* procname)
|
||||
{
|
||||
printf("usage: %s -h [help] -s listen_addr\r\n", procname);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int ch;
|
||||
|
||||
acl::acl_cpp_init();
|
||||
acl::string addr("127.0.0.1:9006");
|
||||
acl::log::stdout_open(true);
|
||||
|
||||
while ((ch = getopt(argc, argv, "hs:")) > 0)
|
||||
{
|
||||
switch (ch)
|
||||
{
|
||||
case 'h':
|
||||
usage(argv[0]);
|
||||
return 0;
|
||||
case 's':
|
||||
addr = optarg;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
acl::server_socket ss;
|
||||
if (ss.open(addr) == false)
|
||||
{
|
||||
printf("listen %s error %s\r\n", addr.c_str(), acl::last_serror());
|
||||
return 1;
|
||||
}
|
||||
printf("listen %s ok\r\n", addr.c_str());
|
||||
|
||||
go[&] {
|
||||
fiber_server(ss);
|
||||
};
|
||||
|
||||
acl::fiber::schedule(); // start fiber schedule
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
参考:
|
||||
1、网络协程编程:http://zsxxsz.iteye.com/blog/2312043
|
||||
2、使用协程编写高并发网络服务:http://zsxxsz.iteye.com/blog/2309654
|
||||
3、使用协程方式编写高并发的WEB服务:http://zsxxsz.iteye.com/blog/2309665
|
||||
|
||||
- 网络协程编程:http://zsxxsz.iteye.com/blog/2312043
|
||||
- 用协程编写高并发网络服务:http://zsxxsz.iteye.com/blog/2309654
|
||||
- 使用协程方式编写高并发的WEB服务:http://zsxxsz.iteye.com/blog/2309665
|
||||
|
2
lib_fiber/samples/server3/Makefile
Normal file
2
lib_fiber/samples/server3/Makefile
Normal file
@ -0,0 +1,2 @@
|
||||
include ../Makefile_cpp.in
|
||||
PROG = server
|
110
lib_fiber/samples/server3/main.cpp
Normal file
110
lib_fiber/samples/server3/main.cpp
Normal file
@ -0,0 +1,110 @@
|
||||
#include "stdafx.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
class fiber_client : public acl::fiber
|
||||
{
|
||||
public:
|
||||
fiber_client(acl::socket_stream* conn) : conn_(conn) {}
|
||||
|
||||
protected:
|
||||
// @override
|
||||
void run(void)
|
||||
{
|
||||
printf("fiber-%d-%d running\r\n", get_id(), acl::fiber::self());
|
||||
|
||||
char buf[8192];
|
||||
while (true)
|
||||
{
|
||||
int ret = conn_->read(buf, sizeof(buf), false);
|
||||
if (ret == -1)
|
||||
break;
|
||||
if (conn_->write(buf, ret) == -1)
|
||||
break;
|
||||
}
|
||||
|
||||
delete conn_;
|
||||
delete this;
|
||||
}
|
||||
|
||||
private:
|
||||
acl::socket_stream* conn_;
|
||||
|
||||
~fiber_client(void) {}
|
||||
};
|
||||
|
||||
class fiber_server : public acl::fiber
|
||||
{
|
||||
public:
|
||||
fiber_server(acl::server_socket& ss) : ss_(ss) {}
|
||||
~fiber_server(void) {}
|
||||
|
||||
protected:
|
||||
// @override
|
||||
void run(void)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
acl::socket_stream* conn = ss_.accept();
|
||||
if (conn == NULL)
|
||||
{
|
||||
printf("accept error %s\r\n", acl::last_serror());
|
||||
break;
|
||||
}
|
||||
|
||||
printf("accept ok, fd: %d\r\n", conn->sock_handle());
|
||||
// create one fiber for one connection
|
||||
fiber_client* fc = new fiber_client(conn);
|
||||
// start the fiber
|
||||
fc->start();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
acl::server_socket& ss_;
|
||||
};
|
||||
|
||||
static void usage(const char* procname)
|
||||
{
|
||||
printf("usage: %s -h [help] -s listen_addr\r\n", procname);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int ch;
|
||||
|
||||
acl::acl_cpp_init();
|
||||
acl::string addr("127.0.0.1:9006");
|
||||
acl::log::stdout_open(true);
|
||||
|
||||
while ((ch = getopt(argc, argv, "hs:")) > 0)
|
||||
{
|
||||
switch (ch)
|
||||
{
|
||||
case 'h':
|
||||
usage(argv[0]);
|
||||
return 0;
|
||||
case 's':
|
||||
addr = optarg;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
acl::server_socket ss;
|
||||
if (ss.open(addr) == false)
|
||||
{
|
||||
printf("listen %s error %s\r\n", addr.c_str(), acl::last_serror());
|
||||
return 1;
|
||||
}
|
||||
printf("listen %s ok\r\n", addr.c_str());
|
||||
|
||||
fiber_server fs(ss);
|
||||
fs.start(); // start listen fiber
|
||||
|
||||
acl::fiber::schedule(); // start fiber schedule
|
||||
|
||||
return 0;
|
||||
}
|
1
lib_fiber/samples/server3/stdafx.cpp
Normal file
1
lib_fiber/samples/server3/stdafx.cpp
Normal file
@ -0,0 +1 @@
|
||||
#include "stdafx.h"
|
20
lib_fiber/samples/server3/stdafx.h
Normal file
20
lib_fiber/samples/server3/stdafx.h
Normal file
@ -0,0 +1,20 @@
|
||||
// stdafx.h : 标准系统包含文件的包含文件,
|
||||
// 或是常用但不常更改的项目特定的包含文件
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
//#include <iostream>
|
||||
//#include <tchar.h>
|
||||
|
||||
// TODO: 在此处引用程序要求的附加头文件
|
||||
|
||||
#include "lib_acl.h"
|
||||
#include "acl_cpp/lib_acl.hpp"
|
||||
#include "fiber/fiber.hpp"
|
||||
|
||||
#ifdef WIN32
|
||||
#define snprintf _snprintf
|
||||
#endif
|
||||
|
2
lib_fiber/samples/server4/Makefile
Normal file
2
lib_fiber/samples/server4/Makefile
Normal file
@ -0,0 +1,2 @@
|
||||
include ../Makefile_cpp.in
|
||||
PROG = server
|
84
lib_fiber/samples/server4/main.cpp
Normal file
84
lib_fiber/samples/server4/main.cpp
Normal file
@ -0,0 +1,84 @@
|
||||
#include "stdafx.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static void fiber_client(acl::socket_stream* conn)
|
||||
{
|
||||
printf("fiber-%d running\r\n", acl::fiber::self());
|
||||
|
||||
char buf[8192];
|
||||
while (true)
|
||||
{
|
||||
int ret = conn->read(buf, sizeof(buf), false);
|
||||
if (ret == -1)
|
||||
break;
|
||||
if (conn->write(buf, ret) == -1)
|
||||
break;
|
||||
}
|
||||
|
||||
delete conn;
|
||||
}
|
||||
|
||||
static void fiber_server(acl::server_socket& ss)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
acl::socket_stream* conn = ss.accept();
|
||||
if (conn == NULL)
|
||||
{
|
||||
printf("accept error %s\r\n", acl::last_serror());
|
||||
break;
|
||||
}
|
||||
|
||||
printf("accept ok, fd: %d\r\n", conn->sock_handle());
|
||||
|
||||
go[=] {
|
||||
fiber_client(conn);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
static void usage(const char* procname)
|
||||
{
|
||||
printf("usage: %s -h [help] -s listen_addr\r\n", procname);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int ch;
|
||||
|
||||
acl::acl_cpp_init();
|
||||
acl::string addr("127.0.0.1:9006");
|
||||
acl::log::stdout_open(true);
|
||||
|
||||
while ((ch = getopt(argc, argv, "hs:")) > 0)
|
||||
{
|
||||
switch (ch)
|
||||
{
|
||||
case 'h':
|
||||
usage(argv[0]);
|
||||
return 0;
|
||||
case 's':
|
||||
addr = optarg;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
acl::server_socket ss;
|
||||
if (ss.open(addr) == false)
|
||||
{
|
||||
printf("listen %s error %s\r\n", addr.c_str(), acl::last_serror());
|
||||
return 1;
|
||||
}
|
||||
printf("listen %s ok\r\n", addr.c_str());
|
||||
|
||||
go[&] {
|
||||
fiber_server(ss);
|
||||
};
|
||||
|
||||
acl::fiber::schedule(); // start fiber schedule
|
||||
|
||||
return 0;
|
||||
}
|
1
lib_fiber/samples/server4/stdafx.cpp
Normal file
1
lib_fiber/samples/server4/stdafx.cpp
Normal file
@ -0,0 +1 @@
|
||||
#include "stdafx.h"
|
20
lib_fiber/samples/server4/stdafx.h
Normal file
20
lib_fiber/samples/server4/stdafx.h
Normal file
@ -0,0 +1,20 @@
|
||||
// stdafx.h : 标准系统包含文件的包含文件,
|
||||
// 或是常用但不常更改的项目特定的包含文件
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
//#include <iostream>
|
||||
//#include <tchar.h>
|
||||
|
||||
// TODO: 在此处引用程序要求的附加头文件
|
||||
|
||||
#include "lib_acl.h"
|
||||
#include "acl_cpp/lib_acl.hpp"
|
||||
#include "fiber/fiber.hpp"
|
||||
|
||||
#ifdef WIN32
|
||||
#define snprintf _snprintf
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user