Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 00:01:50 2018 +0000
Revision:
0:6ad07c9019fd
Codigo de tales para todos los pasculaes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bethory 0:6ad07c9019fd 1 /* Socket
Bethory 0:6ad07c9019fd 2 * Copyright (c) 2015 ARM Limited
Bethory 0:6ad07c9019fd 3 *
Bethory 0:6ad07c9019fd 4 * Licensed under the Apache License, Version 2.0 (the "License");
Bethory 0:6ad07c9019fd 5 * you may not use this file except in compliance with the License.
Bethory 0:6ad07c9019fd 6 * You may obtain a copy of the License at
Bethory 0:6ad07c9019fd 7 *
Bethory 0:6ad07c9019fd 8 * http://www.apache.org/licenses/LICENSE-2.0
Bethory 0:6ad07c9019fd 9 *
Bethory 0:6ad07c9019fd 10 * Unless required by applicable law or agreed to in writing, software
Bethory 0:6ad07c9019fd 11 * distributed under the License is distributed on an "AS IS" BASIS,
Bethory 0:6ad07c9019fd 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Bethory 0:6ad07c9019fd 13 * See the License for the specific language governing permissions and
Bethory 0:6ad07c9019fd 14 * limitations under the License.
Bethory 0:6ad07c9019fd 15 */
Bethory 0:6ad07c9019fd 16
Bethory 0:6ad07c9019fd 17 #include "TCPServer.h"
Bethory 0:6ad07c9019fd 18 #include "mbed.h"
Bethory 0:6ad07c9019fd 19
Bethory 0:6ad07c9019fd 20 TCPServer::TCPServer()
Bethory 0:6ad07c9019fd 21 : _pending(0), _accept_sem(0)
Bethory 0:6ad07c9019fd 22 {
Bethory 0:6ad07c9019fd 23 }
Bethory 0:6ad07c9019fd 24
Bethory 0:6ad07c9019fd 25 TCPServer::~TCPServer()
Bethory 0:6ad07c9019fd 26 {
Bethory 0:6ad07c9019fd 27 close();
Bethory 0:6ad07c9019fd 28 }
Bethory 0:6ad07c9019fd 29
Bethory 0:6ad07c9019fd 30 nsapi_protocol_t TCPServer::get_proto()
Bethory 0:6ad07c9019fd 31 {
Bethory 0:6ad07c9019fd 32 return NSAPI_TCP;
Bethory 0:6ad07c9019fd 33 }
Bethory 0:6ad07c9019fd 34
Bethory 0:6ad07c9019fd 35 nsapi_error_t TCPServer::listen(int backlog)
Bethory 0:6ad07c9019fd 36 {
Bethory 0:6ad07c9019fd 37 _lock.lock();
Bethory 0:6ad07c9019fd 38 nsapi_error_t ret;
Bethory 0:6ad07c9019fd 39
Bethory 0:6ad07c9019fd 40 if (!_socket) {
Bethory 0:6ad07c9019fd 41 ret = NSAPI_ERROR_NO_SOCKET;
Bethory 0:6ad07c9019fd 42 } else {
Bethory 0:6ad07c9019fd 43 ret = _stack->socket_listen(_socket, backlog);
Bethory 0:6ad07c9019fd 44 }
Bethory 0:6ad07c9019fd 45
Bethory 0:6ad07c9019fd 46 _lock.unlock();
Bethory 0:6ad07c9019fd 47 return ret;
Bethory 0:6ad07c9019fd 48 }
Bethory 0:6ad07c9019fd 49
Bethory 0:6ad07c9019fd 50 nsapi_error_t TCPServer::accept(TCPSocket *connection, SocketAddress *address)
Bethory 0:6ad07c9019fd 51 {
Bethory 0:6ad07c9019fd 52 _lock.lock();
Bethory 0:6ad07c9019fd 53 nsapi_error_t ret;
Bethory 0:6ad07c9019fd 54
Bethory 0:6ad07c9019fd 55 while (true) {
Bethory 0:6ad07c9019fd 56 if (!_socket) {
Bethory 0:6ad07c9019fd 57 ret = NSAPI_ERROR_NO_SOCKET;
Bethory 0:6ad07c9019fd 58 break;
Bethory 0:6ad07c9019fd 59 }
Bethory 0:6ad07c9019fd 60
Bethory 0:6ad07c9019fd 61 _pending = 0;
Bethory 0:6ad07c9019fd 62 void *socket;
Bethory 0:6ad07c9019fd 63 ret = _stack->socket_accept(_socket, &socket, address);
Bethory 0:6ad07c9019fd 64
Bethory 0:6ad07c9019fd 65 if (0 == ret) {
Bethory 0:6ad07c9019fd 66 connection->_lock.lock();
Bethory 0:6ad07c9019fd 67
Bethory 0:6ad07c9019fd 68 if (connection->_socket) {
Bethory 0:6ad07c9019fd 69 connection->close();
Bethory 0:6ad07c9019fd 70 }
Bethory 0:6ad07c9019fd 71
Bethory 0:6ad07c9019fd 72 connection->_stack = _stack;
Bethory 0:6ad07c9019fd 73 connection->_socket = socket;
Bethory 0:6ad07c9019fd 74 connection->_event = Callback<void()>(connection, &TCPSocket::event);
Bethory 0:6ad07c9019fd 75 _stack->socket_attach(socket, &Callback<void()>::thunk, &connection->_event);
Bethory 0:6ad07c9019fd 76
Bethory 0:6ad07c9019fd 77 connection->_lock.unlock();
Bethory 0:6ad07c9019fd 78 break;
Bethory 0:6ad07c9019fd 79 } else if (NSAPI_ERROR_WOULD_BLOCK != ret) {
Bethory 0:6ad07c9019fd 80 break;
Bethory 0:6ad07c9019fd 81 } else {
Bethory 0:6ad07c9019fd 82 int32_t count;
Bethory 0:6ad07c9019fd 83
Bethory 0:6ad07c9019fd 84 // Release lock before blocking so other threads
Bethory 0:6ad07c9019fd 85 // accessing this object aren't blocked
Bethory 0:6ad07c9019fd 86 _lock.unlock();
Bethory 0:6ad07c9019fd 87 count = _accept_sem.wait(_timeout);
Bethory 0:6ad07c9019fd 88 _lock.lock();
Bethory 0:6ad07c9019fd 89
Bethory 0:6ad07c9019fd 90 if (count < 1) {
Bethory 0:6ad07c9019fd 91 // Semaphore wait timed out so break out and return
Bethory 0:6ad07c9019fd 92 ret = NSAPI_ERROR_WOULD_BLOCK;
Bethory 0:6ad07c9019fd 93 break;
Bethory 0:6ad07c9019fd 94 }
Bethory 0:6ad07c9019fd 95 }
Bethory 0:6ad07c9019fd 96 }
Bethory 0:6ad07c9019fd 97
Bethory 0:6ad07c9019fd 98 _lock.unlock();
Bethory 0:6ad07c9019fd 99 return ret;
Bethory 0:6ad07c9019fd 100 }
Bethory 0:6ad07c9019fd 101
Bethory 0:6ad07c9019fd 102 void TCPServer::event()
Bethory 0:6ad07c9019fd 103 {
Bethory 0:6ad07c9019fd 104 int32_t acount = _accept_sem.wait(0);
Bethory 0:6ad07c9019fd 105 if (acount <= 1) {
Bethory 0:6ad07c9019fd 106 _accept_sem.release();
Bethory 0:6ad07c9019fd 107 }
Bethory 0:6ad07c9019fd 108
Bethory 0:6ad07c9019fd 109 _pending += 1;
Bethory 0:6ad07c9019fd 110 if (_callback && _pending == 1) {
Bethory 0:6ad07c9019fd 111 _callback();
Bethory 0:6ad07c9019fd 112 }
Bethory 0:6ad07c9019fd 113 }