📄 wk.c
字号:
/* copy it */ int j; for(j=0;j<len-1;j++){ if( src[i+j] == '\0' || ISSPACETAB( src[i+j] ) ){ break; } output[j]=src[i+j]; } output[j]='\0'; return; } } }}static void workerMainLoop( void ){ char linebuf[4096]; int i_flag;//Arminitus && ttom 12/15 time_t retime;#ifndef _FIX_WORKS struct sockaddr_in sin; int addrlen = sizeof( sin ); FILE *fp; int newsockfd ; while(1){ fd_set rfds; struct timeval t; t.tv_sec = timeout; t.tv_usec = 0; FD_ZERO( &rfds ); FD_SET( mainsockfd , &rfds ); select( 1024 , &rfds , (fd_set*) NULL , (fd_set*)NULL , &t ); if( FD_ISSET( mainsockfd , &rfds ) ){ newsockfd = accept( mainsockfd , (struct sockaddr*) &sin , &addrlen ); if( newsockfd < 0 ){ fprintf( stderr,"accept error! \n"); } else { memcpy(&myIP, &sin.sin_addr, sizeof(long)); fprintf( stderr,"accepted. myIP=%ld\n", myIP ); } break; } else { fprintf(stderr, "accept timed out." ); exit(0); } } close(mainsockfd); { int flag = 1; int result = setsockopt( newsockfd, IPPROTO_TCP, TCP_NODELAY, (char*) &flag, sizeof(int)); if( result != 0 ){ fprintf( stderr , "nodelay error!\n" ); } } fp = fdopen( newsockfd , "r+"); if( fp == NULL ){ fprintf( stderr,"fdopen error.abort" ); return ; }#endif //确认CS可连线 if( checkCSconnect() == -1 ) return;#ifdef _FIX_WORKS if( !LoginToAC() ) //login to ac return; retime = time( NULL); while( WORK_TYPE!=-1) {#elsewhile( fgets( linebuf,sizeof(linebuf), fp)){#endif // Spock 2000/10/31 char id[104] ,pas[1024] , flag[8] , process[16]; // Spock end int knret; char deadline[100] = "0"; // _TIMEOUTKICK usleep(10*1000);#ifdef _FIX_WORKS if( (WORK_TYPE = Ac_message_accept( linebuf, sizeof( linebuf))) <= 0 ) continue; retime = time( NULL);#endif memset( id, 0, sizeof( id)); memset( process, 0, sizeof( process)); memset( flag, 0, sizeof( flag)); /* chop */ linebuf[strlen(linebuf)-1]=0; /* kill-\r */ remove_r( linebuf ); easyGetTokenFromString( linebuf , 1 , id , sizeof( id )); easyGetTokenFromString( linebuf , 2 , pas , sizeof( pas )); // Nuke +1 easyGetTokenFromString( linebuf , 3 , flag, sizeof(flag)); // Spock 2000/10/31 easyGetTokenFromString( linebuf , 4 , process , sizeof(process) ); // Spock end // Nuke i_flag=0; //Arminius && ttom i_flag=atoi(flag); //Arminius && ttom if((!process)||(!*process)){ knret=-7; }else{ switch (i_flag) { //Arminius && ttom#ifdef _FIX_MESSAGE // WON ADD 修改封包内容 #ifdef _TIMEOUTKICK case 1: // List#ifdef _AP_CHECK_3_TIMES // WON ADD 减少认证次数 knret = 0;#else knret = doKN( id, pas, flag, "", deadline);#endif break; case 4: // Delete knret = doKN( id , pas , flag, "" , deadline); break; case 2: // Load knret = doKN( id , pas , flag, "" , deadline); break; case 3: // Save knret = doKN( id , pas, flag, "" , deadline); break; case 5: // recheck WGS knret = doKN( id , pas , flag, "" , deadline); break;#else case 1: // List#ifdef _AP_CHECK_3_TIMES // WON ADD 减少认证次数 knret = 0;#else knret = doKN( id, pas, flag, "");#endif break; case 4: // Delete knret = doKN( id , pas , flag, "" ); break; case 2: // Load knret = doKN( id , pas , flag, "" ); break; case 3: // Save knret = doKN( id , pas, flag, "" ); break;#endif#else case 1: // List knret = doKN( id, pas, flag, ""); break; case 4: // Delete knret = doKN( id , pas , flag , "" ); break; case 2: // Load knret = doKN( id , pas , flag , process ); break; case 3: // Save knret = doKN( id , pas , flag , process ); break;#endif default: knret = -7; } }#ifdef _FIX_WORKS {//andy_add int ret; char buf1[256];#ifdef _TIMEOUTKICK sprintf( buf1, "%d %s \n", knret, deadline);#else sprintf( buf1, "%d\n", knret);#endif //Log( buf1); if((ret = write( afd, buf1, sizeof( buf1))) < 0 ) { close( afd); afd = 0; break; } } memset( linebuf, 0, sizeof( linebuf));#else#ifdef _TIMEOUTKICK fprintf( fp , "%d %s \n", knret, deadline );#else fprintf( fp , "%d\n", knret );#endif#endif }#ifdef _FIX_WORKS if( afd > 0 ) close( afd); if( csd > 0 ) close( csd);#else fclose( fp );#endif //if( AcLogFile != NULL) // fclose( AcLogFile); Log( "SAAC已关闭. 异常终止.\n" );}static void parseOpt( int argc , char **argv ){ int c; int option_index;#ifdef _ADD_AC_IP // WON ADD 修改封包内容 memset( AC_IP, -1 , sizeof( AC_IP) );#endif if( argc == 1 ){ strcpy( WGSAddress, "127.0.0.1" ); WGSPort = 9200; } while(1){ static struct option long_options[] = { {"port" , 1, 0 ,'p'}, {"debug" , 1, 0 , 'd'}, {"help" , 0 , 0 , 'h'}, {"logdir", 1, 0, 'l' }, {"csaddr", 1, 0, 'a' }, {"csport", 1, 0, 'c' },#ifdef _ADD_AC_IP // WON ADD 修改封包内容 {"acaddr", 1, 0, 'w' },#endif {0,0,0,0} };#ifdef _FIX_MESSAGE // WON ADD 修改封包内容 c = getopt_long ( argc, argv, "p:dhl:a:c:w:", long_options, &option_index );#else c = getopt_long ( argc, argv, "p:dhl:a:c:", long_options, &option_index );#endif if( c == -1 )break; switch(c ){ case 'l': if( optarg){ snprintf( logdir, sizeof( logdir), "%s", optarg ); } else{ fprintf( stderr , "-l 后面需要日录名.\n" ); } break; case 'p' : port = atoi( optarg ); break; case 'd': debug = 1; break; case 'h': fprintf( stderr, "使用方法: acwk [-d|--调试模试] [-p 端口|--端口 端口] [-h|--帮助] [-l 日志日录]\n" " [-a CS地址] [-c CS端口]\n" "默认调试模试为0.\n" "\nCopyright 2003 龙zoro工作室 / Longzoro system supply\n" ); exit(0); break; // Spock 2000/12/5 case 'a': strcpy( WGSAddress, optarg ); break; case 'c': WGSPort = atoi( optarg ); break; // Spock end#ifdef _ADD_AC_IP // WON ADD 加AC_IP至通讯协定中 case 'w': strcpy( AC_IP, optarg ); Log( AC_IP ); break;#endif } }}int main( int argc , char ** argv ){ // Spock +1 2000/12/5 WGSAddress[0] = 0; parseOpt(argc,argv);#ifndef _FIX_WORKS if( port < 0 ){ //fprintf( stderr , "You have to specify port number by -p option.\n"); //exit(1); WGSPort=9200; }#endif // Spock 2000/12/5 if( strlen(WGSAddress) < 8 || strlen(WGSAddress) > 16 ){ fprintf( stderr , "你指定CS的IP地址不正确.\n"); exit(1); } if( WGSPort < 0 ){ fprintf( stderr , "你指定CS的端口不正确.\n"); exit(1); }#ifndef _FIX_WORKS fprintf( stderr , "\n端口:[%d] 调试模式:[%d] CS地址:[%s] CS端口:[%d]\n" , port , debug , WGSAddress , WGSPort ); // Spock end if( (mainsockfd = createMainSocket( port ))<0){ fprintf( stderr , "cannot create mainsocket: %s\n", strerror( errno )); return 1; }#else fprintf( stderr , "\n调试模式:[%d] CS地址:[%s] CS端口:[%d]\n" , debug , WGSAddress , WGSPort );#endif// Nuke +1 additional_process=time(0)/256; signal(SIGPIPE,sigpipe); signal(SIGUSR1,sigusr1); signal(SIGUSR2,sigusr2); signal(SIGTERM,sigterm); workerMainLoop(); return 0;}/*static char * chop( char *src ){ int i; for(i=0;;i++){ if( src[i] == '\n' ) src[i]= 0; if( src[i] == 0 )break; } return src;}*/void writeLog( char *msg ){ char logbuf[BUF_SIZE]; time_t t; if( sizeof( msg) > sizeof( logbuf)) return; sprintf( logbuf, "%s", msg); Log( logbuf); t = time(NULL);}void Log( char *msg ){#ifdef _DEBUG_WORK FILE *AcLogFile=NULL; if( AcLogFile == NULL ) { char filename[1000]; snprintf( filename, sizeof( filename), "%s/%d.worker" , logdir, port ); AcLogFile = fopen( filename, "a+" ); if(AcLogFile == NULL)return; } fprintf( AcLogFile, "%s" , msg ); fclose( AcLogFile);#else fprintf( stderr,"%s", msg);#endif}#ifdef _FIX_WORKSint LoginToAC(){ Log( "\n连接 SAAC "); usleep( 80*10000); Log( "."); afd = TCPconnect( WGSAddress, ACPORT); if( afd <= 0 ) { close( afd); return 0; } Log( ".成功\n"); usleep( 40*10000); return 1;}char tmpbuf[4096];int readsize;struct timeval select_timeout;int Ac_message_accept( char *buf, int slen){ //int i; int sret; //int accepted = 0; struct timeval t; fd_set rfds, /*wfds,*/ efds; readsize = sizeof( tmpbuf); FD_ZERO( & rfds ); //FD_ZERO( & wfds ); FD_ZERO( & efds ); FD_SET( afd, & rfds ); //FD_SET( afd, & wfds ); FD_SET( afd, & efds ); select_timeout.tv_sec = 0; select_timeout.tv_usec = 0; t = select_timeout; sret = select( 1024 , & rfds , (fd_set*)NULL, & efds , &t); if( ( afd >= 0 ) && FD_ISSET( afd , &rfds ) ){ int rr; rr = read( afd , tmpbuf , readsize ); if( rr > 0 ) { memcpy( buf, tmpbuf, slen); }else { Log("..WORK ERROR\n"); return -1; } }else { return 0; } return 1;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -