Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
util/SmartRestSocket.cpp@115:4eb3c7e945cf, 2015-05-18 (annotated)
- Committer:
- xinlei
- Date:
- Mon May 18 09:29:12 2015 +0000
- Revision:
- 115:4eb3c7e945cf
- Parent:
- 106:fc5f25f0e0d5
- Child:
- 121:68217ccb8cd1
Remove mutex for SmartRestSocket::cachedIP, reverse search for OperationDict::set
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xinlei | 93:61d44636f020 | 1 | #include <string.h> |
xinlei | 93:61d44636f020 | 2 | #include "SmartRestSocket.h" |
xinlei | 93:61d44636f020 | 3 | #include "SmartRestConf.h" |
xinlei | 93:61d44636f020 | 4 | |
xinlei | 93:61d44636f020 | 5 | |
xinlei | 93:61d44636f020 | 6 | int SmartRestSocket::connect() |
xinlei | 93:61d44636f020 | 7 | { |
xinlei | 96:ea056f6be2e8 | 8 | extern MDMSerial *pMdm; |
xinlei | 93:61d44636f020 | 9 | int n = -1; |
xinlei | 93:61d44636f020 | 10 | for (size_t i = 0; i < 3; ++i) { |
xinlei | 106:fc5f25f0e0d5 | 11 | if (cachedIP[0] == 0) { |
xinlei | 98:e369fc75c000 | 12 | MDMParser::IP ip = pMdm->gethostbyname(srHost); |
xinlei | 93:61d44636f020 | 13 | if (ip == NOIP) |
xinlei | 93:61d44636f020 | 14 | continue; |
xinlei | 93:61d44636f020 | 15 | const unsigned char *c = (const unsigned char*)&ip; |
xinlei | 93:61d44636f020 | 16 | snprintf(cachedIP, sizeof(cachedIP), "%u.%u.%u.%u", c[3], c[2], c[1], c[0]); |
xinlei | 93:61d44636f020 | 17 | } |
xinlei | 98:e369fc75c000 | 18 | n = TCPSocketConnection::connect(cachedIP, srPort); |
xinlei | 93:61d44636f020 | 19 | if (n >= 0) { |
xinlei | 105:c61f0d62b625 | 20 | if (timeout == -1) |
xinlei | 105:c61f0d62b625 | 21 | Socket::set_blocking(true); |
xinlei | 105:c61f0d62b625 | 22 | else |
xinlei | 105:c61f0d62b625 | 23 | Socket::set_blocking(false, timeout); |
xinlei | 93:61d44636f020 | 24 | break; |
xinlei | 93:61d44636f020 | 25 | } else { |
xinlei | 106:fc5f25f0e0d5 | 26 | cachedIP[0] = 0; |
xinlei | 93:61d44636f020 | 27 | } |
xinlei | 93:61d44636f020 | 28 | } |
xinlei | 93:61d44636f020 | 29 | return n; |
xinlei | 93:61d44636f020 | 30 | } |
xinlei | 93:61d44636f020 | 31 | |
xinlei | 93:61d44636f020 | 32 | int SmartRestSocket::sendOnly(char *buf, int size) |
xinlei | 93:61d44636f020 | 33 | { |
xinlei | 93:61d44636f020 | 34 | int l = connect(); |
xinlei | 93:61d44636f020 | 35 | if (l < 0) |
xinlei | 93:61d44636f020 | 36 | return -3; |
xinlei | 93:61d44636f020 | 37 | l = send(buf, size); |
xinlei | 93:61d44636f020 | 38 | close(); |
xinlei | 93:61d44636f020 | 39 | if (l < 0) { |
xinlei | 93:61d44636f020 | 40 | return -2; |
xinlei | 93:61d44636f020 | 41 | } else { |
xinlei | 93:61d44636f020 | 42 | return l; |
xinlei | 93:61d44636f020 | 43 | } |
xinlei | 93:61d44636f020 | 44 | } |
xinlei | 93:61d44636f020 | 45 | |
xinlei | 93:61d44636f020 | 46 | int SmartRestSocket::sendAndReceive(char *buf, int size, int maxSize) |
xinlei | 93:61d44636f020 | 47 | { |
xinlei | 93:61d44636f020 | 48 | int l = connect(); |
xinlei | 93:61d44636f020 | 49 | if (l < 0) |
xinlei | 93:61d44636f020 | 50 | return -3; |
xinlei | 93:61d44636f020 | 51 | l = send(buf, size); |
xinlei | 93:61d44636f020 | 52 | if (l < 0) { |
xinlei | 93:61d44636f020 | 53 | close(); |
xinlei | 93:61d44636f020 | 54 | return -2; |
xinlei | 93:61d44636f020 | 55 | } else { |
xinlei | 93:61d44636f020 | 56 | l = receive(buf, maxSize); |
xinlei | 98:e369fc75c000 | 57 | if (l >= 0 && l < maxSize) |
xinlei | 98:e369fc75c000 | 58 | buf[l] = 0; |
xinlei | 93:61d44636f020 | 59 | close(); |
xinlei | 93:61d44636f020 | 60 | return l; |
xinlei | 93:61d44636f020 | 61 | } |
xinlei | 93:61d44636f020 | 62 | } |