コード例 #1
0
//--------------------------------------------------------------------------------------------------
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;
}
コード例 #2
0
//--------------------------------------------------------------------------------------------------
LE_SHARED le_result_t le_comm_Disconnect (void* handle)
{
    HandleRecord_t* connectionRecordPtr = (HandleRecord_t*) handle;
    if (FdMonitorRef != NULL)
    {
        // Disable FD Monitoring
        le_fdMonitor_Disable(FdMonitorRef, PollingEvents);
    }

    le_hashmap_Remove(HandleRecordByFileDescriptor, (void*)(intptr_t) connectionRecordPtr->fd);

    close(connectionRecordPtr->fd);
    connectionRecordPtr->fd = -1;

    return LE_OK;
}
コード例 #3
0
ファイル: mqttClient.c プロジェクト: CoRfr/mangOH-MqttClient
static void mqttClient_socketFdEventHandler(int sockFd, short events)
{
  mqttClient_t* clientData = le_fdMonitor_GetContextPtr();
  int rc = LE_OK;

  LE_ASSERT(clientData);

  LE_DEBUG("events(0x%08x)", events);
  if (events & POLLOUT)
  {
    le_fdMonitor_Disable(clientData->session.sockFdMonitor, POLLOUT);

    if ((clientData->session.sock != MQTT_CLIENT_INVALID_SOCKET) && !clientData->session.isConnected)
    {
      LE_INFO("connected(%s:%d)", clientData->session.config.brokerUrl, clientData->session.config.portNumber);
      rc = le_timer_Stop(clientData->session.connTimer);
      if (rc)
      {
        LE_ERROR("le_timer_Stop() failed(%d)", rc);
        goto cleanup;
      }

      MQTTPacket_connectData data = MQTTPacket_connectData_initializer;       
      data.willFlag = 0;
      data.MQTTVersion = MQTT_CLIENT_MQTT_VERSION;

      LE_INFO("deviceId('%s'), pwd('%s')", clientData->deviceId, clientData->session.secret);
      if (strlen(clientData->deviceId) > 0)
      {
        data.clientID.cstring = clientData->deviceId;
        data.username.cstring = clientData->deviceId;
        data.password.cstring = clientData->session.secret;
      }

      data.keepAliveInterval = clientData->session.config.keepAlive;
      data.cleansession = 1;

      rc = mqttClient_sendConnect(clientData, &data);
      if (rc)
      {
        LE_ERROR("mqttClient_sendConnect() failed(%d)", rc);
        goto cleanup;
      }
    }
    else if (clientData->session.tx.bytesLeft)
    {
      rc = mqttClient_write(clientData, 0);
      if (rc)
      {
        LE_ERROR("mqttClient_write() failed(%d)", rc);
        goto cleanup;
      }
    }
  }
  else if (events & POLLIN)
  {
    uint16_t packetType = MQTTPacket_read(clientData->session.rx.buf, sizeof(clientData->session.rx.buf), mqttClient_read);    
    LE_DEBUG("packet type(%d)", packetType);
    switch (packetType)
    {
    case CONNACK:
      rc = mqttClient_processConnAck(clientData);
      if (rc)
      {
        LE_ERROR("mqttClient_processConnAck() failed(%d)", rc);
        goto cleanup;
      }

      break;

    case PUBACK:
      rc = mqttClient_processPubAck(clientData);
      if (rc)
      {
        LE_ERROR("mqttClient_processPubAck() failed(%d)", rc);
        goto cleanup;
      }

      break;

    case SUBACK:
      rc = mqttClient_processSubAck(clientData);
      if (rc)
      {
        LE_ERROR("mqttClient_processSubAck() failed(%d)", rc);
        goto cleanup;
      }

      break;

    case UNSUBACK:
      rc = mqttClient_processUnSubAck(clientData);
      if (rc)
      {
        LE_ERROR("mqttClient_processUnSubAck() failed(%d)", rc);
        goto cleanup;
      }

      break;

    case PUBLISH:
      rc = mqttClient_processPublish(clientData);
      if (rc)
      {
        LE_ERROR("mqttClient_processPublish() failed(%d)", rc);
        goto cleanup;
      }

      break;
    
    case PUBREC:
      rc = mqttClient_processPubRec(clientData);
      if (rc)
      {
        LE_ERROR("mqttClient_processPubRec() failed(%d)", rc);
        goto cleanup;
      }
  
      break;
    
    case PUBCOMP:
      rc = mqttClient_processPubComp(clientData);
      if (rc)
      {
        LE_ERROR("mqttClient_processPubComp() failed(%d)", rc);
        goto cleanup;
      }

      break;

    case PINGRESP:
      mqttClient_processPingResp(clientData);
      break;

    default:
      LE_ERROR("unknown packet type(%u)", packetType);
      break;
    }

    clientData->session.cmdRetries = 0;
  }

cleanup:
  return;
}