//启动服务 bool CServiceUnits::StartService() { //效验状态 ASSERT(m_ServiceStatus==ServiceStatus_Stop); if (m_ServiceStatus!=ServiceStatus_Stop) return false; //设置状态 SetServiceStatus(ServiceStatus_Config); //创建窗口 if (m_hWnd==NULL) { CRect rcCreate(0,0,0,0); Create(NULL,NULL,WS_CHILD,rcCreate,AfxGetMainWnd(),100); } //创建模块 if (CreateServiceDLL()==false) { ConcludeService(); return false; } //调整参数 if (RectifyServiceParameter()==false) { ConcludeService(); return false; } //配置服务 if (InitializeService()==false) { ConcludeService(); return false; } //启动内核 if (StartKernelService()==false) { ConcludeService(); return false; } //加载配置 SendControlPacket(CT_LOAD_SERVICE_CONFIG,NULL,0); //启动比赛 if (m_GameMatchServiceManager.GetInterface()!=NULL && m_GameMatchServiceManager->StartService()==false) return false; return true; }
static int ServiceMain(int argc, char **argv) { SOCKET s; int status; static struct sockaddr_in sin; int tablesize = FD_SETSIZE; extern fd_set FdActive(); struct timeval timeout = {1,0}; int error_count=0; fd_set readfds; fd_set fdactive; RedirectOutput(); InitializeService(); SetThisServiceStatus(SERVICE_RUNNING,0); if (GetMulti()) { IoRoutines *io; io=LoadIo(GetProtocol()); if (io && io->listen) io->listen(argc,argv); else { fprintf(stderr,"Protocol %s does not support servers\n",GetProtocol()); return 1; } return 0; } else { s = socket(AF_INET, SOCK_STREAM, 0); if (s == -1) { printf("Error getting Connection Socket\n"); exit(1); } memset(&sin,0,sizeof(sin)); sin.sin_port = GetPort(); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; status = bind(s, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)); if (status < 0) { perror("Error binding to service\n"); exit(1); } status = listen(s,128); if (status < 0) { perror("Error listen on port\n"); exit(1); } FD_ZERO(&fdactive); FD_SET(s,&fdactive); for (readfds=fdactive;!shut;readfds=fdactive) { int sstatus; if ((sstatus = select(tablesize, &readfds, 0, 0, &timeout)) != SOCKET_ERROR) { error_count=0; if (FD_ISSET(s, &readfds)){ int len = sizeof(struct sockaddr_in); SOCKET sock = accept(s, (struct sockaddr *)&sin, &len); SpawnWorker(sock); } } else { error_count++; perror("error in main select"); fprintf(stderr,"Error count=%d\n",error_count); fflush(stderr); if (error_count > 100) { fprintf(stderr,"Error count exceeded, shutting down\n"); shut=1; } } } shutdown(s,2); return 1; } }