Version to make it easier to reuse without source modifications
MQTTSocket.h@0:5cd4781e0c88, 2018-03-27 (annotated)
- Committer:
- JMF
- Date:
- Tue Mar 27 17:26:35 2018 +0000
- Revision:
- 0:5cd4781e0c88
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:5cd4781e0c88 | 1 | #if !defined(MQTTSOCKET_H) |
JMF | 0:5cd4781e0c88 | 2 | #define MQTTSOCKET_H |
JMF | 0:5cd4781e0c88 | 3 | |
JMF | 0:5cd4781e0c88 | 4 | #include "MQTTmbed.h" |
JMF | 0:5cd4781e0c88 | 5 | #include <Timer.h> |
JMF | 0:5cd4781e0c88 | 6 | |
JMF | 0:5cd4781e0c88 | 7 | class MQTTSocket |
JMF | 0:5cd4781e0c88 | 8 | { |
JMF | 0:5cd4781e0c88 | 9 | public: |
JMF | 0:5cd4781e0c88 | 10 | MQTTSocket(NetworkInterface *anet) : net(anet) |
JMF | 0:5cd4781e0c88 | 11 | { |
JMF | 0:5cd4781e0c88 | 12 | open = false; |
JMF | 0:5cd4781e0c88 | 13 | } |
JMF | 0:5cd4781e0c88 | 14 | |
JMF | 0:5cd4781e0c88 | 15 | int connect(char* hostname, int port, int timeout=1000) |
JMF | 0:5cd4781e0c88 | 16 | { |
JMF | 0:5cd4781e0c88 | 17 | if (open) |
JMF | 0:5cd4781e0c88 | 18 | disconnect(); |
JMF | 0:5cd4781e0c88 | 19 | nsapi_error_t rc = mysock.open(net); |
JMF | 0:5cd4781e0c88 | 20 | open = true; |
JMF | 0:5cd4781e0c88 | 21 | mysock.set_blocking(true); |
JMF | 0:5cd4781e0c88 | 22 | mysock.set_timeout((unsigned int)timeout); |
JMF | 0:5cd4781e0c88 | 23 | rc = mysock.connect(hostname, port); |
JMF | 0:5cd4781e0c88 | 24 | mysock.set_blocking(false); // blocking timeouts seem not to work |
JMF | 0:5cd4781e0c88 | 25 | return rc; |
JMF | 0:5cd4781e0c88 | 26 | } |
JMF | 0:5cd4781e0c88 | 27 | |
JMF | 0:5cd4781e0c88 | 28 | // common read/write routine, avoiding blocking timeouts |
JMF | 0:5cd4781e0c88 | 29 | int common(unsigned char* buffer, int len, int timeout, bool read) |
JMF | 0:5cd4781e0c88 | 30 | { |
JMF | 0:5cd4781e0c88 | 31 | timer.start(); |
JMF | 0:5cd4781e0c88 | 32 | mysock.set_blocking(false); // blocking timeouts seem not to work |
JMF | 0:5cd4781e0c88 | 33 | int bytes = 0; |
JMF | 0:5cd4781e0c88 | 34 | bool first = true; |
JMF | 0:5cd4781e0c88 | 35 | do |
JMF | 0:5cd4781e0c88 | 36 | { |
JMF | 0:5cd4781e0c88 | 37 | if (first) |
JMF | 0:5cd4781e0c88 | 38 | first = false; |
JMF | 0:5cd4781e0c88 | 39 | else |
JMF | 0:5cd4781e0c88 | 40 | wait_ms(timeout < 100 ? timeout : 100); |
JMF | 0:5cd4781e0c88 | 41 | int rc; |
JMF | 0:5cd4781e0c88 | 42 | if (read) |
JMF | 0:5cd4781e0c88 | 43 | rc = mysock.recv((char*)buffer, len); |
JMF | 0:5cd4781e0c88 | 44 | else |
JMF | 0:5cd4781e0c88 | 45 | rc = mysock.send((char*)buffer, len); |
JMF | 0:5cd4781e0c88 | 46 | if (rc < 0) |
JMF | 0:5cd4781e0c88 | 47 | { |
JMF | 0:5cd4781e0c88 | 48 | if (rc != NSAPI_ERROR_WOULD_BLOCK) |
JMF | 0:5cd4781e0c88 | 49 | { |
JMF | 0:5cd4781e0c88 | 50 | bytes = -1; |
JMF | 0:5cd4781e0c88 | 51 | break; |
JMF | 0:5cd4781e0c88 | 52 | } |
JMF | 0:5cd4781e0c88 | 53 | } |
JMF | 0:5cd4781e0c88 | 54 | else |
JMF | 0:5cd4781e0c88 | 55 | bytes += rc; |
JMF | 0:5cd4781e0c88 | 56 | } |
JMF | 0:5cd4781e0c88 | 57 | while (bytes < len && timer.read_ms() < timeout); |
JMF | 0:5cd4781e0c88 | 58 | timer.stop(); |
JMF | 0:5cd4781e0c88 | 59 | return bytes; |
JMF | 0:5cd4781e0c88 | 60 | } |
JMF | 0:5cd4781e0c88 | 61 | |
JMF | 0:5cd4781e0c88 | 62 | /* returns the number of bytes read, which could be 0. |
JMF | 0:5cd4781e0c88 | 63 | -1 if there was an error on the socket |
JMF | 0:5cd4781e0c88 | 64 | */ |
JMF | 0:5cd4781e0c88 | 65 | int read(unsigned char* buffer, int len, int timeout) |
JMF | 0:5cd4781e0c88 | 66 | { |
JMF | 0:5cd4781e0c88 | 67 | return common(buffer, len, timeout, true); |
JMF | 0:5cd4781e0c88 | 68 | } |
JMF | 0:5cd4781e0c88 | 69 | |
JMF | 0:5cd4781e0c88 | 70 | int write(unsigned char* buffer, int len, int timeout) |
JMF | 0:5cd4781e0c88 | 71 | { |
JMF | 0:5cd4781e0c88 | 72 | return common(buffer, len, timeout, false); |
JMF | 0:5cd4781e0c88 | 73 | } |
JMF | 0:5cd4781e0c88 | 74 | |
JMF | 0:5cd4781e0c88 | 75 | int disconnect() |
JMF | 0:5cd4781e0c88 | 76 | { |
JMF | 0:5cd4781e0c88 | 77 | open = false; |
JMF | 0:5cd4781e0c88 | 78 | return mysock.close(); |
JMF | 0:5cd4781e0c88 | 79 | } |
JMF | 0:5cd4781e0c88 | 80 | |
JMF | 0:5cd4781e0c88 | 81 | /*bool is_connected() |
JMF | 0:5cd4781e0c88 | 82 | { |
JMF | 0:5cd4781e0c88 | 83 | return mysock.is_connected(); |
JMF | 0:5cd4781e0c88 | 84 | }*/ |
JMF | 0:5cd4781e0c88 | 85 | |
JMF | 0:5cd4781e0c88 | 86 | private: |
JMF | 0:5cd4781e0c88 | 87 | |
JMF | 0:5cd4781e0c88 | 88 | bool open; |
JMF | 0:5cd4781e0c88 | 89 | TCPSocket mysock; |
JMF | 0:5cd4781e0c88 | 90 | NetworkInterface *net; |
JMF | 0:5cd4781e0c88 | 91 | Timer timer; |
JMF | 0:5cd4781e0c88 | 92 | |
JMF | 0:5cd4781e0c88 | 93 | }; |
JMF | 0:5cd4781e0c88 | 94 | |
JMF | 0:5cd4781e0c88 | 95 | #endif |