Pinned to some recent date
features/netsocket/TCPSocket.cpp@0:fb7af294d5d9, 2016-11-17 (annotated)
- Committer:
- Simon Cooksey
- Date:
- Thu Nov 17 16:43:53 2016 +0000
- Revision:
- 0:fb7af294d5d9
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Simon Cooksey |
0:fb7af294d5d9 | 1 | /* Socket |
Simon Cooksey |
0:fb7af294d5d9 | 2 | * Copyright (c) 2015 ARM Limited |
Simon Cooksey |
0:fb7af294d5d9 | 3 | * |
Simon Cooksey |
0:fb7af294d5d9 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Simon Cooksey |
0:fb7af294d5d9 | 5 | * you may not use this file except in compliance with the License. |
Simon Cooksey |
0:fb7af294d5d9 | 6 | * You may obtain a copy of the License at |
Simon Cooksey |
0:fb7af294d5d9 | 7 | * |
Simon Cooksey |
0:fb7af294d5d9 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Simon Cooksey |
0:fb7af294d5d9 | 9 | * |
Simon Cooksey |
0:fb7af294d5d9 | 10 | * Unless required by applicable law or agreed to in writing, software |
Simon Cooksey |
0:fb7af294d5d9 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Simon Cooksey |
0:fb7af294d5d9 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Simon Cooksey |
0:fb7af294d5d9 | 13 | * See the License for the specific language governing permissions and |
Simon Cooksey |
0:fb7af294d5d9 | 14 | * limitations under the License. |
Simon Cooksey |
0:fb7af294d5d9 | 15 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 16 | |
Simon Cooksey |
0:fb7af294d5d9 | 17 | #include "TCPSocket.h" |
Simon Cooksey |
0:fb7af294d5d9 | 18 | #include "Timer.h" |
Simon Cooksey |
0:fb7af294d5d9 | 19 | #include "mbed_assert.h" |
Simon Cooksey |
0:fb7af294d5d9 | 20 | |
Simon Cooksey |
0:fb7af294d5d9 | 21 | TCPSocket::TCPSocket() |
Simon Cooksey |
0:fb7af294d5d9 | 22 | : _pending(0), _read_sem(0), _write_sem(0), |
Simon Cooksey |
0:fb7af294d5d9 | 23 | _read_in_progress(false), _write_in_progress(false) |
Simon Cooksey |
0:fb7af294d5d9 | 24 | { |
Simon Cooksey |
0:fb7af294d5d9 | 25 | } |
Simon Cooksey |
0:fb7af294d5d9 | 26 | |
Simon Cooksey |
0:fb7af294d5d9 | 27 | TCPSocket::~TCPSocket() |
Simon Cooksey |
0:fb7af294d5d9 | 28 | { |
Simon Cooksey |
0:fb7af294d5d9 | 29 | close(); |
Simon Cooksey |
0:fb7af294d5d9 | 30 | } |
Simon Cooksey |
0:fb7af294d5d9 | 31 | |
Simon Cooksey |
0:fb7af294d5d9 | 32 | nsapi_protocol_t TCPSocket::get_proto() |
Simon Cooksey |
0:fb7af294d5d9 | 33 | { |
Simon Cooksey |
0:fb7af294d5d9 | 34 | return NSAPI_TCP; |
Simon Cooksey |
0:fb7af294d5d9 | 35 | } |
Simon Cooksey |
0:fb7af294d5d9 | 36 | |
Simon Cooksey |
0:fb7af294d5d9 | 37 | int TCPSocket::connect(const SocketAddress &address) |
Simon Cooksey |
0:fb7af294d5d9 | 38 | { |
Simon Cooksey |
0:fb7af294d5d9 | 39 | _lock.lock(); |
Simon Cooksey |
0:fb7af294d5d9 | 40 | int ret; |
Simon Cooksey |
0:fb7af294d5d9 | 41 | |
Simon Cooksey |
0:fb7af294d5d9 | 42 | if (!_socket) { |
Simon Cooksey |
0:fb7af294d5d9 | 43 | ret = NSAPI_ERROR_NO_SOCKET; |
Simon Cooksey |
0:fb7af294d5d9 | 44 | } else { |
Simon Cooksey |
0:fb7af294d5d9 | 45 | ret = _stack->socket_connect(_socket, address); |
Simon Cooksey |
0:fb7af294d5d9 | 46 | } |
Simon Cooksey |
0:fb7af294d5d9 | 47 | |
Simon Cooksey |
0:fb7af294d5d9 | 48 | _lock.unlock(); |
Simon Cooksey |
0:fb7af294d5d9 | 49 | return ret; |
Simon Cooksey |
0:fb7af294d5d9 | 50 | } |
Simon Cooksey |
0:fb7af294d5d9 | 51 | |
Simon Cooksey |
0:fb7af294d5d9 | 52 | int TCPSocket::connect(const char *host, uint16_t port) |
Simon Cooksey |
0:fb7af294d5d9 | 53 | { |
Simon Cooksey |
0:fb7af294d5d9 | 54 | SocketAddress address; |
Simon Cooksey |
0:fb7af294d5d9 | 55 | int err = _stack->gethostbyname(host, &address); |
Simon Cooksey |
0:fb7af294d5d9 | 56 | if (err) { |
Simon Cooksey |
0:fb7af294d5d9 | 57 | return NSAPI_ERROR_DNS_FAILURE; |
Simon Cooksey |
0:fb7af294d5d9 | 58 | } |
Simon Cooksey |
0:fb7af294d5d9 | 59 | |
Simon Cooksey |
0:fb7af294d5d9 | 60 | address.set_port(port); |
Simon Cooksey |
0:fb7af294d5d9 | 61 | |
Simon Cooksey |
0:fb7af294d5d9 | 62 | // connect is thread safe |
Simon Cooksey |
0:fb7af294d5d9 | 63 | return connect(address); |
Simon Cooksey |
0:fb7af294d5d9 | 64 | } |
Simon Cooksey |
0:fb7af294d5d9 | 65 | |
Simon Cooksey |
0:fb7af294d5d9 | 66 | int TCPSocket::send(const void *data, unsigned size) |
Simon Cooksey |
0:fb7af294d5d9 | 67 | { |
Simon Cooksey |
0:fb7af294d5d9 | 68 | _lock.lock(); |
Simon Cooksey |
0:fb7af294d5d9 | 69 | int ret; |
Simon Cooksey |
0:fb7af294d5d9 | 70 | |
Simon Cooksey |
0:fb7af294d5d9 | 71 | // If this assert is hit then there are two threads |
Simon Cooksey |
0:fb7af294d5d9 | 72 | // performing a send at the same time which is undefined |
Simon Cooksey |
0:fb7af294d5d9 | 73 | // behavior |
Simon Cooksey |
0:fb7af294d5d9 | 74 | MBED_ASSERT(!_write_in_progress); |
Simon Cooksey |
0:fb7af294d5d9 | 75 | _write_in_progress = true; |
Simon Cooksey |
0:fb7af294d5d9 | 76 | |
Simon Cooksey |
0:fb7af294d5d9 | 77 | while (true) { |
Simon Cooksey |
0:fb7af294d5d9 | 78 | if (!_socket) { |
Simon Cooksey |
0:fb7af294d5d9 | 79 | ret = NSAPI_ERROR_NO_SOCKET; |
Simon Cooksey |
0:fb7af294d5d9 | 80 | break; |
Simon Cooksey |
0:fb7af294d5d9 | 81 | } |
Simon Cooksey |
0:fb7af294d5d9 | 82 | |
Simon Cooksey |
0:fb7af294d5d9 | 83 | _pending = 0; |
Simon Cooksey |
0:fb7af294d5d9 | 84 | int sent = _stack->socket_send(_socket, data, size); |
Simon Cooksey |
0:fb7af294d5d9 | 85 | if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != sent)) { |
Simon Cooksey |
0:fb7af294d5d9 | 86 | ret = sent; |
Simon Cooksey |
0:fb7af294d5d9 | 87 | break; |
Simon Cooksey |
0:fb7af294d5d9 | 88 | } else { |
Simon Cooksey |
0:fb7af294d5d9 | 89 | int32_t count; |
Simon Cooksey |
0:fb7af294d5d9 | 90 | |
Simon Cooksey |
0:fb7af294d5d9 | 91 | // Release lock before blocking so other threads |
Simon Cooksey |
0:fb7af294d5d9 | 92 | // accessing this object aren't blocked |
Simon Cooksey |
0:fb7af294d5d9 | 93 | _lock.unlock(); |
Simon Cooksey |
0:fb7af294d5d9 | 94 | count = _write_sem.wait(_timeout); |
Simon Cooksey |
0:fb7af294d5d9 | 95 | _lock.lock(); |
Simon Cooksey |
0:fb7af294d5d9 | 96 | |
Simon Cooksey |
0:fb7af294d5d9 | 97 | if (count < 1) { |
Simon Cooksey |
0:fb7af294d5d9 | 98 | // Semaphore wait timed out so break out and return |
Simon Cooksey |
0:fb7af294d5d9 | 99 | ret = NSAPI_ERROR_WOULD_BLOCK; |
Simon Cooksey |
0:fb7af294d5d9 | 100 | break; |
Simon Cooksey |
0:fb7af294d5d9 | 101 | } |
Simon Cooksey |
0:fb7af294d5d9 | 102 | } |
Simon Cooksey |
0:fb7af294d5d9 | 103 | } |
Simon Cooksey |
0:fb7af294d5d9 | 104 | |
Simon Cooksey |
0:fb7af294d5d9 | 105 | _write_in_progress = false; |
Simon Cooksey |
0:fb7af294d5d9 | 106 | _lock.unlock(); |
Simon Cooksey |
0:fb7af294d5d9 | 107 | return ret; |
Simon Cooksey |
0:fb7af294d5d9 | 108 | } |
Simon Cooksey |
0:fb7af294d5d9 | 109 | |
Simon Cooksey |
0:fb7af294d5d9 | 110 | int TCPSocket::recv(void *data, unsigned size) |
Simon Cooksey |
0:fb7af294d5d9 | 111 | { |
Simon Cooksey |
0:fb7af294d5d9 | 112 | _lock.lock(); |
Simon Cooksey |
0:fb7af294d5d9 | 113 | int ret; |
Simon Cooksey |
0:fb7af294d5d9 | 114 | |
Simon Cooksey |
0:fb7af294d5d9 | 115 | // If this assert is hit then there are two threads |
Simon Cooksey |
0:fb7af294d5d9 | 116 | // performing a recv at the same time which is undefined |
Simon Cooksey |
0:fb7af294d5d9 | 117 | // behavior |
Simon Cooksey |
0:fb7af294d5d9 | 118 | MBED_ASSERT(!_read_in_progress); |
Simon Cooksey |
0:fb7af294d5d9 | 119 | _read_in_progress = true; |
Simon Cooksey |
0:fb7af294d5d9 | 120 | |
Simon Cooksey |
0:fb7af294d5d9 | 121 | while (true) { |
Simon Cooksey |
0:fb7af294d5d9 | 122 | if (!_socket) { |
Simon Cooksey |
0:fb7af294d5d9 | 123 | ret = NSAPI_ERROR_NO_SOCKET; |
Simon Cooksey |
0:fb7af294d5d9 | 124 | break; |
Simon Cooksey |
0:fb7af294d5d9 | 125 | } |
Simon Cooksey |
0:fb7af294d5d9 | 126 | |
Simon Cooksey |
0:fb7af294d5d9 | 127 | _pending = 0; |
Simon Cooksey |
0:fb7af294d5d9 | 128 | int recv = _stack->socket_recv(_socket, data, size); |
Simon Cooksey |
0:fb7af294d5d9 | 129 | if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != recv)) { |
Simon Cooksey |
0:fb7af294d5d9 | 130 | ret = recv; |
Simon Cooksey |
0:fb7af294d5d9 | 131 | break; |
Simon Cooksey |
0:fb7af294d5d9 | 132 | } else { |
Simon Cooksey |
0:fb7af294d5d9 | 133 | int32_t count; |
Simon Cooksey |
0:fb7af294d5d9 | 134 | |
Simon Cooksey |
0:fb7af294d5d9 | 135 | // Release lock before blocking so other threads |
Simon Cooksey |
0:fb7af294d5d9 | 136 | // accessing this object aren't blocked |
Simon Cooksey |
0:fb7af294d5d9 | 137 | _lock.unlock(); |
Simon Cooksey |
0:fb7af294d5d9 | 138 | count = _read_sem.wait(_timeout); |
Simon Cooksey |
0:fb7af294d5d9 | 139 | _lock.lock(); |
Simon Cooksey |
0:fb7af294d5d9 | 140 | |
Simon Cooksey |
0:fb7af294d5d9 | 141 | if (count < 1) { |
Simon Cooksey |
0:fb7af294d5d9 | 142 | // Semaphore wait timed out so break out and return |
Simon Cooksey |
0:fb7af294d5d9 | 143 | ret = NSAPI_ERROR_WOULD_BLOCK; |
Simon Cooksey |
0:fb7af294d5d9 | 144 | break; |
Simon Cooksey |
0:fb7af294d5d9 | 145 | } |
Simon Cooksey |
0:fb7af294d5d9 | 146 | } |
Simon Cooksey |
0:fb7af294d5d9 | 147 | } |
Simon Cooksey |
0:fb7af294d5d9 | 148 | |
Simon Cooksey |
0:fb7af294d5d9 | 149 | _read_in_progress = false; |
Simon Cooksey |
0:fb7af294d5d9 | 150 | _lock.unlock(); |
Simon Cooksey |
0:fb7af294d5d9 | 151 | return ret; |
Simon Cooksey |
0:fb7af294d5d9 | 152 | } |
Simon Cooksey |
0:fb7af294d5d9 | 153 | |
Simon Cooksey |
0:fb7af294d5d9 | 154 | void TCPSocket::event() |
Simon Cooksey |
0:fb7af294d5d9 | 155 | { |
Simon Cooksey |
0:fb7af294d5d9 | 156 | int32_t wcount = _write_sem.wait(0); |
Simon Cooksey |
0:fb7af294d5d9 | 157 | if (wcount <= 1) { |
Simon Cooksey |
0:fb7af294d5d9 | 158 | _write_sem.release(); |
Simon Cooksey |
0:fb7af294d5d9 | 159 | } |
Simon Cooksey |
0:fb7af294d5d9 | 160 | int32_t rcount = _read_sem.wait(0); |
Simon Cooksey |
0:fb7af294d5d9 | 161 | if (rcount <= 1) { |
Simon Cooksey |
0:fb7af294d5d9 | 162 | _read_sem.release(); |
Simon Cooksey |
0:fb7af294d5d9 | 163 | } |
Simon Cooksey |
0:fb7af294d5d9 | 164 | |
Simon Cooksey |
0:fb7af294d5d9 | 165 | _pending += 1; |
Simon Cooksey |
0:fb7af294d5d9 | 166 | if (_callback && _pending == 1) { |
Simon Cooksey |
0:fb7af294d5d9 | 167 | _callback(); |
Simon Cooksey |
0:fb7af294d5d9 | 168 | } |
Simon Cooksey |
0:fb7af294d5d9 | 169 | } |