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