-
Notifications
You must be signed in to change notification settings - Fork 0
/
libevent.cpp
executable file
·148 lines (125 loc) · 2.67 KB
/
libevent.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "libevent.h"
libevent::libevent()
{
listenfd = -1;
ser_sockfd = -1;
cli_sockfd = -1;
epfd = epoll_create(3);
assert(-1 != epfd);
}
libevent::~libevent()
{
close(listenfd);
close(epfd);
}
void libevent::socket_listen(char * ip, char * port)
{
listenfd = socket(AF_INET, SOCK_STREAM, 0);
assert(-1 != listenfd);
ifreq ifr;
strcpy(ifr.ifr_name, "eth0");
ioctl(listenfd, SIOCGIFADDR, &ifr);
struct sockaddr_in ser;
memset(&ser, 0, sizeof(ser));
ser.sin_family = AF_INET;
if( NULL == port )
{
ser.sin_port = 0;
}
else
{
ser.sin_port = htons(atoi(port));
}
if( NULL == ip )
{
ser.sin_addr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
}
else
{
ser.sin_addr.s_addr = inet_addr(ip);
}
int res = bind(listenfd, (struct sockaddr*)&ser, sizeof(ser));
assert(-1 != res);
struct sockaddr_in tcp_addr;
unsigned len = sizeof(tcp_addr);
getsockname(listenfd, (struct sockaddr*)&tcp_addr, &len);
std::cout << "IP = " << inet_ntoa(tcp_addr.sin_addr) << std::endl;
std::cout << "PORT= " << ntohs(tcp_addr.sin_port)<<std::endl;
listen(listenfd, 5);
struct epoll_event event;
event.events = EPOLLIN;
event.data.fd = listenfd;
epoll_ctl(epfd, EPOLL_CTL_ADD, listenfd, &event);
}
void libevent::sock_connect(char * ip, char * port)
{
ser_sockfd = socket(AF_INET, SOCK_STREAM, 0);
assert(-1 != ser_sockfd);
ifreq ifr;
strcpy(ifr.ifr_name, "eth0");
ioctl(ser_sockfd, SIOCGIFADDR, &ifr);
struct sockaddr_in ser;
memset(&ser, 0, sizeof(ser));
ser.sin_family = AF_INET;
if( NULL == port )
{
ser.sin_port = 0;
}
else
{
ser.sin_port = htons(atoi(port));
}
if( NULL == ip )
{
ser.sin_addr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
}
else
{
ser.sin_addr.s_addr = inet_addr(ip);
}
int res = connect(ser_sockfd, (struct sockaddr*)&ser, sizeof(ser));
printf("%m \n",errno);
assert(-1 != res);
}
void libevent::socket_run()
{
while (1)
{
struct epoll_event events[100];
int n = epoll_wait(epfd, events, 100, -1);
if (n <= 0)
{
exit(0);
}
for(int i = 0; i < n; i++)
{
int fd = events[i].data.fd;
if( fd == listenfd )
{
_connect_callback( fd );
}
else if(events[i].events & EPOLLRDHUP )
{
_disconect(fd);
}
else
{
_readable(fd);
}
}
}
}
void libevent::add_event(epoll_event* event, int fd)
{
epoll_ctl(epfd, EPOLL_CTL_ADD, fd, event );
}
void libevent::change_endian(char* str, int len)
{
char* ptem = new char[len];
memcpy(ptem, str, len);
for(int i = 0; i < len; i++)
{
str[i] = ptem[len - 1 - i];
}
delete[] ptem;
}