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

📄 svc.cpp

📁 WIN NT/2000 聊天服务程序样例
💻 CPP
字号:
#include "stdhdr.h"
#include "main.h"
#include "svc.h"
#include "slot.h"

extern "C" BOOL bDebug;			// defined in SERVICE.C

CSvc::CSvc()
{
	m_pHead = NULL;
	InitializeCriticalSection(&cs);
}

CSvc::~CSvc()
{
	SendAll("Service shutdown in progress.");
	while (m_pHead != NULL) delete m_pHead;
}

void CSvc::AddSlot(CSlot *pSlot)
{
	EnterCriticalSection(&cs);
	CSlot **ppSlot = &m_pHead;
	while (*ppSlot != NULL) ppSlot = &((*ppSlot)->m_pNext);
	pSlot->m_pNext = NULL;
	*ppSlot = pSlot;
	pSlot->m_pSvc = this;
	LeaveCriticalSection(&cs);
	if (bDebug) cout << "Created slot #" << pSlot->GetId() << "." << endl;
}

void CSvc::RemoveSlot(CSlot *pSlot)
{
	EnterCriticalSection(&cs);
	CSlot **ppSlot = &m_pHead;
	while (*ppSlot != pSlot)
	{
		assert(*ppSlot != NULL);
		ppSlot = &((*ppSlot)->m_pNext);
	}
	*ppSlot = pSlot->m_pNext;
	pSlot->m_pNext = NULL;
	LeaveCriticalSection(&cs);
	if (bDebug) cout << "Destroyed slot #" << pSlot->GetId() << "." << endl;
}

void CSvc::SendAll(char *pBuf, CSlot *pSender, bool bSystem)
{
	char buf[1020], num[17];
	strcpy(buf, "[");
	if (!bSystem)
	{
		strcat(buf, itoa((pSender != NULL ? pSender->GetId() : 0), num, 10));
		strcat(buf, "] ");
	}
	strcat(buf, pBuf);
	if (bSystem) strcat(buf, "]");
	if (bDebug) cout << buf << endl;
	strcat(buf, "\n");

	EnterCriticalSection(&cs);
	CSlot *pSlot = m_pHead;
	while (pSlot != NULL)
	{
		if (pSlot != pSender)
		{
			pSlot->Send(buf);
		}
		pSlot = pSlot->m_pNext;
	}
	LeaveCriticalSection(&cs);
}

⌨️ 快捷键说明

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