示例#1
0
BaseProtocol *InboundHTTP4RTMP::Bind(string sid) {
	BaseProtocol *pResult = NULL;
	if (_pNearProtocol == NULL) {
		//14. This might be a new connection. Do we have that sid generated?
		if (!MAP_HAS1(_generatedSids, sid)) {
			FATAL("Invalid sid: %s", STR(sid));
			return false;
		}

		//15. See if we have to generate a new connection or we just pick up
		//a disconnected one
		if (MAP_HAS1(_protocolsBySid, sid)) {
			pResult = ProtocolManager::GetProtocol(_protocolsBySid[sid]);
		} else {
			pResult = new InboundRTMPProtocol();
			pResult->Initialize(GetCustomParameters());
			pResult->SetApplication(GetApplication());
			_protocolsBySid[sid] = pResult->GetId();
			SetNearProtocol(pResult);
			pResult->SetFarProtocol(this);
		}
	} else {
		pResult = _pNearProtocol;
	}

	return pResult;
}
bool InboundRTMPSDiscriminatorProtocol::BindSSL(IOBuffer &buffer) {
	//1. Create the RTMP protocol
	BaseProtocol *pRTMP = new InboundRTMPProtocol();
	if (!pRTMP->Initialize(GetCustomParameters())) {
		FATAL("Unable to create RTMP protocol");
		pRTMP->EnqueueForDelete();
		return false;
	}

	//2. Destroy the link
	BaseProtocol *pFar = _pFarProtocol;
	pFar->ResetNearProtocol();
	ResetFarProtocol();

	//3. Create the new links
	pFar->SetNearProtocol(pRTMP);
	pRTMP->SetFarProtocol(pFar);

	//4. Set the application
	pRTMP->SetApplication(GetApplication());

	//5. Enqueue for delete this protocol
	EnqueueForDelete();

	//6. Process the data
	if (!pRTMP->SignalInputData(buffer)) {
		FATAL("Unable to process data");
		pRTMP->EnqueueForDelete();
	}

	return true;
}
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;
}
示例#4
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;
}