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

📄 transferthread.cpp

📁 关于联通的一个统一定制程序
💻 CPP
字号:
// TransferThread.cpp: implementation of the CTransferThread class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "TransferThread.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNCREATE(CTransferThread, CWinThread)

CTransferThread::CTransferThread()
{
}

CTransferThread::~CTransferThread()
{
}

BOOL CTransferThread::InitInstance()
{
	// TODO:  perform and per-thread initialization here
	return TRUE;
}

int CTransferThread::ExitInstance()
{
	// TODO:  perform any per-thread cleanup here
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CTransferThread, CWinThread)
	//{{AFX_MSG_MAP(CTransferThread)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTransferThread message handlers

int CTransferThread::Run()
{
	IOD_Info info;

	while (!theData.m_bServiceExit)
	{
		if (theData.GetTransferMsg(&info))
			Work(&info);
		else
			Sleep(1);
	}

	return CWinThread::Run();
}

void CTransferThread::Work(LPIOD_Info pInfo)
{
	CString szLog;

	TCHAR buf[1024];
	memset(&buf, 0x0, sizeof(buf));
	int index=4;
	long len=0;

	strcpy(buf+index, pInfo->szDestMobile);
	index+=strlen(pInfo->szDestMobile)+1;
	len=htonl(pInfo->lMsgLevel);
	memcpy(buf+index, &len, 4);
	index+=4;
	strcpy(buf+index, pInfo->szSubID);
	index+=strlen(pInfo->szSubID)+1;
	strcpy(buf+index, pInfo->szSrcTermID);
	index+=strlen(pInfo->szSrcTermID)+1;
	strcpy(buf+index, pInfo->szSrcGWID);
	index+=strlen(pInfo->szSrcGWID)+1;
	len=htonl(pInfo->lMsgFmt);
	memcpy(buf+index, &len, 4);
	index+=4;
	len=htonl(pInfo->lMsgLen);
	memcpy(buf+index, &len, 4);
	index+=4;
	memcpy(buf+index, pInfo->szContent, pInfo->lMsgLen);
	index+=pInfo->lMsgLen;
	len=htonl(index);
	memcpy(buf, &len, 4);

	SOCKET sock;
	sockaddr_in remote;

	remote.sin_family = AF_INET;
	remote.sin_port = htons(pInfo->nServicePort);
	remote.sin_addr.s_addr = inet_addr(pInfo->szServiceIP);

	do
	{
		sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
		if (INVALID_SOCKET == sock)
			break;

		if (connect(sock, (const struct sockaddr*)&remote, sizeof(remote)) != 0)
		{
			closesocket(sock);
			break;
		}

		if (send(sock, buf, index, 0) == SOCKET_ERROR)
		{
			closesocket(sock);
			break;
		}

		closesocket(sock);

		szLog.Format("向[%s:%d]发送数据包[%s:%s:%s]成功!", pInfo->szServiceIP, pInfo->nServicePort, pInfo->szDestMobile, pInfo->szSubID, pInfo->szContent);
		theData.WriteLog(szLog, "Xiaoxin");

		return;
	}while (FALSE);

	pInfo->nLife++;
	pInfo->lTime = CTime::GetCurrentTime().GetTime();
	if (pInfo->nLife >= theData.theCfgInfo.nMaxRetryTime)
	{
		pInfo->lMsgFmt = 15;
		lstrcpy(pInfo->szContent, _TEXT("对不起,系统正繁忙,请稍后再试!"));
		pInfo->lMsgLen = lstrlen(pInfo->szContent);
		lstrcpy(pInfo->szSubID, "CWXXI00000");
		lstrcpy(pInfo->szFeeCode, "0");
		lstrcpy(pInfo->szFeeType, "00");
		pInfo->nLife = 0;
		theData.AddOneSendMsg(pInfo);

		theData.WriteLog(LT_ISD, pInfo->szDestMobile, pInfo->szSubID, pInfo->lMsgFmt, pInfo->lMsgLen,
				pInfo->szContent, "", pInfo->szSrcGWID, pInfo->szSrcTermID, pInfo->lMsgLevel,
				"", pInfo->szFeeType, pInfo->szFeeCode, 0, "-trans");
	}
	else
		theData.AddTransferMsg(pInfo);
}

⌨️ 快捷键说明

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