-
Notifications
You must be signed in to change notification settings - Fork 1
/
poll_tool.cpp
57 lines (52 loc) · 1.41 KB
/
poll_tool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// Created by caesar on 2020/1/13.
//
#include "poll_tool.h"
#include <logger.h>
#ifdef __FILENAME__
const char *poll_tool::TAG = __FILENAME__;
#else
const char *poll_tool::TAG = "poll_tool";
#endif
poll_tool::poll_tool(int *fd) {
this->fd = fd;
}
long int poll_tool::check_read_count(int timeout) {
return check_read_count(*fd, timeout);
}
/**
* 检查是否有数据
* @return
*/
long int poll_tool::check_read_count(int fd, int timeout) {
if (fd <= 0) {
logger::instance()->e(TAG, __LINE__, "fd is error");
return -1;
}
struct pollfd client{};
client.fd = fd;
client.revents = 0;
#ifdef WIN32
client.events = POLLIN;
int poll_ret = WSAPoll(&client, 1, timeout);
#else
// client.events = POLLIN | POLLPRI | POLLRDNORM;
client.events = POLLIN;
int poll_ret = poll(&client, 1, timeout);
#endif
if (poll_ret < 0) {
if (errno != EINTR) {
logger::instance()->e(TAG, __LINE__, "poll is not EINTR ;errno is %d ", errno);
return poll_ret;
}
logger::instance()->w(TAG, __LINE__, "poll is EINTR");
return poll_ret;
} else if (poll_ret == 0) {
// logger::instance()->d(TAG, __LINE__, "poll is time out");
return poll_ret;
} else if (poll_ret > 0) {
// logger::instance()->d(TAG, __LINE__, "%s POLLIN is %d", __FUNCTION__, client.fd);
return poll_ret;
}
return 1;
}