ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TCPServer.h Source File

TCPServer.h

00001 
00002 /** \addtogroup netsocket */
00003 /** @{*/
00004 /* TCPServer
00005  * Copyright (c) 2015 ARM Limited
00006  *
00007  * Licensed under the Apache License, Version 2.0 (the "License");
00008  * you may not use this file except in compliance with the License.
00009  * You may obtain a copy of the License at
00010  *
00011  *     http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  * Unless required by applicable law or agreed to in writing, software
00014  * distributed under the License is distributed on an "AS IS" BASIS,
00015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  * See the License for the specific language governing permissions and
00017  * limitations under the License.
00018  */
00019 
00020 #ifndef TCPSERVER_H
00021 #define TCPSERVER_H
00022 
00023 #include "netsocket/Socket.h"
00024 #include "netsocket/TCPSocket.h"
00025 #include "netsocket/NetworkStack.h"
00026 #include "netsocket/NetworkInterface.h"
00027 #include "rtos/Semaphore.h"
00028 
00029 
00030 /** TCP socket server
00031   */
00032 class TCPServer : public Socket {
00033 public:
00034     /** Create an uninitialized socket
00035      *
00036      *  Must call open to initialize the socket on a network stack.
00037      */
00038     TCPServer();
00039 
00040     /** Create a socket on a network interface
00041      *
00042      *  Creates and opens a socket on the network stack of the given
00043      *  network interface.
00044      *
00045      *  @param stack    Network stack as target for socket
00046      */
00047     template <typename S>
00048     TCPServer(S *stack)
00049         : _pending(1), _accept_sem(0)
00050     {
00051         open(stack);
00052     }
00053 
00054     /** Destroy a socket
00055      *
00056      *  Closes socket if the socket is still open
00057      */
00058     virtual ~TCPServer();
00059 
00060     /** Listen for connections on a TCP socket
00061      *
00062      *  Marks the socket as a passive socket that can be used to accept
00063      *  incoming connections.
00064      *
00065      *  @param backlog  Number of pending connections that can be queued
00066      *                  simultaneously, defaults to 1
00067      *  @return         0 on success, negative error code on failure
00068      */
00069     nsapi_error_t listen(int backlog = 1);
00070     
00071     /** Accepts a connection on a TCP socket
00072      *
00073      *  The server socket must be bound and set to listen for connections.
00074      *  On a new connection, creates a network socket using the specified
00075      *  socket instance.
00076      *
00077      *  By default, accept blocks until data is sent. If socket is set to
00078      *  non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
00079      *  immediately.
00080      *
00081      *  @param socket   TCPSocket instance that will handle the incoming connection.
00082      *  @param address  Destination for the remote address or NULL
00083      *  @return         0 on success, negative error code on failure
00084      */
00085     nsapi_error_t accept(TCPSocket *connection, SocketAddress *address = NULL);
00086 
00087 protected:
00088     virtual nsapi_protocol_t get_proto();
00089     virtual void event();
00090 
00091     volatile unsigned _pending;
00092     rtos::Semaphore _accept_sem;
00093 };
00094 
00095 
00096 #endif
00097 
00098 /** @}*/