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

📄 main.cpp

📁 linux 下的sgip 协议实现, 能够从server 接受短信内容, 并且能够发送短信给手机用户
💻 CPP
字号:
/****************************************************************
Copyright ?, 1989-2002, Hisunsray Tech. Co., Ltd.
File name: main.cpp
Author: lizhigang
Version:  V3.0.0.0#20030320-1
Date: 2003-03-20
Description:
History:
*****************************************************************/


#include "RTCStype.h"
#include "RTCSerror.h"
#include "RTCSserver.h"
#include "stdarg.h"
#include "SMGModule.h"
#include "PublicDefine.h"
#include "EMSNgor.h"
#include "RTCStime.h"
#include "SMASGIPObject.h"

#ifdef _LINUX
#include <pthread.h>
#include <ctype.h>
#endif

RTCS_Server *server	= NULL;
TSMGModule	*appservermodule = NULL;
TSMASGIP	*smgmodule = NULL;


#ifdef _LINUX
	pthread_t MainThreadID;
#else
	thread_t MainThreadID;
#endif

RTCS_Lock  ConsoleLock;

void CatchSignal(void);
void OnSignalProc(int signal);
RTCS_CTuint InitInstance(void);
void ExitInstance();

extern void PrintConsole(LPCTSTR ModuleName,char* fmt,...);

int main(void)
{
    int childpid;

    if(( childpid = fork()) < 0 )
        printf("Can't fork first child.\n");
    else if( childpid > 0 )
        exit(0);    //exit parent process

    if(setsid() == -1)  //release control terminal
        printf("Can't change process group.\n");

	CatchSignal();
	printf("SMG starting up ...\n");
	if( InitInstance() == RTCS_Error_OK ) 
	{
        #ifdef _LINUX
		MainThreadID = pthread_self();
	    #else
		MainThreadID = thr_self();
	    #endif

        sleep(1);
        //printf("Inintialization is OK!\n");
        #ifdef _LINUX
			//pthread_suspend(MainThreadID);
			while(1) sleep(1000);
	    #else
	    	thr_suspend(MainThreadID);
	    #endif
	}
    else
        printf("SMG Start Fail\n");
        
    printf("SMG Exit\n");
    ExitInstance() ;
    return 1;
}

void IgnoreSignal()
{
	signal(SIGINT, SIG_IGN);  //2
	signal(SIGHUP, SIG_IGN);  //1
	signal(SIGQUIT, SIG_IGN); //3
	signal(SIGABRT, SIG_IGN); //6
	signal(SIGTERM, SIG_IGN); //15
	signal(SIGUSR1, SIG_IGN); //16
}


void OnSignalProc(int s)
{
	IgnoreSignal();

    printf("Catch signal :%d\n", s);

	if(s == SIGUSR1)
	{
		printf("Snapshot().\n");
		if( appservermodule ) 
			appservermodule->Snapshot();

		CatchSignal();
		return;
	}

    printf("Programe will be exited...Thread id=%d\n", MainThreadID);
    if( MainThreadID ) 
	{
        #ifdef _LINUX
	#else
	thr_continue( MainThreadID );
	#endif

        sleep(1);
    }
}

void CatchSignal(void)
{
	signal(SIGINT, OnSignalProc);  //2
	signal(SIGHUP, OnSignalProc);  //1
	signal(SIGQUIT, OnSignalProc); //3
	signal(SIGABRT, OnSignalProc); //6
	signal(SIGTERM, OnSignalProc); //15
	signal(SIGUSR1, OnSignalProc); //16
}

RTCS_CTuint InitInstance()
{
	//读取通信服务器配置
	char ServerName[256] = "";
	char ClientName[256] = "";
	GetPrivateProfileString("SMGateway", "RTCS_STATION_NAME", "", ServerName, 256, MSGIniFileName);
	GetPrivateProfileString("SMGateway", "RTCS_MODULE_NAME", "", ClientName, 256, MSGIniFileName);

	char Title[256] = "";
	sprintf(Title, "$Version: 综合付费平台 - 短消息网关(%s) Version:%s$", ServerName, VERSION);
	printf("%s\n", Title);

	//启动通信服务器
	server = new RTCS_Server(ServerName, ServerIniFileName, RTCSLogDir);
	printf("消息服务器创建成功!!!\n");
	if(!server)
	{
		PrintConsole("应用服务器","消息服务器运行失败,请检查配置文件并重启。");
		return RTCS_Error_FAIL;
	}
	server->Run();
	PrintConsole("应用服务器","消息服务器运行成功");
	
	RTCS_CTuint ThreadNum=GetPrivateProfileInt("SMGateway", "THREADNUM", 5, MSGIniFileName);
	RTCS_CTuint QueueNum=GetPrivateProfileInt("SMGateway", "QUEUENUM", 100, MSGIniFileName);

	//创建流程解释模块
	appservermodule = new TSMGModule(ClientName);
	if(!appservermodule)
	{
		PrintConsole("应用服务器","应用服务器模块创建失败,请检查配置文件并重启。");
		return RTCS_Error_FAIL;
	}
	PrintConsole("应用服务器","应用服务器模块创建成功");
	appservermodule->UseBus(server);
	appservermodule->InsertBus();
	PrintConsole("应用服务器","应用服务器模块注册成功");

	//创建网关模块
	smgmodule = new TSMASGIP();
	if(!smgmodule)
	{
		PrintConsole("短消息网关","网关接口模块创建失败,请检查配置文件并重启。");
		return RTCS_Error_FAIL;
	}
	PrintConsole("短消息网关","网关接口模块创建成功");
	smgmodule->UseBus(server);
	smgmodule->InsertBus();
	PrintConsole("短消息网关","网关接口模块注册成功");
	
	return RTCS_Error_OK;
}

void ExitInstance()
{
	try
	{
		if(appservermodule)
		{
			appservermodule->DistachBus();
			Sleep(500);
			delete appservermodule;
			appservermodule = NULL;
			PrintConsole("应用服务器","应用服务器模块退出......");
		}

		if(smgmodule)
		{
			smgmodule->DistachBus();
			Sleep(500);
			delete smgmodule;
			smgmodule = NULL;
			PrintConsole("应用服务器","网关接口模块退出......");
		}

		if( server )
		{
			delete server;
			server = NULL;
		}
		PrintConsole("应用服务器","通信服务器退出......");
	}
	catch(...)
	{
	}
	return;
}

void PrintConsole(const char *ModuleName, char *fmt, ...)
{
	RTCS_SingleLock Lock(&ConsoleLock);
	Lock.Lock();

    va_list args;
    va_start(args, fmt);

	RTCS_CTime currenttime;
	RTCS_CTstring tmpstring = "";
	currenttime.Format( tmpstring );

    printf("%s %s  ", (LPCTSTR)tmpstring, ModuleName);
    vprintf(fmt, args);
    printf("\n");
    va_end(args);
}

char * strupr(char *mystr)
{
    char *p = mystr;
    while(*p)
    {
	*p = toupper(*p);
	p++;
    }
    
    return mystr; 
}

char * strlwr(char *mystr)
{
    char *p = mystr;
    while(*p)
    {
	*p = tolower(*p);
	p++;
    }

    return mystr;
}

⌨️ 快捷键说明

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