void accept_cb(chk_acceptor *a,int32_t fd,chk_sockaddr *addr,void *ud,int32_t err) { int i; chk_stream_socket_option option = { .recv_buffer_size = 1024*16, .recv_timeout = 0, .send_timeout = 0, .decoder = (chk_decoder*)_decoder_new(4096), }; ++client_count; chk_stream_socket *s = chk_stream_socket_new(fd,&option); for(i = 0 ;i < 1000; ++i) { if(!clients[i]) { clients[i] = s; break; } } chk_loop_add_handle(loop,(chk_handle*)s,(chk_event_callback)data_event_cb); } int32_t on_timeout_cb(uint64_t tick,void*ud) { printf("client_count:%d,packet_count:%u/s,chunk_count:%u,buffercount:%u\n", client_count,packet_count,chunkcount,buffercount); packet_count = 0; return 0; }
chk_acceptor *chk_listen_tcp_ip4(chk_event_loop *loop,const char *ip,int16_t port,chk_acceptor_cb cb,void *ud) { chk_sockaddr server; int32_t fd,ret; chk_acceptor *a = NULL; do { if(chk_error_ok != easy_sockaddr_ip4(&server,ip,port)) break; if(0 > (fd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))){ CHK_SYSLOG(LOG_ERROR,"socket() failed errno:%d",errno); break; } easy_addr_reuse(fd,1); if(chk_error_ok == (ret = easy_listen(fd,&server))){ a = chk_acceptor_new(fd,ud); chk_loop_add_handle(loop,(chk_handle*)a,cb); }else close(fd); }while(0); return a; }
int main(int argc,char **argv) { signal(SIGPIPE,SIG_IGN); loop = chk_loop_new(); chk_sockaddr server; if(0 != easy_sockaddr_ip4(&server,argv[1],atoi(argv[2]))) { printf("invaild address:%s\n",argv[1]); } int32_t fd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); easy_addr_reuse(fd,1); if(0 == easy_listen(fd,&server)){ chk_acceptor *a = chk_acceptor_new(fd,NULL); assert(0 == chk_loop_add_handle(loop,(chk_handle*)a,(chk_event_callback)accept_cb)); chk_loop_addtimer(loop,1000,on_timeout_cb,NULL); chk_loop_run(loop); }else{ close(fd); printf("server start error\n"); } return 0; }