RTC auf true
features/netsocket/UDPSocket.h@2:7aab896b1a3b, 2019-03-13 (annotated)
- Committer:
- kevman
- Date:
- Wed Mar 13 11:03:24 2019 +0000
- Revision:
- 2:7aab896b1a3b
- Parent:
- 0:38ceb79fef03
2019-03-13
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kevman | 0:38ceb79fef03 | 1 | |
kevman | 0:38ceb79fef03 | 2 | /** \addtogroup netsocket */ |
kevman | 0:38ceb79fef03 | 3 | /** @{*/ |
kevman | 0:38ceb79fef03 | 4 | /* UDPSocket |
kevman | 0:38ceb79fef03 | 5 | * Copyright (c) 2015 ARM Limited |
kevman | 0:38ceb79fef03 | 6 | * |
kevman | 0:38ceb79fef03 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kevman | 0:38ceb79fef03 | 8 | * you may not use this file except in compliance with the License. |
kevman | 0:38ceb79fef03 | 9 | * You may obtain a copy of the License at |
kevman | 0:38ceb79fef03 | 10 | * |
kevman | 0:38ceb79fef03 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
kevman | 0:38ceb79fef03 | 12 | * |
kevman | 0:38ceb79fef03 | 13 | * Unless required by applicable law or agreed to in writing, software |
kevman | 0:38ceb79fef03 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
kevman | 0:38ceb79fef03 | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kevman | 0:38ceb79fef03 | 16 | * See the License for the specific language governing permissions and |
kevman | 0:38ceb79fef03 | 17 | * limitations under the License. |
kevman | 0:38ceb79fef03 | 18 | */ |
kevman | 0:38ceb79fef03 | 19 | |
kevman | 0:38ceb79fef03 | 20 | #ifndef UDPSOCKET_H |
kevman | 0:38ceb79fef03 | 21 | #define UDPSOCKET_H |
kevman | 0:38ceb79fef03 | 22 | |
kevman | 0:38ceb79fef03 | 23 | #include "netsocket/InternetSocket.h" |
kevman | 0:38ceb79fef03 | 24 | #include "netsocket/NetworkStack.h" |
kevman | 0:38ceb79fef03 | 25 | #include "netsocket/NetworkInterface.h" |
kevman | 0:38ceb79fef03 | 26 | #include "rtos/EventFlags.h" |
kevman | 0:38ceb79fef03 | 27 | |
kevman | 0:38ceb79fef03 | 28 | |
kevman | 0:38ceb79fef03 | 29 | /** UDP socket implementation. |
kevman | 0:38ceb79fef03 | 30 | */ |
kevman | 0:38ceb79fef03 | 31 | class UDPSocket : public InternetSocket { |
kevman | 0:38ceb79fef03 | 32 | public: |
kevman | 0:38ceb79fef03 | 33 | /** Create an uninitialized socket. |
kevman | 0:38ceb79fef03 | 34 | * |
kevman | 0:38ceb79fef03 | 35 | * @note Must call open to initialize the socket on a network stack. |
kevman | 0:38ceb79fef03 | 36 | */ |
kevman | 0:38ceb79fef03 | 37 | UDPSocket(); |
kevman | 0:38ceb79fef03 | 38 | |
kevman | 0:38ceb79fef03 | 39 | /** Create and open a socket on the network stack of the given |
kevman | 0:38ceb79fef03 | 40 | * network interface. |
kevman | 0:38ceb79fef03 | 41 | * |
kevman | 0:38ceb79fef03 | 42 | * @tparam S Type of the Network stack. |
kevman | 0:38ceb79fef03 | 43 | * @param stack Network stack as target for socket. |
kevman | 0:38ceb79fef03 | 44 | */ |
kevman | 0:38ceb79fef03 | 45 | template <typename S> |
kevman | 0:38ceb79fef03 | 46 | UDPSocket(S *stack) |
kevman | 0:38ceb79fef03 | 47 | { |
kevman | 0:38ceb79fef03 | 48 | open(stack); |
kevman | 0:38ceb79fef03 | 49 | } |
kevman | 0:38ceb79fef03 | 50 | |
kevman | 0:38ceb79fef03 | 51 | /** Destroy a socket. |
kevman | 0:38ceb79fef03 | 52 | * |
kevman | 0:38ceb79fef03 | 53 | * @note Closes socket if the socket is still open. |
kevman | 0:38ceb79fef03 | 54 | */ |
kevman | 0:38ceb79fef03 | 55 | virtual ~UDPSocket(); |
kevman | 0:38ceb79fef03 | 56 | |
kevman | 0:38ceb79fef03 | 57 | /** Send data to the specified host and port. |
kevman | 0:38ceb79fef03 | 58 | * |
kevman | 0:38ceb79fef03 | 59 | * By default, sendto blocks until data is sent. If socket is set to |
kevman | 0:38ceb79fef03 | 60 | * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned |
kevman | 0:38ceb79fef03 | 61 | * immediately. |
kevman | 0:38ceb79fef03 | 62 | * |
kevman | 0:38ceb79fef03 | 63 | * @param host Domain name of the remote host or a dotted notation IP address. |
kevman | 0:38ceb79fef03 | 64 | * @param port Port of the remote host. |
kevman | 0:38ceb79fef03 | 65 | * @param data Buffer of data to send to the host. |
kevman | 0:38ceb79fef03 | 66 | * @param size Size of the buffer in bytes. |
kevman | 0:38ceb79fef03 | 67 | * @return Number of sent bytes on success, negative error |
kevman | 0:38ceb79fef03 | 68 | * code on failure. |
kevman | 0:38ceb79fef03 | 69 | */ |
kevman | 0:38ceb79fef03 | 70 | virtual nsapi_size_or_error_t sendto(const char *host, uint16_t port, |
kevman | 0:38ceb79fef03 | 71 | const void *data, nsapi_size_t size); |
kevman | 0:38ceb79fef03 | 72 | |
kevman | 0:38ceb79fef03 | 73 | /** Send data to the specified address. |
kevman | 0:38ceb79fef03 | 74 | * |
kevman | 0:38ceb79fef03 | 75 | * By default, sendto blocks until data is sent. If socket is set to |
kevman | 0:38ceb79fef03 | 76 | * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned |
kevman | 0:38ceb79fef03 | 77 | * immediately. |
kevman | 0:38ceb79fef03 | 78 | * |
kevman | 0:38ceb79fef03 | 79 | * @param address The SocketAddress of the remote host. |
kevman | 0:38ceb79fef03 | 80 | * @param data Buffer of data to send to the host. |
kevman | 0:38ceb79fef03 | 81 | * @param size Size of the buffer in bytes. |
kevman | 0:38ceb79fef03 | 82 | * @return Number of sent bytes on success, negative error |
kevman | 0:38ceb79fef03 | 83 | * code on failure. |
kevman | 0:38ceb79fef03 | 84 | */ |
kevman | 0:38ceb79fef03 | 85 | virtual nsapi_size_or_error_t sendto(const SocketAddress &address, |
kevman | 0:38ceb79fef03 | 86 | const void *data, nsapi_size_t size); |
kevman | 0:38ceb79fef03 | 87 | |
kevman | 0:38ceb79fef03 | 88 | /** Receive a datagram and store the source address in address if it's not NULL. |
kevman | 0:38ceb79fef03 | 89 | * |
kevman | 0:38ceb79fef03 | 90 | * By default, recvfrom blocks until a datagram is received. If socket is set to |
kevman | 0:38ceb79fef03 | 91 | * nonblocking or times out with no datagram, NSAPI_ERROR_WOULD_BLOCK |
kevman | 0:38ceb79fef03 | 92 | * is returned. |
kevman | 0:38ceb79fef03 | 93 | * |
kevman | 0:38ceb79fef03 | 94 | * @note If the datagram is larger than the buffer, the excess data is silently discarded. |
kevman | 0:38ceb79fef03 | 95 | * |
kevman | 0:38ceb79fef03 | 96 | * @note If socket is connected, only packets coming from connected peer address |
kevman | 0:38ceb79fef03 | 97 | * are accepted. |
kevman | 0:38ceb79fef03 | 98 | * |
kevman | 0:38ceb79fef03 | 99 | * @note recvfrom() is allowed write to address and data buffers even if error occurs. |
kevman | 0:38ceb79fef03 | 100 | * |
kevman | 0:38ceb79fef03 | 101 | * @param address Destination for the source address or NULL. |
kevman | 0:38ceb79fef03 | 102 | * @param data Destination buffer for datagram received from the host. |
kevman | 0:38ceb79fef03 | 103 | * @param size Size of the buffer in bytes. |
kevman | 0:38ceb79fef03 | 104 | * @return Number of received bytes on success, negative error |
kevman | 0:38ceb79fef03 | 105 | * code on failure. |
kevman | 0:38ceb79fef03 | 106 | */ |
kevman | 0:38ceb79fef03 | 107 | virtual nsapi_size_or_error_t recvfrom(SocketAddress *address, |
kevman | 0:38ceb79fef03 | 108 | void *data, nsapi_size_t size); |
kevman | 0:38ceb79fef03 | 109 | |
kevman | 0:38ceb79fef03 | 110 | /** Set the remote address for next send() call and filtering |
kevman | 0:38ceb79fef03 | 111 | * of incoming packets. To reset the address, zero initialized |
kevman | 0:38ceb79fef03 | 112 | * SocketAddress must be in the address parameter. |
kevman | 0:38ceb79fef03 | 113 | * |
kevman | 0:38ceb79fef03 | 114 | * @param address The SocketAddress of the remote host. |
kevman | 0:38ceb79fef03 | 115 | * @return 0 on success, negative error code on failure. |
kevman | 0:38ceb79fef03 | 116 | */ |
kevman | 0:38ceb79fef03 | 117 | virtual nsapi_error_t connect(const SocketAddress &address); |
kevman | 0:38ceb79fef03 | 118 | |
kevman | 0:38ceb79fef03 | 119 | /** Send a datagram to connected remote address. |
kevman | 0:38ceb79fef03 | 120 | * |
kevman | 0:38ceb79fef03 | 121 | * By default, send blocks until all data is sent. If socket is set to |
kevman | 0:38ceb79fef03 | 122 | * nonblocking or times out, a partial amount can be written. |
kevman | 0:38ceb79fef03 | 123 | * NSAPI_ERROR_WOULD_BLOCK is returned if no data was written. |
kevman | 0:38ceb79fef03 | 124 | * |
kevman | 0:38ceb79fef03 | 125 | * @note The socket must be connected to a remote host before send() call. |
kevman | 0:38ceb79fef03 | 126 | * |
kevman | 0:38ceb79fef03 | 127 | * @param data Buffer of data to send to the host. |
kevman | 0:38ceb79fef03 | 128 | * @param size Size of the buffer in bytes. |
kevman | 0:38ceb79fef03 | 129 | * @return Number of sent bytes on success, negative error |
kevman | 0:38ceb79fef03 | 130 | * code on failure. |
kevman | 0:38ceb79fef03 | 131 | */ |
kevman | 0:38ceb79fef03 | 132 | virtual nsapi_size_or_error_t send(const void *data, nsapi_size_t size); |
kevman | 0:38ceb79fef03 | 133 | |
kevman | 0:38ceb79fef03 | 134 | /** Receive data from a socket. |
kevman | 0:38ceb79fef03 | 135 | * |
kevman | 0:38ceb79fef03 | 136 | * This is equivalent to calling recvfrom(NULL, data, size). |
kevman | 0:38ceb79fef03 | 137 | * |
kevman | 0:38ceb79fef03 | 138 | * If the socket is connected, only packets coming from a connected peer address |
kevman | 0:38ceb79fef03 | 139 | * are accepted. |
kevman | 0:38ceb79fef03 | 140 | * |
kevman | 0:38ceb79fef03 | 141 | * By default, recv blocks until some data is received. If socket is set to |
kevman | 0:38ceb79fef03 | 142 | * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to |
kevman | 0:38ceb79fef03 | 143 | * indicate no data. |
kevman | 0:38ceb79fef03 | 144 | * |
kevman | 0:38ceb79fef03 | 145 | * @note recv() is allowed write to data buffer even if error occurs. |
kevman | 0:38ceb79fef03 | 146 | * |
kevman | 0:38ceb79fef03 | 147 | * @param data Pointer to buffer for data received from the host. |
kevman | 0:38ceb79fef03 | 148 | * @param size Size of the buffer in bytes. |
kevman | 0:38ceb79fef03 | 149 | * @return Number of received bytes on success, negative error |
kevman | 0:38ceb79fef03 | 150 | * code on failure. |
kevman | 0:38ceb79fef03 | 151 | */ |
kevman | 0:38ceb79fef03 | 152 | virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size); |
kevman | 0:38ceb79fef03 | 153 | |
kevman | 0:38ceb79fef03 | 154 | /** Not implemented for UDP. |
kevman | 0:38ceb79fef03 | 155 | * |
kevman | 0:38ceb79fef03 | 156 | * @param error Not used. |
kevman | 0:38ceb79fef03 | 157 | * @return NSAPI_ERROR_UNSUPPORTED |
kevman | 0:38ceb79fef03 | 158 | */ |
kevman | 0:38ceb79fef03 | 159 | virtual Socket *accept(nsapi_error_t *error = NULL); |
kevman | 0:38ceb79fef03 | 160 | |
kevman | 0:38ceb79fef03 | 161 | /** Not implemented for UDP. |
kevman | 0:38ceb79fef03 | 162 | * |
kevman | 0:38ceb79fef03 | 163 | * @param backlog Not used. |
kevman | 0:38ceb79fef03 | 164 | * @return NSAPI_ERROR_UNSUPPORTED |
kevman | 0:38ceb79fef03 | 165 | */ |
kevman | 0:38ceb79fef03 | 166 | virtual nsapi_error_t listen(int backlog = 1); |
kevman | 0:38ceb79fef03 | 167 | |
kevman | 0:38ceb79fef03 | 168 | #if !defined(DOXYGEN_ONLY) |
kevman | 0:38ceb79fef03 | 169 | |
kevman | 0:38ceb79fef03 | 170 | protected: |
kevman | 0:38ceb79fef03 | 171 | virtual nsapi_protocol_t get_proto(); |
kevman | 0:38ceb79fef03 | 172 | |
kevman | 0:38ceb79fef03 | 173 | #endif //!defined(DOXYGEN_ONLY) |
kevman | 0:38ceb79fef03 | 174 | }; |
kevman | 0:38ceb79fef03 | 175 | |
kevman | 0:38ceb79fef03 | 176 | |
kevman | 0:38ceb79fef03 | 177 | #endif |
kevman | 0:38ceb79fef03 | 178 | |
kevman | 0:38ceb79fef03 | 179 | /** @}*/ |