]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/net/lwip/Socket/TCPSocketConnection.cpp
1 /* Copyright (C) 2012 mbed.org, MIT License
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:
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
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.
18 #include "TCPSocketConnection.h"
24 TCPSocketConnection::TCPSocketConnection() :
25 _is_connected(false) {
28 int TCPSocketConnection::connect(const char* host
, const int port
) {
29 if (init_socket(SOCK_STREAM
) < 0)
32 if (set_address(host
, port
) != 0)
35 if (lwip_connect(_sock_fd
, (const struct sockaddr
*) &_remoteHost
, sizeof(_remoteHost
)) < 0) {
44 bool TCPSocketConnection::is_connected(void) {
48 int TCPSocketConnection::send(char* data
, int length
) {
49 if ((_sock_fd
< 0) || !_is_connected
)
53 TimeInterval
timeout(_timeout
);
54 if (wait_writable(timeout
) != 0)
58 int n
= lwip_send(_sock_fd
, data
, length
, 0);
59 _is_connected
= (n
!= 0);
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
)
70 TimeInterval
timeout(_timeout
);
71 while (writtenLen
< length
) {
73 // Wait for socket to be writeable
74 if (wait_writable(timeout
) != 0)
78 int ret
= lwip_send(_sock_fd
, data
+ writtenLen
, length
- writtenLen
, 0);
82 } else if (ret
== 0) {
83 _is_connected
= false;
86 return -1; //Connnection error
92 int TCPSocketConnection::receive(char* data
, int length
) {
93 if ((_sock_fd
< 0) || !_is_connected
)
97 TimeInterval
timeout(_timeout
);
98 if (wait_readable(timeout
) != 0)
102 int n
= lwip_recv(_sock_fd
, data
, length
, 0);
103 _is_connected
= (n
!= 0);
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
)
114 TimeInterval
timeout(_timeout
);
115 while (readLen
< length
) {
117 //Wait for socket to be readable
118 if (wait_readable(timeout
) != 0)
122 int ret
= lwip_recv(_sock_fd
, data
+ readLen
, length
- readLen
, 0);
125 } else if (ret
== 0) {
126 _is_connected
= false;
129 return -1; //Connnection error