Marco Zecchini
/
Example_RTOS
Rtos API example
mbed-os/features/netsocket/TCPSocket.cpp@0:9fca2b23d0ba, 2019-02-23 (annotated)
- Committer:
- marcozecchini
- Date:
- Sat Feb 23 12:13:36 2019 +0000
- Revision:
- 0:9fca2b23d0ba
final commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcozecchini | 0:9fca2b23d0ba | 1 | /* Socket |
marcozecchini | 0:9fca2b23d0ba | 2 | * Copyright (c) 2015 ARM Limited |
marcozecchini | 0:9fca2b23d0ba | 3 | * |
marcozecchini | 0:9fca2b23d0ba | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
marcozecchini | 0:9fca2b23d0ba | 5 | * you may not use this file except in compliance with the License. |
marcozecchini | 0:9fca2b23d0ba | 6 | * You may obtain a copy of the License at |
marcozecchini | 0:9fca2b23d0ba | 7 | * |
marcozecchini | 0:9fca2b23d0ba | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
marcozecchini | 0:9fca2b23d0ba | 9 | * |
marcozecchini | 0:9fca2b23d0ba | 10 | * Unless required by applicable law or agreed to in writing, software |
marcozecchini | 0:9fca2b23d0ba | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
marcozecchini | 0:9fca2b23d0ba | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
marcozecchini | 0:9fca2b23d0ba | 13 | * See the License for the specific language governing permissions and |
marcozecchini | 0:9fca2b23d0ba | 14 | * limitations under the License. |
marcozecchini | 0:9fca2b23d0ba | 15 | */ |
marcozecchini | 0:9fca2b23d0ba | 16 | |
marcozecchini | 0:9fca2b23d0ba | 17 | #include "TCPSocket.h" |
marcozecchini | 0:9fca2b23d0ba | 18 | #include "Timer.h" |
marcozecchini | 0:9fca2b23d0ba | 19 | #include "mbed_assert.h" |
marcozecchini | 0:9fca2b23d0ba | 20 | |
marcozecchini | 0:9fca2b23d0ba | 21 | #define READ_FLAG 0x1u |
marcozecchini | 0:9fca2b23d0ba | 22 | #define WRITE_FLAG 0x2u |
marcozecchini | 0:9fca2b23d0ba | 23 | |
marcozecchini | 0:9fca2b23d0ba | 24 | TCPSocket::TCPSocket() |
marcozecchini | 0:9fca2b23d0ba | 25 | : _pending(0), _event_flag(), |
marcozecchini | 0:9fca2b23d0ba | 26 | _read_in_progress(false), _write_in_progress(false) |
marcozecchini | 0:9fca2b23d0ba | 27 | { |
marcozecchini | 0:9fca2b23d0ba | 28 | } |
marcozecchini | 0:9fca2b23d0ba | 29 | |
marcozecchini | 0:9fca2b23d0ba | 30 | TCPSocket::~TCPSocket() |
marcozecchini | 0:9fca2b23d0ba | 31 | { |
marcozecchini | 0:9fca2b23d0ba | 32 | close(); |
marcozecchini | 0:9fca2b23d0ba | 33 | } |
marcozecchini | 0:9fca2b23d0ba | 34 | |
marcozecchini | 0:9fca2b23d0ba | 35 | nsapi_protocol_t TCPSocket::get_proto() |
marcozecchini | 0:9fca2b23d0ba | 36 | { |
marcozecchini | 0:9fca2b23d0ba | 37 | return NSAPI_TCP; |
marcozecchini | 0:9fca2b23d0ba | 38 | } |
marcozecchini | 0:9fca2b23d0ba | 39 | |
marcozecchini | 0:9fca2b23d0ba | 40 | nsapi_error_t TCPSocket::connect(const SocketAddress &address) |
marcozecchini | 0:9fca2b23d0ba | 41 | { |
marcozecchini | 0:9fca2b23d0ba | 42 | _lock.lock(); |
marcozecchini | 0:9fca2b23d0ba | 43 | nsapi_error_t ret; |
marcozecchini | 0:9fca2b23d0ba | 44 | |
marcozecchini | 0:9fca2b23d0ba | 45 | // If this assert is hit then there are two threads |
marcozecchini | 0:9fca2b23d0ba | 46 | // performing a send at the same time which is undefined |
marcozecchini | 0:9fca2b23d0ba | 47 | // behavior |
marcozecchini | 0:9fca2b23d0ba | 48 | MBED_ASSERT(!_write_in_progress); |
marcozecchini | 0:9fca2b23d0ba | 49 | _write_in_progress = true; |
marcozecchini | 0:9fca2b23d0ba | 50 | |
marcozecchini | 0:9fca2b23d0ba | 51 | bool blocking_connect_in_progress = false; |
marcozecchini | 0:9fca2b23d0ba | 52 | |
marcozecchini | 0:9fca2b23d0ba | 53 | while (true) { |
marcozecchini | 0:9fca2b23d0ba | 54 | if (!_socket) { |
marcozecchini | 0:9fca2b23d0ba | 55 | ret = NSAPI_ERROR_NO_SOCKET; |
marcozecchini | 0:9fca2b23d0ba | 56 | break; |
marcozecchini | 0:9fca2b23d0ba | 57 | } |
marcozecchini | 0:9fca2b23d0ba | 58 | |
marcozecchini | 0:9fca2b23d0ba | 59 | _pending = 0; |
marcozecchini | 0:9fca2b23d0ba | 60 | ret = _stack->socket_connect(_socket, address); |
marcozecchini | 0:9fca2b23d0ba | 61 | if ((_timeout == 0) || !(ret == NSAPI_ERROR_IN_PROGRESS || ret == NSAPI_ERROR_ALREADY)) { |
marcozecchini | 0:9fca2b23d0ba | 62 | break; |
marcozecchini | 0:9fca2b23d0ba | 63 | } else { |
marcozecchini | 0:9fca2b23d0ba | 64 | blocking_connect_in_progress = true; |
marcozecchini | 0:9fca2b23d0ba | 65 | |
marcozecchini | 0:9fca2b23d0ba | 66 | uint32_t flag; |
marcozecchini | 0:9fca2b23d0ba | 67 | |
marcozecchini | 0:9fca2b23d0ba | 68 | // Release lock before blocking so other threads |
marcozecchini | 0:9fca2b23d0ba | 69 | // accessing this object aren't blocked |
marcozecchini | 0:9fca2b23d0ba | 70 | _lock.unlock(); |
marcozecchini | 0:9fca2b23d0ba | 71 | flag = _event_flag.wait_any(WRITE_FLAG, _timeout); |
marcozecchini | 0:9fca2b23d0ba | 72 | _lock.lock(); |
marcozecchini | 0:9fca2b23d0ba | 73 | if (flag & osFlagsError) { |
marcozecchini | 0:9fca2b23d0ba | 74 | // Timeout break |
marcozecchini | 0:9fca2b23d0ba | 75 | break; |
marcozecchini | 0:9fca2b23d0ba | 76 | } |
marcozecchini | 0:9fca2b23d0ba | 77 | } |
marcozecchini | 0:9fca2b23d0ba | 78 | } |
marcozecchini | 0:9fca2b23d0ba | 79 | |
marcozecchini | 0:9fca2b23d0ba | 80 | _write_in_progress = false; |
marcozecchini | 0:9fca2b23d0ba | 81 | |
marcozecchini | 0:9fca2b23d0ba | 82 | /* Non-blocking connect gives "EISCONN" once done - convert to OK for blocking mode if we became connected during this call */ |
marcozecchini | 0:9fca2b23d0ba | 83 | if (ret == NSAPI_ERROR_IS_CONNECTED && blocking_connect_in_progress) { |
marcozecchini | 0:9fca2b23d0ba | 84 | ret = NSAPI_ERROR_OK; |
marcozecchini | 0:9fca2b23d0ba | 85 | } |
marcozecchini | 0:9fca2b23d0ba | 86 | |
marcozecchini | 0:9fca2b23d0ba | 87 | _lock.unlock(); |
marcozecchini | 0:9fca2b23d0ba | 88 | return ret; |
marcozecchini | 0:9fca2b23d0ba | 89 | } |
marcozecchini | 0:9fca2b23d0ba | 90 | |
marcozecchini | 0:9fca2b23d0ba | 91 | nsapi_error_t TCPSocket::connect(const char *host, uint16_t port) |
marcozecchini | 0:9fca2b23d0ba | 92 | { |
marcozecchini | 0:9fca2b23d0ba | 93 | SocketAddress address; |
marcozecchini | 0:9fca2b23d0ba | 94 | nsapi_error_t err = _stack->gethostbyname(host, &address); |
marcozecchini | 0:9fca2b23d0ba | 95 | if (err) { |
marcozecchini | 0:9fca2b23d0ba | 96 | return NSAPI_ERROR_DNS_FAILURE; |
marcozecchini | 0:9fca2b23d0ba | 97 | } |
marcozecchini | 0:9fca2b23d0ba | 98 | |
marcozecchini | 0:9fca2b23d0ba | 99 | address.set_port(port); |
marcozecchini | 0:9fca2b23d0ba | 100 | |
marcozecchini | 0:9fca2b23d0ba | 101 | // connect is thread safe |
marcozecchini | 0:9fca2b23d0ba | 102 | return connect(address); |
marcozecchini | 0:9fca2b23d0ba | 103 | } |
marcozecchini | 0:9fca2b23d0ba | 104 | |
marcozecchini | 0:9fca2b23d0ba | 105 | nsapi_size_or_error_t TCPSocket::send(const void *data, nsapi_size_t size) |
marcozecchini | 0:9fca2b23d0ba | 106 | { |
marcozecchini | 0:9fca2b23d0ba | 107 | _lock.lock(); |
marcozecchini | 0:9fca2b23d0ba | 108 | nsapi_size_or_error_t ret; |
marcozecchini | 0:9fca2b23d0ba | 109 | |
marcozecchini | 0:9fca2b23d0ba | 110 | // If this assert is hit then there are two threads |
marcozecchini | 0:9fca2b23d0ba | 111 | // performing a send at the same time which is undefined |
marcozecchini | 0:9fca2b23d0ba | 112 | // behavior |
marcozecchini | 0:9fca2b23d0ba | 113 | MBED_ASSERT(!_write_in_progress); |
marcozecchini | 0:9fca2b23d0ba | 114 | _write_in_progress = true; |
marcozecchini | 0:9fca2b23d0ba | 115 | |
marcozecchini | 0:9fca2b23d0ba | 116 | while (true) { |
marcozecchini | 0:9fca2b23d0ba | 117 | if (!_socket) { |
marcozecchini | 0:9fca2b23d0ba | 118 | ret = NSAPI_ERROR_NO_SOCKET; |
marcozecchini | 0:9fca2b23d0ba | 119 | break; |
marcozecchini | 0:9fca2b23d0ba | 120 | } |
marcozecchini | 0:9fca2b23d0ba | 121 | |
marcozecchini | 0:9fca2b23d0ba | 122 | _pending = 0; |
marcozecchini | 0:9fca2b23d0ba | 123 | ret = _stack->socket_send(_socket, data, size); |
marcozecchini | 0:9fca2b23d0ba | 124 | if ((_timeout == 0) || (ret != NSAPI_ERROR_WOULD_BLOCK)) { |
marcozecchini | 0:9fca2b23d0ba | 125 | break; |
marcozecchini | 0:9fca2b23d0ba | 126 | } else { |
marcozecchini | 0:9fca2b23d0ba | 127 | uint32_t flag; |
marcozecchini | 0:9fca2b23d0ba | 128 | |
marcozecchini | 0:9fca2b23d0ba | 129 | // Release lock before blocking so other threads |
marcozecchini | 0:9fca2b23d0ba | 130 | // accessing this object aren't blocked |
marcozecchini | 0:9fca2b23d0ba | 131 | _lock.unlock(); |
marcozecchini | 0:9fca2b23d0ba | 132 | flag = _event_flag.wait_any(WRITE_FLAG, _timeout); |
marcozecchini | 0:9fca2b23d0ba | 133 | _lock.lock(); |
marcozecchini | 0:9fca2b23d0ba | 134 | |
marcozecchini | 0:9fca2b23d0ba | 135 | if (flag & osFlagsError) { |
marcozecchini | 0:9fca2b23d0ba | 136 | // Timeout break |
marcozecchini | 0:9fca2b23d0ba | 137 | ret = NSAPI_ERROR_WOULD_BLOCK; |
marcozecchini | 0:9fca2b23d0ba | 138 | break; |
marcozecchini | 0:9fca2b23d0ba | 139 | } |
marcozecchini | 0:9fca2b23d0ba | 140 | } |
marcozecchini | 0:9fca2b23d0ba | 141 | } |
marcozecchini | 0:9fca2b23d0ba | 142 | |
marcozecchini | 0:9fca2b23d0ba | 143 | _write_in_progress = false; |
marcozecchini | 0:9fca2b23d0ba | 144 | _lock.unlock(); |
marcozecchini | 0:9fca2b23d0ba | 145 | return ret; |
marcozecchini | 0:9fca2b23d0ba | 146 | } |
marcozecchini | 0:9fca2b23d0ba | 147 | |
marcozecchini | 0:9fca2b23d0ba | 148 | nsapi_size_or_error_t TCPSocket::recv(void *data, nsapi_size_t size) |
marcozecchini | 0:9fca2b23d0ba | 149 | { |
marcozecchini | 0:9fca2b23d0ba | 150 | _lock.lock(); |
marcozecchini | 0:9fca2b23d0ba | 151 | nsapi_size_or_error_t ret; |
marcozecchini | 0:9fca2b23d0ba | 152 | |
marcozecchini | 0:9fca2b23d0ba | 153 | // If this assert is hit then there are two threads |
marcozecchini | 0:9fca2b23d0ba | 154 | // performing a recv at the same time which is undefined |
marcozecchini | 0:9fca2b23d0ba | 155 | // behavior |
marcozecchini | 0:9fca2b23d0ba | 156 | MBED_ASSERT(!_read_in_progress); |
marcozecchini | 0:9fca2b23d0ba | 157 | _read_in_progress = true; |
marcozecchini | 0:9fca2b23d0ba | 158 | |
marcozecchini | 0:9fca2b23d0ba | 159 | while (true) { |
marcozecchini | 0:9fca2b23d0ba | 160 | if (!_socket) { |
marcozecchini | 0:9fca2b23d0ba | 161 | ret = NSAPI_ERROR_NO_SOCKET; |
marcozecchini | 0:9fca2b23d0ba | 162 | break; |
marcozecchini | 0:9fca2b23d0ba | 163 | } |
marcozecchini | 0:9fca2b23d0ba | 164 | |
marcozecchini | 0:9fca2b23d0ba | 165 | _pending = 0; |
marcozecchini | 0:9fca2b23d0ba | 166 | ret = _stack->socket_recv(_socket, data, size); |
marcozecchini | 0:9fca2b23d0ba | 167 | if ((_timeout == 0) || (ret != NSAPI_ERROR_WOULD_BLOCK)) { |
marcozecchini | 0:9fca2b23d0ba | 168 | break; |
marcozecchini | 0:9fca2b23d0ba | 169 | } else { |
marcozecchini | 0:9fca2b23d0ba | 170 | uint32_t flag; |
marcozecchini | 0:9fca2b23d0ba | 171 | |
marcozecchini | 0:9fca2b23d0ba | 172 | // Release lock before blocking so other threads |
marcozecchini | 0:9fca2b23d0ba | 173 | // accessing this object aren't blocked |
marcozecchini | 0:9fca2b23d0ba | 174 | _lock.unlock(); |
marcozecchini | 0:9fca2b23d0ba | 175 | flag = _event_flag.wait_any(READ_FLAG, _timeout); |
marcozecchini | 0:9fca2b23d0ba | 176 | _lock.lock(); |
marcozecchini | 0:9fca2b23d0ba | 177 | |
marcozecchini | 0:9fca2b23d0ba | 178 | if (flag & osFlagsError) { |
marcozecchini | 0:9fca2b23d0ba | 179 | // Timeout break |
marcozecchini | 0:9fca2b23d0ba | 180 | ret = NSAPI_ERROR_WOULD_BLOCK; |
marcozecchini | 0:9fca2b23d0ba | 181 | break; |
marcozecchini | 0:9fca2b23d0ba | 182 | } |
marcozecchini | 0:9fca2b23d0ba | 183 | } |
marcozecchini | 0:9fca2b23d0ba | 184 | } |
marcozecchini | 0:9fca2b23d0ba | 185 | |
marcozecchini | 0:9fca2b23d0ba | 186 | _read_in_progress = false; |
marcozecchini | 0:9fca2b23d0ba | 187 | _lock.unlock(); |
marcozecchini | 0:9fca2b23d0ba | 188 | return ret; |
marcozecchini | 0:9fca2b23d0ba | 189 | } |
marcozecchini | 0:9fca2b23d0ba | 190 | |
marcozecchini | 0:9fca2b23d0ba | 191 | void TCPSocket::event() |
marcozecchini | 0:9fca2b23d0ba | 192 | { |
marcozecchini | 0:9fca2b23d0ba | 193 | _event_flag.set(READ_FLAG|WRITE_FLAG); |
marcozecchini | 0:9fca2b23d0ba | 194 | |
marcozecchini | 0:9fca2b23d0ba | 195 | _pending += 1; |
marcozecchini | 0:9fca2b23d0ba | 196 | if (_callback && _pending == 1) { |
marcozecchini | 0:9fca2b23d0ba | 197 | _callback(); |
marcozecchini | 0:9fca2b23d0ba | 198 | } |
marcozecchini | 0:9fca2b23d0ba | 199 | } |