int main(int argc,char **argv){ kn_net_open(); kn_proactor_t p = kn_new_proactor(); kn_sockaddr local; kn_fd_t l; kn_addr_init_in(&local,argv[1],atoi(argv[2])); l = kn_dgram_listen(p,SOCK_DGRAM,&local,transfer_finish); struct dgram_session *session = calloc(1,sizeof(*session)); session->s = l; kn_fd_setud(l,session); dgram_session_recv(session); uint64_t tick,now; tick = now = kn_systemms64(); while(1){ kn_proactor_run(p,50); now = kn_systemms64(); if(now - tick > 1000) { uint32_t elapse = (uint32_t)(now-tick); totalbytes = (totalbytes/elapse)/1024; printf("totalbytes:%lldMB/s\n",totalbytes); tick = now; totalbytes = 0; } } return 0; }
int32_t kn_epoll_loop(kn_proactor_t p,int32_t ms) { uint64_t sleep_ms; uint64_t timeout = kn_systemms64() + (uint64_t)ms; uint64_t current_tick; int32_t nfds; int32_t i; uint64_t l_now; kn_dlist* actived; kn_fd_t s; kn_epoll* ep = (kn_epoll*)p; if(ms < 0) ms = 0; do{ if(!kn_dlist_empty(&p->connecting)){ l_now = kn_systemms64(); kn_dlist_check_remove(&p->connecting,check_connect_timeout,(void*)&l_now); } actived = kn_proactor_activelist(p); if(!kn_dlist_empty(actived)){ p->actived_index = (p->actived_index+1)%2; while((s = (kn_fd_t)kn_dlist_pop(actived)) != NULL){ if(s->process(s)) kn_procator_putin_active(p,s); } } current_tick = kn_systemms64(); actived = kn_proactor_activelist(p); if(kn_dlist_empty(actived)) sleep_ms = timeout > current_tick ? timeout - current_tick:0; else sleep_ms = 0; nfds = _epoll_wait(ep->epfd,ep->events,ep->eventsize,(uint32_t)sleep_ms); if(nfds < 0) return -1; for(i=0; i < nfds ; ++i) { s = (kn_fd_t)ep->events[i].data.ptr; s->on_active(s,ep->events[i].events); } current_tick = kn_systemms64(); }while(timeout > current_tick); return 0; }
int main(int argc,char **argv){ kn_net_open(); kn_proactor_t p = kn_new_proactor(); kn_sockaddr local; kn_addr_init_in(&local,argv[1],atoi(argv[2])); kn_listen(p,&local,on_accept,(void*)p); uint64_t tick,now; tick = now = kn_systemms64(); while(1){ kn_proactor_run(p,50); now = kn_systemms64(); if(now - tick > 1000) { uint32_t elapse = (uint32_t)(now-tick); totalbytes = (totalbytes/elapse)/1024; printf("client_count:%d,totalbytes:%lldMB/s\n",client_count,totalbytes); tick = now; totalbytes = 0; } } return 0; }
static int32_t _epoll_wait(int epfd, struct epoll_event *events,int maxevents, int timeout) { uint64_t _timeout = kn_systemms64() + (uint64_t)timeout; if(0 == maxevents) usleep(timeout*1000); else{ for(;;){ int32_t nfds = epoll_wait(epfd,events,maxevents,timeout); if(nfds < 0 && errno == EINTR){ uint64_t cur_tick = kn_systemms64(); if(_timeout > cur_tick){ timeout = (int)(_timeout - cur_tick); errno = 0; } else return 0; }else return nfds; } } return 0; }
int main(){ msgque = kn_new_msgque(32); kn_thread_t t1 = kn_create_thread(THREAD_JOINABLE); kn_thread_t t2 = kn_create_thread(THREAD_JOINABLE); kn_thread_start_run(t1,routine1,NULL); kn_thread_start_run(t2,routine2,NULL); uint64_t tick,now; tick = now = kn_systemms64(); while(1){ kn_sleepms(100); now = kn_systemms64(); if(now - tick > 1000) { uint32_t elapse = (uint32_t)(now-tick); printf("count:%d\n",count/elapse*1000); tick = now; count = 0; } } return 0; }
int main(int argc,char **argv) { kn_net_open(); kn_proactor_t p = kn_new_proactor(); kn_sockaddr local; kn_addr_init_in(&local,argv[1],atoi(argv[2])); kn_stream_server_t s = kn_new_stream_server(p,&local,new_client); uint64_t tick,now; tick = now = kn_systemms64(); while(1){ kn_proactor_run(p,50); now = kn_systemms64(); if(now - tick > 1000) { uint32_t elapse = (uint32_t)(now-tick); recvcount = recvcount*1000/elapse; printf("client_count:%d,recvcount:%d/s,buffer_count:%u\n",client_count,recvcount,buffer_count); tick = now; recvcount = 0; } } return 0; }
void *routine1(void *arg) { printf("routine1\n"); kn_proactor_t p = kn_new_proactor(); kn_channel_bind(p,channel1,on_msg1,NULL); int i = 0; for(; i < 10000; ++i){ kn_channel_putmsg(channel2,NULL,malloc(1)); } uint64_t tick,now; tick = now = kn_systemms64(); while(1){ kn_proactor_run(p,50); now = kn_systemms64(); if(now - tick > 1000) { uint32_t elapse = (uint32_t)(now-tick); printf("count:%d\n",count/elapse*1000); tick = now; count = 0; } } return NULL; }