Exemplo n.º 1
0
void
UnixSocketImpl::Accept()
{
  MOZ_ASSERT(!NS_IsMainThread());
  mTask = nullptr;

  if (!mConnector) {
    NS_WARNING("No connector object available!");
    return;
  }

  // This will set things we don't particularly care about, but it will hand
  // back the correct structure size which is what we do care about.
  if (!mConnector->CreateAddr(true, mAddrSize, mAddr, nullptr)) {
    NS_WARNING("Cannot create socket address!");
    return;
  }

  if (mFd.get() < 0)
  {
    mFd = mConnector->Create();
    if (mFd.get() < 0) {
      return;
    }

    if (!SetNonblockFlags()) {
      return;
    }

    if (bind(mFd.get(), (struct sockaddr*)&mAddr, mAddrSize)) {
#ifdef DEBUG
      LOG("...bind(%d) gave errno %d", mFd.get(), errno);
#endif
      return;
    }

    if (listen(mFd.get(), 1)) {
#ifdef DEBUG
      LOG("...listen(%d) gave errno %d", mFd.get(), errno);
#endif
      return;
    }

  }

  int client_fd;
  client_fd = accept(mFd.get(), (struct sockaddr*)&mAddr, &mAddrSize);
  if (client_fd < 0) {
    EnqueueTask(SOCKET_RETRY_TIME_MS, new SocketAcceptTask(this));
    return;
  }

  if (!mConnector->SetUp(client_fd)) {
    NS_WARNING("Could not set up socket!");
    return;
  }
  mFd.reset(client_fd);

  nsRefPtr<OnSocketEventTask> t =
    new OnSocketEventTask(this, OnSocketEventTask::CONNECT_SUCCESS);
  NS_DispatchToMainThread(t);

  SetUpIO();
}
Exemplo n.º 2
0
void CSessionThread::ReceiverRun(void)
{
	struct packet_s in_pkt;

	if (m_stop||!m_open) { CloseSession(); return; }

	++m_receiver_thread_instances;

	CSingleLock GateKeeper(&m_ReceiverGateKeeper, TRUE);

	//AddEvent(EVENT_SENT, 0, _T("Fila de entrada ativada"));

	while (TRUE)
	{
		if (m_stop||!m_open) break;

		int retcode = Receive(in_pkt);
		if (!retcode)
		{
			if (!m_stop) ForceClose();
			break;
		}
		WORD wType = ntohs(in_pkt.header.type);

		/////////// DEBUG STUFF
		/*
		{
			static WORD wPreviousType = 0;
			if (wPreviousType != wType)
			{
				wPreviousType = wType;
				AddEvent(EVENT_RECEIVED, 0, _T("[0x%04lx] Received"), (LONG)wType);
			}
		}
		*/

		if (wType == PKT_ABORT)
		{
			AddEvent(EVENT_RECEIVED, 0, _T("Sessão encerrada pelo servidor"));
			SendABORTACK();
			ForceClose();
			break;
		}
		else if (wType == PKT_ABORT_ACK)
		{
			AddEvent(EVENT_RECEIVED, 0, _T("Fim de sessão confirmado"));
			ForceClose();
			break;
		}
		else if (wType == PKT_EOJ)
		{
			AddEvent(EVENT_RECEIVED, 0, _T("Fim da recepção"));
			EnqueueTask(in_pkt);
			break;
		}
		else if (wType == PKT_EOJ_ACK)
		{
			AddEvent(EVENT_RECEIVED, 0, _T("Fim da transmissão confirmado"));
			CloseSession();
			break;
		}
		else
		{
			EnqueueTask(in_pkt);
		}
	}

	--m_receiver_thread_instances;
}
Exemplo n.º 3
0
void
UnixSocketImpl::Accept()
{
  socklen_t addr_sz;
  struct sockaddr addr;

  if (!mConnector) {
    NS_WARNING("No connector object available!");
    return;
  }

  // This will set things we don't particularly care about, but it will hand
  // back the correct structure size which is what we do care about.
  mConnector->CreateAddr(true, addr_sz, &addr, nullptr);

  if(mFd.get() < 0)
  {
    mFd = mConnector->Create();
    if (mFd.get() < 0) {
      return;
    }

    if (!SetNonblockFlags()) {
      return;
    }

    if (bind(mFd.get(), &addr, addr_sz)) {
#ifdef DEBUG
      LOG("...bind(%d) gave errno %d", mFd.get(), errno);
#endif
      return;
    }

    if (listen(mFd.get(), 1)) {
#ifdef DEBUG
      LOG("...listen(%d) gave errno %d", mFd.get(), errno);
#endif
      return;
    }

  }

  int client_fd;
  client_fd = accept(mFd.get(), &addr, &addr_sz);
  if (client_fd < 0) {
    EnqueueTask(SOCKET_RETRY_TIME_MS, new SocketAcceptTask(this));
    return;
  }

  if (!mConnector->SetUp(client_fd)) {
    NS_WARNING("Could not set up socket!");
    return;
  }
  mFd.reset(client_fd);

  nsRefPtr<OnSocketEventTask> t =
    new OnSocketEventTask(this, OnSocketEventTask::CONNECT_SUCCESS);
  NS_DispatchToMainThread(t);

  // Due to the fact that we've dispatched our OnConnectSuccess message before
  // starting reading, we're guaranteed that any subsequent read tasks will
  // happen after the object has been notified of a successful connect.
  XRE_GetIOMessageLoop()->PostTask(FROM_HERE,
                                   new StartImplReadingTask(this));
}
Exemplo n.º 4
0
void CSessionThread::SessionRun()
{
	struct packet_s out_pkt;

	ClearBuffer(out_pkt);

	if (m_ask_cobranca)
	{
		out_pkt.header.type = htons(PKT_ASK_REFILL_COBRANCA);
		EnqueueMessage(out_pkt);
	}
	
	if (m_ask_planos)
	{
		out_pkt.header.type = htons(PKT_ASK_REFILL_PLANOS);
		EnqueueMessage(out_pkt);
	}

	CDBBlocos dbBlocos;
	if (m_ask_bloco || !dbBlocos.HaveBlocosReserva())
	{
		out_pkt.header.type = htons(PKT_ASK_BLOCO);
		EnqueueMessage(out_pkt);
	}
	
	if (m_ask_refill_clientes)
	{
		out_pkt.header.type = htons(PKT_ASK_REFILL_CLIENTES);
		EnqueueMessage(out_pkt);
	}
	else
	{
		out_pkt.header.type = htons(PKT_ASK_NOVOS_CLIENTES);
		EnqueueMessage(out_pkt);
	}

	if (m_ask_refill_produtos)
	{
		out_pkt.header.type = htons(PKT_ASK_REFILL_PRODUTOS);
		EnqueueMessage(out_pkt);
	}
	else
	{
		out_pkt.header.type = htons(PKT_ASK_NOVOS_PRODUTOS);
		EnqueueMessage(out_pkt);

		out_pkt.header.type = htons(PKT_ASK_NOVOS_PRECOS);
		EnqueueMessage(out_pkt);

		out_pkt.header.type = htons(PKT_ASK_NOVOS_SALDOS);
		EnqueueMessage(out_pkt);
	}
	
	if (m_ask_refill_titulos)
	{
		out_pkt.header.type = htons(PKT_ASK_REFILL_TITULOS);
		EnqueueMessage(out_pkt);
	}
	else
	{
		out_pkt.header.type = htons(PKT_ASK_NOVOS_TITULOS);
		EnqueueMessage(out_pkt);
	}

	//Solicitações locais
	if (m_snd_pedidos)
	{
		out_pkt.header.type = htons(PKT_SND_PEDIDO);
		EnqueueTask(out_pkt);
	}
	if (m_snd_clientes)
	{
		/*
		out_pkt.header.type = htons(PKT_SND_NOVO_CLIENTE);
		EnqueueTask(out_pkt);
		out_pkt.header.type = htons(PKT_SND_ATUALIZA_CLIENTE);
		EnqueueTask(out_pkt);
		*/
	}

	//Fim das solicitações
	out_pkt.header.type = htons(PKT_EOQ);
	EnqueueMessage(out_pkt);

	TriggerDispatcherThread();
	TriggerReceiverThread();
}