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