mbed mqtt

Dependencies:   TextLCD MQTT IAP

MQTTNetwork.h

Committer:
ncshy
Date:
2018-11-29
Revision:
2:562744909841
Parent:
1:91e33a7fe0b5

File content as of revision 2:562744909841:

#ifndef _MQTTNETWORK_H_
#define _MQTTNETWORK_H_

#include "NetworkInterface.h"

class MQTTNetwork {
public:
    MQTTNetwork(NetworkInterface* aNetwork) : network(aNetwork) {
        socket = new TCPSocket();
    }

    ~MQTTNetwork() {
        delete socket;
    }

    int read(unsigned char* buffer, int len, int timeout) {
        socket->set_blocking(false);
        socket->set_timeout(timeout);
        int ret =  socket->recv(buffer, len);
        if (NSAPI_ERROR_WOULD_BLOCK == ret)
            return 0;
        else
            return ret;
    }

    int write(unsigned char* buffer, int len, int timeout) {
        return socket->send(buffer, len);
    }

    int connect(const char* hostname, int port) {
        socket->open(network);
        return socket->connect(hostname, port);
    }

    int disconnect() {
        return socket->close();
    }

private:
    NetworkInterface* network;
    TCPSocket* socket;
};

#endif // _MQTTNETWORK_H_