Beispiel #1
0
static int mqttClient_write(mqttClient_t* clientData, int length)
{
  int bytes = length ? length:clientData->session.tx.bytesLeft;
  le_result_t rc = LE_OK;

  LE_ASSERT(clientData);

  clientData->session.cmdLen = length;

  if (clientData->session.sock == MQTT_CLIENT_INVALID_SOCKET)
  {
    rc = mqttClient_connect(clientData);
    if (rc)
    {
      LE_ERROR("mqttClient_connect() failed(%d)", errno);
      rc = LE_IO_ERROR;
      goto cleanup;
    }
  }
  else
  {
    while (bytes > 0)
    {
      mqttClient_dumpBuffer(clientData->session.tx.ptr, bytes);
      int sent = write(clientData->session.sock, clientData->session.tx.ptr, bytes);
      if (sent == -1)
      {
        if (errno == EAGAIN)
        {
          le_fdMonitor_Enable(clientData->session.sockFdMonitor, POLLOUT);
          clientData->session.tx.bytesLeft = bytes;
          LE_WARN("send blocked(%u)", clientData->session.tx.bytesLeft);
          goto cleanup;
        }
        else
        {
          LE_ERROR("write() failed(%d)", errno);
          rc = LE_IO_ERROR;
          goto cleanup;
        }
      }
      else
      {
        clientData->session.tx.ptr += sent;
        bytes -= sent;
      }
    }

    le_timer_Restart(clientData->session.pingTimer);

    clientData->session.tx.ptr = clientData->session.tx.buf;
    clientData->session.rx.ptr = clientData->session.rx.buf;
  }

cleanup:
  return rc;
}
//--------------------------------------------------------------------------------------------------
LE_SHARED le_result_t le_comm_Connect (void* handle)
{
    HandleRecord_t* connectionRecordPtr = (HandleRecord_t*) handle;
    int result = LE_OK;

    LE_INFO("Connecting AF_INET socket, fd %d .........", connectionRecordPtr->fd);

#ifndef SOCKET_SERVER
    struct sockaddr_in sockAddr;

    sockAddr.sin_addr.s_addr = inet_addr(NetworkSocketIpAddress);
    sockAddr.sin_family = AF_INET;
    sockAddr.sin_port = htons(NetworkSocketTCPListeningPort);

    do
    {
        result = connect(connectionRecordPtr->fd, (struct sockaddr *) &sockAddr, sizeof(sockAddr));
    }
    while ((result == -1) && (errno == EINTR));

    if (result != 0)
    {
        if (FdMonitorRef != NULL)
        {
            // Disable FD Monitoring
            le_fdMonitor_Disable(FdMonitorRef, PollingEvents);
        }

        switch (errno)
        {
            case EACCES:
                return LE_NOT_PERMITTED;

           case ECONNREFUSED:
                return LE_NOT_FOUND;

            case EINPROGRESS:
                return LE_WOULD_BLOCK;

            default:
                LE_ERROR("Connect failed with errno %d", errno);
                return LE_FAULT;
        }
    }
#endif

    if (FdMonitorRef != NULL)
    {
        // Enable FD Monitoring
        le_fdMonitor_Enable(FdMonitorRef, PollingEvents);
    }

    LE_INFO("Connecting AF_INET socket, fd %d ......... [DONE]", connectionRecordPtr->fd);
    return result;
}