コード例 #1
0
bool UnixDomainSocketAcceptor::Accept() {
  struct sockaddr_un remoteAddress;
  memset(&remoteAddress, 0, sizeof (struct sockaddr_un));
  socklen_t len = sizeof (struct sockaddr_un);
  int32_t fd;
  int32_t error;

  //1. Accept the connection
  fd = accept(_inboundFd, (sockaddr *)&remoteAddress, &len);
  error = LASTSOCKETERROR;
  if (fd < 0) {
    FATAL("Unable to accept UX client connection: %s (%d)", strerror(error), error);
    return false;
  }
  if (!_enabled) {
    CLOSE_SOCKET(fd);
    _droppedCount++;
    WARN("Acceptor is not enabled. UX Client dropped: %s", STR(_sockPath));
    return true;
  }
  INFO("Client connected: %s", STR(_sockPath));

  //if (!setFdOptions(fd, false)) {
  //  FATAL("Unable to set unix socket options");
  //  CLOSE_SOCKET(fd);
  //  return false;
  //}

  //2. Create the chain
  BaseProtocol *pProtocol = ProtocolFactoryManager::CreateProtocolChain(
      _protocolChain, _parameters);
  if (pProtocol == NULL) {
    FATAL("Unable to create protocol chain");
    CLOSE_SOCKET(fd);
    return false;
  }

  //3. Create the carrier and bind it
  UnixDomainSocketCarrier *pUnixDomainSocketCarrier = new UnixDomainSocketCarrier(fd, _sockPath);
  pUnixDomainSocketCarrier->SetProtocol(pProtocol->GetFarEndpoint());
  pProtocol->GetFarEndpoint()->SetIOHandler(pUnixDomainSocketCarrier);

  //4. Register protocol for thread access
  UnixDomainSocketManager::RegisterUXThreadProtocol(pUnixDomainSocketCarrier->GetSocketName(),
      (UnixDomainSocketProtocol *)pUnixDomainSocketCarrier->GetProtocol());

  //5. Register the protocol stack with an application
  if (_pApplication != NULL) {
    pProtocol = pProtocol->GetNearEndpoint();
    pProtocol->SetApplication(_pApplication);
  }

  if (pProtocol->GetNearEndpoint()->GetOutputBuffer() != NULL)
    pProtocol->GetNearEndpoint()->EnqueueForOutbound();

  _acceptedCount++;

  return true;
}
コード例 #2
0
bool TCPAcceptor::Accept() {
    sockaddr address;
    memset(&address, 0, sizeof (sockaddr));
    socklen_t len = sizeof (sockaddr);
    int32_t fd;
    int32_t error;

    //1. Accept the connection
    fd = accept(_inboundFd, &address, &len);
    error = errno;
    if (fd < 0) {
        FATAL("Unable to accept client connection: %s (%d)", strerror(error), error);
        return false;
    }
    if (!_enabled) {
        CLOSE_SOCKET(fd);
        _droppedCount++;
        WARN("Acceptor is not enabled. Client dropped: %s:%"PRIu16" -> %s:%"PRIu16,
             inet_ntoa(((sockaddr_in *) & address)->sin_addr),
             ENTOHS(((sockaddr_in *) & address)->sin_port),
             STR(_ipAddress),
             _port);
        return true;
    }
    INFO("Client connected: %s:%"PRIu16" -> %s:%"PRIu16,
         inet_ntoa(((sockaddr_in *) & address)->sin_addr),
         ENTOHS(((sockaddr_in *) & address)->sin_port),
         STR(_ipAddress),
         _port);

    if (!setFdOptions(fd)) {
        FATAL("Unable to set socket options");
        CLOSE_SOCKET(fd);
        return false;
    }

    //4. Create the chain
    BaseProtocol *pProtocol = ProtocolFactoryManager::CreateProtocolChain(_protocolChain, _parameters);
    if (pProtocol == NULL) {
        FATAL("Unable to create protocol chain");
        CLOSE_SOCKET(fd);
        return false;
    }

    //5. Create the carrier and bind it
    TCPCarrier *pTCPCarrier = new TCPCarrier(fd);
    pTCPCarrier->SetProtocol(pProtocol->GetFarEndpoint());
    pProtocol->GetFarEndpoint()->SetIOHandler(pTCPCarrier);

    //6. Register the protocol stack with an application
    if (_pApplication != NULL) {
        pProtocol = pProtocol->GetNearEndpoint();
        pProtocol->SetApplication(_pApplication);
    }

    if (pProtocol->GetNearEndpoint()->GetOutputBuffer() != NULL)
        pProtocol->GetNearEndpoint()->EnqueueForOutbound();

    _acceptedCount++;

    //7. Done
    return true;
}
コード例 #3
0
ファイル: api.cpp プロジェクト: 2php/crtmpserver-1
void EnvRun(string ip, uint16_t port, int logLevel)
#endif /* ANDROID */
{
	//1. Initialize the logger
	logLevel = logLevel < 0 ? 0 : logLevel;
	logLevel = logLevel >= _FINEST_ ? _FINEST_ : logLevel;
	Logger::Init();
	BaseLogLocation *pLogLocation = NULL;
#ifdef ANDROID
	pLogLocation = new LogCatLogLocation();
	pLogLocation->SetLevel(logLevel);
	Logger::AddLogLocation(pLogLocation);
#else
	pLogLocation = new ConsoleLogLocation(true);
	pLogLocation->SetLevel(logLevel);
	Logger::AddLogLocation(pLogLocation);
#endif /* ANDROID */

#ifdef BUILD_SIGNATURE
	//1.1 Create the settings string
	FATAL("Build signature:\n%s", BUILD_SIGNATURE);
#endif /* BUILD_SIGNATURE */

	//2. Create the default protocol factory
	DefaultProtocolFactory *pFactory = new DefaultProtocolFactory();
	if (!ProtocolFactoryManager::RegisterProtocolFactory(pFactory)) {
		ASSERT("Unable to register default protocols factory");
	}

	//3. Initialize the I/O
	IOHandlerManager::Initialize();

	//4. Create the AppleStreamingClientApplication application
	Variant configuration;
	configuration[CONF_APPLICATION_NAME] = "applestreaminclient";
	configuration[CONF_APPLICATION_ALIASES].PushToArray("asc");
	configuration[CONF_APPLICATION_DEFAULT] = (bool)true;
	configuration[CONF_APPLICATION_VALIDATEHANDSHAKE] = (bool)true;
	configuration[CONF_APPLICATION_KEYFRAMESEEK] = (bool)true;
	configuration[CONF_APPLICATION_CLIENTSIDEBUFFER] = (int32_t) 15;
	configuration[CONF_APPLICATION_SEEKGRANULARITY] = 1.00;
	configuration[CONF_APPLICATION_MEDIAFOLDER] = "./";
	configuration[CONF_APPLICATION_GENERATE_META_FILES] = (bool)false;
	configuration["rtspHost"] = format("rtsp://127.0.0.1:%hu/", port);
	AppleStreamingClientApplication *pApp = new AppleStreamingClientApplication(
			configuration);
#ifdef ANDROID
	pApp->SetJavaCallBackInterface(ci);
#endif /* ANDROID */
	if (!pApp->Initialize()) {
		ASSERT("Unable to initialize the application");
	}

	//6. Register it to the app manager
	if (!ClientApplicationManager::RegisterApplication(pApp)) {
		ASSERT("Unable to register application");
	}

	//7. Create the RTSP acceptor
	Variant acceptorConfig;
	acceptorConfig[CONF_IP] = ip;
	acceptorConfig[CONF_PORT] = (uint16_t) port;
	acceptorConfig[CONF_PROTOCOL] = CONF_PROTOCOL_INBOUND_RTSP;
	vector<uint64_t> chain;
	chain = ProtocolFactoryManager::ResolveProtocolChain(acceptorConfig[CONF_PROTOCOL]);
	if (chain.size() == 0) {
		ASSERT("Invalid protocol chain: %s", STR(acceptorConfig[CONF_PROTOCOL]));
	}
	TCPAcceptor *pAcceptor = new TCPAcceptor(acceptorConfig[CONF_IP],
			(uint16_t) acceptorConfig[CONF_PORT], acceptorConfig, chain);
	if (!pAcceptor->StartAccept(pApp)) {
		ASSERT("Unable to fire up acceptor");
	}

	//8. Create the bin variant acceptor
	acceptorConfig[CONF_PORT] = (uint16_t) (port + 1);
	acceptorConfig[CONF_PROTOCOL] = CONF_PROTOCOL_INBOUND_BIN_VARIANT;
	chain = ProtocolFactoryManager::ResolveProtocolChain(acceptorConfig[CONF_PROTOCOL]);
	if (chain.size() == 0) {
		ASSERT("Invalid protocol chain: %s", STR(acceptorConfig[CONF_PROTOCOL]));
	}
	pAcceptor = new TCPAcceptor(acceptorConfig[CONF_IP], (uint16_t) acceptorConfig[CONF_PORT],
			acceptorConfig, chain);
	if (!pAcceptor->StartAccept(pApp)) {
		ASSERT("Unable to fire up acceptor");
	}

	//9. Create the xml variant acceptor
	acceptorConfig[CONF_PORT] = (uint16_t) (port + 2);
	acceptorConfig[CONF_PROTOCOL] = CONF_PROTOCOL_INBOUND_XML_VARIANT;
	chain = ProtocolFactoryManager::ResolveProtocolChain(acceptorConfig[CONF_PROTOCOL]);
	if (chain.size() == 0) {
		ASSERT("Invalid protocol chain: %s", STR(acceptorConfig[CONF_PROTOCOL]));
	}
	pAcceptor = new TCPAcceptor(acceptorConfig[CONF_IP], (uint16_t) acceptorConfig[CONF_PORT],
			acceptorConfig, chain);
	if (!pAcceptor->StartAccept(pApp)) {
		ASSERT("Unable to fire up acceptor");
	}

#ifdef HAS_PROTOCOL_RTMP
	//9. Create the RTMP acceptor
	acceptorConfig[CONF_PORT] = (uint16_t) (1935);
	acceptorConfig[CONF_PROTOCOL] = CONF_PROTOCOL_INBOUND_RTMP;
	chain = ProtocolFactoryManager::ResolveProtocolChain(acceptorConfig[CONF_PROTOCOL]);
	if (chain.size() == 0) {
		ASSERT("Invalid protocol chain: %s", STR(acceptorConfig[CONF_PROTOCOL]));
	}
	pAcceptor = new TCPAcceptor(acceptorConfig[CONF_IP], (uint16_t) acceptorConfig[CONF_PORT],
			acceptorConfig, chain);
	if (!pAcceptor->StartAccept(pApp)) {
		ASSERT("Unable to fire up acceptor");
	}
#endif /* HAS_PROTOCOL_RTMP */

	//10. Create the timer UDP protocol
#ifdef HAS_MS_TIMER
	acceptorConfig[CONF_PORT] = (uint16_t) (port + 3);
	acceptorConfig[CONF_PROTOCOL] = "fineTimer";
	acceptorConfig["FineTimerPeriod"] = 0.2;
	chain = ProtocolFactoryManager::ResolveProtocolChain(acceptorConfig[CONF_PROTOCOL]);
	if (chain.size() == 0) {
		ASSERT("Invalid protocol chain: %s", STR(acceptorConfig[CONF_PROTOCOL]));
	}
	UDPCarrier *pUDPCarrier = UDPCarrier::Create(acceptorConfig[CONF_IP], (uint16_t) acceptorConfig[CONF_PORT]);
	if (pUDPCarrier == NULL) {
		ASSERT("Unable to bind on udp://%s:%hu", STR(acceptorConfig[CONF_IP]), (uint16_t) acceptorConfig[CONF_PORT]);
	}
	BaseProtocol *pTimer = ProtocolFactoryManager::CreateProtocolChain(chain, acceptorConfig);
	pTimer->GetFarEndpoint()->SetIOHandler(pUDPCarrier);
	pUDPCarrier->SetProtocol(pTimer->GetFarEndpoint());
	pApp->SetFineTimerId(pTimer->GetId());
#endif /* HAS_MS_TIMER */

	inet_aton("127.0.0.1", &gAddress.sin_addr);
	memset(&gAddress, 0, sizeof (gAddress));
	gAddress.sin_family = AF_INET;
	gAddress.sin_port = EHTONS(port + 1);

	//10. Run
	while (IOHandlerManager::Pulse()) {
		IOHandlerManager::DeleteDeadHandlers();
		ProtocolManager::CleanupDeadProtocols();
	}

	//11. Shutting down protocols manager;
	ProtocolManager::Shutdown();
	ProtocolManager::CleanupDeadProtocols();

	//12. Shutting down I/O handlers manager
	IOHandlerManager::ShutdownIOHandlers();
	IOHandlerManager::DeleteDeadHandlers();
	IOHandlerManager::Shutdown();

	//13. Unregister and delete default protocol handler
	ProtocolFactoryManager::UnRegisterProtocolFactory(pFactory);
	delete pFactory;
	pFactory = NULL;

	//14. Shutting down applications
	ClientApplicationManager::Shutdown();

	//15. Shutting down the logger leaving you in the dark. Bye bye
	Logger::Free(true);
}