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

📄 myservice.cpp

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

#include "stdafx.h"
#include "MyService.h"
#include "RecThread.h"
#include "WorkThread.h"
#include "SendThread.h"
#include "TransferThread.h"
#include "0000Thread.h"

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

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

CMyService::CMyService(LPCTSTR lpServiceName, LPCTSTR lpDisplayName)
			: CNTService(lpServiceName, lpDisplayName)
{
	m_hEvent = 0;
}

CMyService::~CMyService()
{
}

void CMyService::Run(DWORD argc, LPTSTR *argv)
{
	// report to the SCM that we're about to start
	ReportStatus(SERVICE_START_PENDING);

	// the Service Control Manager (SCM) starts "Run" in a different thread than "Stop",
	// so we need something for syncronization.
	m_hEvent = ::CreateEvent(0, TRUE, FALSE, 0);
	if (0 == m_hEvent)
		return;

	if (!Start())
	{
		theData.WriteLog("服务启动失败!", _T("I199"));
		CloseHandle(m_hEvent);
		return;
	}

	theData.WriteLog("服务正常启动!");

	// report SERVICE_RUNNING immediately before you enter the main-loop
	// DON'T FORGET THIS!
	ReportStatus(SERVICE_RUNNING);

	// enter main-loop
	// If the Stop() method sets the event, then we will break out of this loop.
	long nTime = 0;
	while (::WaitForSingleObject(m_hEvent, 10) != WAIT_OBJECT_0)
	{
		long cTime = CTime::GetCurrentTime().GetTime();
		if (nTime == 0 || abs(cTime - nTime) >= 60)
		{
			MonitorProc();
			nTime = cTime;
		}
	}

	if (m_hEvent)
		::CloseHandle(m_hEvent);
}

void CMyService::Stop()
{
	// report to the SCM that we're about to stop
	// Note that the service might Sleep(), so we have to tell the SCM
	// "The next operation may take me up to 11 seconds. Please be patient."
	ReportStatus(SERVICE_STOP_PENDING, 11000);

	theData.m_bServiceExit = TRUE;

	theData.CloseSocket();
	Sleep(2);	// waiting for all sub-thread exiting.
	POSITION pos = m_ThreadList.GetHeadPosition();
	while (pos)
	{
		CWinThread* pThread = (CWinThread*)m_ThreadList.GetNext(pos);
		TerminateThread(pThread->m_hThread, 0);
		pThread->Delete();
	}
	Sleep(2);
	theData.WriteLog("服务停止!");
	theData.CloseLog();

	if( m_hEvent )
		::SetEvent(m_hEvent);

	ReportStatus(SERVICE_STOPPED);
}

BOOL CMyService::Start()
{
	theData.GetCfgInfo();

	if (!theData.InitLog())
	{
		cerr << _T("初始化日志模块失败!") << endl;
		return FALSE;
	}

	if (!theData.CreateSocket())
		return FALSE;

	BOOL bBuilt = FALSE;
	CWinThread* pThread = NULL;
	for (int i = 0; i < theData.theCfgInfo.nRecThread; i++)
	{
		pThread = AfxBeginThread(RUNTIME_CLASS(CRecThread), NULL);
		if (pThread != NULL)
		{
			bBuilt = TRUE;
			m_ThreadList.AddTail(pThread);
		}
	}
	if (!bBuilt)
	{
		theData.WriteLog("不能成功的建立任一接收线程!", _T("I198"));
		return FALSE;
	}
	theData.WriteLog("建立接收线程成功!");
	bBuilt = FALSE;
	for (i = 0; i < theData.theCfgInfo.nWorkThread; i++)
	{
		pThread = AfxBeginThread(RUNTIME_CLASS(CWorkThread), NULL);
		if (pThread != NULL)
		{
			bBuilt = TRUE;
			m_ThreadList.AddTail(pThread);
		}
	}
	if (!bBuilt)
	{
		theData.WriteLog("不能成功的建立任一工作线程!", _T("I198"));
		return FALSE;
	}
	theData.WriteLog("建立工作线程成功!");
	bBuilt = FALSE;
	for (i = 0; i < theData.theCfgInfo.nSendThread; i++)
	{
		pThread = AfxBeginThread(RUNTIME_CLASS(CSendThread), NULL);
		if (pThread != NULL)
		{
			bBuilt = TRUE;
			m_ThreadList.AddTail(pThread);
		}
	}
	if (!bBuilt)
	{
		theData.WriteLog("不能成功的建立任一发送线程!", _T("I198"));
		return FALSE;
	}
	theData.WriteLog("建立发送线程成功!");
	// Transfer
	bBuilt = FALSE;
	for (i = 0; i < theData.theCfgInfo.nWorkThread; i++)
	{
		pThread = AfxBeginThread(RUNTIME_CLASS(CTransferThread), NULL);
		if (pThread != NULL)
		{
			bBuilt = TRUE;
			m_ThreadList.AddTail(pThread);
		}
	}
	if (!bBuilt)
	{
		theData.WriteLog("不能成功的建立任一转发线程!", _T("I198"));
		return FALSE;
	}
	theData.WriteLog("建立转发线程成功!");
	// 0000
	bBuilt = FALSE;
	for (i = 0; i < theData.theCfgInfo.nWorkThread; i++)
	{
		pThread = AfxBeginThread(RUNTIME_CLASS(C0000Thread), NULL);
		if (pThread != NULL)
		{
			bBuilt = TRUE;
			m_ThreadList.AddTail(pThread);
		}
	}
	if (!bBuilt)
	{
		theData.WriteLog("不能成功的建立任一0000线程!", _T("I198"));
		return FALSE;
	}
	theData.WriteLog("建立0000线程成功!");

	theData.RegisterDispatcher();

	return TRUE;
}

UINT CMyService::MonitorProc()
{
	try
	{
		CString szFile;
		if (theData.theCfgInfo.szLogFolder[lstrlen(theData.theCfgInfo.szLogFolder) - 1] == '\\')
			szFile.Format("%s%s", theData.theCfgInfo.szLogFolder, MONITOR_File);
		else
			szFile.Format("%s\\%s", theData.theCfgInfo.szLogFolder, MONITOR_File);
		CFile file(szFile, CFile::modeCreate | CFile::shareDenyNone);
		file.Close();
	}
	catch (CFileException* e)
	{
		theData.WriteLog("操作空文件失败!", _T("I270"));
		e->Delete();
	}
	catch (...)
	{
	}

	return 0;
}

⌨️ 快捷键说明

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