⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nettester.cpp

📁 跨操作系统的微型中间件
💻 CPP
字号:
#include <stdio.h>
#include <string.h>


#include "RunServer.h"
#include "OSHeaders.h"


void usage();

//检查类存泄漏
#ifdef WIN32
	#define _CRTDBG_MAP_ALLOC 
	#include <stdlib.h>
	#include <crtdbg.h>
#endif

#ifdef __linux__
	#include <sys/types.h>
	#include <sys/wait.h>
#endif

#ifdef WIN32
	#define DEFAULT_CONFIG_FILE	".\\config.xml"
#else
	#define DEFAULT_CONFIG_FILE	"./config.xml"
#endif


extern "C" void test();

#ifdef __NO_MAIN__
	int main_entry(int argc, char* argv[])
#else
	int main(int argc, char* argv[])
#endif
{
	test();

	Bool bDaemon = FALSE;
	Bool bFork = FALSE;

	//version
	if (argc > 1)
	{
		for (Int32 i = 1; i < argc; i++)
		{
			if (strcmp(argv[i],"-h") == 0)
			{
				usage();
				return 0;
			}
			else if (strcmp(argv[i],"-v") == 0)
			{
				EchoVersion();
				return 0;
			}
			else if (strcmp(argv[i],"-d") == 0)
			{
				bDaemon = TRUE;
			}
			else if (strcmp(argv[i],"-s") == 0)
			{
				bFork = TRUE;
			}
		}
	}

#ifdef WIN32
	//检查类存泄漏
	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
//	_CrtSetBreakAlloc(99);
	//end
#endif
	
#if __linux__
	//daemon
	if(bDaemon) 
	{	
		if(daemon(1,0) != 0) 
		{//redirect input output errput to /dev/null.
			printf("Change to daemon failed\n");
			return -1;
		}
	}

	//fork
	if(!bFork) 
	{
		//single process
		return RunServer(DEFAULT_CONFIG_FILE);
	}
	else {
		//multi-process
		while(TRUE) 
		{
			pid_t child;
			child = fork(); 
			if( child <0 ) 
			{
				printf("Fork child process failed\n");
			}
			else if( child == 0) 
			{
				//child
				bDaemon = TRUE;
				return RunServer(DEFAULT_CONFIG_FILE);
			}
			else
			{
				//parent
				int iStatus;
				int iRet = wait(&iStatus);
				if(iRet >=0 ) 
				{
					/*
						if child return error code, quit processes
						else restart child
					*/
					if(WIFEXITED(iStatus)) 
					{
						//child quit normally
						printf("Child process quit normally, restart it.\n");
					}
					if(WEXITSTATUS(iStatus)) 
					{
						//child quit with error code
						printf("Child process quit on error code[%d]!!!\n",iStatus);
						break;
					}
					if(WIFSIGNALED(iStatus)) 
					{
						printf("Child process terminate on exception!!! restart it\n");
					}
				} 
				else 
				{
					printf("Wait child failed\n");
					return -1;
				}
			}
		}
	}

#else

	return RunServer(DEFAULT_CONFIG_FILE);
#endif		

	return 0;
}

void usage()
{
	printf("Usage: StreamingServer [-h] [-v] [-s] [-d] \n"
		"	-h  : help information\n"
		"	-v  : version\n"
		"	-s  : single process\n"
		"	-d  : non-daemon\n"
		);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -