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

📄 tcplisten.cpp

📁 本源码为移动公司话费查询中间件TUXEDO使用的实例
💻 CPP
字号:


#include "TCPListen.h"
#include "atmi.h"

TPINIT *tpinitbuf;

/******************class CTCPListen*********************/
CTCPListen::CTCPListen()
{
}

CTCPListen::~CTCPListen()
{

	CSHPPSessionInterface* pshppSession;
	//释放映射链中的各个元素
	for(int i = 0; i<m_shppSessionMap.GetNumEntries();i++)
	{
		pshppSession = m_shppSessionMap.GetTableEntry(i);
		pshppSession->Join();
		delete pshppSession;
	}

#ifdef _WIN32
	::CoUninitialize();
#endif

}

SInt64 CTCPListen::Run()
{

	
	KN_Error     knError = KN_OK;
	CSHPPSocket  sConnect;
	CSHPPSession *pSession;
	CSHPPSessionTemp *pSessionTemp;

//	CSockAddr    saClient;
	sockaddr_in saClient;

	char         pClientIP[20];
	CStrPtrLen   splClientIP;
	CStrPtrLen   splIsLine("0");
	CDBAdmin     dbAdmin;
	long         lPosNO,lBusinessId;

//	UInt32       ClinetAddr;
//	CSockAddr saTcpServ(INADDR_ANY,3000);//可从文件中读取
	sockaddr_in saTcpServ;
	UInt16 nPost;

	int nTemp;
	int nSet;

#ifdef _WIN32
	::CoInitialize(NULL);
#endif

//	knError = dbAdmin.DBConnectDatabase("hebyd","known","known");
	knError = dbAdmin.DBConnectDatabase("known","known","known");
	if(knError != KN_OK)
	{
		printf("连接数据库失败,服务器无法启动\r\n");
#ifdef _WIN32
		::CoUninitialize();
#endif
		return knError;
	}
	else{
		nPost = 5000;
		printf("print server post = %d\n",nPost);		
		
		tpinitbuf = (TPINIT *)tpalloc("TPINIT", NULL, TPINITNEED(0));
		tpinitbuf->flags = TPMULTICONTEXTS;  				
	}


	try{
#ifdef _WIN32
		memset(&saTcpServ,0,sizeof(struct sockaddr_in));
#else
		bzero(&saTcpServ,sizeof(struct sockaddr_in));
#endif 
		saTcpServ.sin_family = AF_INET;
		saTcpServ.sin_port = htons(nPost );
		saTcpServ.sin_addr.s_addr = htonl(INADDR_ANY); 

		this->Create();
		SOCKET tsock = (SOCKET)this->m_hSocket;
		printf("start Create == %d\r\n",this->m_hSocket);

		//设置接受缓冲区大小
		bool bKeepalive = true;
		nTemp = 240 * 1024;
#ifdef _WIN32
		nSet = setsockopt(tsock,SOL_SOCKET,SO_RCVBUF,(char*)&nTemp,sizeof(nTemp));
#else
		nSet = setsockopt(tsock,SOL_SOCKET,SO_RCVBUF,(UInt16*)&nTemp,sizeof(nTemp));
#endif
		//2小时内没有数据交换,给对方发 保持存活探测分节
		if(nSet!=0)
		{
			return nSet;
		}
//		nSet = setsockopt(tsock,SOL_SOCKET,SO_KEEPALIVE,(char*)&bKeepalive,sizeof(bKeepalive));
		this->Bind((LPCSOCKADDR)&saTcpServ);
		printf("start Bind\r\n");
		this->Listen();// start listening

		printf("start listening\r\n");

		UInt32 nSessionID = 0;
		nTemp = 0;
		while( 1)
		{
	
			this->Accept(sConnect, (LPCSOCKADDR)&saClient);
			//获得IP
			strcpy(pClientIP , inet_ntoa(saClient.sin_addr));//saClient.DottedDecimal();

			splClientIP.Set(pClientIP);
			//查找SHPP Session
			printf("Map:m_pchPtr = %s,m_unLen = %d\n",splClientIP.m_pchPtr,splClientIP.m_unLen);
			pSession = m_shppSessionMap.Map(&splClientIP);
			if(pSession)
			{
				if(pSession->IsRun() == 1)
				{
					pSessionTemp = m_shppSessionMapTemp.Map(&splIsLine);
					if(pSessionTemp == NULL)
					{
						nTemp++;

						pSessionTemp = new CSHPPSessionTemp(&sConnect,&dbAdmin);
						m_shppSessionMapTemp.Add(pSessionTemp,pSessionTemp->GetSplInLine());
					}
					else
					{
//						pSessionTemp->Join();
						pSessionTemp->SetSocket(&sConnect);
					}

					pSessionTemp->SetError( shppSystemErr );
					pSessionTemp->SetClientIP(pClientIP);
					pSessionTemp->Start(1);
					continue;

				}
				else
				{
//					pSession->Join();
					nSessionID++;
					pSession->SetSessionID(nSessionID); //新的请求就要有新的ID
					pSession->SetSocket(&sConnect);
				}
			}
			else
			{
				char chBusines[2][FIELDBUFFER];

#ifndef _WIN32
				pthread_mutex_lock(&CSHPPSessionInterface::db_mutex);
#else
				WaitForSingleObject(CSHPPSessionInterface::db_mutex,INFINITE);
#endif //_WIN32
				printf("pClienIP = %s,DBQueryPosByIp11\n",pClientIP);
				char chArea[20],chFont[10];
				memset(chArea,0,20);
				memset(chFont,0,10);
				chBusines[0][0]=0;
				chBusines[1][0]=0;
				knError = dbAdmin.DBQueryPosByIp(pClientIP,lPosNO,lBusinessId,chBusines[0],chBusines[1],
					chArea,	chFont);
#ifndef _WIN32
				pthread_mutex_unlock(&CSHPPSessionInterface::db_mutex);
#else
				ReleaseMutex(CSHPPSessionInterface::db_mutex);
#endif //_WIN32
//				knError = 0 ;
				printf("knError = %d,pClienIP = %s:%s,\n",knError,pClientIP,	chBusines[0]);

				if(knError != KN_OK)
				{
					pSessionTemp = m_shppSessionMapTemp.Map(&splIsLine);
					if(pSessionTemp == NULL)
					{
						nTemp++;
						pSessionTemp = new CSHPPSessionTemp(&sConnect,&dbAdmin);
						m_shppSessionMapTemp.Add(pSessionTemp,pSessionTemp->GetSplInLine());
					}
					else
					{
//						pSessionTemp->Join();
						pSessionTemp->SetSocket(&sConnect);
					}

					printf("IP = %s,----CSHPPSessionTemp = %d开始登入\n",pClientIP,nTemp);
					pSessionTemp->SetError(shppRegisterErr);
					pSessionTemp->SetClientIP(pClientIP);
					pSessionTemp->Start(1);
					continue;
				}
				nSessionID++;
//				try{
					pSession = new CSHPPSession(&sConnect,&dbAdmin,nSessionID,lPosNO,pClientIP);
//				}
/*				catch(...)
				{
					printf("========================\n");
					printf("*****申请pSession 失败**\n");
					printf("========================\n");
					//100002200020000000000000000000000000000000010029系统资源忙稍后再试
					char buf[256];
					int num=sprintf(buf,"%s\r\n","100002200010000000000000000000000000000000010029系统资源忙稍后再试");
					sConnect.Send(buf,num,60000);
					sConnect.Cleanup();
					continue;
				}
*/			/*	if( pSession == NULL )
				{
					printf("========================\n");
					printf("#####申请pSession 失败##\n");
					printf("========================\n");
					char buf[256];
					int num=sprintf(buf,"%s\r\n","100002200010000000000000000000000000000000010029系统资源忙稍后再试");
					sConnect.Send(buf,num,60000);
					sConnect.Cleanup();
					continue;
				}
				*/
				pSession->SetPrintFont(chFont);
				pSession->SetPrintArea(chArea);
				pSession->SetBusinessTitle(chBusines[0]);
				pSession->SetBusinessAddr(chBusines[1]);
				pSession->InitSysParam(&dbAdmin);
				m_shppSessionMap.Add(pSession,pSession->GetClientIP());
			}

			pSession->Start(1);
//等待修改
		}
	}
#ifdef _DEBUG
	catch(CSocketException* pe)
	{
		char text[200];
		pe->GetErrorMessage(text);
		delete pe;
		this->Cleanup();

		knError = KN_SocketError;
	}
#else
	catch(...)//KN_Error error)
	{
//		knError = error;

		printf("Accept1:网络出错\n");
//		return error;
	}
#endif
	dbAdmin.DBDisconnectDatabase();
	printf("Accept2:网络出错%d\n",knError);

	this->Close();

#ifdef _WIN32
	::CoUninitialize();
#endif

	return knError;
}

//***************CSchedulerPrint************************
CSchedulerPrint::CSchedulerPrint(COSHashTable<CSHPPSession,CStrPtrLen> *pSessionMapHead,UInt16 *pnIsLive)
{
	m_pnIsLive = pnIsLive;
	m_pSessionMapHead = pSessionMapHead;
}

SInt64	CSchedulerPrint::Run()
{
	int nLink;
	CSHPPSession *pSession;
	while(*m_pnIsLive)
	{
		nLink = m_pSessionMapHead->GetNumEntries();
		for(int i = 0;i< nLink;i++)
		{
			pSession = m_pSessionMapHead->GetTableEntry(i);
		}

//		sleep(60);
	}
	return 0;
}

⌨️ 快捷键说明

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