cc
MQTTSocket.h@48:66826ea709ea, 2016-09-28 (annotated)
- Committer:
- mapellil
- Date:
- Wed Sep 28 15:34:12 2016 +0000
- Revision:
- 48:66826ea709ea
- Parent:
- 47:24f2bf2819a5
Code cleanup
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
icraggs | 31:a51dd239b78e | 1 | #if !defined(MQTTSOCKET_H) |
icraggs | 31:a51dd239b78e | 2 | #define MQTTSOCKET_H |
icraggs | 31:a51dd239b78e | 3 | |
icraggs | 43:21da1f744243 | 4 | #include "MQTTmbed.h" |
mapellil | 47:24f2bf2819a5 | 5 | #include "TCPSocket.h" |
icraggs | 31:a51dd239b78e | 6 | |
icraggs | 31:a51dd239b78e | 7 | class MQTTSocket |
icraggs | 31:a51dd239b78e | 8 | { |
mapellil | 47:24f2bf2819a5 | 9 | public: |
mapellil | 47:24f2bf2819a5 | 10 | |
mapellil | 47:24f2bf2819a5 | 11 | int open(NetworkStack *ipstack) |
mapellil | 47:24f2bf2819a5 | 12 | { |
mapellil | 47:24f2bf2819a5 | 13 | return mysock.open(ipstack); |
mapellil | 47:24f2bf2819a5 | 14 | } |
mapellil | 47:24f2bf2819a5 | 15 | |
icraggs | 31:a51dd239b78e | 16 | int connect(char* hostname, int port, int timeout=1000) |
icraggs | 31:a51dd239b78e | 17 | { |
mapellil | 47:24f2bf2819a5 | 18 | int err; |
mapellil | 47:24f2bf2819a5 | 19 | mysock.set_timeout(timeout); |
mapellil | 47:24f2bf2819a5 | 20 | err = mysock.connect(hostname, port); |
mapellil | 47:24f2bf2819a5 | 21 | // t.start(); |
mapellil | 47:24f2bf2819a5 | 22 | return err; |
icraggs | 31:a51dd239b78e | 23 | } |
icraggs | 31:a51dd239b78e | 24 | |
icraggs | 36:2f1ada427e56 | 25 | int read(unsigned char* buffer, int len, int timeout) |
icraggs | 31:a51dd239b78e | 26 | { |
mapellil | 47:24f2bf2819a5 | 27 | mysock.set_timeout(timeout); |
mapellil | 47:24f2bf2819a5 | 28 | //t.reset(); |
mapellil | 47:24f2bf2819a5 | 29 | // int start = t.read_ms(); |
mapellil | 47:24f2bf2819a5 | 30 | int rc = mysock.recv((char*)buffer, len); |
mapellil | 47:24f2bf2819a5 | 31 | // int stop = t.read_ms(); |
mapellil | 48:66826ea709ea | 32 | // if (rc>0) printf ("recv File: %s, Line: %d Read nB: %d rc: %d timeout: %d elaps: %d\n\r",__FILE__,__LINE__, len, rc, timeout, stop-start); |
mapellil | 47:24f2bf2819a5 | 33 | return rc; |
icraggs | 31:a51dd239b78e | 34 | } |
icraggs | 31:a51dd239b78e | 35 | |
icraggs | 36:2f1ada427e56 | 36 | int write(unsigned char* buffer, int len, int timeout) |
icraggs | 31:a51dd239b78e | 37 | { |
mapellil | 47:24f2bf2819a5 | 38 | mysock.set_timeout(timeout); |
mapellil | 47:24f2bf2819a5 | 39 | // mysock.set_blocking(false, timeout); |
mapellil | 47:24f2bf2819a5 | 40 | // mysock.set_blocking(false); |
mapellil | 47:24f2bf2819a5 | 41 | int rc = mysock.send((char*)buffer, len); |
mapellil | 47:24f2bf2819a5 | 42 | // printf ("send File: %s, Line: %d Write nB: %d rc: %d\n\r",__FILE__,__LINE__, len, rc); |
mapellil | 47:24f2bf2819a5 | 43 | return rc; |
icraggs | 31:a51dd239b78e | 44 | } |
icraggs | 31:a51dd239b78e | 45 | |
icraggs | 31:a51dd239b78e | 46 | int disconnect() |
icraggs | 31:a51dd239b78e | 47 | { |
mapellil | 47:24f2bf2819a5 | 48 | // t.stop(); |
icraggs | 31:a51dd239b78e | 49 | return mysock.close(); |
icraggs | 31:a51dd239b78e | 50 | } |
mapellil | 47:24f2bf2819a5 | 51 | |
icraggs | 31:a51dd239b78e | 52 | private: |
mapellil | 47:24f2bf2819a5 | 53 | TCPSocket mysock; |
mapellil | 47:24f2bf2819a5 | 54 | // Timer t; |
icraggs | 31:a51dd239b78e | 55 | |
icraggs | 31:a51dd239b78e | 56 | }; |
icraggs | 31:a51dd239b78e | 57 | #endif |