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

📄 cnserver.cpp

📁 这是一个远程控制程序
💻 CPP
字号:
//---------------------------------------------------------------------------

#include "CNServer.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)
// Constructor/destructor
CNServer::CNServer(UINT port )
{
	// Initialise some important stuffs...
	m_socketConn = NULL;
	m_port = port;

	// General options
	m_loopbackOnly = FALSE;

	// Clear the client mapping table
	for (int x=0; x<MAX_CLIENTS; x++)
		m_clientmap[x] = NULL;
	m_nextid = 0;

	// Signal set when a client quits
	m_clientquitsig = new omni_condition(&m_clientsLock);
}
CNServer::~CNServer()
{
	// If there is a socket_conn object then delete it
	if (m_socketConn != NULL)
	{
		delete m_socketConn;
		m_socketConn = NULL;
	}

	if (m_clientquitsig != NULL)
	{
		delete m_clientquitsig;
		m_clientquitsig = NULL;
	}
}

BOOL CNServer::LoopbackOnly()
{
	return m_loopbackOnly;
}
BOOL CNServer::SetLoopbackOnly(BOOL loopbackOnly)
{
	if (loopbackOnly != m_loopbackOnly)
	{
		m_loopbackOnly = loopbackOnly;
		BOOL socketConn = SockConnected();
		SockConnect(FALSE);
		SockConnect(socketConn);
	}
	return TRUE;
}

// Socket connection handling
BOOL CNServer::SockConnect(BOOL On)
{
	// Are we being asked to switch socket connects on or off?
	if (On)
	{
		// Is there a listening socket?
		if (m_socketConn == NULL)
		{
			m_socketConn = new CNSockConnect();
			if (m_socketConn == NULL)
				return FALSE;
                }
                // No autoportselect
                if (!m_socketConn->Init(this, m_port))
                {
                        delete m_socketConn;
                        m_socketConn = NULL;
                        return FALSE;
                }
        }
        else
        {
		// Is there a listening socket?
		if (m_socketConn != NULL)
		{
			// Close the socket
			delete m_socketConn;
			m_socketConn = NULL;
		}
        }
	return TRUE;
}
BOOL CNServer::SockConnected()
{
	return m_socketConn != NULL;
}

CNServerClientId CNServer::AddClient(VSocket *socket)
{
	CNServerClient *client;

	omni_mutex_lock l(m_clientsLock);

	// Try to allocate a client id...
	CNServerClientId clientid = m_nextid;
	do
	{
		clientid = (clientid+1) % MAX_CLIENTS;
		if (clientid == m_nextid)
		{
			delete socket;
			return -1;
		}
	}
	while (m_clientmap[clientid] != NULL);

	// Create a new client and add it to the relevant client list
	client = new CNServerClient();
	if (client == NULL) {
		delete socket;
		return -1;
	}
	// Start the client
	if (!client->Init(this, socket,clientid))
	{
		delete client;
		return -1;
	}
	m_clientmap[clientid] = client;

	return clientid;

}
void CNServer::RemoveClient(CNServerClientId clientid)
{
        omni_mutex_lock l(m_clientsLock);
        m_clientmap[clientid] = NULL;
}
void CNServer::SetPort(const UINT port)
{
        if (m_port != port)
        {
                /////////////////////////////////
                // Adjust the listen socket
                // Set the port number to use
                m_port = port;

                // If there is already a listening socket then close and re-open it...
                BOOL socketon = SockConnected();

                SockConnect(FALSE);
                if (socketon)
                        SockConnect(TRUE);

        }
}
UINT CNServer::GetPort()
{
	return m_port;
}

⌨️ 快捷键说明

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