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