Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Simon Cooksey 0:fb7af294d5d9 1
Simon Cooksey 0:fb7af294d5d9 2 /** \addtogroup netsocket */
Simon Cooksey 0:fb7af294d5d9 3 /** @{*/
Simon Cooksey 0:fb7af294d5d9 4 /* TCPSocket
Simon Cooksey 0:fb7af294d5d9 5 * Copyright (c) 2015 ARM Limited
Simon Cooksey 0:fb7af294d5d9 6 *
Simon Cooksey 0:fb7af294d5d9 7 * Licensed under the Apache License, Version 2.0 (the "License");
Simon Cooksey 0:fb7af294d5d9 8 * you may not use this file except in compliance with the License.
Simon Cooksey 0:fb7af294d5d9 9 * You may obtain a copy of the License at
Simon Cooksey 0:fb7af294d5d9 10 *
Simon Cooksey 0:fb7af294d5d9 11 * http://www.apache.org/licenses/LICENSE-2.0
Simon Cooksey 0:fb7af294d5d9 12 *
Simon Cooksey 0:fb7af294d5d9 13 * Unless required by applicable law or agreed to in writing, software
Simon Cooksey 0:fb7af294d5d9 14 * distributed under the License is distributed on an "AS IS" BASIS,
Simon Cooksey 0:fb7af294d5d9 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Simon Cooksey 0:fb7af294d5d9 16 * See the License for the specific language governing permissions and
Simon Cooksey 0:fb7af294d5d9 17 * limitations under the License.
Simon Cooksey 0:fb7af294d5d9 18 */
Simon Cooksey 0:fb7af294d5d9 19
Simon Cooksey 0:fb7af294d5d9 20 #ifndef TCPSOCKET_H
Simon Cooksey 0:fb7af294d5d9 21 #define TCPSOCKET_H
Simon Cooksey 0:fb7af294d5d9 22
Simon Cooksey 0:fb7af294d5d9 23 #include "netsocket/Socket.h"
Simon Cooksey 0:fb7af294d5d9 24 #include "netsocket/NetworkStack.h"
Simon Cooksey 0:fb7af294d5d9 25 #include "netsocket/NetworkInterface.h"
Simon Cooksey 0:fb7af294d5d9 26 #include "rtos/Semaphore.h"
Simon Cooksey 0:fb7af294d5d9 27
Simon Cooksey 0:fb7af294d5d9 28
Simon Cooksey 0:fb7af294d5d9 29 /** TCP socket connection
Simon Cooksey 0:fb7af294d5d9 30 */
Simon Cooksey 0:fb7af294d5d9 31 class TCPSocket : public Socket {
Simon Cooksey 0:fb7af294d5d9 32 public:
Simon Cooksey 0:fb7af294d5d9 33 /** Create an uninitialized socket
Simon Cooksey 0:fb7af294d5d9 34 *
Simon Cooksey 0:fb7af294d5d9 35 * Must call open to initialize the socket on a network stack.
Simon Cooksey 0:fb7af294d5d9 36 */
Simon Cooksey 0:fb7af294d5d9 37 TCPSocket();
Simon Cooksey 0:fb7af294d5d9 38
Simon Cooksey 0:fb7af294d5d9 39 /** Create a socket on a network interface
Simon Cooksey 0:fb7af294d5d9 40 *
Simon Cooksey 0:fb7af294d5d9 41 * Creates and opens a socket on the network stack of the given
Simon Cooksey 0:fb7af294d5d9 42 * network interface.
Simon Cooksey 0:fb7af294d5d9 43 *
Simon Cooksey 0:fb7af294d5d9 44 * @param stack Network stack as target for socket
Simon Cooksey 0:fb7af294d5d9 45 */
Simon Cooksey 0:fb7af294d5d9 46 template <typename S>
Simon Cooksey 0:fb7af294d5d9 47 TCPSocket(S *stack)
Simon Cooksey 0:fb7af294d5d9 48 : _pending(0), _read_sem(0), _write_sem(0),
Simon Cooksey 0:fb7af294d5d9 49 _read_in_progress(false), _write_in_progress(false)
Simon Cooksey 0:fb7af294d5d9 50 {
Simon Cooksey 0:fb7af294d5d9 51 open(stack);
Simon Cooksey 0:fb7af294d5d9 52 }
Simon Cooksey 0:fb7af294d5d9 53
Simon Cooksey 0:fb7af294d5d9 54 /** Destroy a socket
Simon Cooksey 0:fb7af294d5d9 55 *
Simon Cooksey 0:fb7af294d5d9 56 * Closes socket if the socket is still open
Simon Cooksey 0:fb7af294d5d9 57 */
Simon Cooksey 0:fb7af294d5d9 58 virtual ~TCPSocket();
Simon Cooksey 0:fb7af294d5d9 59
Simon Cooksey 0:fb7af294d5d9 60 /** Connects TCP socket to a remote host
Simon Cooksey 0:fb7af294d5d9 61 *
Simon Cooksey 0:fb7af294d5d9 62 * Initiates a connection to a remote server specified by either
Simon Cooksey 0:fb7af294d5d9 63 * a domain name or an IP address and a port.
Simon Cooksey 0:fb7af294d5d9 64 *
Simon Cooksey 0:fb7af294d5d9 65 * @param host Hostname of the remote host
Simon Cooksey 0:fb7af294d5d9 66 * @param port Port of the remote host
Simon Cooksey 0:fb7af294d5d9 67 * @return 0 on success, negative error code on failure
Simon Cooksey 0:fb7af294d5d9 68 */
Simon Cooksey 0:fb7af294d5d9 69 int connect(const char *host, uint16_t port);
Simon Cooksey 0:fb7af294d5d9 70
Simon Cooksey 0:fb7af294d5d9 71 /** Connects TCP socket to a remote host
Simon Cooksey 0:fb7af294d5d9 72 *
Simon Cooksey 0:fb7af294d5d9 73 * Initiates a connection to a remote server specified by the
Simon Cooksey 0:fb7af294d5d9 74 * indicated address.
Simon Cooksey 0:fb7af294d5d9 75 *
Simon Cooksey 0:fb7af294d5d9 76 * @param address The SocketAddress of the remote host
Simon Cooksey 0:fb7af294d5d9 77 * @return 0 on success, negative error code on failure
Simon Cooksey 0:fb7af294d5d9 78 */
Simon Cooksey 0:fb7af294d5d9 79 int connect(const SocketAddress &address);
Simon Cooksey 0:fb7af294d5d9 80
Simon Cooksey 0:fb7af294d5d9 81 /** Send data over a TCP socket
Simon Cooksey 0:fb7af294d5d9 82 *
Simon Cooksey 0:fb7af294d5d9 83 * The socket must be connected to a remote host. Returns the number of
Simon Cooksey 0:fb7af294d5d9 84 * bytes sent from the buffer.
Simon Cooksey 0:fb7af294d5d9 85 *
Simon Cooksey 0:fb7af294d5d9 86 * By default, send blocks until data is sent. If socket is set to
Simon Cooksey 0:fb7af294d5d9 87 * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
Simon Cooksey 0:fb7af294d5d9 88 * immediately.
Simon Cooksey 0:fb7af294d5d9 89 *
Simon Cooksey 0:fb7af294d5d9 90 * @param data Buffer of data to send to the host
Simon Cooksey 0:fb7af294d5d9 91 * @param size Size of the buffer in bytes
Simon Cooksey 0:fb7af294d5d9 92 * @return Number of sent bytes on success, negative error
Simon Cooksey 0:fb7af294d5d9 93 * code on failure
Simon Cooksey 0:fb7af294d5d9 94 */
Simon Cooksey 0:fb7af294d5d9 95 int send(const void *data, unsigned size);
Simon Cooksey 0:fb7af294d5d9 96
Simon Cooksey 0:fb7af294d5d9 97 /** Receive data over a TCP socket
Simon Cooksey 0:fb7af294d5d9 98 *
Simon Cooksey 0:fb7af294d5d9 99 * The socket must be connected to a remote host. Returns the number of
Simon Cooksey 0:fb7af294d5d9 100 * bytes received into the buffer.
Simon Cooksey 0:fb7af294d5d9 101 *
Simon Cooksey 0:fb7af294d5d9 102 * By default, recv blocks until data is sent. If socket is set to
Simon Cooksey 0:fb7af294d5d9 103 * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
Simon Cooksey 0:fb7af294d5d9 104 * immediately.
Simon Cooksey 0:fb7af294d5d9 105 *
Simon Cooksey 0:fb7af294d5d9 106 * @param data Destination buffer for data received from the host
Simon Cooksey 0:fb7af294d5d9 107 * @param size Size of the buffer in bytes
Simon Cooksey 0:fb7af294d5d9 108 * @return Number of received bytes on success, negative error
Simon Cooksey 0:fb7af294d5d9 109 * code on failure
Simon Cooksey 0:fb7af294d5d9 110 */
Simon Cooksey 0:fb7af294d5d9 111 int recv(void *data, unsigned size);
Simon Cooksey 0:fb7af294d5d9 112
Simon Cooksey 0:fb7af294d5d9 113 protected:
Simon Cooksey 0:fb7af294d5d9 114 friend class TCPServer;
Simon Cooksey 0:fb7af294d5d9 115
Simon Cooksey 0:fb7af294d5d9 116 virtual nsapi_protocol_t get_proto();
Simon Cooksey 0:fb7af294d5d9 117 virtual void event();
Simon Cooksey 0:fb7af294d5d9 118
Simon Cooksey 0:fb7af294d5d9 119 volatile unsigned _pending;
Simon Cooksey 0:fb7af294d5d9 120 rtos::Semaphore _read_sem;
Simon Cooksey 0:fb7af294d5d9 121 rtos::Semaphore _write_sem;
Simon Cooksey 0:fb7af294d5d9 122 bool _read_in_progress;
Simon Cooksey 0:fb7af294d5d9 123 bool _write_in_progress;
Simon Cooksey 0:fb7af294d5d9 124 };
Simon Cooksey 0:fb7af294d5d9 125
Simon Cooksey 0:fb7af294d5d9 126
Simon Cooksey 0:fb7af294d5d9 127 #endif
Simon Cooksey 0:fb7af294d5d9 128
Simon Cooksey 0:fb7af294d5d9 129 /** @}*/