static int getLineReadBuffer( int index , char *buf, int len ) { int top = con[index].mbtop_ri; int ti = 0 , breakflag = 0; for(;;){ int i; int l = mb[top].len; if( top == -1 )break; for( i=0 ; i < l ; i++){ if( mb[top].buf[i] == '\n' ){ breakflag = 1; break; } ti ++; } if( breakflag )break; top = mb[top].next; } if( ti > len ){ /* 1µæ»¥Ø¦»¥ÔÊ¿ºÔÂ£Û Ø¦¾Þ·Â¡õë¾®ÒüÁù */ return TCPSTRUCT_ETOOLONG; } /* µæ»¥¶ØÔÀØÆ»¯Ø¦ÖÐ */ if( breakflag == 0 ){ return 0; } return consumeMemBufList( con[index].mbtop_ri , buf , ti+1 , 1 , 1 ); }
static int getLineReadBuffer( int index , char *buf, int len ) { int top = con[index].mbtop_ri; int ti = 0 , breakflag = 0; for(;;){ int i; int l = mb[top].len; if( top == -1 )break; for( i=0 ; i < l ; i++){ if( mb[top].buf[i] == '\n' ){ breakflag = 1; break; } ti ++; } if( breakflag )break; top = mb[top].next; } if( ti > len ){ /* 1�滥ئ���ʿ��£� ئ�¡�ë������ */ return TCPSTRUCT_ETOOLONG; } /* �滥�����ƻ�ئ�� */ if( breakflag == 0 ){ return 0; } return consumeMemBufList( con[index].mbtop_ri , buf , ti+1 , 1 , 1 ); }
/* Ðijð¸êÊÖ¼°»¥ÊÖµ¤Ø¦ÈÊ»¯£ýØƾ®ÊÖ remoteclose ·ÖÔÈÐ×ÈÕ -1 ë¾®ÒüÔÊ */ int tcpstruct_read( int ti , char *buf , int len ) { int l; if( ti < 0 || ti >= MAXCONNECTION || con[ti].use == 0 ) return TCPSTRUCT_EINVCIND; l = consumeMemBufList( con[ti].mbtop_ri , buf , len , 1 , 1); if( l == 0 && con[ti].closed_by_remote ) return TCPSTRUCT_EREADFIN; return l; }
int tcpstruct_close( int ti ) { if( ti < 0 || ti >= MAXCONNECTION )return TCPSTRUCT_EINVCIND; if( con[ti].use == 0 ){ return TCPSTRUCT_ECLOSEAGAIN; } close( con[ti].fd ); con[ti].use = 0; con[ti].fd = -1; /* Øøµ©ÐþëÐ×ÉýÔÈ»¯òå ëÛÍØøʧÔÊÔ */ consumeMemBufList( con[ti].mbtop_ri , NULL, mbsize * sizeof( mb[0].buf ), 1, 0 ); consumeMemBufList( con[ti].mbtop_wi , NULL, mbsize * sizeof( mb[0].buf ), 1, 0 ); unregMemBuf( con[ti].mbtop_ri ); unregMemBuf( con[ti].mbtop_wi ); con[ti].mbtop_ri = -1; con[ti].mbtop_wi = -1; return OK; }
int tcpstruct_accept( int *tis , int ticount ) { int i; int sret; int accepted = 0; struct timeval t; struct sockaddr_in sin; fd_set rfds, wfds , efds; FD_ZERO( & rfds ); FD_ZERO( & wfds ); FD_ZERO( & efds ); for(i=0;i<MAXCONNECTION;i++){ if( con[i].use && con[i].fd >= 0 && con[i].closed_by_remote ==0 ){ FD_SET( con[i].fd , & rfds ); FD_SET( con[i].fd , & wfds ); FD_SET( con[i].fd , & efds ); } } t = select_timeout; sret = select( 1024, & rfds , (fd_set*)NULL, & efds , &t); if( sret > 0 ) { for(i=0;i< MAXCONNECTION;i++){ if( ( con[i].fd >= 0 ) && FD_ISSET( con[i].fd , &rfds ) ){ int fr = getFreeMem( ); int rr , readsize ; if( fr <= 0 ) continue; if( fr > sizeof(tmpbuf ) ){ readsize = sizeof( tmpbuf); } else { readsize = fr; } rr = read( con[i].fd , tmpbuf , readsize ); if( rr <= 0 ){ con[i].closed_by_remote = 1; } else { appendReadBuffer( i , tmpbuf , rr ); } } } } /* write */ t = select_timeout; sret = select( 1024, (fd_set*)NULL, &wfds, & efds , &t); if( sret > 0 ) { for(i=0;i<MAXCONNECTION;i++){ if( ( con[i].fd >= 0 ) && FD_ISSET( con[i].fd , &wfds )){ char send_buf[4096]; int l , rr; memset( send_buf, 0, sizeof( send_buf)); l = consumeMemBufList( con[i].mbtop_wi ,send_buf, sizeof(send_buf),0 , 1 ); rr = write( con[i].fd , send_buf , l ); if( rr < 0 ){ con[i].closed_by_remote = 1; } else { consumeMemBufList( con[i].mbtop_wi , send_buf, l, 1 , 0 ); } } } } for( i=0; i<ticount; i++){ int asret; struct timeval t; t.tv_sec =0; t.tv_usec =0; FD_ZERO( & rfds ); FD_ZERO( & wfds ); FD_ZERO( & efds ); FD_SET( mainsockfd , & rfds ); FD_SET( mainsockfd , & wfds ); FD_SET( mainsockfd , & efds ); asret = select( 1024, &rfds , &wfds , &efds, &t ); // Nuke 20040610: add asret>0 to avoid signal interrupt in select if( (asret>0) && FD_ISSET( mainsockfd , & rfds )){ struct sockaddr_in c; int len , newsockfd; int newcon; bzero( &c , sizeof( c )); len = sizeof( c ); fprintf( stderr, "i can accept " ); newcon = findregBlankCon( ); if( newcon < 0 ) continue; newsockfd = accept( mainsockfd, (struct sockaddr*)&c , &len ); log( "ͬÒâ: %d\n" , newsockfd ); if( newsockfd < 0 ){ unregMemBuf( newcon ); continue; } char ipbuf[64]; char ipmsg[2048]; FILE *ipfp; ipfp = fopen( "ip.txt" , "r" ); if( ipfp != NULL ){ //log("111111111\n"); while(fgets(ipbuf, sizeof(ipbuf), ipfp)){ sprintf(ipmsg,"%s%s",ipmsg,ipbuf); } fclose(ipfp); unsigned long sinip; memcpy( &sinip, &c.sin_addr, 4); int ipa,ipb,ipc,ipd; char ip[32]; ipa=(sinip % 0x100); sinip=sinip / 0x100; ipb=(sinip % 0x100); sinip=sinip / 0x100; ipc=(sinip % 0x100); sinip=sinip / 0x100; ipd=(sinip % 0x100); sprintf(ip,".%d.%d.%d.%d.",ipa,ipb,ipc,ipd); //log("ip=%s,ip1=%s\n",ip,ipmsg); if(strstr(ipmsg,ip)==NULL){ close(newsockfd); unregMemBuf( newcon ); continue; } } set_nodelay( newsockfd ); con[newcon].fd = newsockfd; memcpy( &con[newcon].remoteaddr , &c ,sizeof(c)); tis[accepted] = newcon; accepted ++; } } return accepted; }
int tcpstruct_accept( int *tis , int ticount ) { int i; int sret; int accepted = 0; struct timeval t; fd_set rfds, wfds , efds; FD_ZERO( & rfds ); FD_ZERO( & wfds ); FD_ZERO( & efds ); for(i=0;i<MAXCONNECTION;i++){ if( con[i].use && con[i].fd >= 0 && con[i].closed_by_remote ==0 ){ FD_SET( con[i].fd , & rfds ); FD_SET( con[i].fd , & wfds ); FD_SET( con[i].fd , & efds ); } } t = select_timeout; sret = select( 1024, & rfds , (fd_set*)NULL, & efds , &t); if( sret > 0 ) { for(i=0;i< MAXCONNECTION;i++){ if( ( con[i].fd >= 0 ) && FD_ISSET( con[i].fd , &rfds ) ){ int fr = getFreeMem( ); int rr , readsize ; if( fr <= 0 ) continue; if( fr > sizeof(tmpbuf ) ){ readsize = sizeof( tmpbuf); } else { readsize = fr; } rr = read( con[i].fd , tmpbuf , readsize ); if( rr <= 0 ){ con[i].closed_by_remote = 1; } else { appendReadBuffer( i , tmpbuf , rr ); } } } } /* write */ t = select_timeout; sret = select( 1024, (fd_set*)NULL, &wfds, & efds , &t); if( sret > 0 ) { for(i=0;i<MAXCONNECTION;i++){ if( ( con[i].fd >= 0 ) && FD_ISSET( con[i].fd , &wfds )){ char send_buf[4096]; int l , rr; memset( send_buf, 0, sizeof( send_buf)); l = consumeMemBufList( con[i].mbtop_wi ,send_buf, sizeof(send_buf),0 , 1 ); rr = write( con[i].fd , send_buf , l ); if( rr < 0 ){ con[i].closed_by_remote = 1; } else { consumeMemBufList( con[i].mbtop_wi , send_buf, l, 1 , 0 ); } } } } for( i=0; i<ticount; i++){ int asret; struct timeval t; t.tv_sec =0; t.tv_usec =0; FD_ZERO( & rfds ); FD_ZERO( & wfds ); FD_ZERO( & efds ); FD_SET( mainsockfd , & rfds ); FD_SET( mainsockfd , & wfds ); FD_SET( mainsockfd , & efds ); asret = select( 1024, &rfds , &wfds , &efds, &t ); // Nuke 20040610: add asret>0 to avoid signal interrupt in select if( (asret>0) && FD_ISSET( mainsockfd , & rfds )){ struct sockaddr_in c; int len , newsockfd; int newcon; bzero( &c , sizeof( c )); len = sizeof( c ); fprintf( stderr, "i can accept " ); newcon = findregBlankCon( ); if( newcon < 0 ) continue; newsockfd = accept( mainsockfd, (struct sockaddr*)&c , &len ); log( "ͬ��: %d\n" , newsockfd ); if( newsockfd < 0 ){ unregMemBuf( newcon ); continue; } set_nodelay( newsockfd ); con[newcon].fd = newsockfd; memcpy( &con[newcon].remoteaddr , &c ,sizeof(c)); tis[accepted] = newcon; accepted ++; } } return accepted; }