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 "TCPServer.h"
borlanic 0:fbdae7e6d805 18 #include "mbed.h"
borlanic 0:fbdae7e6d805 19
borlanic 0:fbdae7e6d805 20 TCPServer::TCPServer()
borlanic 0:fbdae7e6d805 21 : _pending(0), _accept_sem(0)
borlanic 0:fbdae7e6d805 22 {
borlanic 0:fbdae7e6d805 23 }
borlanic 0:fbdae7e6d805 24
borlanic 0:fbdae7e6d805 25 TCPServer::~TCPServer()
borlanic 0:fbdae7e6d805 26 {
borlanic 0:fbdae7e6d805 27 close();
borlanic 0:fbdae7e6d805 28 }
borlanic 0:fbdae7e6d805 29
borlanic 0:fbdae7e6d805 30 nsapi_protocol_t TCPServer::get_proto()
borlanic 0:fbdae7e6d805 31 {
borlanic 0:fbdae7e6d805 32 return NSAPI_TCP;
borlanic 0:fbdae7e6d805 33 }
borlanic 0:fbdae7e6d805 34
borlanic 0:fbdae7e6d805 35 nsapi_error_t TCPServer::listen(int backlog)
borlanic 0:fbdae7e6d805 36 {
borlanic 0:fbdae7e6d805 37 _lock.lock();
borlanic 0:fbdae7e6d805 38 nsapi_error_t ret;
borlanic 0:fbdae7e6d805 39
borlanic 0:fbdae7e6d805 40 if (!_socket) {
borlanic 0:fbdae7e6d805 41 ret = NSAPI_ERROR_NO_SOCKET;
borlanic 0:fbdae7e6d805 42 } else {
borlanic 0:fbdae7e6d805 43 ret = _stack->socket_listen(_socket, backlog);
borlanic 0:fbdae7e6d805 44 }
borlanic 0:fbdae7e6d805 45
borlanic 0:fbdae7e6d805 46 _lock.unlock();
borlanic 0:fbdae7e6d805 47 return ret;
borlanic 0:fbdae7e6d805 48 }
borlanic 0:fbdae7e6d805 49
borlanic 0:fbdae7e6d805 50 nsapi_error_t TCPServer::accept(TCPSocket *connection, SocketAddress *address)
borlanic 0:fbdae7e6d805 51 {
borlanic 0:fbdae7e6d805 52 _lock.lock();
borlanic 0:fbdae7e6d805 53 nsapi_error_t ret;
borlanic 0:fbdae7e6d805 54
borlanic 0:fbdae7e6d805 55 while (true) {
borlanic 0:fbdae7e6d805 56 if (!_socket) {
borlanic 0:fbdae7e6d805 57 ret = NSAPI_ERROR_NO_SOCKET;
borlanic 0:fbdae7e6d805 58 break;
borlanic 0:fbdae7e6d805 59 }
borlanic 0:fbdae7e6d805 60
borlanic 0:fbdae7e6d805 61 _pending = 0;
borlanic 0:fbdae7e6d805 62 void *socket;
borlanic 0:fbdae7e6d805 63 ret = _stack->socket_accept(_socket, &socket, address);
borlanic 0:fbdae7e6d805 64
borlanic 0:fbdae7e6d805 65 if (0 == ret) {
borlanic 0:fbdae7e6d805 66 connection->_lock.lock();
borlanic 0:fbdae7e6d805 67
borlanic 0:fbdae7e6d805 68 if (connection->_socket) {
borlanic 0:fbdae7e6d805 69 connection->close();
borlanic 0:fbdae7e6d805 70 }
borlanic 0:fbdae7e6d805 71
borlanic 0:fbdae7e6d805 72 connection->_stack = _stack;
borlanic 0:fbdae7e6d805 73 connection->_socket = socket;
borlanic 0:fbdae7e6d805 74 connection->_event = Callback<void()>(connection, &TCPSocket::event);
borlanic 0:fbdae7e6d805 75 _stack->socket_attach(socket, &Callback<void()>::thunk, &connection->_event);
borlanic 0:fbdae7e6d805 76
borlanic 0:fbdae7e6d805 77 connection->_lock.unlock();
borlanic 0:fbdae7e6d805 78 break;
borlanic 0:fbdae7e6d805 79 } else if (NSAPI_ERROR_WOULD_BLOCK != ret) {
borlanic 0:fbdae7e6d805 80 break;
borlanic 0:fbdae7e6d805 81 } else {
borlanic 0:fbdae7e6d805 82 int32_t count;
borlanic 0:fbdae7e6d805 83
borlanic 0:fbdae7e6d805 84 // Release lock before blocking so other threads
borlanic 0:fbdae7e6d805 85 // accessing this object aren't blocked
borlanic 0:fbdae7e6d805 86 _lock.unlock();
borlanic 0:fbdae7e6d805 87 count = _accept_sem.wait(_timeout);
borlanic 0:fbdae7e6d805 88 _lock.lock();
borlanic 0:fbdae7e6d805 89
borlanic 0:fbdae7e6d805 90 if (count < 1) {
borlanic 0:fbdae7e6d805 91 // Semaphore wait timed out so break out and return
borlanic 0:fbdae7e6d805 92 ret = NSAPI_ERROR_WOULD_BLOCK;
borlanic 0:fbdae7e6d805 93 break;
borlanic 0:fbdae7e6d805 94 }
borlanic 0:fbdae7e6d805 95 }
borlanic 0:fbdae7e6d805 96 }
borlanic 0:fbdae7e6d805 97
borlanic 0:fbdae7e6d805 98 _lock.unlock();
borlanic 0:fbdae7e6d805 99 return ret;
borlanic 0:fbdae7e6d805 100 }
borlanic 0:fbdae7e6d805 101
borlanic 0:fbdae7e6d805 102 void TCPServer::event()
borlanic 0:fbdae7e6d805 103 {
borlanic 0:fbdae7e6d805 104 int32_t acount = _accept_sem.wait(0);
borlanic 0:fbdae7e6d805 105 if (acount <= 1) {
borlanic 0:fbdae7e6d805 106 _accept_sem.release();
borlanic 0:fbdae7e6d805 107 }
borlanic 0:fbdae7e6d805 108
borlanic 0:fbdae7e6d805 109 _pending += 1;
borlanic 0:fbdae7e6d805 110 if (_callback && _pending == 1) {
borlanic 0:fbdae7e6d805 111 _callback();
borlanic 0:fbdae7e6d805 112 }
borlanic 0:fbdae7e6d805 113 }