BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:fbdae7e6d805 1 /* Socket
borlanic 0:fbdae7e6d805 2 * Copyright (c) 2015 ARM Limited
borlanic 0:fbdae7e6d805 3 *
borlanic 0:fbdae7e6d805 4 * Licensed under the Apache License, Version 2.0 (the "License");
borlanic 0:fbdae7e6d805 5 * you may not use this file except in compliance with the License.
borlanic 0:fbdae7e6d805 6 * You may obtain a copy of the License at
borlanic 0:fbdae7e6d805 7 *
borlanic 0:fbdae7e6d805 8 * http://www.apache.org/licenses/LICENSE-2.0
borlanic 0:fbdae7e6d805 9 *
borlanic 0:fbdae7e6d805 10 * Unless required by applicable law or agreed to in writing, software
borlanic 0:fbdae7e6d805 11 * distributed under the License is distributed on an "AS IS" BASIS,
borlanic 0:fbdae7e6d805 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
borlanic 0:fbdae7e6d805 13 * See the License for the specific language governing permissions and
borlanic 0:fbdae7e6d805 14 * limitations under the License.
borlanic 0:fbdae7e6d805 15 */
borlanic 0:fbdae7e6d805 16
borlanic 0:fbdae7e6d805 17 #include "UDPSocket.h"
borlanic 0:fbdae7e6d805 18 #include "Timer.h"
borlanic 0:fbdae7e6d805 19 #include "mbed_assert.h"
borlanic 0:fbdae7e6d805 20
borlanic 0:fbdae7e6d805 21 #define TCP_EVENT "UDP_Events"
borlanic 0:fbdae7e6d805 22 #define READ_FLAG 0x1u
borlanic 0:fbdae7e6d805 23 #define WRITE_FLAG 0x2u
borlanic 0:fbdae7e6d805 24
borlanic 0:fbdae7e6d805 25 UDPSocket::UDPSocket()
borlanic 0:fbdae7e6d805 26 : _pending(0), _event_flag()
borlanic 0:fbdae7e6d805 27 {
borlanic 0:fbdae7e6d805 28 }
borlanic 0:fbdae7e6d805 29
borlanic 0:fbdae7e6d805 30 UDPSocket::~UDPSocket()
borlanic 0:fbdae7e6d805 31 {
borlanic 0:fbdae7e6d805 32 close();
borlanic 0:fbdae7e6d805 33 }
borlanic 0:fbdae7e6d805 34
borlanic 0:fbdae7e6d805 35 nsapi_protocol_t UDPSocket::get_proto()
borlanic 0:fbdae7e6d805 36 {
borlanic 0:fbdae7e6d805 37 return NSAPI_UDP;
borlanic 0:fbdae7e6d805 38 }
borlanic 0:fbdae7e6d805 39
borlanic 0:fbdae7e6d805 40
borlanic 0:fbdae7e6d805 41 nsapi_size_or_error_t UDPSocket::sendto(const char *host, uint16_t port, const void *data, nsapi_size_t size)
borlanic 0:fbdae7e6d805 42 {
borlanic 0:fbdae7e6d805 43 SocketAddress address;
borlanic 0:fbdae7e6d805 44 nsapi_size_or_error_t err = _stack->gethostbyname(host, &address);
borlanic 0:fbdae7e6d805 45 if (err) {
borlanic 0:fbdae7e6d805 46 return NSAPI_ERROR_DNS_FAILURE;
borlanic 0:fbdae7e6d805 47 }
borlanic 0:fbdae7e6d805 48
borlanic 0:fbdae7e6d805 49 address.set_port(port);
borlanic 0:fbdae7e6d805 50
borlanic 0:fbdae7e6d805 51 // sendto is thread safe
borlanic 0:fbdae7e6d805 52 return sendto(address, data, size);
borlanic 0:fbdae7e6d805 53 }
borlanic 0:fbdae7e6d805 54
borlanic 0:fbdae7e6d805 55 nsapi_size_or_error_t UDPSocket::sendto(const SocketAddress &address, const void *data, nsapi_size_t size)
borlanic 0:fbdae7e6d805 56 {
borlanic 0:fbdae7e6d805 57 _lock.lock();
borlanic 0:fbdae7e6d805 58 nsapi_size_or_error_t ret;
borlanic 0:fbdae7e6d805 59
borlanic 0:fbdae7e6d805 60 while (true) {
borlanic 0:fbdae7e6d805 61 if (!_socket) {
borlanic 0:fbdae7e6d805 62 ret = NSAPI_ERROR_NO_SOCKET;
borlanic 0:fbdae7e6d805 63 break;
borlanic 0:fbdae7e6d805 64 }
borlanic 0:fbdae7e6d805 65
borlanic 0:fbdae7e6d805 66 _pending = 0;
borlanic 0:fbdae7e6d805 67 nsapi_size_or_error_t sent = _stack->socket_sendto(_socket, address, data, size);
borlanic 0:fbdae7e6d805 68 if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != sent)) {
borlanic 0:fbdae7e6d805 69 ret = sent;
borlanic 0:fbdae7e6d805 70 break;
borlanic 0:fbdae7e6d805 71 } else {
borlanic 0:fbdae7e6d805 72 uint32_t flag;
borlanic 0:fbdae7e6d805 73
borlanic 0:fbdae7e6d805 74 // Release lock before blocking so other threads
borlanic 0:fbdae7e6d805 75 // accessing this object aren't blocked
borlanic 0:fbdae7e6d805 76 _lock.unlock();
borlanic 0:fbdae7e6d805 77 flag = _event_flag.wait_any(WRITE_FLAG, _timeout);
borlanic 0:fbdae7e6d805 78 _lock.lock();
borlanic 0:fbdae7e6d805 79
borlanic 0:fbdae7e6d805 80 if (flag & osFlagsError) {
borlanic 0:fbdae7e6d805 81 // Timeout break
borlanic 0:fbdae7e6d805 82 ret = NSAPI_ERROR_WOULD_BLOCK;
borlanic 0:fbdae7e6d805 83 break;
borlanic 0:fbdae7e6d805 84 }
borlanic 0:fbdae7e6d805 85 }
borlanic 0:fbdae7e6d805 86 }
borlanic 0:fbdae7e6d805 87
borlanic 0:fbdae7e6d805 88 _lock.unlock();
borlanic 0:fbdae7e6d805 89 return ret;
borlanic 0:fbdae7e6d805 90 }
borlanic 0:fbdae7e6d805 91
borlanic 0:fbdae7e6d805 92 nsapi_size_or_error_t UDPSocket::recvfrom(SocketAddress *address, void *buffer, nsapi_size_t size)
borlanic 0:fbdae7e6d805 93 {
borlanic 0:fbdae7e6d805 94 _lock.lock();
borlanic 0:fbdae7e6d805 95 nsapi_size_or_error_t ret;
borlanic 0:fbdae7e6d805 96
borlanic 0:fbdae7e6d805 97 while (true) {
borlanic 0:fbdae7e6d805 98 if (!_socket) {
borlanic 0:fbdae7e6d805 99 ret = NSAPI_ERROR_NO_SOCKET;
borlanic 0:fbdae7e6d805 100 break;
borlanic 0:fbdae7e6d805 101 }
borlanic 0:fbdae7e6d805 102
borlanic 0:fbdae7e6d805 103 _pending = 0;
borlanic 0:fbdae7e6d805 104 nsapi_size_or_error_t recv = _stack->socket_recvfrom(_socket, address, buffer, size);
borlanic 0:fbdae7e6d805 105 if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != recv)) {
borlanic 0:fbdae7e6d805 106 ret = recv;
borlanic 0:fbdae7e6d805 107 break;
borlanic 0:fbdae7e6d805 108 } else {
borlanic 0:fbdae7e6d805 109 uint32_t flag;
borlanic 0:fbdae7e6d805 110
borlanic 0:fbdae7e6d805 111 // Release lock before blocking so other threads
borlanic 0:fbdae7e6d805 112 // accessing this object aren't blocked
borlanic 0:fbdae7e6d805 113 _lock.unlock();
borlanic 0:fbdae7e6d805 114 flag = _event_flag.wait_any(READ_FLAG, _timeout);
borlanic 0:fbdae7e6d805 115 _lock.lock();
borlanic 0:fbdae7e6d805 116
borlanic 0:fbdae7e6d805 117 if (flag & osFlagsError) {
borlanic 0:fbdae7e6d805 118 // Timeout break
borlanic 0:fbdae7e6d805 119 ret = NSAPI_ERROR_WOULD_BLOCK;
borlanic 0:fbdae7e6d805 120 break;
borlanic 0:fbdae7e6d805 121 }
borlanic 0:fbdae7e6d805 122 }
borlanic 0:fbdae7e6d805 123 }
borlanic 0:fbdae7e6d805 124
borlanic 0:fbdae7e6d805 125 _lock.unlock();
borlanic 0:fbdae7e6d805 126 return ret;
borlanic 0:fbdae7e6d805 127 }
borlanic 0:fbdae7e6d805 128
borlanic 0:fbdae7e6d805 129 void UDPSocket::event()
borlanic 0:fbdae7e6d805 130 {
borlanic 0:fbdae7e6d805 131 _event_flag.set(READ_FLAG|WRITE_FLAG);
borlanic 0:fbdae7e6d805 132
borlanic 0:fbdae7e6d805 133 _pending += 1;
borlanic 0:fbdae7e6d805 134 if (_callback && _pending == 1) {
borlanic 0:fbdae7e6d805 135 _callback();
borlanic 0:fbdae7e6d805 136 }
borlanic 0:fbdae7e6d805 137 }