RTC auf true
features/netsocket/TCPServer.cpp@2:7aab896b1a3b, 2019-03-13 (annotated)
- Committer:
- kevman
- Date:
- Wed Mar 13 11:03:24 2019 +0000
- Revision:
- 2:7aab896b1a3b
- Parent:
- 0:38ceb79fef03
2019-03-13
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kevman | 0:38ceb79fef03 | 1 | /* Socket |
kevman | 0:38ceb79fef03 | 2 | * Copyright (c) 2015 ARM Limited |
kevman | 0:38ceb79fef03 | 3 | * |
kevman | 0:38ceb79fef03 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kevman | 0:38ceb79fef03 | 5 | * you may not use this file except in compliance with the License. |
kevman | 0:38ceb79fef03 | 6 | * You may obtain a copy of the License at |
kevman | 0:38ceb79fef03 | 7 | * |
kevman | 0:38ceb79fef03 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kevman | 0:38ceb79fef03 | 9 | * |
kevman | 0:38ceb79fef03 | 10 | * Unless required by applicable law or agreed to in writing, software |
kevman | 0:38ceb79fef03 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
kevman | 0:38ceb79fef03 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kevman | 0:38ceb79fef03 | 13 | * See the License for the specific language governing permissions and |
kevman | 0:38ceb79fef03 | 14 | * limitations under the License. |
kevman | 0:38ceb79fef03 | 15 | */ |
kevman | 0:38ceb79fef03 | 16 | |
kevman | 0:38ceb79fef03 | 17 | #include "TCPServer.h" |
kevman | 0:38ceb79fef03 | 18 | |
kevman | 0:38ceb79fef03 | 19 | using mbed::Callback; |
kevman | 0:38ceb79fef03 | 20 | |
kevman | 0:38ceb79fef03 | 21 | TCPServer::TCPServer() |
kevman | 0:38ceb79fef03 | 22 | { |
kevman | 0:38ceb79fef03 | 23 | } |
kevman | 0:38ceb79fef03 | 24 | |
kevman | 0:38ceb79fef03 | 25 | TCPServer::~TCPServer() |
kevman | 0:38ceb79fef03 | 26 | { |
kevman | 0:38ceb79fef03 | 27 | } |
kevman | 0:38ceb79fef03 | 28 | |
kevman | 0:38ceb79fef03 | 29 | nsapi_error_t TCPServer::accept(TCPSocket *connection, SocketAddress *address) |
kevman | 0:38ceb79fef03 | 30 | { |
kevman | 0:38ceb79fef03 | 31 | _lock.lock(); |
kevman | 0:38ceb79fef03 | 32 | nsapi_error_t ret; |
kevman | 0:38ceb79fef03 | 33 | |
kevman | 0:38ceb79fef03 | 34 | while (true) { |
kevman | 0:38ceb79fef03 | 35 | if (!_socket) { |
kevman | 0:38ceb79fef03 | 36 | ret = NSAPI_ERROR_NO_SOCKET; |
kevman | 0:38ceb79fef03 | 37 | break; |
kevman | 0:38ceb79fef03 | 38 | } |
kevman | 0:38ceb79fef03 | 39 | |
kevman | 0:38ceb79fef03 | 40 | _pending = 0; |
kevman | 0:38ceb79fef03 | 41 | void *socket; |
kevman | 0:38ceb79fef03 | 42 | ret = _stack->socket_accept(_socket, &socket, address); |
kevman | 0:38ceb79fef03 | 43 | |
kevman | 0:38ceb79fef03 | 44 | if (0 == ret) { |
kevman | 0:38ceb79fef03 | 45 | connection->_lock.lock(); |
kevman | 0:38ceb79fef03 | 46 | |
kevman | 0:38ceb79fef03 | 47 | if (connection->_socket) { |
kevman | 0:38ceb79fef03 | 48 | connection->close(); |
kevman | 0:38ceb79fef03 | 49 | } |
kevman | 0:38ceb79fef03 | 50 | |
kevman | 0:38ceb79fef03 | 51 | connection->_stack = _stack; |
kevman | 0:38ceb79fef03 | 52 | connection->_socket = socket; |
kevman | 0:38ceb79fef03 | 53 | connection->_event = Callback<void()>(connection, &TCPSocket::event); |
kevman | 0:38ceb79fef03 | 54 | _stack->socket_attach(socket, &Callback<void()>::thunk, &connection->_event); |
kevman | 0:38ceb79fef03 | 55 | |
kevman | 0:38ceb79fef03 | 56 | connection->_lock.unlock(); |
kevman | 0:38ceb79fef03 | 57 | break; |
kevman | 0:38ceb79fef03 | 58 | } else if ((_timeout == 0) || (ret != NSAPI_ERROR_WOULD_BLOCK)) { |
kevman | 0:38ceb79fef03 | 59 | break; |
kevman | 0:38ceb79fef03 | 60 | } else { |
kevman | 0:38ceb79fef03 | 61 | uint32_t flag; |
kevman | 0:38ceb79fef03 | 62 | |
kevman | 0:38ceb79fef03 | 63 | // Release lock before blocking so other threads |
kevman | 0:38ceb79fef03 | 64 | // accessing this object aren't blocked |
kevman | 0:38ceb79fef03 | 65 | _lock.unlock(); |
kevman | 0:38ceb79fef03 | 66 | flag = _event_flag.wait_any(READ_FLAG, _timeout); |
kevman | 0:38ceb79fef03 | 67 | _lock.lock(); |
kevman | 0:38ceb79fef03 | 68 | |
kevman | 0:38ceb79fef03 | 69 | if (flag & osFlagsError) { |
kevman | 0:38ceb79fef03 | 70 | // Timeout break |
kevman | 0:38ceb79fef03 | 71 | ret = NSAPI_ERROR_WOULD_BLOCK; |
kevman | 0:38ceb79fef03 | 72 | break; |
kevman | 0:38ceb79fef03 | 73 | } |
kevman | 0:38ceb79fef03 | 74 | } |
kevman | 0:38ceb79fef03 | 75 | } |
kevman | 0:38ceb79fef03 | 76 | |
kevman | 0:38ceb79fef03 | 77 | _lock.unlock(); |
kevman | 0:38ceb79fef03 | 78 | return ret; |
kevman | 0:38ceb79fef03 | 79 | } |