Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
xinlei
Date:
Wed May 20 14:30:26 2015 +0000
Revision:
121:68217ccb8cd1
Parent:
115:4eb3c7e945cf
fix for properly closing a socket when connect failed

Who changed what in which revision?

UserRevisionLine numberNew 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 121:68217ccb8cd1 19 if (n == 0) {
xinlei 93:61d44636f020 20 break;
xinlei 93:61d44636f020 21 } else {
xinlei 106:fc5f25f0e0d5 22 cachedIP[0] = 0;
xinlei 93:61d44636f020 23 }
xinlei 93:61d44636f020 24 }
xinlei 93:61d44636f020 25 return n;
xinlei 93:61d44636f020 26 }
xinlei 93:61d44636f020 27
xinlei 93:61d44636f020 28 int SmartRestSocket::sendOnly(char *buf, int size)
xinlei 93:61d44636f020 29 {
xinlei 93:61d44636f020 30 int l = connect();
xinlei 121:68217ccb8cd1 31 if (l < 0) {
xinlei 121:68217ccb8cd1 32 close();
xinlei 93:61d44636f020 33 return -3;
xinlei 121:68217ccb8cd1 34 }
xinlei 93:61d44636f020 35 l = send(buf, size);
xinlei 93:61d44636f020 36 close();
xinlei 93:61d44636f020 37 if (l < 0) {
xinlei 93:61d44636f020 38 return -2;
xinlei 93:61d44636f020 39 } else {
xinlei 93:61d44636f020 40 return l;
xinlei 93:61d44636f020 41 }
xinlei 93:61d44636f020 42 }
xinlei 93:61d44636f020 43
xinlei 93:61d44636f020 44 int SmartRestSocket::sendAndReceive(char *buf, int size, int maxSize)
xinlei 93:61d44636f020 45 {
xinlei 93:61d44636f020 46 int l = connect();
xinlei 121:68217ccb8cd1 47 if (l < 0) {
xinlei 121:68217ccb8cd1 48 close();
xinlei 93:61d44636f020 49 return -3;
xinlei 121:68217ccb8cd1 50 }
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 121:68217ccb8cd1 57 close();
xinlei 98:e369fc75c000 58 if (l >= 0 && l < maxSize)
xinlei 98:e369fc75c000 59 buf[l] = 0;
xinlei 93:61d44636f020 60 return l;
xinlei 93:61d44636f020 61 }
xinlei 93:61d44636f020 62 }