NetworkSocketAPI

Dependencies:   DnsQuery

Dependents:   HelloWizFi250Interface

Fork of NetworkSocketAPI by NetworkSocketAPI

Committer:
Christopher Haster
Date:
Tue Apr 19 18:22:15 2016 -0500
Revision:
90:0a988e4abb72
Parent:
89:b1d417383c0d
Child:
92:dd5f19874adf
Add open call as alternative to passing NetworkInterface at construction

Pros
- Allows memory to be statically allocated
- Avoids issues with Thread creation before entering main
- Matches existing APIs such as FunctionPointer and Ticker

Cons
- Does not enforce passing a NetworkInterface

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 89:b1d417383c0d 1 /* Socket
Christopher Haster 89:b1d417383c0d 2 * Copyright (c) 2015 ARM Limited
Christopher Haster 89:b1d417383c0d 3 *
Christopher Haster 89:b1d417383c0d 4 * Licensed under the Apache License, Version 2.0 (the "License");
Christopher Haster 89:b1d417383c0d 5 * you may not use this file except in compliance with the License.
Christopher Haster 89:b1d417383c0d 6 * You may obtain a copy of the License at
Christopher Haster 89:b1d417383c0d 7 *
Christopher Haster 89:b1d417383c0d 8 * http://www.apache.org/licenses/LICENSE-2.0
Christopher Haster 89:b1d417383c0d 9 *
Christopher Haster 89:b1d417383c0d 10 * Unless required by applicable law or agreed to in writing, software
Christopher Haster 89:b1d417383c0d 11 * distributed under the License is distributed on an "AS IS" BASIS,
Christopher Haster 89:b1d417383c0d 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Christopher Haster 89:b1d417383c0d 13 * See the License for the specific language governing permissions and
Christopher Haster 89:b1d417383c0d 14 * limitations under the License.
Christopher Haster 89:b1d417383c0d 15 */
Christopher Haster 89:b1d417383c0d 16
Christopher Haster 89:b1d417383c0d 17 #include "TCPServer.h"
Christopher Haster 89:b1d417383c0d 18 #include "Timer.h"
Christopher Haster 89:b1d417383c0d 19
Christopher Haster 90:0a988e4abb72 20 TCPServer::TCPServer()
Christopher Haster 90:0a988e4abb72 21 {
Christopher Haster 90:0a988e4abb72 22 }
Christopher Haster 90:0a988e4abb72 23
Christopher Haster 89:b1d417383c0d 24 TCPServer::TCPServer(NetworkInterface *iface)
Christopher Haster 89:b1d417383c0d 25 {
Christopher Haster 90:0a988e4abb72 26 open(iface);
Christopher Haster 90:0a988e4abb72 27 }
Christopher Haster 90:0a988e4abb72 28
Christopher Haster 90:0a988e4abb72 29 int TCPServer::open(NetworkInterface *iface)
Christopher Haster 90:0a988e4abb72 30 {
Christopher Haster 90:0a988e4abb72 31 return Socket::open(iface, NSAPI_TCP);
Christopher Haster 89:b1d417383c0d 32 }
Christopher Haster 89:b1d417383c0d 33
Christopher Haster 89:b1d417383c0d 34 int TCPServer::bind(uint16_t port)
Christopher Haster 89:b1d417383c0d 35 {
Christopher Haster 89:b1d417383c0d 36 if (!_socket) {
Christopher Haster 89:b1d417383c0d 37 return NSAPI_ERROR_NO_SOCKET;
Christopher Haster 89:b1d417383c0d 38 }
Christopher Haster 89:b1d417383c0d 39
Christopher Haster 89:b1d417383c0d 40 return _iface->socket_bind(_socket, port);
Christopher Haster 89:b1d417383c0d 41 }
Christopher Haster 89:b1d417383c0d 42
Christopher Haster 89:b1d417383c0d 43 int TCPServer::listen(int backlog)
Christopher Haster 89:b1d417383c0d 44 {
Christopher Haster 89:b1d417383c0d 45 if (!_socket) {
Christopher Haster 89:b1d417383c0d 46 return NSAPI_ERROR_NO_SOCKET;
Christopher Haster 89:b1d417383c0d 47 }
Christopher Haster 89:b1d417383c0d 48
Christopher Haster 89:b1d417383c0d 49 return _iface->socket_listen(_socket, backlog);
Christopher Haster 89:b1d417383c0d 50 }
Christopher Haster 89:b1d417383c0d 51
Christopher Haster 89:b1d417383c0d 52 int TCPServer::accept(TCPSocket *connection)
Christopher Haster 89:b1d417383c0d 53 {
Christopher Haster 89:b1d417383c0d 54 mbed::Timer timer;
Christopher Haster 89:b1d417383c0d 55 timer.start();
Christopher Haster 89:b1d417383c0d 56
Christopher Haster 90:0a988e4abb72 57 if (connection->_socket) {
Christopher Haster 90:0a988e4abb72 58 connection->close();
Christopher Haster 90:0a988e4abb72 59 }
Christopher Haster 89:b1d417383c0d 60
Christopher Haster 89:b1d417383c0d 61 while (true) {
Christopher Haster 89:b1d417383c0d 62 if (!_socket) {
Christopher Haster 89:b1d417383c0d 63 return NSAPI_ERROR_NO_SOCKET;
Christopher Haster 89:b1d417383c0d 64 }
Christopher Haster 89:b1d417383c0d 65
Christopher Haster 90:0a988e4abb72 66 void *socket;
Christopher Haster 89:b1d417383c0d 67 int err = _iface->socket_accept(_socket, &socket);
Christopher Haster 89:b1d417383c0d 68
Christopher Haster 89:b1d417383c0d 69 if (err > 0) {
Christopher Haster 89:b1d417383c0d 70 connection->_socket = socket;
Christopher Haster 89:b1d417383c0d 71 }
Christopher Haster 89:b1d417383c0d 72
Christopher Haster 89:b1d417383c0d 73 if (err != NSAPI_ERROR_WOULD_BLOCK || !_blocking ||
Christopher Haster 89:b1d417383c0d 74 (_timeout && timer.read_ms() > _timeout)) {
Christopher Haster 89:b1d417383c0d 75 return err;
Christopher Haster 89:b1d417383c0d 76 }
Christopher Haster 89:b1d417383c0d 77 }
Christopher Haster 89:b1d417383c0d 78 }
Christopher Haster 89:b1d417383c0d 79
Christopher Haster 89:b1d417383c0d 80
Christopher Haster 89:b1d417383c0d 81 void TCPServer::attach_accept(FunctionPointer callback)
Christopher Haster 89:b1d417383c0d 82 {
Christopher Haster 89:b1d417383c0d 83 _accept_cb = callback;
Christopher Haster 89:b1d417383c0d 84
Christopher Haster 89:b1d417383c0d 85 if (_socket && _accept_cb) {
Christopher Haster 89:b1d417383c0d 86 return _iface->socket_attach_accept(_socket, Socket::thunk, &_accept_cb);
Christopher Haster 89:b1d417383c0d 87 } else if (_socket) {
Christopher Haster 89:b1d417383c0d 88 return _iface->socket_attach_accept(_socket, 0, 0);
Christopher Haster 89:b1d417383c0d 89 }
Christopher Haster 89:b1d417383c0d 90 }
Christopher Haster 89:b1d417383c0d 91
Christopher Haster 89:b1d417383c0d 92 TCPServer::~TCPServer()
Christopher Haster 89:b1d417383c0d 93 {
Christopher Haster 89:b1d417383c0d 94 if (_socket && _accept_cb) {
Christopher Haster 89:b1d417383c0d 95 _iface->socket_attach_accept(_socket, 0, 0);
Christopher Haster 89:b1d417383c0d 96 }
Christopher Haster 89:b1d417383c0d 97 }