📄 routerassistserver.c
字号:
// get file content
szFile = malloc( ret );
if ( szFile==NULL )
{
szFile = "malloc failure.";
goto sEnd;
}
ret = FileReadWhole( VIEW_LOG_FILE, szFile, ret );
if ( ret<0 )
{
free( szFile );
szFile = "Get File content failure.";
}
sEnd:
ret = SetRouterData( buf, DATA_VIEW_LOG, strlen(szFile), szFile );
free( szFile );
ret = nWriten( fd, buf, ret );
if ( ret<0 )
{
PrintS( "send error." );
return -1;
}
return 0;
}
void ProcessDebugRunApp(void)
{
int ret;
TimerClose();
if ( close( gClientFd )<0 )
perror("close error");
if ( close( gListenFd )<0 )
perror("close error");
// ret = fcntl( gClientFd, F_SETFD, FD_CLOEXEC );
// dPrintInt( ret );
//
// ret = fcntl( gListenFd, F_SETFD, FD_CLOEXEC );
// dPrintInt( ret );
// dPrintS( "sleep 5 second..." );
// sleep( 5 );
// dPrintS( "restart..." );
// //ret = system( APP_PATH );
// ret = execl( APP_PATH, "RouterAssistServer" );
// dPrintInt( ret );
exit(0);
}
// get file content from offset, length: size.
// free buf after running.
char * GetFileContentAfterSize( char * filename, int offset, int size )
{
FILE * fp = NULL;
// int filesize ;
// int size;
char * buf;
if ( filename==NULL || offset < 0 || size==0 )
{
printf( "parameter error.\n" );
return NULL;
}
//dPrintInt( offset );
//dPrintInt( size );
// if ( size==-1 ) // from offset to end of file
// {
// filesize = GetFileSize( filename );
// PrintInt( filesize );
// if ( offset > filesize )
// {
// printf( "offset more than filesize.\n" );
// return NULL;
// }
// }
fp = fopen( filename, "r" );
if ( fp==NULL )
{
perror("open file failure");
return NULL;
}
// size = filesize - offset;
// dPrintInt( size );
// if ( size==0 )
// return strdup("");
buf = (char *)malloc( size );
if ( buf==NULL )
{
perror("strdup error");
return NULL;
}
// PrintInt( size );
//if ( offset==0 ) offset = 1;
if ( fseek( fp, offset, SEEK_SET ) < 0 )
{
perror( "fseek error" );
return NULL;
}
// PrintInt( size );
if ( fread( buf, size, 1, fp ) < 0 )
{
perror( "fread error" );
return NULL;
}
// PrintInt( size );
fclose(fp);
return buf;
}
void * SendLogInfoThrdProc( void * para )
{
int filesize = 0;
int len, tlen;
char * buf;
//char SendBuf[2048];
static int nCount = 0;
while (1)
{
if ( gClientFd==-1 ) // disconnect.
{
PrintS("gClientFd = -1");
break;
}
// check file size. if file size more than prev size, get file content.
len = GetFileLength( STDERR_LOG );
if ( len > filesize )
{
//dPrintInt( len );
//dPrintInt( filesize );
tlen = len - filesize;
buf = GetFileContentAfterSize( STDERR_LOG, filesize, tlen );
filesize = len;
//dPrintStr( buf );
//dPrintInt( tlen );
if ( buf==NULL )
{
buf = strdup( "get log failure." );
tlen = strlen( buf );
}
//dPrintStr( buf );
tlen = SetRouterDataAndSend( DATA_DEBUG_LOG_INFO, tlen, buf );
if ( tlen<0 )
{
PrintInt( tlen );
PrintS( "make frame and send data error." );
}
free( buf );
}
nCount++;
if ( nCount>10)
{
nCount = 0;
TimerHandle( 0 );
}
usleep(500000); // 0.5 second
}
return NULL;
}
void * RcvThrdProc( void * para )
{
int fd = (int)para;
char buf[8];
int ret;
if ( para==NULL )
return NULL;
while(1)
{
unsigned int len;
PrintS("Receive data...");
ret = nReadn( fd, buf, 5 );
if ( ret==0 )
{
PrintS( "Client disconnect." );
break;
}
else if ( ret != 5 )
{
dPrintInt( ret );
perror( "read data error" );
break;
}
//TimerClose();
memcpy( &len, buf+1, 4 );
dPrintInt( buf[0] );
switch( buf[0] )
{
case DATA_TCP:
ProcessFtpData(fd, len);
break;
case DATA_TELNET:
ProcessTelnetData(fd, len);
break;
case DATA_VIEW_LOG:
ProcessViewLog(fd);
break;
case DATA_DEBUG_RUN_APP:
ProcessDebugRunApp();
break;
}
//TimerStartEx( 1000, TimerHandle );
}
dPrintS( "RcvThrdProc Exit..." );
//ret = pause();
//dPrintInt( ret );
gClientFd = -1;
TimerClose();
pthread_exit( NULL );
return NULL;
}
int ProcessEvent( int newfd )
{
// char buf[4096];
// int len;
// int ret;
// int i;
pthread_t tid;
pthread_t tid1;
// char *data;
PrintS( "start pthread_create for receiving data ." );
// for receive data
if (pthread_create( &tid, NULL, RcvThrdProc, (void *)newfd) != 0)
{
PrintS( "pthread_create error." );
return -2;
}
PrintS( "start pthread_create for send log file." );
// for receive data
if (pthread_create( &tid1, NULL, SendLogInfoThrdProc, (void *)newfd) != 0)
{
PrintS( "pthread_create error." );
return -2;
}
return 0;
}
// for router assist
int RouterAssistServer(void*para)
{
int fd;
PrintS( "Server start..." );
// Create Socket fd
gListenFd = fd = nServerTcp( GatewayPort );
if ( fd < 0 )
{
PrintS( "create error\n" );
return -1;
}
while (1)
{
int ret;
int newfd;
PrintS( "Accept..." );
gClientFd = newfd = nAccept( fd );
dPrintInt( newfd );
if ( newfd < 0 )
{
//dPrintInt( errno );
//dPrintInt( EINTR );
//perror( "accept error\n" );
//if ( newfd==-2 )
// continue;
return -3;
}
// Send print info data
//TimerStartEx( 5000, TimerHandle );
ret = ProcessEvent( newfd );
dPrintInt( ret );
if ( ret==0 )
dPrintS("=====Transsmision Success=====");
else
{
close( newfd );
//close( fd );
PrintInt ( ret );
PrintS("=====Transsmision Error=====");
}
}
close( fd );
TimerClose();
PrintS( "Server End" );
}
void TimerHandle_SIGALRM(int x)
{
dPrintS( "SIGALRM" );
}
void TimerHandle_SIGINT(int x)
{
dPrintS( "SIGINT" );
exit(0);
}
void TimerHandle_SIGHUP(int x)
{
dPrintS( "SIGHUP" );
exit(0);
}
void TimerHandle_SIGKILL(int x)
{
dPrintS( "SIGKILL" );
exit(0);
}
void TimerHandle_SIGQUIT(int x)
{
dPrintS( "TimerHandle_SIGQUIT" );
exit(0);
}
void TimerHandle_SIGSEGV(int x)
{
dPrintS( "TimerHandle_SIGSEGV" );
exit(0);
}
void TimerHandle_SIGTERM(int x)
{
dPrintS( "TimerHandle_SIGTERM" );
exit(0);
}
void TimerHandle_SIGCHLD(int x)
{
dPrintS( "TimerHandle_SIGCHLD" );
exit(0);
}
void TimerHandle_SIGCONT(int x)
{
dPrintS( "TimerHandle_SIGCONT" );
exit(0);
}
void TimerHandle_SIGSTOP(int x)
{
dPrintS( "TimerHandle_SIGSTOP" );
exit(0);
}
void TimerHandle_SIGTSTP(int x)
{
dPrintS( "TimerHandle_SIGTSTP" );
exit(0);
}
int main(int argc, char *argv[])
{
int ret;
// struct sigaction act;
printf("=====Start RouterAssistServer=====\n");
//signal( SIGALRM, TimerHandle_SIGALRM );
// //signal( SIGALRM, TimerHandle );
// act.sa_handler = TimerHandle;
// sigemptyset( &act.sa_mask );
// act.sa_flags = 0;
// sigaction( SIGALRM, &act, 0 );
signal( SIGINT, TimerHandle_SIGINT );
signal( SIGHUP, TimerHandle_SIGHUP );
signal( SIGKILL, TimerHandle_SIGKILL ); // can not catch
signal( SIGQUIT, TimerHandle_SIGQUIT );
signal( SIGSEGV, TimerHandle_SIGSEGV );
signal( SIGTERM, TimerHandle_SIGTERM );
signal( SIGCHLD, TimerHandle_SIGCHLD );
signal( SIGSTOP, TimerHandle_SIGSTOP ); // can not catch
signal( SIGTSTP, TimerHandle_SIGTSTP );
/* Ignore broken pipes */
//signal(SIGPIPE, SIG_IGN);
ret = RouterAssistServer(NULL);
dPrintInt( ret );
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -