Example #1
0
void* dabInputSlipThread(void* args)
{
    dabInputSlipData* data = (dabInputSlipData*)args;
    TcpSocket* client;

    while ((client = data->server->accept()) != NULL) {
        int size = 0;
        etiLog.log(info, "SLIP server got a new client.\n");

#ifdef _WIN32
        WaitForSingleObject(data->semWrite, INFINITE);
                WaitForSingleObject(data->semQueue, INFINITE);
#else
        sem_wait(&data->semWrite);
        sem_wait(&data->semQueue);
#endif
        UdpPacket* packet = data->packets[data->nbPackets];
#ifdef _WIN32
        ReleaseSemaphore(data->semQueue, 1, NULL);
#else
        sem_post(&data->semQueue);
#endif

        while ((size = client->read(packet->getData(), packet->getSize()))
                    > 0) {
            packet->setLength(size);
#ifdef _WIN32
            WaitForSingleObject(data->semQueue, INFINITE);
#else
            sem_wait(&data->semQueue);
#endif
            data->nbPackets++;
#ifdef _WIN32
            ReleaseSemaphore(data->semQueue, 1, NULL);
#else
            sem_post(&data->semQueue);
#endif
            
#ifdef _WIN32
            WaitForSingleObject(data->semWrite, INFINITE);
            WaitForSingleObject(data->semQueue, INFINITE);
#else
            sem_wait(&data->semWrite);
            sem_wait(&data->semQueue);
#endif
            packet = data->packets[data->nbPackets];
#ifdef _WIN32
            ReleaseSemaphore(data->semQueue, 1, NULL);
#else
            sem_post(&data->semQueue);
#endif
        }
        etiLog.log(info, "SLIP server client deconnected.\n");
        client->close();
    }
    etiLog.log(error, "SLIP thread can't accept new client (%s)\n",
            inetErrDesc, inetErrMsg);

    return NULL;
}
Example #2
0
/**
 *  Receive an UDP packet.
 *  @param packet The packet that will receive data. The address will be set
 *                to the source address.
 *  @return 0 if ok, -1 if error
 */
int UdpSocket::receive(UdpPacket &packet)
{
  TRACE_CLASS("UdpSocket", "receive(UdpPacket)");
  socklen_t addrSize;
  addrSize = sizeof(*packet.getAddress().getAddress());
  int ret = recvfrom(listenSocket, packet.getData(), packet.getSize() - packet.getOffset(), 0,
		     packet.getAddress().getAddress(), &addrSize);
  if (ret == SOCKET_ERROR) {
    packet.setLength(0);
#ifndef _WIN32
  if (errno == EAGAIN)
    return 0;
#endif
    setInetError("Can't receive UDP packet");
    return -1;
  }
  packet.setLength(ret);
  if (ret == (long)packet.getSize()) {
    packet.setSize(packet.getSize() << 1);
  }
  return 0;
}