Boolean VHTTPConnectionListener::DoRun()
{
	XBOX::VError error = VE_OK;

	if (NULL != fRequestLogger)
		fRequestLogger->Log ('SRNT', 0, "SERVER_NET::VHTTPConnectionListener::DoRun()::Enter", 1);

	while (IsListening())
	{
        //jmo - Do not let errors accumulate in thread's error stack
        XBOX::VErrorContext errCtx(false, false);
        
		XTCPSock *	newConnection = fServerListener->GetNewConnectedSocket (1000 /*MsTimeout*/);

		if (NULL != newConnection)
		{
			XBOX::VTCPEndPoint *endPoint = new XBOX::VTCPEndPoint (newConnection, fSelectIOPool);

			if (testAssert (NULL != endPoint))
			{
				XBOX::VConnectionHandler * connectionHandler = fConnectionHandlerFactory->CreateConnectionHandler (error);

				if (testAssert ((NULL != connectionHandler) && (XBOX::VE_OK == error)))
				{
					connectionHandler->_ResetRedistributionCount();
					error = connectionHandler->SetEndPoint (endPoint);
					assert (XBOX::VE_OK == error);
				}

				/* Transfer connectionHandler to the thread pool for execution. */
				if (testAssert (NULL != fWorkerPool) && IsListening())
				{
					error = fWorkerPool->AddConnectionHandler (connectionHandler);
					assert (XBOX::VE_OK == error);
				}
			}
		}
	}

	DeInit();

	XBOX::VTask::GetCurrent()->Sleep (1000);

	if (NULL != fRequestLogger)
		fRequestLogger->Log ('SRNT', 0, "SERVER_NET::VHTTPConnectionListener::DoRun()::Exit", 1);

	return false;
}
Exemplo n.º 2
0
bool cServer::Start(void)
{
	for (auto port: m_Ports)
	{
		UInt16 PortNum;
		if (!StringToInteger(port, PortNum))
		{
			LOGWARNING("Invalid port specified for server: \"%s\". Ignoring.", port.c_str());
			continue;
		}
		auto Handle = cNetwork::Listen(PortNum, std::make_shared<cServerListenCallbacks>(*this, PortNum));
		if (Handle->IsListening())
		{
			m_ServerHandles.push_back(Handle);
		}
	}  // for port - Ports[]
	if (m_ServerHandles.empty())
	{
		LOGERROR("Couldn't open any ports. Aborting the server");
		return false;
	}
	if (!m_TickThread.Start())
	{
		return false;
	}
	return true;
}
Exemplo n.º 3
0
BOOL CNetwork::SendPush(GGUID* pGUID, DWORD nIndex)
{
	CSingleLock pLock( &Network.m_pSection );
	if ( ! pLock.Lock( 250 ) ) return TRUE;

	if ( ! IsListening() ) return FALSE;
	
	GGUID pGUID2 = *pGUID;
	SOCKADDR_IN pEndpoint;
	CNeighbour* pOrigin;
	int nCount = 0;
	
	while ( GetNodeRoute( &pGUID2, &pOrigin, &pEndpoint ) )
	{
		if ( pOrigin != NULL && pOrigin->m_nProtocol == PROTOCOL_G1 )
		{
			CG1Packet* pPacket = CG1Packet::New( G1_PACKET_PUSH,
				Settings.Gnutella1.MaximumTTL - 1 );
			
			pPacket->Write( pGUID, 16 );
			pPacket->WriteLongLE( nIndex );
			pPacket->WriteLongLE( m_pHost.sin_addr.S_un.S_addr );
			pPacket->WriteShortLE( htons( m_pHost.sin_port ) );
			
			pOrigin->Send( pPacket );
		}
		else
		{
			CG2Packet* pPacket = CG2Packet::New( G2_PACKET_PUSH, TRUE );
			
			pPacket->WritePacket( G2_PACKET_TO, 16 );
			pPacket->Write( pGUID, 16 );
			
			pPacket->WriteByte( 0 );
			pPacket->WriteLongLE( m_pHost.sin_addr.S_un.S_addr );
			pPacket->WriteShortBE( htons( m_pHost.sin_port ) );
			
			if ( pOrigin != NULL )
			{
				pOrigin->Send( pPacket );
			}
			else
			{
				Datagrams.Send( &pEndpoint, pPacket );
			}
		}
		
		pGUID2.n[15] ++;
		nCount++;
	}
	
	return nCount > 0;
}
Exemplo n.º 4
0
    bool Accept(TcpSocket& connection, bool blocking = true) const
    {
      if (!IsListening())
      {
        throw std::runtime_error("TcpSocket.Accept - Called on a socket that does not have listening mode enabled");
      }

      auto newSocket = SOCKET(0);
      auto address = sockaddr_in();
      auto tryAgain = bool();

      do
      {
        tryAgain = false;
        if (!Winsock::Accept(socket, address, newSocket))
        {
          switch (WSAGetLastError())
          {
          case WSAEWOULDBLOCK:
            //if (!IsBlocking())
            //{
            //  throw std::runtime_error("TcpSocket.Accept - Received would-block error for non-blocking socket");
            //}
            return false;

          case WSAECONNRESET:
            tryAgain = true;
            break;

          default:
            return false;
          }
        }
      } while (tryAgain);

      if (connection.IsOpen())
      {
        connection.Close();
      }

      connection.socket = newSocket;
      connection.isListening = false;

      if (!Winsock::IoctlSocket(connection.socket, blocking))
      {
        throw std::runtime_error("TcpSocket.Accept - Unable to set blocking mode on new connection");
      }
      connection.isBlocking = blocking;

      return true;
    }
