mirror of
https://github.com/Kiritow/wg-ops.git
synced 2024-03-22 13:11:37 +08:00
Use epoll ET for better performance
This commit is contained in:
parent
e77afc3264
commit
6c46cab599
59
w2u.c
59
w2u.c
|
@ -77,11 +77,11 @@ int main(int argc, char* argv[])
|
||||||
ep = epoll_create(1024);
|
ep = epoll_create(1024);
|
||||||
|
|
||||||
struct epoll_event ev1;
|
struct epoll_event ev1;
|
||||||
ev1.events = EPOLLIN;
|
ev1.events = EPOLLIN | EPOLLET;
|
||||||
ev1.data.fd = listener;
|
ev1.data.fd = listener;
|
||||||
|
|
||||||
struct epoll_event ev2;
|
struct epoll_event ev2;
|
||||||
ev2.events = EPOLLIN;
|
ev2.events = EPOLLIN | EPOLLET;
|
||||||
ev2.data.fd = sender;
|
ev2.data.fd = sender;
|
||||||
|
|
||||||
epoll_ctl(ep, EPOLL_CTL_ADD, listener, &ev1);
|
epoll_ctl(ep, EPOLL_CTL_ADD, listener, &ev1);
|
||||||
|
@ -89,9 +89,27 @@ int main(int argc, char* argv[])
|
||||||
|
|
||||||
struct epoll_event events[1024];
|
struct epoll_event events[1024];
|
||||||
|
|
||||||
|
struct sockaddr_in* addrTargets = (struct sockaddr_in*)malloc(sizeof(struct sockaddr_in) * PORT_UR_SIZE);
|
||||||
|
memset(addrTargets, 0, sizeof(struct sockaddr_in) * PORT_UR_SIZE);
|
||||||
|
for(int i=0; i<PORT_UR_SIZE; i++) {
|
||||||
|
addrTargets[i].sin_family = AF_INET;
|
||||||
|
inet_pton(AF_INET, "127.0.0.1", &addrTargets[i].sin_addr);
|
||||||
|
addrTargets[i].sin_port = htons(PORT_UR_BEGIN + i);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct sockaddr_in addrWg;
|
||||||
|
memset(&addrWg, 0, sizeof(addrWg));
|
||||||
|
addrWg.sin_family = AF_INET;
|
||||||
|
inet_pton(AF_INET, "127.0.0.1", &addrWg.sin_addr);
|
||||||
|
addrWg.sin_port = htons(PORT_WG);
|
||||||
|
|
||||||
|
int rrOffset = 0;
|
||||||
|
|
||||||
|
char buffer[4096];
|
||||||
|
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
int nfds = epoll_wait(ep, events, 1024, -1);
|
int nfds = epoll_wait(ep, events, sizeof(events), -1);
|
||||||
|
|
||||||
if (nfds < 0) {
|
if (nfds < 0) {
|
||||||
perror("epoll_wait");
|
perror("epoll_wait");
|
||||||
|
@ -102,42 +120,25 @@ int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
if (events[i].data.fd == listener)
|
if (events[i].data.fd == listener)
|
||||||
{
|
{
|
||||||
struct sockaddr_in addr;
|
int nsize = recvfrom(listener, buffer, sizeof(buffer), 0, NULL, NULL);
|
||||||
socklen_t alen = sizeof(addr);
|
|
||||||
|
|
||||||
char buffer[2048];
|
|
||||||
int nsize = recvfrom(listener, &buffer, 2048, 0, (struct sockaddr*)&addr, &alen);
|
|
||||||
|
|
||||||
if (nsize < 0) {
|
if (nsize < 0) {
|
||||||
perror("recvfrom");
|
perror("recvfrom");
|
||||||
} else {
|
} else {
|
||||||
memset(&addr, 0, sizeof(addr));
|
sendto(sender, buffer, nsize, 0, (const struct sockaddr*)(addrTargets + rrOffset), sizeof(struct sockaddr_in));
|
||||||
addr.sin_family = AF_INET;
|
if (++rrOffset >= PORT_UR_SIZE) {
|
||||||
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
|
rrOffset = 0;
|
||||||
int randPort = PORT_UR_BEGIN + rand()%PORT_UR_SIZE;
|
}
|
||||||
addr.sin_port = htons(randPort);
|
|
||||||
sendto(sender, buffer, nsize, 0, (const struct sockaddr*)&addr, sizeof(addr));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (events[i].data.fd == sender)
|
else if (events[i].data.fd == sender)
|
||||||
{
|
{
|
||||||
struct sockaddr_in addr;
|
int nsize = recvfrom(sender, buffer, sizeof(buffer), 0, NULL, NULL);
|
||||||
socklen_t alen = sizeof(addr);
|
|
||||||
|
|
||||||
char buffer[2048];
|
if (nsize < 0) {
|
||||||
int nsize = recvfrom(sender, &buffer, 2048, 0, (struct sockaddr*)&addr, &alen);
|
|
||||||
|
|
||||||
if (nsize < 0)
|
|
||||||
{
|
|
||||||
perror("recvfrom");
|
perror("recvfrom");
|
||||||
}
|
} else {
|
||||||
else
|
sendto(listener, buffer, nsize, 0, (const struct sockaddr*)&addrWg, sizeof(struct sockaddr_in));
|
||||||
{
|
|
||||||
memset(&addr, 0, sizeof(addr));
|
|
||||||
addr.sin_family = AF_INET;
|
|
||||||
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
|
|
||||||
addr.sin_port = htons(PORT_WG);
|
|
||||||
sendto(listener, buffer, nsize, 0, (const struct sockaddr*)&addr, sizeof(addr));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user