HevStun: Reuse task in UDP mode.

This commit is contained in:
hev
2022-10-21 19:26:49 +08:00
parent 5a8605d10b
commit e20081f145

View File

@@ -70,8 +70,7 @@ struct _StunMappedAddr
};
static HevTask *task;
static int sfd = -1;
static int bport;
static int once;
static int
cmp_addr (int family, unsigned int *maddr, unsigned short mport,
@@ -251,67 +250,59 @@ stun_bind (int fd, int mode, int bport)
static void
task_entry (void *data)
{
int fd = (intptr_t)data;
const char *iface;
const char *stun;
int bport;
int mode;
int res;
int tfd;
int fd;
tfd = (intptr_t)data;
mode = hev_conf_mode ();
stun = hev_conf_stun ();
iface = hev_conf_iface ();
if (sfd < 0) {
const char *iface;
const char *stun;
stun = hev_conf_stun ();
iface = hev_conf_iface ();
sfd = hev_sock_client_stun (fd, mode, stun, "3478", iface, &bport);
if (sfd < 0) {
LOG (E);
hev_xnsk_kill ();
task = NULL;
return;
}
} else {
hev_task_add_fd (task, sfd, POLLIN | POLLOUT);
}
if (fd >= 0) {
close (fd);
}
res = stun_bind (sfd, mode, bport);
if (res < 0) {
fd = hev_sock_client_stun (tfd, mode, stun, "3478", iface, &bport);
close (tfd);
if (fd < 0) {
LOG (E);
close (sfd);
hev_xnsk_kill ();
task = NULL;
return;
}
if (mode == SOCK_STREAM) {
close (sfd);
sfd = -1;
int res = stun_bind (fd, mode, bport);
if (res < 0) {
LOG (E);
close (fd);
hev_xnsk_kill ();
task = NULL;
return;
}
} else {
hev_task_del_fd (task, sfd);
for (;;) {
stun_bind (fd, mode, bport);
for (once = 0; !once;) {
hev_task_yield (HEV_TASK_WAITIO);
}
}
}
close (fd);
task = NULL;
}
void
hev_stun_run (int fd)
{
if (!task) {
if (fd >= 0) {
if (sfd >= 0) {
close (sfd);
sfd = -1;
}
fd = hev_task_io_dup (fd);
}
task = hev_task_new (-1);
hev_task_run (task, task_entry, (void *)(intptr_t)fd);
if (task) {
once = 1;
hev_task_wakeup (task);
return;
}
task = hev_task_new (-1);
fd = hev_task_io_dup (fd);
hev_task_run (task, task_entry, (void *)(intptr_t)fd);
}