Exemplo n.º 5
0
/*
 * This is a pretty expensive routine -- it loops through all the fd's,
 * and finds the active clients (and servers and opers) and places them
 * on the "busy client" list
 */
void check_fdlists()
{
#ifdef CLIENT_SERVER
#define BUSY_CLIENT(x)	(((x)->priority < 55) || (!lifesux && ((x)->priority < 75)))
#else
#define BUSY_CLIENT(x)	(((x)->priority < 40) || (!lifesux && ((x)->priority < 60)))
#endif
#define FDLISTCHKFREQ  2

aClient *cptr;
int i, j;

   j = 0;
   for (i = highest_fd; i >= 0; i--) 
   {
      if (!(cptr = local[i]))
	 continue;
      if (IsServer(cptr) || IsListening(cptr) || IsOper(cptr)) 
      {
	 busycli_fdlist.entry[++j] = i;
	 continue;
      }
      if (cptr->receiveM == cptr->lastrecvM) 
      {
	 cptr->priority += 2;	/* lower a bit */
	 if (cptr->priority > 90)
	    cptr->priority = 90;
	 else if (BUSY_CLIENT(cptr))
	    busycli_fdlist.entry[++j] = i;
	 continue;
      }
      else 
      {
	 cptr->lastrecvM = cptr->receiveM;
	 cptr->priority -= 30;	/* active client */
	 if (cptr->priority < 0) 
	 {
	    cptr->priority = 0;
	    busycli_fdlist.entry[++j] = i;
	 }
	 else if (BUSY_CLIENT(cptr))
	    busycli_fdlist.entry[++j] = i;
      }
   }
   busycli_fdlist.last_entry = j;	/* rest of the fdlist is garbage */
/*   return (now + FDLISTCHKFREQ + (lifesux + 1)); */
}
Exemplo n.º 6
0
/** Binds cLuaServerHandle::IsListening */
static int tolua_cServerHandle_IsListening(lua_State * L)
{
	// Function signature:
	// ServerInstance:IsListening() -> bool

	cLuaState S(L);
	if (
		!S.CheckParamSelf("cServerHandle") ||
		!S.CheckParamEnd(2)
	)
	{
		return 0;
	}

	// Get the server handle:
	auto Srv = *static_cast<cLuaServerHandle **>(lua_touserdata(L, 1));
	ASSERT(Srv != nullptr);  // Checked by CheckParamSelf()

	// Query it:
	S.Push(Srv->IsListening());
	return 1;
}
Exemplo n.º 7
0
bool cHTTPServer::Start(cCallbacks & a_Callbacks, const AStringVector & a_Ports)
{
	m_Callbacks = &a_Callbacks;

	// Open up requested ports:
	for (auto port : a_Ports)
	{
		UInt16 PortNum;
		if (!StringToInteger(port, PortNum))
		{
			LOGWARNING("WebServer: Invalid port value: \"%s\". Ignoring.", port.c_str());
			continue;
		}
		auto Handle = cNetwork::Listen(PortNum, std::make_shared<cHTTPServerListenCallbacks>(*this, PortNum));
		if (Handle->IsListening())
		{
			m_ServerHandles.push_back(Handle);
		}
	}  // for port - a_Ports[]
	
	// Report success if at least one port opened successfully:
	return !m_ServerHandles.empty();
}
Exemplo n.º 8
0
bool cHTTPServer::Start(cCallbacks & a_Callbacks, const AStringVector & a_Ports)
{
	m_Callbacks = &a_Callbacks;

	// Open up requested ports:
	AStringVector ports;
	for (auto port : a_Ports)
	{
		UInt16 PortNum;
		if (!StringToInteger(port, PortNum))
		{
			LOGWARNING("WebServer: Invalid port value: \"%s\". Ignoring.", port.c_str());
			continue;
		}
		auto Handle = cNetwork::Listen(PortNum, std::make_shared<cHTTPServerListenCallbacks>(*this, PortNum));
		if (Handle->IsListening())
		{
			m_ServerHandles.push_back(Handle);
			ports.push_back(port);
		}
	}  // for port - a_Ports[]

	// Inform the admin about the ports opened:
	AString reportPorts;
	for (const auto & port: ports)
	{
		if (!reportPorts.empty())
		{
			reportPorts.append(", ");
		}
		reportPorts.append(port);
	}
	LOGINFO("WebAdmin is running on port(s) %s", reportPorts.c_str());

	// Report success if at least one port opened successfully:
	return !m_ServerHandles.empty();
}
Exemplo n.º 9
0
BOOL CNetwork::IsStable() const
{
	return IsListening() && ( Handshakes.m_nStableCount > 0 );
}