| 199 | |
| 200 | |
| 201 | int main(int argc,char *argv[]) |
| 202 | { |
| 203 | if(argc<5){ |
| 204 | printf("Usage:\n" |
| 205 | "example_echosvr [IP] [PORT] [TASK_COUNT] [PROCESS_COUNT]\n" |
| 206 | "example_echosvr [IP] [PORT] [TASK_COUNT] [PROCESS_COUNT] -d # daemonize mode\n"); |
| 207 | return -1; |
| 208 | } |
| 209 | const char *ip = argv[1]; |
| 210 | int port = atoi( argv[2] ); |
| 211 | int cnt = atoi( argv[3] ); |
| 212 | int proccnt = atoi( argv[4] ); |
| 213 | bool deamonize = argc >= 6 && strcmp(argv[5], "-d") == 0; |
| 214 | |
| 215 | g_listen_fd = CreateTcpSocket( port,ip,true ); |
| 216 | listen( g_listen_fd,1024 ); |
| 217 | if(g_listen_fd==-1){ |
| 218 | printf("Port %d is in use\n", port); |
| 219 | return -1; |
| 220 | } |
| 221 | printf("listen %d %s:%d\n",g_listen_fd,ip,port); |
| 222 | |
| 223 | SetNonBlock( g_listen_fd ); |
| 224 | |
| 225 | for(int k=0;k<proccnt;k++) |
| 226 | { |
| 227 | |
| 228 | pid_t pid = fork(); |
| 229 | if( pid > 0 ) |
| 230 | { |
| 231 | continue; |
| 232 | } |
| 233 | else if( pid < 0 ) |
| 234 | { |
| 235 | break; |
| 236 | } |
| 237 | for(int i=0;i<cnt;i++) |
| 238 | { |
| 239 | task_t * task = (task_t*)calloc( 1,sizeof(task_t) ); |
| 240 | task->fd = -1; |
| 241 | |
| 242 | co_create( &(task->co),NULL,readwrite_routine,task ); |
| 243 | co_resume( task->co ); |
| 244 | |
| 245 | } |
| 246 | stCoRoutine_t *accept_co = NULL; |
| 247 | co_create( &accept_co,NULL,accept_routine,0 ); |
| 248 | co_resume( accept_co ); |
| 249 | |
| 250 | co_eventloop( co_get_epoll_ct(),0,0 ); |
| 251 | |
| 252 | exit(0); |
| 253 | } |
| 254 | if(!deamonize) wait(NULL); |
| 255 | return 0; |
| 256 | } |
| 257 |
nothing calls this directly
no test coverage detected