2022-04-11 18:28:41 +08:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
class myfiber : public acl::fiber {
|
|
|
|
public:
|
|
|
|
myfiber(const char* addr, acl::fiber_sem* sem,
|
|
|
|
acl::fiber_tbox<bool>* box)
|
|
|
|
: addr_(addr), sem_(sem), box_(box)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~myfiber(void) {}
|
|
|
|
|
|
|
|
void wait(void) {
|
|
|
|
if (sem_) {
|
|
|
|
printf("%s: wait sem\r\n", __FUNCTION__);
|
|
|
|
sem_->wait();
|
|
|
|
printf("%s: wait sem ok\r\n", __FUNCTION__);
|
|
|
|
} else if (box_) {
|
|
|
|
printf("%s: wait box\r\n", __FUNCTION__);
|
|
|
|
box_->pop();
|
|
|
|
printf("%s: wait box ok\r\n", __FUNCTION__);
|
|
|
|
} else {
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// @override
|
|
|
|
void run(void) {
|
|
|
|
acl::socket_stream conn;
|
|
|
|
|
|
|
|
printf("%s: begin to connect %s\r\n", __FUNCTION__, addr_.c_str());
|
2022-04-11 19:09:39 +08:00
|
|
|
|
|
|
|
printf("\r\n");
|
2022-04-11 18:28:41 +08:00
|
|
|
if (conn.open(addr_, 0, 0)) {
|
2022-04-11 19:09:39 +08:00
|
|
|
printf(">>> %s: connect %s ok\r\n", __FUNCTION__, addr_.c_str());
|
2022-04-11 18:28:41 +08:00
|
|
|
} else {
|
2022-04-11 19:09:39 +08:00
|
|
|
printf(">>> %s: connect %s error %s\r\n", __FUNCTION__,
|
2022-04-11 18:28:41 +08:00
|
|
|
addr_.c_str(), acl::last_serror());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sem_) {
|
|
|
|
sem_->post();
|
|
|
|
} else if (box_) {
|
|
|
|
box_->push(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
acl::string addr_;
|
|
|
|
acl::fiber_sem* sem_;
|
|
|
|
acl::fiber_tbox<bool>* box_;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void fiber_redis(const char* addr, bool use_sem)
|
|
|
|
{
|
|
|
|
acl::socket_stream conn;
|
|
|
|
|
2022-04-11 19:09:39 +08:00
|
|
|
printf("\r\n");
|
2022-04-11 18:28:41 +08:00
|
|
|
if (conn.open(addr, 0, 0)) {
|
2022-04-11 19:09:39 +08:00
|
|
|
printf(">>> %s: connect %s ok\r\n", __FUNCTION__, addr);
|
2022-04-11 18:28:41 +08:00
|
|
|
} else {
|
2022-04-11 19:09:39 +08:00
|
|
|
printf(">>> %s: connect %s error %s\r\n",
|
2022-04-11 18:28:41 +08:00
|
|
|
__FUNCTION__, addr, acl::last_serror());
|
|
|
|
}
|
|
|
|
|
|
|
|
acl::fiber_sem sem(0);
|
|
|
|
acl::fiber_tbox<bool> box;
|
|
|
|
|
|
|
|
myfiber* fb;
|
|
|
|
|
|
|
|
if (use_sem) {
|
|
|
|
fb = new myfiber(addr, &sem, NULL);
|
|
|
|
} else {
|
|
|
|
fb = new myfiber(addr, NULL, &box);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%s: begin create one fiber\r\n", __FUNCTION__);
|
|
|
|
fb->start();
|
|
|
|
|
|
|
|
printf("%s: begin to wait fiber\r\n", __FUNCTION__);
|
|
|
|
fb->wait();
|
|
|
|
|
|
|
|
printf("\r\n");
|
|
|
|
delete fb;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static void usage(const char* procname)
|
|
|
|
{
|
|
|
|
printf("usage: %s -h [help]\r\n"
|
|
|
|
" -s addr\r\n"
|
|
|
|
" -S [if use fiber_sem or use fiber_tbox]\r\n", procname);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
bool use_sem = false;
|
|
|
|
acl::string addr("127.0.0.1:6379");
|
|
|
|
|
|
|
|
acl::acl_cpp_init();
|
|
|
|
acl::log::stdout_open(true);
|
|
|
|
acl::fiber::stdout_open(true);
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "hs:S")) > 0) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'h':
|
|
|
|
usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
case 's':
|
|
|
|
addr = optarg;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
use_sem = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\r\n");
|
|
|
|
|
|
|
|
go[&] {
|
|
|
|
fiber_redis(addr, use_sem);
|
|
|
|
};
|
|
|
|
|
|
|
|
acl::fiber::schedule();
|
|
|
|
return 0;
|
|
|
|
}
|