Pinned to some recent date
features/netsocket/UDPSocket.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 "UDPSocket.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 | UDPSocket::UDPSocket() |
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 | UDPSocket::~UDPSocket() |
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 UDPSocket::get_proto() |
Simon Cooksey |
0:fb7af294d5d9 | 33 | { |
Simon Cooksey |
0:fb7af294d5d9 | 34 | return NSAPI_UDP; |
Simon Cooksey |
0:fb7af294d5d9 | 35 | } |
Simon Cooksey |
0:fb7af294d5d9 | 36 | |
Simon Cooksey |
0:fb7af294d5d9 | 37 | int UDPSocket::sendto(const char *host, uint16_t port, const void *data, unsigned size) |
Simon Cooksey |
0:fb7af294d5d9 | 38 | { |
Simon Cooksey |
0:fb7af294d5d9 | 39 | SocketAddress address; |
Simon Cooksey |
0:fb7af294d5d9 | 40 | int err = _stack->gethostbyname(host, &address); |
Simon Cooksey |
0:fb7af294d5d9 | 41 | if (err) { |
Simon Cooksey |
0:fb7af294d5d9 | 42 | return NSAPI_ERROR_DNS_FAILURE; |
Simon Cooksey |
0:fb7af294d5d9 | 43 | } |
Simon Cooksey |
0:fb7af294d5d9 | 44 | |
Simon Cooksey |
0:fb7af294d5d9 | 45 | address.set_port(port); |
Simon Cooksey |
0:fb7af294d5d9 | 46 | |
Simon Cooksey |
0:fb7af294d5d9 | 47 | // sendto is thread safe |
Simon Cooksey |
0:fb7af294d5d9 | 48 | return sendto(address, data, size); |
Simon Cooksey |
0:fb7af294d5d9 | 49 | } |
Simon Cooksey |
0:fb7af294d5d9 | 50 | |
Simon Cooksey |
0:fb7af294d5d9 | 51 | int UDPSocket::sendto(const SocketAddress &address, const void *data, unsigned size) |
Simon Cooksey |
0:fb7af294d5d9 | 52 | { |
Simon Cooksey |
0:fb7af294d5d9 | 53 | _lock.lock(); |
Simon Cooksey |
0:fb7af294d5d9 | 54 | int ret; |
Simon Cooksey |
0:fb7af294d5d9 | 55 | |
Simon Cooksey |
0:fb7af294d5d9 | 56 | // If this assert is hit then there are two threads |
Simon Cooksey |
0:fb7af294d5d9 | 57 | // performing a send at the same time which is undefined |
Simon Cooksey |
0:fb7af294d5d9 | 58 | // behavior |
Simon Cooksey |
0:fb7af294d5d9 | 59 | MBED_ASSERT(!_write_in_progress); |
Simon Cooksey |
0:fb7af294d5d9 | 60 | _write_in_progress = true; |
Simon Cooksey |
0:fb7af294d5d9 | 61 | |
Simon Cooksey |
0:fb7af294d5d9 | 62 | while (true) { |
Simon Cooksey |
0:fb7af294d5d9 | 63 | if (!_socket) { |
Simon Cooksey |
0:fb7af294d5d9 | 64 | ret = NSAPI_ERROR_NO_SOCKET; |
Simon Cooksey |
0:fb7af294d5d9 | 65 | break; |
Simon Cooksey |
0:fb7af294d5d9 | 66 | } |
Simon Cooksey |
0:fb7af294d5d9 | 67 | |
Simon Cooksey |
0:fb7af294d5d9 | 68 | _pending = 0; |
Simon Cooksey |
0:fb7af294d5d9 | 69 | int sent = _stack->socket_sendto(_socket, address, data, size); |
Simon Cooksey |
0:fb7af294d5d9 | 70 | if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != sent)) { |
Simon Cooksey |
0:fb7af294d5d9 | 71 | ret = sent; |
Simon Cooksey |
0:fb7af294d5d9 | 72 | break; |
Simon Cooksey |
0:fb7af294d5d9 | 73 | } else { |
Simon Cooksey |
0:fb7af294d5d9 | 74 | int32_t count; |
Simon Cooksey |
0:fb7af294d5d9 | 75 | |
Simon Cooksey |
0:fb7af294d5d9 | 76 | // Release lock before blocking so other threads |
Simon Cooksey |
0:fb7af294d5d9 | 77 | // accessing this object aren't blocked |
Simon Cooksey |
0:fb7af294d5d9 | 78 | _lock.unlock(); |
Simon Cooksey |
0:fb7af294d5d9 | 79 | count = _write_sem.wait(_timeout); |
Simon Cooksey |
0:fb7af294d5d9 | 80 | _lock.lock(); |
Simon Cooksey |
0:fb7af294d5d9 | 81 | |
Simon Cooksey |
0:fb7af294d5d9 | 82 | if (count < 1) { |
Simon Cooksey |
0:fb7af294d5d9 | 83 | // Semaphore wait timed out so break out and return |
Simon Cooksey |
0:fb7af294d5d9 | 84 | ret = NSAPI_ERROR_WOULD_BLOCK; |
Simon Cooksey |
0:fb7af294d5d9 | 85 | break; |
Simon Cooksey |
0:fb7af294d5d9 | 86 | } |
Simon Cooksey |
0:fb7af294d5d9 | 87 | } |
Simon Cooksey |
0:fb7af294d5d9 | 88 | } |
Simon Cooksey |
0:fb7af294d5d9 | 89 | |
Simon Cooksey |
0:fb7af294d5d9 | 90 | _write_in_progress = false; |
Simon Cooksey |
0:fb7af294d5d9 | 91 | _lock.unlock(); |
Simon Cooksey |
0:fb7af294d5d9 | 92 | return ret; |
Simon Cooksey |
0:fb7af294d5d9 | 93 | } |
Simon Cooksey |
0:fb7af294d5d9 | 94 | |
Simon Cooksey |
0:fb7af294d5d9 | 95 | int UDPSocket::recvfrom(SocketAddress *address, void *buffer, unsigned size) |
Simon Cooksey |
0:fb7af294d5d9 | 96 | { |
Simon Cooksey |
0:fb7af294d5d9 | 97 | _lock.lock(); |
Simon Cooksey |
0:fb7af294d5d9 | 98 | int ret; |
Simon Cooksey |
0:fb7af294d5d9 | 99 | |
Simon Cooksey |
0:fb7af294d5d9 | 100 | // If this assert is hit then there are two threads |
Simon Cooksey |
0:fb7af294d5d9 | 101 | // performing a recv at the same time which is undefined |
Simon Cooksey |
0:fb7af294d5d9 | 102 | // behavior |
Simon Cooksey |
0:fb7af294d5d9 | 103 | MBED_ASSERT(!_read_in_progress); |
Simon Cooksey |
0:fb7af294d5d9 | 104 | _read_in_progress = true; |
Simon Cooksey |
0:fb7af294d5d9 | 105 | |
Simon Cooksey |
0:fb7af294d5d9 | 106 | while (true) { |
Simon Cooksey |
0:fb7af294d5d9 | 107 | if (!_socket) { |
Simon Cooksey |
0:fb7af294d5d9 | 108 | ret = NSAPI_ERROR_NO_SOCKET; |
Simon Cooksey |
0:fb7af294d5d9 | 109 | break; |
Simon Cooksey |
0:fb7af294d5d9 | 110 | } |
Simon Cooksey |
0:fb7af294d5d9 | 111 | |
Simon Cooksey |
0:fb7af294d5d9 | 112 | _pending = 0; |
Simon Cooksey |
0:fb7af294d5d9 | 113 | int recv = _stack->socket_recvfrom(_socket, address, buffer, size); |
Simon Cooksey |
0:fb7af294d5d9 | 114 | if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != recv)) { |
Simon Cooksey |
0:fb7af294d5d9 | 115 | ret = recv; |
Simon Cooksey |
0:fb7af294d5d9 | 116 | break; |
Simon Cooksey |
0:fb7af294d5d9 | 117 | } else { |
Simon Cooksey |
0:fb7af294d5d9 | 118 | int32_t count; |
Simon Cooksey |
0:fb7af294d5d9 | 119 | |
Simon Cooksey |
0:fb7af294d5d9 | 120 | // Release lock before blocking so other threads |
Simon Cooksey |
0:fb7af294d5d9 | 121 | // accessing this object aren't blocked |
Simon Cooksey |
0:fb7af294d5d9 | 122 | _lock.unlock(); |
Simon Cooksey |
0:fb7af294d5d9 | 123 | count = _read_sem.wait(_timeout); |
Simon Cooksey |
0:fb7af294d5d9 | 124 | _lock.lock(); |
Simon Cooksey |
0:fb7af294d5d9 | 125 | |
Simon Cooksey |
0:fb7af294d5d9 | 126 | if (count < 1) { |
Simon Cooksey |
0:fb7af294d5d9 | 127 | // Semaphore wait timed out so break out and return |
Simon Cooksey |
0:fb7af294d5d9 | 128 | ret = NSAPI_ERROR_WOULD_BLOCK; |
Simon Cooksey |
0:fb7af294d5d9 | 129 | break; |
Simon Cooksey |
0:fb7af294d5d9 | 130 | } |
Simon Cooksey |
0:fb7af294d5d9 | 131 | } |
Simon Cooksey |
0:fb7af294d5d9 | 132 | } |
Simon Cooksey |
0:fb7af294d5d9 | 133 | |
Simon Cooksey |
0:fb7af294d5d9 | 134 | _read_in_progress = false; |
Simon Cooksey |
0:fb7af294d5d9 | 135 | _lock.unlock(); |
Simon Cooksey |
0:fb7af294d5d9 | 136 | return ret; |
Simon Cooksey |
0:fb7af294d5d9 | 137 | } |
Simon Cooksey |
0:fb7af294d5d9 | 138 | |
Simon Cooksey |
0:fb7af294d5d9 | 139 | void UDPSocket::event() |
Simon Cooksey |
0:fb7af294d5d9 | 140 | { |
Simon Cooksey |
0:fb7af294d5d9 | 141 | int32_t wcount = _write_sem.wait(0); |
Simon Cooksey |
0:fb7af294d5d9 | 142 | if (wcount <= 1) { |
Simon Cooksey |
0:fb7af294d5d9 | 143 | _write_sem.release(); |
Simon Cooksey |
0:fb7af294d5d9 | 144 | } |
Simon Cooksey |
0:fb7af294d5d9 | 145 | int32_t rcount = _read_sem.wait(0); |
Simon Cooksey |
0:fb7af294d5d9 | 146 | if (rcount <= 1) { |
Simon Cooksey |
0:fb7af294d5d9 | 147 | _read_sem.release(); |
Simon Cooksey |
0:fb7af294d5d9 | 148 | } |
Simon Cooksey |
0:fb7af294d5d9 | 149 | |
Simon Cooksey |
0:fb7af294d5d9 | 150 | _pending += 1; |
Simon Cooksey |
0:fb7af294d5d9 | 151 | if (_callback && _pending == 1) { |
Simon Cooksey |
0:fb7af294d5d9 | 152 | _callback(); |
Simon Cooksey |
0:fb7af294d5d9 | 153 | } |
Simon Cooksey |
0:fb7af294d5d9 | 154 | } |