Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
慢慢CG
TDengine
提交
c46f6ac0
T
TDengine
项目概览
慢慢CG
/
TDengine
与 Fork 源项目一致
Fork自
taosdata / TDengine
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
c46f6ac0
编写于
1月 15, 2021
作者:
F
freemine
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add epoll.c tests
上级
a649f551
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
183 addition
and
0 deletion
+183
-0
tests/examples/c/epoll.c
tests/examples/c/epoll.c
+183
-0
未找到文件。
tests/examples/c/epoll.c
0 → 100644
浏览文件 @
c46f6ac0
#ifdef __APPLE__
#include "eok.h"
#else
#include <sys/epoll.h>
#endif
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <arpa/inet.h>
#include <libgen.h>
#define D(fmt, ...) fprintf(stderr, "%s[%d]%s(): " fmt "\n", basename(__FILE__), __LINE__, __func__, ##__VA_ARGS__)
#define A(statement, fmt, ...) do { \
if (statement) break; \
fprintf(stderr, "%s[%d]%s(): assert [%s] failed: %d[%s]: " fmt "\n", \
basename(__FILE__), __LINE__, __func__, \
#statement, errno, strerror(errno), \
##__VA_ARGS__); \
abort(); \
} while (0)
#define E(fmt, ...) do { \
fprintf(stderr, "%s[%d]%s(): %d[%s]: " fmt "\n", \
basename(__FILE__), __LINE__, __func__, \
errno, strerror(errno), \
##__VA_ARGS__); \
} while (0)
typedef
struct
ep_s
ep_t
;
struct
ep_s
{
int
ep
;
pthread_mutex_t
lock
;
int
sv
[
2
];
// 0 for read, 1 for write;
pthread_t
thread
;
volatile
unsigned
int
stopping
:
1
;
volatile
unsigned
int
waiting
:
1
;
volatile
unsigned
int
wakenup
:
1
;
};
static
int
ep_dummy
=
0
;
static
ep_t
*
ep_create
(
void
);
static
void
ep_destroy
(
ep_t
*
ep
);
static
void
*
routine
(
void
*
arg
);
static
int
open_connect
(
unsigned
short
port
);
int
main
(
int
argc
,
char
*
argv
[])
{
ep_t
*
ep
=
ep_create
();
A
(
ep
,
"failed"
);
int
skt
=
open_connect
(
6789
);
if
(
skt
!=-
1
)
{
struct
epoll_event
ev
=
{
0
};
ev
.
events
=
EPOLLIN
|
EPOLLERR
|
EPOLLHUP
|
EPOLLRDHUP
;
ev
.
data
.
ptr
=
&
skt
;
A
(
0
==
epoll_ctl
(
ep
->
ep
,
EPOLL_CTL_ADD
,
skt
,
&
ev
),
""
);
}
getchar
();
ep_destroy
(
ep
);
D
(
""
);
return
0
;
}
ep_t
*
ep_create
(
void
)
{
ep_t
*
ep
=
(
ep_t
*
)
calloc
(
1
,
sizeof
(
*
ep
));
A
(
ep
,
"out of memory"
);
A
(
-
1
!=
(
ep
->
ep
=
epoll_create
(
1
)),
""
);
ep
->
sv
[
0
]
=
-
1
;
ep
->
sv
[
1
]
=
-
1
;
A
(
0
==
socketpair
(
AF_LOCAL
,
SOCK_STREAM
,
0
,
ep
->
sv
),
""
);
A
(
0
==
pthread_mutex_init
(
&
ep
->
lock
,
NULL
),
""
);
A
(
0
==
pthread_mutex_lock
(
&
ep
->
lock
),
""
);
struct
epoll_event
ev
=
{
0
};
ev
.
events
=
EPOLLIN
;
ev
.
data
.
ptr
=
&
ep_dummy
;
A
(
0
==
epoll_ctl
(
ep
->
ep
,
EPOLL_CTL_ADD
,
ep
->
sv
[
0
],
&
ev
),
""
);
A
(
0
==
pthread_create
(
&
ep
->
thread
,
NULL
,
routine
,
ep
),
""
);
A
(
0
==
pthread_mutex_unlock
(
&
ep
->
lock
),
""
);
return
ep
;
}
static
void
ep_destroy
(
ep_t
*
ep
)
{
A
(
ep
,
"invalid argument"
);
ep
->
stopping
=
1
;
A
(
1
==
send
(
ep
->
sv
[
1
],
"1"
,
1
,
0
),
""
);
A
(
0
==
pthread_join
(
ep
->
thread
,
NULL
),
""
);
A
(
0
==
pthread_mutex_destroy
(
&
ep
->
lock
),
""
);
A
(
0
==
close
(
ep
->
sv
[
0
]),
""
);
A
(
0
==
close
(
ep
->
sv
[
1
]),
""
);
A
(
0
==
close
(
ep
->
ep
),
""
);
free
(
ep
);
}
static
void
*
routine
(
void
*
arg
)
{
A
(
arg
,
"invalid argument"
);
ep_t
*
ep
=
(
ep_t
*
)
arg
;
while
(
!
ep
->
stopping
)
{
struct
epoll_event
evs
[
10
]
=
{
0
};
A
(
0
==
pthread_mutex_lock
(
&
ep
->
lock
),
""
);
A
(
ep
->
waiting
==
0
,
"internal logic error"
);
ep
->
waiting
=
1
;
A
(
0
==
pthread_mutex_unlock
(
&
ep
->
lock
),
""
);
int
r
=
epoll_wait
(
ep
->
ep
,
evs
,
sizeof
(
evs
)
/
sizeof
(
evs
[
0
]),
-
1
);
A
(
r
>
0
,
"indefinite epoll_wait shall not timeout"
);
A
(
0
==
pthread_mutex_lock
(
&
ep
->
lock
),
""
);
A
(
ep
->
waiting
==
1
,
"internal logic error"
);
ep
->
waiting
=
0
;
A
(
0
==
pthread_mutex_unlock
(
&
ep
->
lock
),
""
);
for
(
int
i
=
0
;
i
<
r
;
++
i
)
{
struct
epoll_event
*
ev
=
evs
+
i
;
if
(
ev
->
data
.
ptr
==
&
ep_dummy
)
{
char
c
=
'\0'
;
A
(
1
==
recv
(
ep
->
sv
[
0
],
&
c
,
1
,
0
),
"internal logic error"
);
A
(
0
==
pthread_mutex_lock
(
&
ep
->
lock
),
""
);
ep
->
wakenup
=
0
;
A
(
0
==
pthread_mutex_unlock
(
&
ep
->
lock
),
""
);
D
(
"........"
);
continue
;
}
A
(
ev
->
data
.
ptr
,
"internal logic error"
);
int
skt
=
*
(
int
*
)
ev
->
data
.
ptr
;
if
(
ev
->
events
&
EPOLLIN
)
{
char
buf
[
4
];
int
n
=
recv
(
skt
,
buf
,
sizeof
(
buf
)
-
1
,
0
);
A
(
n
>=
0
&&
n
<
sizeof
(
buf
),
"internal logic error"
);
buf
[
n
]
=
'\0'
;
fprintf
(
stderr
,
"events[%x]:%s
\n
"
,
ev
->
events
,
buf
);
}
if
(
ev
->
events
&
EPOLLRDHUP
)
{
A
(
0
==
epoll_ctl
(
ep
->
ep
,
EPOLL_CTL_DEL
,
skt
,
NULL
),
""
);
}
continue
;
}
}
return
NULL
;
}
static
int
open_connect
(
unsigned
short
port
)
{
int
r
=
0
;
int
skt
=
socket
(
AF_INET
,
SOCK_STREAM
,
IPPROTO_TCP
);
if
(
skt
==-
1
)
{
E
(
"socket() failed"
);
return
-
1
;
}
do
{
struct
sockaddr_in
si
=
{
0
};
si
.
sin_family
=
AF_INET
;
si
.
sin_addr
.
s_addr
=
inet_addr
(
"127.0.0.1"
);
si
.
sin_port
=
htons
(
port
);
r
=
connect
(
skt
,
(
struct
sockaddr
*
)
&
si
,
sizeof
(
si
));
if
(
r
)
{
E
(
"connect(%u) failed"
,
port
);
break
;
}
memset
(
&
si
,
0
,
sizeof
(
si
));
socklen_t
len
=
sizeof
(
si
);
r
=
getsockname
(
skt
,
(
struct
sockaddr
*
)
&
si
,
&
len
);
if
(
r
)
{
E
(
"getsockname() failed"
);
}
A
(
len
==
sizeof
(
si
),
"internal logic error"
);
D
(
"connected: %d"
,
ntohs
(
si
.
sin_port
));
return
skt
;
}
while
(
0
);
close
(
skt
);
return
-
1
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录