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