Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Socket by
TCPSocketConnection.cpp@11:3d83c348fb8b, 2012-08-01 (annotated)
- Committer:
- emilmont
- Date:
- Wed Aug 01 13:02:32 2012 +0000
- Revision:
- 11:3d83c348fb8b
- Parent:
- 10:d24738f4ef99
- Child:
- 19:434906b5b977
[Socket library] Remove redundant UDPPacket class. Use centralized socket destructor. Add is_connected check.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 3:e6474399e057 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
emilmont | 3:e6474399e057 | 2 | * |
emilmont | 3:e6474399e057 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
emilmont | 3:e6474399e057 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
emilmont | 3:e6474399e057 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
emilmont | 3:e6474399e057 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
emilmont | 3:e6474399e057 | 7 | * furnished to do so, subject to the following conditions: |
emilmont | 3:e6474399e057 | 8 | * |
emilmont | 3:e6474399e057 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
emilmont | 3:e6474399e057 | 10 | * substantial portions of the Software. |
emilmont | 3:e6474399e057 | 11 | * |
emilmont | 3:e6474399e057 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
emilmont | 3:e6474399e057 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
emilmont | 3:e6474399e057 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
emilmont | 3:e6474399e057 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
emilmont | 3:e6474399e057 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
emilmont | 3:e6474399e057 | 17 | */ |
emilmont | 3:e6474399e057 | 18 | #include "TCPSocketConnection.h" |
emilmont | 3:e6474399e057 | 19 | #include <cstring> |
emilmont | 3:e6474399e057 | 20 | |
emilmont | 3:e6474399e057 | 21 | using std::memset; |
emilmont | 3:e6474399e057 | 22 | using std::memcpy; |
emilmont | 3:e6474399e057 | 23 | |
emilmont | 3:e6474399e057 | 24 | TCPSocketConnection::TCPSocketConnection() : |
emilmont | 11:3d83c348fb8b | 25 | _is_connected(false) { |
emilmont | 3:e6474399e057 | 26 | } |
emilmont | 3:e6474399e057 | 27 | |
emilmont | 4:75988d748e4d | 28 | int TCPSocketConnection::connect(const char* host, const int port) { |
emilmont | 5:300e7ad2dc1d | 29 | if (init_socket(SOCK_STREAM) < 0) |
emilmont | 3:e6474399e057 | 30 | return -1; |
emilmont | 3:e6474399e057 | 31 | |
emilmont | 5:300e7ad2dc1d | 32 | if (set_address(host, port) != 0) |
emilmont | 5:300e7ad2dc1d | 33 | return -1; |
emilmont | 3:e6474399e057 | 34 | |
emilmont | 3:e6474399e057 | 35 | if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) { |
emilmont | 3:e6474399e057 | 36 | close(); |
emilmont | 3:e6474399e057 | 37 | return -1; |
emilmont | 3:e6474399e057 | 38 | } |
emilmont | 11:3d83c348fb8b | 39 | _is_connected = true; |
emilmont | 3:e6474399e057 | 40 | |
emilmont | 3:e6474399e057 | 41 | return 0; |
emilmont | 3:e6474399e057 | 42 | } |
emilmont | 3:e6474399e057 | 43 | |
emilmont | 11:3d83c348fb8b | 44 | bool TCPSocketConnection::is_connected(void) { |
emilmont | 11:3d83c348fb8b | 45 | return _is_connected; |
emilmont | 11:3d83c348fb8b | 46 | } |
emilmont | 11:3d83c348fb8b | 47 | |
emilmont | 10:d24738f4ef99 | 48 | int TCPSocketConnection::send(char* data, int length) { |
emilmont | 11:3d83c348fb8b | 49 | if ((_sock_fd < 0) || !_is_connected) |
emilmont | 3:e6474399e057 | 50 | return -1; |
emilmont | 3:e6474399e057 | 51 | |
emilmont | 10:d24738f4ef99 | 52 | if (!_blocking) { |
emilmont | 10:d24738f4ef99 | 53 | TimeInterval timeout(_timeout); |
emilmont | 10:d24738f4ef99 | 54 | if (wait_writable(timeout) != 0) |
emilmont | 10:d24738f4ef99 | 55 | return -1; |
emilmont | 10:d24738f4ef99 | 56 | } |
emilmont | 3:e6474399e057 | 57 | |
emilmont | 3:e6474399e057 | 58 | int n = lwip_send(_sock_fd, data, length, 0); |
emilmont | 11:3d83c348fb8b | 59 | _is_connected = (n != 0); |
emilmont | 3:e6474399e057 | 60 | |
emilmont | 3:e6474399e057 | 61 | return n; |
emilmont | 3:e6474399e057 | 62 | } |
emilmont | 3:e6474399e057 | 63 | |
emilmont | 3:e6474399e057 | 64 | // -1 if unsuccessful, else number of bytes written |
emilmont | 10:d24738f4ef99 | 65 | int TCPSocketConnection::send_all(char* data, int length) { |
emilmont | 11:3d83c348fb8b | 66 | if ((_sock_fd < 0) || !_is_connected) |
emilmont | 3:e6474399e057 | 67 | return -1; |
emilmont | 3:e6474399e057 | 68 | |
emilmont | 3:e6474399e057 | 69 | size_t writtenLen = 0; |
emilmont | 10:d24738f4ef99 | 70 | TimeInterval timeout(_timeout); |
emilmont | 3:e6474399e057 | 71 | while (writtenLen < length) { |
emilmont | 10:d24738f4ef99 | 72 | if (!_blocking) { |
emilmont | 10:d24738f4ef99 | 73 | // Wait for socket to be writeable |
emilmont | 10:d24738f4ef99 | 74 | if (wait_writable(timeout) != 0) |
emilmont | 11:3d83c348fb8b | 75 | return writtenLen; |
emilmont | 10:d24738f4ef99 | 76 | } |
emilmont | 3:e6474399e057 | 77 | |
emilmont | 3:e6474399e057 | 78 | int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0); |
emilmont | 3:e6474399e057 | 79 | if (ret > 0) { |
emilmont | 3:e6474399e057 | 80 | writtenLen += ret; |
emilmont | 3:e6474399e057 | 81 | continue; |
emilmont | 3:e6474399e057 | 82 | } else if (ret == 0) { |
emilmont | 11:3d83c348fb8b | 83 | _is_connected = false; |
emilmont | 11:3d83c348fb8b | 84 | return writtenLen; |
emilmont | 3:e6474399e057 | 85 | } else { |
emilmont | 3:e6474399e057 | 86 | return -1; //Connnection error |
emilmont | 3:e6474399e057 | 87 | } |
emilmont | 3:e6474399e057 | 88 | } |
emilmont | 3:e6474399e057 | 89 | return writtenLen; |
emilmont | 3:e6474399e057 | 90 | } |
emilmont | 3:e6474399e057 | 91 | |
emilmont | 10:d24738f4ef99 | 92 | int TCPSocketConnection::receive(char* data, int length) { |
emilmont | 11:3d83c348fb8b | 93 | if ((_sock_fd < 0) || !_is_connected) |
emilmont | 3:e6474399e057 | 94 | return -1; |
emilmont | 3:e6474399e057 | 95 | |
emilmont | 10:d24738f4ef99 | 96 | if (!_blocking) { |
emilmont | 10:d24738f4ef99 | 97 | TimeInterval timeout(_timeout); |
emilmont | 10:d24738f4ef99 | 98 | if (wait_readable(timeout) != 0) |
emilmont | 10:d24738f4ef99 | 99 | return -1; |
emilmont | 10:d24738f4ef99 | 100 | } |
emilmont | 3:e6474399e057 | 101 | |
emilmont | 3:e6474399e057 | 102 | int n = lwip_recv(_sock_fd, data, length, 0); |
emilmont | 11:3d83c348fb8b | 103 | _is_connected = (n != 0); |
emilmont | 3:e6474399e057 | 104 | |
emilmont | 3:e6474399e057 | 105 | return n; |
emilmont | 3:e6474399e057 | 106 | } |
emilmont | 3:e6474399e057 | 107 | |
emilmont | 3:e6474399e057 | 108 | // -1 if unsuccessful, else number of bytes received |
emilmont | 10:d24738f4ef99 | 109 | int TCPSocketConnection::receive_all(char* data, int length) { |
emilmont | 11:3d83c348fb8b | 110 | if ((_sock_fd < 0) || !_is_connected) |
emilmont | 3:e6474399e057 | 111 | return -1; |
emilmont | 3:e6474399e057 | 112 | |
emilmont | 3:e6474399e057 | 113 | size_t readLen = 0; |
emilmont | 10:d24738f4ef99 | 114 | TimeInterval timeout(_timeout); |
emilmont | 3:e6474399e057 | 115 | while (readLen < length) { |
emilmont | 10:d24738f4ef99 | 116 | if (!_blocking) { |
emilmont | 10:d24738f4ef99 | 117 | //Wait for socket to be readable |
emilmont | 10:d24738f4ef99 | 118 | if (wait_readable(timeout) != 0) |
emilmont | 11:3d83c348fb8b | 119 | return readLen; |
emilmont | 10:d24738f4ef99 | 120 | } |
emilmont | 3:e6474399e057 | 121 | |
emilmont | 3:e6474399e057 | 122 | int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0); |
emilmont | 3:e6474399e057 | 123 | if (ret > 0) { |
emilmont | 3:e6474399e057 | 124 | readLen += ret; |
emilmont | 3:e6474399e057 | 125 | } else if (ret == 0) { |
emilmont | 11:3d83c348fb8b | 126 | _is_connected = false; |
emilmont | 11:3d83c348fb8b | 127 | return readLen; |
emilmont | 3:e6474399e057 | 128 | } else { |
emilmont | 3:e6474399e057 | 129 | return -1; //Connnection error |
emilmont | 3:e6474399e057 | 130 | } |
emilmont | 3:e6474399e057 | 131 | } |
emilmont | 3:e6474399e057 | 132 | return readLen; |
emilmont | 3:e6474399e057 | 133 | } |