int startServer( appnetServer *serv ) { int sockfd[2]; int sock_count = 0; set_daemon( serv ); // memory alloc initServer( serv ); listenToPort( serv->listen_ip , serv->port , sockfd , &sock_count ); serv->listenfd = sockfd[0]; createWorkerProcess( serv ); createReactorThreads( serv ); __SLEEP_WAIT__; runMainReactor( serv ); destroyServer( serv ); __SLEEP_WAIT__; return 0; }
void redis_test_init(int port) { int fd[16]; int count = 0; if (listenToPort(port, &fd[0], &count) == C_ERR) exit(1); int i; for (i = 0; i < count; i++) { if (aeCreateFileEvent(server.el, fd[i], AE_READABLE, redis_test_accept,NULL) == AE_ERR) { serverPanic( "Unrecoverable error creating server.ipfd file event."); } } printf("%s %d: port[%d] count[%d]\n", __FUNCTION__, __LINE__, port, count); }
int startServer( aeServer* serv ) { int sockfd[2]; int sock_count = 0; //监听TCP端口,这个接口其实可以同时监听多个端口的。 listenToPort( serv->listen_ip, serv->port , sockfd , &sock_count ); serv->listenfd = sockfd[0]; //创建进程要先于线程,否则,会连线程一起fork了,好像会这样。。。 createWorkerProcess( serv ); //创建子线程,每个线程都监听所有worker管道 createReactorThreads( serv ); __SLEEP_WAIT__; //运行主reactor runMainReactor( serv ); //退出 destroyServer( serv ); return 0; }
static void createRedisSocket () { int j; if (server.port != 0 && listenToPort (server.port, server.ipfd, &server.ipfd_count) == REDIS_ERR) { redisLog (REDIS_WARNING, "Opening port %d: %s", server.port, server.neterr); exit (1); } if (server.unixsocket != NULL) { unlink (server.unixsocket); /* don't care if this fails */ server.sofd = anetUnixServer (server.neterr, server.unixsocket, server.unixsocketperm, server.tcp_backlog); if (server.sofd == ANET_ERR) { redisLog (REDIS_WARNING, "Opening socket: %s", server.neterr); exit (1); } } if (server.ipfd_count == 0 && server.sofd < 0) { redisLog (REDIS_WARNING, "Configured to not listen anywhere, exiting."); exit (1); } if (aeCreateTimeEvent (server.el, 1, serverCron, NULL, NULL) == AE_ERR) { redisPanic ("Can't create the serverCron time event."); exit (1); } /* Create an event handler for accepting new connections in TCP and Unix * domain sockets. */ for (j = 0; j < server.ipfd_count; j++) { if (aeCreateFileEvent (server.el, server.ipfd[j], AE_READABLE, acceptTcpHandler, NULL) == AE_ERR) { redisPanic ("Unrecoverable error creating server.ipfd file event."); } } if (server.sofd > 0 && aeCreateFileEvent (server.el, server.sofd, AE_READABLE, acceptUnixHandler, NULL) == AE_ERR) redisPanic ("Unrecoverable error creating server.sofd file event."); if (server.aof_state == REDIS_AOF_ON) { server.aof_fd = open (server.aof_filename, O_WRONLY | O_APPEND | O_CREAT, 0644); if (server.aof_fd == -1) { redisLog (REDIS_WARNING, "Can't open the append-only file: %s", strerror (errno)); exit (1); } } if (server.ipfd_count > 0) redisLog (REDIS_NOTICE, "The server is now ready to accept connections on port %d", server.port); if (server.sofd > 0) redisLog (REDIS_NOTICE, "The server is now ready to accept connections at %s", server.unixsocket); }