Junichi Katsu / Mbed OS mbed-os-example-milkcocoa

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTSocket.h Source File

MQTTSocket.h

00001 #if !defined(MQTTSOCKET_H)
00002 #define MQTTSOCKET_H
00003 
00004 #include "MQTTmbed.h"
00005 #include "TCPSocket.h"
00006 
00007 class MQTTSocket
00008 {
00009 public:
00010     int open(NetworkInterface* nif)
00011     {
00012         return socket.open(nif);
00013     }
00014     
00015     int connect(char* hostname, int port, int timeout=1000)
00016     {
00017         socket.set_blocking (false);
00018         socket.set_timeout(timeout);
00019         _mutex.lock();
00020         int ret = (int)socket.connect(hostname, port);
00021         _mutex.unlock();
00022         
00023         return ret;
00024     }
00025 
00026     int read(unsigned char* buffer, int len, int timeout)
00027     {
00028         socket.set_blocking (false);
00029         socket.set_timeout(timeout);
00030         _mutex.lock();
00031         int ret = (int)socket.recv((char*)buffer, len);
00032         _mutex.unlock();
00033         
00034         return ret;
00035     }
00036     
00037     int write(unsigned char* buffer, int len, int timeout)
00038     {
00039         socket.set_blocking (false);
00040         socket.set_timeout(timeout); 
00041         _mutex.lock();
00042         int ret = (int)socket.send((char*)buffer, len);
00043         _mutex.unlock();
00044         
00045         return ret;
00046     }
00047     
00048     int disconnect()
00049     {
00050         _mutex.lock();
00051         int ret = (int)socket.close();
00052         _mutex.unlock();
00053         
00054         return ret;
00055     }
00056     
00057 private:
00058     Mutex _mutex;
00059     TCPSocket socket;
00060     
00061 };
00062 
00063 
00064 
00065 #endif