Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TCPServer.cpp Source File

TCPServer.cpp

00001 /* Socket
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "TCPServer.h"
00018 
00019 using mbed::Callback;
00020 
00021 TCPServer::TCPServer()
00022 {
00023     _socket_stats.stats_update_proto(this, NSAPI_TCP );
00024 }
00025 
00026 TCPServer::~TCPServer()
00027 {
00028 }
00029 
00030 nsapi_error_t TCPServer::accept(TCPSocket *connection, SocketAddress *address)
00031 {
00032     _lock.lock();
00033     nsapi_error_t ret;
00034 
00035     while (true) {
00036         if (!_socket) {
00037             ret = NSAPI_ERROR_NO_SOCKET ;
00038             break;
00039         }
00040 
00041         core_util_atomic_flag_clear(&_pending);
00042         void *socket;
00043         ret = _stack->socket_accept(_socket, &socket, address);
00044 
00045         if (0 == ret) {
00046             connection->_lock.lock();
00047 
00048             if (connection->_socket) {
00049                 connection->close();
00050             }
00051 
00052             connection->_stack = _stack;
00053             connection->_socket = socket;
00054             connection->_event = Callback<void()>(connection, &TCPSocket::event);
00055             _stack->socket_attach(socket, &Callback<void()>::thunk, &connection->_event);
00056             _socket_stats.stats_update_peer(connection, *address);
00057             _socket_stats.stats_update_socket_state(connection, SOCK_CONNECTED);
00058             connection->_lock.unlock();
00059             break;
00060         } else if ((_timeout == 0) || (ret != NSAPI_ERROR_WOULD_BLOCK )) {
00061             break;
00062         } else {
00063             uint32_t flag;
00064 
00065             // Release lock before blocking so other threads
00066             // accessing this object aren't blocked
00067             _lock.unlock();
00068             flag = _event_flag.wait_any(READ_FLAG, _timeout);
00069             _lock.lock();
00070 
00071             if (flag & osFlagsError) {
00072                 // Timeout break
00073                 ret = NSAPI_ERROR_WOULD_BLOCK ;
00074                 break;
00075             }
00076         }
00077     }
00078 
00079     _lock.unlock();
00080     return ret;
00081 }