mbed-os

Fork of mbed-os by erkin yucel

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?

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