Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
kehengzhong authored Jul 19, 2021
1 parent 776a355 commit c1f9798
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 39 deletions.
6 changes: 6 additions & 0 deletions src/epcore.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ void * epcore_new (int maxfd, int dispmode)
time(&pcore->startup_time);
pcore->quit = 0;

#ifdef HAVE_EVENTFD
pcore->wakeupfd = -1;
#else
pcore->informport = 0;
pcore->informfd = INVALID_SOCKET;
pcore->wakeupfd = INVALID_SOCKET;
#endif
pcore->wakeupdev = NULL;

/* initialize memory pool resource */
Expand Down
44 changes: 40 additions & 4 deletions src/epdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int hostn_to_dot_format (void * name, int len, void * pdst, int dstlen)
{
uint8 * hostn = (uint8 * )name;
uint8 * dst = (uint8 *)pdst;
uint32 labellen = 0;
int labellen = 0;
int i, j;

if (!hostn) return -1;
Expand Down Expand Up @@ -219,19 +219,27 @@ int dns_nsrv_load (void * vmgmt, char * nsip, char * resolv_file)
{
DnsMgmt * mgmt = (DnsMgmt *)vmgmt;
DnsHost * host = NULL;
#ifdef UNIX
FILE * fp = NULL;
char buf[1024];
char * p = NULL;
int len = 0;

#endif
#ifdef _WIN32
FIXED_INFO fi;
ULONG ulen = sizeof(fi);
IP_ADDR_STRING * paddr = NULL;
#endif

if (!mgmt) return -1;

if (nsip) {
host = dns_host_new(NULL, nsip, 0);
if (host)
dns_nsrv_add(mgmt->nsrv, host);
}


#ifdef UNIX
if (!resolv_file)
resolv_file = "/etc/resolv.conf";

Expand All @@ -256,6 +264,33 @@ int dns_nsrv_load (void * vmgmt, char * nsip, char * resolv_file)
}

fclose(fp);
#endif

#ifdef _WIN32
/* retrieves network parameters for the local computer */
if (GetNetworkParams(&fi, &ulen) != ERROR_SUCCESS) {
return -100;
}

if (fi.DomainName && strlen(fi.DomainName) > 0) {
host = dns_host_new(NULL, fi.DomainName, 0);
if (host) dns_nsrv_add(mgmt->nsrv, host);
}

if (fi.DnsServerList.IpAddress.String && strlen(fi.DnsServerList.IpAddress.String) > 0) {
host = dns_host_new(NULL, fi.DnsServerList.IpAddress.String, 0);
if (host) dns_nsrv_add(mgmt->nsrv, host);
}

paddr = fi.DnsServerList.Next;
while(paddr != NULL) {
if (paddr->IpAddress.String && strlen(paddr->IpAddress.String) > 0) {
host = dns_host_new(NULL, paddr->IpAddress.String, 0);
if (host) dns_nsrv_add(mgmt->nsrv, host);
}
paddr = paddr->Next;
}
#endif

/*if (dns_nsrv_num(mgmt->nsrv) <= 1) {
host = dns_host_new(NULL, "8.8.8.8", 0);
Expand Down Expand Up @@ -1902,6 +1937,7 @@ int dns_recv (void * vmgmt, void * pobj)

frm = frame_new(toread);

socklen = sizeof(sock);
memset(&sock, 0, sizeof(sock));
ret = recvfrom(iodev_fd(pdev), frameP(frm), toread, 0,
(struct sockaddr *)&sock, (socklen_t *)&socklen);
Expand Down
4 changes: 2 additions & 2 deletions src/epepoll.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003-2020 Ke Hengzhong <[email protected]>
* Copyright (c) 2003-2021 Ke Hengzhong <[email protected]>
* All rights reserved. See MIT LICENSE for redistribution.
*/

Expand Down Expand Up @@ -176,7 +176,7 @@ int epump_epoll_dispatch (void * veps, btime_t * delay)
int len;
int ret = 0;
int sockerr = 0;
struct sockaddr sock;
ep_sockaddr_t sock;

if (!epump) return -1;

Expand Down
6 changes: 5 additions & 1 deletion src/epselect.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ int epump_select_dispatch (void * veps, btime_t * delay)
int addrlen;
rbtnode_t * rbt = NULL;
struct timeval * waitout, timeout;
struct sockaddr sock;
ep_sockaddr_t sock;

if (!epump) return -1;

Expand Down Expand Up @@ -183,6 +183,8 @@ int epump_select_dispatch (void * veps, btime_t * delay)
if (pdev->rwflag & RWF_READ && FD_ISSET(pdev->fd, &rFds)) {
nfds--;
if (pdev->fdtype == FDT_LISTEN) {
epump_select_clearpoll(epump, pdev);

PushConnAcceptEvent(epump, pdev);

#ifdef HAVE_EVENTFD
Expand All @@ -193,6 +195,8 @@ int epump_select_dispatch (void * veps, btime_t * delay)
epcore_wakeup_recv(pcore);

} else {
epump_select_clearpoll(epump, pdev);

PushReadableEvent(epump, pdev);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/eptcp.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003-2020 Ke Hengzhong <[email protected]>
* Copyright (c) 2003-2021 Ke Hengzhong <[email protected]>
* All rights reserved. See MIT LICENSE for redistribution.
*/

Expand Down Expand Up @@ -126,8 +126,8 @@ void * eptcp_accept (void * vpcore, void * vld, void * para, int * retval,
iodev_t * pdev = NULL;
socklen_t addrlen;
SOCKET clifd;
struct sockaddr cliaddr;
struct sockaddr sock;
ep_sockaddr_t cliaddr;
ep_sockaddr_t sock;

if (retval) *retval = -1;
if (!pcore || !listendev) return NULL;
Expand Down
5 changes: 4 additions & 1 deletion src/epump.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ void epump_recycle (void * vepump)
if (!epump) return;

pcore = (epcore_t *)epump->epcore;
if (!pcore) return epump_free(epump);
if (!pcore) {
epump_free(epump);
return;
}

epump->quit = 1;
epump->threadid = 0;
Expand Down
6 changes: 5 additions & 1 deletion src/epwakeup.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ int epcore_wakeup_init (void * vpcore)
do {

pcore->informport += 1;
pcore->wakeupfd = udp_listen(localip, pcore->informport);
pcore->wakeupfd = udp_listen(localip, pcore->informport, NULL);

} while (pcore->wakeupfd == INVALID_SOCKET && times++ < 20000);

Expand Down Expand Up @@ -350,6 +350,8 @@ int epcore_wakeup_getmon (void * vpcore, void * veps)
if (!pcore) return -1;
if (!epump) return -2;

epump_iodev_add(epump, pcore->wakeupdev);

ev.data.ptr = pcore->wakeupdev;
ev.events = EPOLLIN;
if (epoll_ctl(epump->epoll_fd, EPOLL_CTL_ADD, pcore->wakeupfd, &ev) < 0) {
Expand All @@ -363,6 +365,8 @@ int epcore_wakeup_getmon (void * vpcore, void * veps)
if (!pcore) return -1;
if (!epump) return -2;

epump_iodev_add(epump, pcore->wakeupdev);

if (!FD_ISSET(pcore->wakeupfd, &epump->readFds)) {
FD_SET(pcore->wakeupfd, &epump->readFds);
}
Expand Down
82 changes: 56 additions & 26 deletions src/iodev.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,60 +341,97 @@ int iodev_rwflag_set(void * vpdev, uint8 rwflag)
}


int iodev_add_notify (void * vdev, uint8 rwflag)
int iodev_set_poll (void * vdev)
{
iodev_t * pdev = (iodev_t *)vdev;
epcore_t * pcore = NULL;
epump_t * epump = NULL;

if (!pdev) return -1;

if (pdev->fd == INVALID_SOCKET) {
iodev_close(pdev);
return -2;
}

if (pdev->bindtype == BIND_ALL_EPUMP) { //3
pcore = (epcore_t *)pdev->epcore;
if (pcore)
return epump_thread_setpoll(pcore, pdev);
} else {
epump = (epump_t *)pdev->epump;
if (epump)
return (*epump->setpoll)(epump, pdev);
}

return -100;
}

int iodev_clear_poll (void * vdev)
{
iodev_t * pdev = (iodev_t *)vdev;
epcore_t * pcore = NULL;
epump_t * epump = NULL;

if (!pdev) return -1;

if (pdev->fd == INVALID_SOCKET) {
iodev_close(pdev);
return -2;
}

if (pdev->bindtype == BIND_ALL_EPUMP) { //3
pcore = (epcore_t *)pdev->epcore;
if (pcore)
return epump_thread_delpoll(pcore, pdev);
} else {
epump = (epump_t *)pdev->epump;
if (epump)
return (*epump->delpoll)(epump, pdev);
}

return -100;
}

int iodev_add_notify (void * vdev, uint8 rwflag)
{
iodev_t * pdev = (iodev_t *)vdev;
uint8 tmpflag = 0;

if (!pdev) return -1;

if (pdev->fd == INVALID_SOCKET) {
iodev_close(pdev);
return 0;
}

if (rwflag == 0) return 0;

pcore = (epcore_t *)pdev->epcore;
if (!pcore) return -2;

EnterCriticalSection(&pdev->fdCS);
tmpflag = pdev->rwflag | rwflag;
if (pdev->rwflag != tmpflag) {
pdev->rwflag = tmpflag;
}
LeaveCriticalSection(&pdev->fdCS);

if (pdev->bindtype == BIND_ALL_EPUMP) { //3
epump_thread_setpoll(pcore, pdev);
} else {
epump = (epump_t *)pdev->epump;
if (epump) (*epump->setpoll)(epump, pdev);
}

return 0;
return iodev_set_poll(pdev);
}

int iodev_del_notify (void * vdev, uint8 rwflag)
{
iodev_t * pdev = (iodev_t *)vdev;
epcore_t * pcore = NULL;
epump_t * epump = NULL;
uint8 tmpflag = 0;
int setpoll = 0;

if (!pdev) return -1;

if (pdev->fd == INVALID_SOCKET) {
iodev_close(pdev);
return 0;
}

if (rwflag == 0) return 0;

pcore = (epcore_t *)pdev->epcore;
if (!pcore) return -2;


EnterCriticalSection(&pdev->fdCS);
tmpflag = pdev->rwflag & ~rwflag;
if (pdev->rwflag != tmpflag) {
Expand All @@ -405,14 +442,7 @@ int iodev_del_notify (void * vdev, uint8 rwflag)

if (!setpoll) return 0;

if (pdev->bindtype == BIND_ALL_EPUMP) { //3
epump_thread_setpoll(pcore, pdev);
} else {
epump = (epump_t *)pdev->epump;
if (epump) (*epump->setpoll)(epump, pdev);
}

return 0;
return iodev_set_poll(pdev);
}


Expand Down
25 changes: 24 additions & 1 deletion src/ioevent.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ int ioevent_push (void * vepump, int event, void * obj, void * cb, void * cbpara
EnterCriticalSection(&epump->epcore->eventnumCS);
epump->epcore->acc_event_num++;
LeaveCriticalSection(&epump->epcore->eventnumCS);

if (arr_num(epump->epcore->worker_list) <= 0)
return epump_ioevent_push(epump, ioe);

Expand Down Expand Up @@ -242,6 +242,10 @@ void * ioevent_execute (void * vpcore, void * vioe)
iotimer_t * piot = NULL;
GeneralCB * gcb = NULL;
IOHandler * iocb = NULL;
#if defined (HAVE_SELECT)
iodev_t * ptmp = NULL;
ulong curid = 0;
#endif

DnsMsg * dnsmsg = NULL;

Expand Down Expand Up @@ -281,11 +285,30 @@ void * ioevent_execute (void * vpcore, void * vioe)
ioe->type == IOE_ACCEPT)
pdev->iostate = IOS_READWRITE;

#if defined (HAVE_SELECT)
curid = pdev->id;
#endif

if (pdev->callback)
(*pdev->callback)(pdev->cbpara, pdev, ioe->type, pdev->fdtype);
else if (pcore->callback)
(*pcore->callback)(pcore->cbpara, pdev, ioe->type, pdev->fdtype);

#if defined (HAVE_SELECT)
/* pdev may be closed during the execution of callback */

ptmp = epcore_iodev_find(pcore, curid);

if (ioe->type == IOE_INVALID_DEV && ptmp) {
iodev_close(ptmp);
ptmp = NULL;

} else if (ptmp && ptmp->fd != INVALID_SOCKET) {
/* if underlying fd watching is epoll and ONESHOT mode,
READ notify should be added each time after callback */
iodev_set_poll(ptmp);
}
#endif
break;

case IOE_TIMEOUT:
Expand Down

0 comments on commit c1f9798

Please sign in to comment.