acl/lib_acl/samples/event/main.c

178 lines
4.1 KiB
C
Raw Normal View History

2020-08-11 17:21:04 +08:00
#include <stdio.h>
#include <stdlib.h>
#include "lib_acl.h"
2014-11-19 00:25:21 +08:00
typedef struct {
ACL_VSTREAM *in;
ACL_VSTRING *buf;
ACL_EVENT *event;
} STREAM_IN;
2020-08-11 17:21:04 +08:00
static void trigger_event2(int event_type acl_unused,
2014-11-19 00:25:21 +08:00
ACL_EVENT *event acl_unused, void *context acl_unused)
{
2020-08-11 17:21:04 +08:00
printf("%s: please input: \r\n", __FUNCTION__);
}
2014-11-19 00:25:21 +08:00
2020-08-11 17:21:04 +08:00
static void trigger_event1(int event_type acl_unused,
ACL_EVENT *event acl_unused, void *context acl_unused)
{
printf("%s: please input: \r\n", __FUNCTION__);
acl_event_cancel_timer(event, trigger_event2, NULL);
2014-11-19 00:25:21 +08:00
}
static int __stop_event = 0;
static STREAM_IN *create_stream(ACL_EVENT *eventp)
{
STREAM_IN *in = (STREAM_IN*) acl_mymalloc(sizeof(STREAM_IN));
2020-08-11 17:21:04 +08:00
/* <20>ñ<EFBFBD>׼<EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
in->in = acl_vstream_fhopen(fileno(stdin), 0);
in->buf = acl_vstring_alloc(256);
2014-11-19 00:25:21 +08:00
in->event = eventp;
return in;
}
static void free_stream(STREAM_IN *in)
{
acl_vstream_free(in->in);
acl_vstring_free(in->buf);
acl_myfree(in);
}
static void read_callback(int event_type acl_unused, ACL_EVENT *event,
ACL_VSTREAM *stream, void *context)
{
STREAM_IN *in = (STREAM_IN *) context;
2020-08-11 17:21:04 +08:00
int ready, ret;
2014-11-19 00:25:21 +08:00
acl_assert(in->in == stream);
acl_assert(in->event == event);
/* <20><><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>ȡһ<C8A1><D2BB><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>ȥ<EFBFBD><C8A5>β<EFBFBD><CEB2><EFBFBD>Ļس<C4BB><D8B3><EFBFBD><EFBFBD>з<EFBFBD> */
2020-08-11 17:21:04 +08:00
ret = acl_vstream_gets_nonl_peek(stream, in->buf, &ready);
if (ret == ACL_VSTREAM_EOF) {
2014-11-19 00:25:21 +08:00
acl_event_disable_read(event, stream);
printf(">>>>gets error\r\n");
free_stream(in);
__stop_event = 1;
} else if (ready) {
#define STR acl_vstring_str
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD>ʾ֮ */
2014-11-19 00:25:21 +08:00
printf(">>>>gets one line: %s\r\n", STR(in->buf));
if (strcasecmp(STR(in->buf), "QUIT") == 0) {
__stop_event = 1;
free_stream(in);
} else {
ACL_VSTRING_RESET(in->buf); /* <20><><EFBFBD>ջ<EFBFBD><D5BB><EFBFBD><EFBFBD><EFBFBD> */
2014-11-19 00:25:21 +08:00
}
}
}
static void usage(const char *procname)
{
printf("usage: %s -h [help]\r\n"
" -M [printf time meter]\r\n"
" -t select|poll|kernel [default: select]\r\n"
" -s delay_sec [defaut: 1]\r\n"
" -u delay_usec [default: 0]\r\n"
" -m timer_delay [default: 100 microsecond, disable timer if < 0]\r\n"
" -k [if timer keep on, default: 1]\r\n", procname);
}
int main(int argc acl_unused, char *argv[] acl_unused)
{
ACL_EVENT *eventp;
STREAM_IN *in;
char event_type[64];
int ch, delay_sec = 1, delay_usec = 0;
int timer_delay = 100, timer_keep = 0;
int meter = 0;
/* <20><>ʼ<EFBFBD><CABC> acl <20><> */
acl_lib_init();
2014-11-19 00:25:21 +08:00
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׼<EFBFBD><D7BC><EFBFBD><EFBFBD> */
2014-11-19 00:25:21 +08:00
acl_msg_stdout_enable(1);
event_type[0] = 0;
while ((ch = getopt(argc, argv, "ht:s:u:m:kM")) > 0) {
switch (ch) {
case 'h':
usage(argv[0]);
return 0;
case 't':
ACL_SAFE_STRNCPY(event_type, optarg, sizeof(event_type));
break;
case 's':
delay_sec = atoi(optarg);
2020-08-11 17:21:04 +08:00
if (delay_sec < 0) {
2014-11-19 00:25:21 +08:00
delay_sec = 0;
2020-08-11 17:21:04 +08:00
}
2014-11-19 00:25:21 +08:00
break;
case 'u':
delay_usec = atoi(optarg);
2020-08-11 17:21:04 +08:00
if (delay_usec < 0) {
2014-11-19 00:25:21 +08:00
delay_usec = 0;
2020-08-11 17:21:04 +08:00
}
2014-11-19 00:25:21 +08:00
break;
case 'm':
timer_delay = atoi(optarg);
break;
case 'k':
timer_keep = 1;
break;
case 'M':
meter = 1;
break;
default:
break;
}
}
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD> */
2020-08-11 17:21:04 +08:00
if (strcasecmp(event_type, "kernel") == 0) {
2014-11-19 00:25:21 +08:00
eventp = acl_event_new_kernel(delay_sec, delay_usec);
2020-08-11 17:21:04 +08:00
} else if (strcasecmp(event_type, "poll") == 0) {
2014-11-19 00:25:21 +08:00
eventp = acl_event_new_poll(delay_sec, delay_usec);
2020-08-11 17:21:04 +08:00
} else {
2014-11-19 00:25:21 +08:00
eventp = acl_event_new_select(delay_sec, delay_usec);
2020-08-11 17:21:04 +08:00
}
2014-11-19 00:25:21 +08:00
2020-08-11 17:21:04 +08:00
if (timer_delay >= 0) {
acl_event_request_timer(eventp, trigger_event1, NULL,
2014-11-19 00:25:21 +08:00
timer_delay, timer_keep);
2020-08-11 17:21:04 +08:00
acl_event_request_timer(eventp, trigger_event2, NULL,
timer_delay, timer_keep);
}
2014-11-19 00:25:21 +08:00
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
2014-11-19 00:25:21 +08:00
in = create_stream(eventp);
/* <20><><EFBFBD><EFBFBD>׼<EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>첽 IO <20><><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD> */
2014-11-19 00:25:21 +08:00
acl_event_enable_read(eventp, in->in, 0, read_callback, in);
printf("begin wait input from standard in\r\n");
/* <20><><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD> */
2014-11-19 00:25:21 +08:00
while (!__stop_event) {
2020-08-11 17:21:04 +08:00
if (meter) {
2014-11-19 00:25:21 +08:00
ACL_METER_TIME("begin event");
2020-08-11 17:21:04 +08:00
}
2014-11-19 00:25:21 +08:00
acl_event_loop(eventp);
2020-08-11 17:21:04 +08:00
if (meter) {
2014-11-19 00:25:21 +08:00
ACL_METER_TIME("end event");
2020-08-11 17:21:04 +08:00
}
2014-11-19 00:25:21 +08:00
}
/* <20>ͷ<EFBFBD><CDB7>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD> */
2014-11-19 00:25:21 +08:00
acl_event_free(eventp);
printf("event stopped!\r\n");
2020-08-11 17:21:04 +08:00
return 0;
2014-11-19 00:25:21 +08:00
}