Esempio n. 1
0
    void CServer::Start()
    {
        m_pZMQSocket = new zmq::socket_t(*m_pZMQContext, ZMQ_REP);
        m_pZMQSocket4StopServer = new zmq::socket_t(*m_pZMQContext, ZMQ_REP);
        m_pZMQSocket->bind(m_szListeningAddr.c_str());
        m_pZMQSocket4StopServer->bind(ARAB_SERVER_STOP_SOCKET);

        zmq::pollitem_t items[] = {
                {*m_pZMQSocket,0,ZMQ_POLLIN,0},
                {*m_pZMQSocket4StopServer,0,ZMQ_POLLIN,0}
        };
        size_t lenOfItem = sizeof(items)/sizeof(items[0]);
        while (1)
        {
            zmq::poll(items,lenOfItem,-1);
            if(items[0].revents & ZMQ_POLLIN)
            {
                OnRPCMessage();
            }
            if(items[1].revents & ZMQ_POLLIN)
            {
                OnStopServer();
            }
        }
    }
BOOL CxServerSocketTCP::StopServer()
{
	if ( m_ThreadContext.hThread != NULL )
	{
		WSASetEvent( m_hCleanupEvent );
		if ( WAIT_OBJECT_0 != ::WaitForSingleObject( m_ThreadContext.hThread, 3000 ) )
		{
			::TerminateThread( m_ThreadContext.hThread, 0 );
		}
		::CloseHandle( m_ThreadContext.hThread );
		m_ThreadContext.hThread = NULL;
	}

	if ( m_hCleanupEvent != WSA_INVALID_EVENT )
		::WSACloseEvent( m_hCleanupEvent );
	m_hCleanupEvent = WSA_INVALID_EVENT;

	if ( m_ThreadContext.hNetEvent[1] != WSA_INVALID_EVENT )
		WSACloseEvent( m_ThreadContext.hNetEvent[1] );
	m_ThreadContext.hNetEvent[1] = WSA_INVALID_EVENT;

	if ( m_ThreadContext.hSocket != INVALID_SOCKET )
	{
		char szBye[3] = { 0, };
		send( m_ThreadContext.hSocket, szBye, 3, 0 );
		shutdown( m_ThreadContext.hSocket, SD_BOTH );
		closesocket( m_ThreadContext.hSocket );
		m_ThreadContext.hSocket = INVALID_SOCKET;
	}

	m_bStarted = FALSE;

	OnStopServer();

	return TRUE;
}