mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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