int top_publish(char *name, char *message, RpcEndpoint *ep) { int ret = 0; Topic *st; if (tshm_get(topicTable, name, (void **)&st)) { ret = 1; pthread_mutex_lock(&(st->lock)); if (ll_size(st->regAUs) > 0L) { Iterator *iter; iter = ll_it_create(st->regAUs); if (iter) { Event *event; event = ev_create(name, message, ep, ll_size(st->regAUs)); if (event) { unsigned long id; while (it_hasNext(iter)) { (void) it_next(iter, (void **)&id); au_publish(id, event); } } it_destroy(iter); } } pthread_mutex_unlock(&(st->lock)); } return ret; }
INT main(INT iArgc, CHAR *pcArgv[]) { ULONG ulRet = ERROR_SUCCESS; INT iRcvFd = INVALID_FD; INT iSndFd = INVALID_FD; EV_HANDLE hEv; g_pcProgramName = pcArgv[0]; /* ½âÎö²ÎÊý */ ulRet = parseParamter(iArgc, pcArgv); if (ERROR_SUCCESS != ulRet) { usage(); return 1; } hEv = ev_create(); if (EV_INVALID_HANDLE == hEv) { printf("Failed to create ev.\n"); return ERROR_FAILED; } iSndFd = openSndFd(); if (INVALID_FD == iSndFd) { printf("Failed to open send fd.\n"); ev_destroy(hEv); return 1; } iRcvFd = UDP_CreateServer(inet_addr("0.0.0.0"), g_usLocalPort); if (INVALID_FD == iRcvFd) { printf("Failed to open receive fd.\n"); close(iSndFd); ev_destroy(hEv); return 1; } printf("Open receive UDP fd at %s:%hu\n", "0.0.0.0", g_usLocalPort); ulRet = ev_addfd(hEv, iRcvFd, EV_READ, fdEvProcCB, (VOID *)(ULONG)iSndFd); if (ERROR_SUCCESS != ulRet) { close(iSndFd); close(iRcvFd); ev_destroy(hEv); return ERROR_FAILED; } EV_RUN(hEv, 0); close(iSndFd); close(iRcvFd); ev_destroy(hEv); return 0; }
int main(int argc, char *argv[]){ int sfd; sfd = tcpsock_create(); sock_setopt(sfd); sock_listen(sfd, SERVER_PORT, BACKLOG); fd_setnb(sfd); int evfd; evfd = ev_create(); ev_add(evfd, sfd, EV_IN); struct ev_event events[MAX_EVENT]; int n; int i; int cfd; unsigned char buf[BUF_SIZE]; ssize_t rbytes; while(1) { n = ev_wait(evfd, events, MAX_EVENT, -1); printf("%d\n", n); for(i=0; i < n; i++){ printf("%d, %d, %d, %d, %d\n", events[i].events, events[i].fd, events[i].readable, events[i].writable, events[i].closable); if (events[i].fd == sfd) { while(1) { cfd = sock_accept(sfd); if (cfd == -1) { break; } fd_setnb(cfd); ev_add(evfd, cfd, EV_IN); } } else if (events[i].closable == true) { close(events[i].fd); } else if (events[i].readable == true) { while(1) { rbytes = sock_recv(events[i].fd, buf, BUF_SIZE); if (rbytes == -1) { break; } buf[rbytes] = '\0'; printf("%s", buf); } } } } }