now this shit works

Dependencies:   BufferedSerial

Dependents:   IoTWeatherStation

Fork of ESP8266NodeMCUInterface by ESP8266

Committer:
mbedAustin
Date:
Wed Apr 29 22:43:03 2015 +0000
Revision:
30:c035696b9397
Child:
31:fd0eaf273b11
Safety Save

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 30:c035696b9397 1 /* Copyright (C) 2012 mbed.org, MIT License
mbedAustin 30:c035696b9397 2 *
mbedAustin 30:c035696b9397 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
mbedAustin 30:c035696b9397 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
mbedAustin 30:c035696b9397 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
mbedAustin 30:c035696b9397 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
mbedAustin 30:c035696b9397 7 * furnished to do so, subject to the following conditions:
mbedAustin 30:c035696b9397 8 *
mbedAustin 30:c035696b9397 9 * The above copyright notice and this permission notice shall be included in all copies or
mbedAustin 30:c035696b9397 10 * substantial portions of the Software.
mbedAustin 30:c035696b9397 11 *
mbedAustin 30:c035696b9397 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
mbedAustin 30:c035696b9397 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
mbedAustin 30:c035696b9397 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
mbedAustin 30:c035696b9397 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mbedAustin 30:c035696b9397 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mbedAustin 30:c035696b9397 17 */
mbedAustin 30:c035696b9397 18 #include "TCPSocketConnection.h"
mbedAustin 30:c035696b9397 19 #include <cstring>
mbedAustin 30:c035696b9397 20
mbedAustin 30:c035696b9397 21 using std::memset;
mbedAustin 30:c035696b9397 22 using std::memcpy;
mbedAustin 30:c035696b9397 23
mbedAustin 30:c035696b9397 24 TCPSocketConnection::TCPSocketConnection() :
mbedAustin 30:c035696b9397 25 _is_connected(false)
mbedAustin 30:c035696b9397 26 {
mbedAustin 30:c035696b9397 27 }
mbedAustin 30:c035696b9397 28
mbedAustin 30:c035696b9397 29 int TCPSocketConnection::connect(const char* host, const int port)
mbedAustin 30:c035696b9397 30 {
mbedAustin 30:c035696b9397 31 // if (init_socket(SOCK_STREAM) < 0)
mbedAustin 30:c035696b9397 32 // return -1;
mbedAustin 30:c035696b9397 33 //
mbedAustin 30:c035696b9397 34 // if (set_address(host, port) != 0)
mbedAustin 30:c035696b9397 35 // return -1;
mbedAustin 30:c035696b9397 36 //
mbedAustin 30:c035696b9397 37 // if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) {
mbedAustin 30:c035696b9397 38 // close();
mbedAustin 30:c035696b9397 39 // return -1;
mbedAustin 30:c035696b9397 40 // }
mbedAustin 30:c035696b9397 41 // _is_connected = true;
mbedAustin 30:c035696b9397 42 _is_connected();
mbedAustin 30:c035696b9397 43
mbedAustin 30:c035696b9397 44 return 0;
mbedAustin 30:c035696b9397 45 }
mbedAustin 30:c035696b9397 46
mbedAustin 30:c035696b9397 47 bool TCPSocketConnection::is_connected(void)
mbedAustin 30:c035696b9397 48 {
mbedAustin 30:c035696b9397 49 return _is_connected;
mbedAustin 30:c035696b9397 50 }
mbedAustin 30:c035696b9397 51
mbedAustin 30:c035696b9397 52 int TCPSocketConnection::send(char* data, int length)
mbedAustin 30:c035696b9397 53 {
mbedAustin 30:c035696b9397 54 // if ((_sock_fd < 0) || !_is_connected)
mbedAustin 30:c035696b9397 55 // return -1;
mbedAustin 30:c035696b9397 56 //
mbedAustin 30:c035696b9397 57 // if (!_blocking) {
mbedAustin 30:c035696b9397 58 // TimeInterval timeout(_timeout);
mbedAustin 30:c035696b9397 59 // if (wait_writable(timeout) != 0)
mbedAustin 30:c035696b9397 60 // return -1;
mbedAustin 30:c035696b9397 61 // }
mbedAustin 30:c035696b9397 62 //
mbedAustin 30:c035696b9397 63 // int n = lwip_send(_sock_fd, data, length, 0);
mbedAustin 30:c035696b9397 64 // _is_connected = (n != 0);
mbedAustin 30:c035696b9397 65 //
mbedAustin 30:c035696b9397 66 // return n;
mbedAustin 30:c035696b9397 67 return 0;
mbedAustin 30:c035696b9397 68 }
mbedAustin 30:c035696b9397 69
mbedAustin 30:c035696b9397 70 // -1 if unsuccessful, else number of bytes written
mbedAustin 30:c035696b9397 71 int TCPSocketConnection::send_all(char* data, int length)
mbedAustin 30:c035696b9397 72 {
mbedAustin 30:c035696b9397 73 // if ((_sock_fd < 0) || !_is_connected)
mbedAustin 30:c035696b9397 74 // return -1;
mbedAustin 30:c035696b9397 75 //
mbedAustin 30:c035696b9397 76 // int writtenLen = 0;
mbedAustin 30:c035696b9397 77 // TimeInterval timeout(_timeout);
mbedAustin 30:c035696b9397 78 // while (writtenLen < length) {
mbedAustin 30:c035696b9397 79 // if (!_blocking) {
mbedAustin 30:c035696b9397 80 // // Wait for socket to be writeable
mbedAustin 30:c035696b9397 81 // if (wait_writable(timeout) != 0)
mbedAustin 30:c035696b9397 82 // return writtenLen;
mbedAustin 30:c035696b9397 83 // }
mbedAustin 30:c035696b9397 84 //
mbedAustin 30:c035696b9397 85 // int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0);
mbedAustin 30:c035696b9397 86 // if (ret > 0) {
mbedAustin 30:c035696b9397 87 // writtenLen += ret;
mbedAustin 30:c035696b9397 88 // continue;
mbedAustin 30:c035696b9397 89 // } else if (ret == 0) {
mbedAustin 30:c035696b9397 90 // _is_connected = false;
mbedAustin 30:c035696b9397 91 // return writtenLen;
mbedAustin 30:c035696b9397 92 // } else {
mbedAustin 30:c035696b9397 93 // return -1; //Connnection error
mbedAustin 30:c035696b9397 94 // }
mbedAustin 30:c035696b9397 95 // }
mbedAustin 30:c035696b9397 96 // return writtenLen;
mbedAustin 30:c035696b9397 97 return 0;
mbedAustin 30:c035696b9397 98 }
mbedAustin 30:c035696b9397 99
mbedAustin 30:c035696b9397 100 int TCPSocketConnection::receive(char* data, int length)
mbedAustin 30:c035696b9397 101 {
mbedAustin 30:c035696b9397 102 // if ((_sock_fd < 0) || !_is_connected)
mbedAustin 30:c035696b9397 103 // return -1;
mbedAustin 30:c035696b9397 104 //
mbedAustin 30:c035696b9397 105 // if (!_blocking) {
mbedAustin 30:c035696b9397 106 // TimeInterval timeout(_timeout);
mbedAustin 30:c035696b9397 107 // if (wait_readable(timeout) != 0)
mbedAustin 30:c035696b9397 108 // return -1;
mbedAustin 30:c035696b9397 109 // }
mbedAustin 30:c035696b9397 110 //
mbedAustin 30:c035696b9397 111 // int n = lwip_recv(_sock_fd, data, length, 0);
mbedAustin 30:c035696b9397 112 // _is_connected = (n != 0);
mbedAustin 30:c035696b9397 113 //
mbedAustin 30:c035696b9397 114 // return n;
mbedAustin 30:c035696b9397 115 return 0;
mbedAustin 30:c035696b9397 116 }
mbedAustin 30:c035696b9397 117
mbedAustin 30:c035696b9397 118 // -1 if unsuccessful, else number of bytes received
mbedAustin 30:c035696b9397 119 int TCPSocketConnection::receive_all(char* data, int length)
mbedAustin 30:c035696b9397 120 {
mbedAustin 30:c035696b9397 121 // if ((_sock_fd < 0) || !_is_connected)
mbedAustin 30:c035696b9397 122 // return -1;
mbedAustin 30:c035696b9397 123 //
mbedAustin 30:c035696b9397 124 // int readLen = 0;
mbedAustin 30:c035696b9397 125 // TimeInterval timeout(_timeout);
mbedAustin 30:c035696b9397 126 // while (readLen < length) {
mbedAustin 30:c035696b9397 127 // if (!_blocking) {
mbedAustin 30:c035696b9397 128 // //Wait for socket to be readable
mbedAustin 30:c035696b9397 129 // if (wait_readable(timeout) != 0)
mbedAustin 30:c035696b9397 130 // return readLen;
mbedAustin 30:c035696b9397 131 // }
mbedAustin 30:c035696b9397 132 //
mbedAustin 30:c035696b9397 133 // int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0);
mbedAustin 30:c035696b9397 134 // if (ret > 0) {
mbedAustin 30:c035696b9397 135 // readLen += ret;
mbedAustin 30:c035696b9397 136 // } else if (ret == 0) {
mbedAustin 30:c035696b9397 137 // _is_connected = false;
mbedAustin 30:c035696b9397 138 // return readLen;
mbedAustin 30:c035696b9397 139 // } else {
mbedAustin 30:c035696b9397 140 // return -1; //Connnection error
mbedAustin 30:c035696b9397 141 // }
mbedAustin 30:c035696b9397 142 // }
mbedAustin 30:c035696b9397 143 // return readLen;
mbedAustin 30:c035696b9397 144 return 0;
mbedAustin 30:c035696b9397 145 }