None

Fork of cc3000_hostdriver_mbedsocket by Martin Kojtal

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TCPSocketConnection.cpp Source File

TCPSocketConnection.cpp

00001 /* Copyright (C) 2013 mbed.org, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 
00019 #include "TCPSocketConnection.h"
00020 #include <algorithm>
00021 
00022 TCPSocketConnection::TCPSocketConnection() : _is_connected(false) {
00023     _cc3000_module = cc3000::get_instance();
00024     if (_cc3000_module == NULL) {
00025         error("Endpoint constructor error: no cc3000 instance available!\r\n");
00026     }
00027 }
00028 
00029 int TCPSocketConnection::connect(const char *host, const int port) {
00030     if (init_socket(SOCK_STREAM, IPPROTO_TCP) < 0) {
00031         DBG_SOCKET("Failed to create tcp socket");
00032         return -1;
00033     }
00034 
00035     if (set_address(host, port) != 0) {
00036         DBG_SOCKET("Failed to set address (tcp)");
00037         return -1;
00038     }
00039 
00040     if (_cc3000_module->_socket.connect(_sock_fd, (const sockaddr *)&_remote_host, sizeof(_remote_host)) < 0) {
00041         DBG_SOCKET("Failed to connect (tcp)");
00042         close();
00043         return -1;
00044     }
00045 
00046     _is_connected = true;
00047 
00048     return 0;
00049 }
00050 
00051 bool TCPSocketConnection::is_connected(void) {
00052     return _is_connected;
00053 }
00054 
00055 int TCPSocketConnection::send(char* data, int length) {
00056     if ((_sock_fd < 0) || !_is_connected) {
00057         return -1;
00058     }
00059 
00060     if (!_blocking) {
00061         TimeInterval timeout(_timeout);
00062         if (wait_writable(timeout) != 0) {
00063             return -1;
00064         }
00065     }
00066 
00067     int n = _cc3000_module->_socket.send(_sock_fd, data, length, 0);
00068     _is_connected = (n != 0);
00069 
00070     return n;
00071 }
00072 
00073 int TCPSocketConnection::send_all(char *data, int length) {
00074     if ((_sock_fd < 0) || !_is_connected) {
00075         return -1;
00076     }
00077 
00078     int writtenLen = 0;
00079     TimeInterval timeout(_timeout);
00080     while (writtenLen < length) {
00081         if (!_blocking) {
00082             // Wait for socket to be writeable
00083             if (wait_writable(timeout) != 0) {
00084                 return writtenLen;
00085             }
00086         }
00087 
00088         int ret = _cc3000_module->_socket.send(_sock_fd, data + writtenLen, length - writtenLen, 0);
00089         if (ret > 0) {
00090             writtenLen += ret;
00091             continue;
00092         } else if (ret == 0) {
00093             _is_connected = false;
00094             return writtenLen;
00095         } else {
00096             return -1; //Connnection error
00097         }
00098     }
00099 
00100     return writtenLen;
00101 }
00102 
00103 int TCPSocketConnection::receive(char *data, int length) {
00104     if ((_sock_fd < 0) || !_is_connected) {
00105         return -1;
00106     }
00107 
00108     if (!_blocking) {
00109         TimeInterval timeout(_timeout);
00110         if (wait_readable(timeout) != 0)
00111             return -1;
00112     }
00113 
00114     int n = _cc3000_module->_socket.recv(_sock_fd, data, length, 0);
00115     if (n >= 0) {
00116         _is_connected = 1;
00117     } else {
00118         _is_connected = 0;
00119     }
00120 
00121     return n;
00122 }
00123 
00124 int TCPSocketConnection::receive_all(char *data, int length) {
00125     if ((_sock_fd < 0) || !_is_connected) {
00126         return -1;
00127     }
00128 
00129     int readLen = 0;
00130     TimeInterval timeout(_timeout);
00131     while (readLen < length) {
00132         if (!_blocking) {
00133             //Wait for socket to be readable
00134             if (wait_readable(timeout) != 0)
00135                 return readLen;
00136         }
00137 
00138         int ret = _cc3000_module->_socket.recv(_sock_fd, data + readLen, length - readLen, 0);
00139         if (ret > 0) {
00140             readLen += ret;
00141         } else if (ret == 0) {
00142             _is_connected = false;
00143             return readLen;
00144         } else {
00145             return -1; //Connnection error
00146         }
00147     }
00148     return readLen;
00149 }