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