Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mbed-os/features/netsocket/UDPSocket.h@0:9fca2b23d0ba, 2019-02-23 (annotated)
- Committer:
- marcozecchini
- Date:
- Sat Feb 23 12:13:36 2019 +0000
- Revision:
- 0:9fca2b23d0ba
final commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| marcozecchini | 0:9fca2b23d0ba | 1 | |
| marcozecchini | 0:9fca2b23d0ba | 2 | /** \addtogroup netsocket */ | 
| marcozecchini | 0:9fca2b23d0ba | 3 | /** @{*/ | 
| marcozecchini | 0:9fca2b23d0ba | 4 | /* UDPSocket | 
| marcozecchini | 0:9fca2b23d0ba | 5 | * Copyright (c) 2015 ARM Limited | 
| marcozecchini | 0:9fca2b23d0ba | 6 | * | 
| marcozecchini | 0:9fca2b23d0ba | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
| marcozecchini | 0:9fca2b23d0ba | 8 | * you may not use this file except in compliance with the License. | 
| marcozecchini | 0:9fca2b23d0ba | 9 | * You may obtain a copy of the License at | 
| marcozecchini | 0:9fca2b23d0ba | 10 | * | 
| marcozecchini | 0:9fca2b23d0ba | 11 | * http://www.apache.org/licenses/LICENSE-2.0 | 
| marcozecchini | 0:9fca2b23d0ba | 12 | * | 
| marcozecchini | 0:9fca2b23d0ba | 13 | * Unless required by applicable law or agreed to in writing, software | 
| marcozecchini | 0:9fca2b23d0ba | 14 | * distributed under the License is distributed on an "AS IS" BASIS, | 
| marcozecchini | 0:9fca2b23d0ba | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| marcozecchini | 0:9fca2b23d0ba | 16 | * See the License for the specific language governing permissions and | 
| marcozecchini | 0:9fca2b23d0ba | 17 | * limitations under the License. | 
| marcozecchini | 0:9fca2b23d0ba | 18 | */ | 
| marcozecchini | 0:9fca2b23d0ba | 19 | |
| marcozecchini | 0:9fca2b23d0ba | 20 | #ifndef UDPSOCKET_H | 
| marcozecchini | 0:9fca2b23d0ba | 21 | #define UDPSOCKET_H | 
| marcozecchini | 0:9fca2b23d0ba | 22 | |
| marcozecchini | 0:9fca2b23d0ba | 23 | #include "netsocket/Socket.h" | 
| marcozecchini | 0:9fca2b23d0ba | 24 | #include "netsocket/NetworkStack.h" | 
| marcozecchini | 0:9fca2b23d0ba | 25 | #include "netsocket/NetworkInterface.h" | 
| marcozecchini | 0:9fca2b23d0ba | 26 | #include "rtos/EventFlags.h" | 
| marcozecchini | 0:9fca2b23d0ba | 27 | |
| marcozecchini | 0:9fca2b23d0ba | 28 | |
| marcozecchini | 0:9fca2b23d0ba | 29 | /** UDP socket | 
| marcozecchini | 0:9fca2b23d0ba | 30 | */ | 
| marcozecchini | 0:9fca2b23d0ba | 31 | class UDPSocket : public Socket { | 
| marcozecchini | 0:9fca2b23d0ba | 32 | public: | 
| marcozecchini | 0:9fca2b23d0ba | 33 | /** Create an uninitialized socket | 
| marcozecchini | 0:9fca2b23d0ba | 34 | * | 
| marcozecchini | 0:9fca2b23d0ba | 35 | * Must call open to initialize the socket on a network stack. | 
| marcozecchini | 0:9fca2b23d0ba | 36 | */ | 
| marcozecchini | 0:9fca2b23d0ba | 37 | UDPSocket(); | 
| marcozecchini | 0:9fca2b23d0ba | 38 | |
| marcozecchini | 0:9fca2b23d0ba | 39 | /** Create a socket on a network interface | 
| marcozecchini | 0:9fca2b23d0ba | 40 | * | 
| marcozecchini | 0:9fca2b23d0ba | 41 | * Creates and opens a socket on the network stack of the given | 
| marcozecchini | 0:9fca2b23d0ba | 42 | * network interface. | 
| marcozecchini | 0:9fca2b23d0ba | 43 | * | 
| marcozecchini | 0:9fca2b23d0ba | 44 | * @param stack Network stack as target for socket | 
| marcozecchini | 0:9fca2b23d0ba | 45 | */ | 
| marcozecchini | 0:9fca2b23d0ba | 46 | template <typename S> | 
| marcozecchini | 0:9fca2b23d0ba | 47 | UDPSocket(S *stack) | 
| marcozecchini | 0:9fca2b23d0ba | 48 | : _pending(0), _event_flag(0) | 
| marcozecchini | 0:9fca2b23d0ba | 49 | { | 
| marcozecchini | 0:9fca2b23d0ba | 50 | open(stack); | 
| marcozecchini | 0:9fca2b23d0ba | 51 | } | 
| marcozecchini | 0:9fca2b23d0ba | 52 | |
| marcozecchini | 0:9fca2b23d0ba | 53 | /** Destroy a socket | 
| marcozecchini | 0:9fca2b23d0ba | 54 | * | 
| marcozecchini | 0:9fca2b23d0ba | 55 | * Closes socket if the socket is still open | 
| marcozecchini | 0:9fca2b23d0ba | 56 | */ | 
| marcozecchini | 0:9fca2b23d0ba | 57 | virtual ~UDPSocket(); | 
| marcozecchini | 0:9fca2b23d0ba | 58 | |
| marcozecchini | 0:9fca2b23d0ba | 59 | /** Send a packet over a UDP socket | 
| marcozecchini | 0:9fca2b23d0ba | 60 | * | 
| marcozecchini | 0:9fca2b23d0ba | 61 | * Sends data to the specified address specified by either a domain name | 
| marcozecchini | 0:9fca2b23d0ba | 62 | * or an IP address and port. Returns the number of bytes sent from the | 
| marcozecchini | 0:9fca2b23d0ba | 63 | * buffer. | 
| marcozecchini | 0:9fca2b23d0ba | 64 | * | 
| marcozecchini | 0:9fca2b23d0ba | 65 | * By default, sendto blocks until data is sent. If socket is set to | 
| marcozecchini | 0:9fca2b23d0ba | 66 | * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned | 
| marcozecchini | 0:9fca2b23d0ba | 67 | * immediately. | 
| marcozecchini | 0:9fca2b23d0ba | 68 | * | 
| marcozecchini | 0:9fca2b23d0ba | 69 | * @param host Hostname of the remote host | 
| marcozecchini | 0:9fca2b23d0ba | 70 | * @param port Port of the remote host | 
| marcozecchini | 0:9fca2b23d0ba | 71 | * @param data Buffer of data to send to the host | 
| marcozecchini | 0:9fca2b23d0ba | 72 | * @param size Size of the buffer in bytes | 
| marcozecchini | 0:9fca2b23d0ba | 73 | * @return Number of sent bytes on success, negative error | 
| marcozecchini | 0:9fca2b23d0ba | 74 | * code on failure | 
| marcozecchini | 0:9fca2b23d0ba | 75 | */ | 
| marcozecchini | 0:9fca2b23d0ba | 76 | nsapi_size_or_error_t sendto(const char *host, uint16_t port, | 
| marcozecchini | 0:9fca2b23d0ba | 77 | const void *data, nsapi_size_t size); | 
| marcozecchini | 0:9fca2b23d0ba | 78 | |
| marcozecchini | 0:9fca2b23d0ba | 79 | /** Send a packet over a UDP socket | 
| marcozecchini | 0:9fca2b23d0ba | 80 | * | 
| marcozecchini | 0:9fca2b23d0ba | 81 | * Sends data to the specified address. Returns the number of bytes | 
| marcozecchini | 0:9fca2b23d0ba | 82 | * sent from the buffer. | 
| marcozecchini | 0:9fca2b23d0ba | 83 | * | 
| marcozecchini | 0:9fca2b23d0ba | 84 | * By default, sendto blocks until data is sent. If socket is set to | 
| marcozecchini | 0:9fca2b23d0ba | 85 | * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned | 
| marcozecchini | 0:9fca2b23d0ba | 86 | * immediately. | 
| marcozecchini | 0:9fca2b23d0ba | 87 | * | 
| marcozecchini | 0:9fca2b23d0ba | 88 | * @param address The SocketAddress of the remote host | 
| marcozecchini | 0:9fca2b23d0ba | 89 | * @param data Buffer of data to send to the host | 
| marcozecchini | 0:9fca2b23d0ba | 90 | * @param size Size of the buffer in bytes | 
| marcozecchini | 0:9fca2b23d0ba | 91 | * @return Number of sent bytes on success, negative error | 
| marcozecchini | 0:9fca2b23d0ba | 92 | * code on failure | 
| marcozecchini | 0:9fca2b23d0ba | 93 | */ | 
| marcozecchini | 0:9fca2b23d0ba | 94 | nsapi_size_or_error_t sendto(const SocketAddress &address, | 
| marcozecchini | 0:9fca2b23d0ba | 95 | const void *data, nsapi_size_t size); | 
| marcozecchini | 0:9fca2b23d0ba | 96 | |
| marcozecchini | 0:9fca2b23d0ba | 97 | /** Receive a packet over a UDP socket | 
| marcozecchini | 0:9fca2b23d0ba | 98 | * | 
| marcozecchini | 0:9fca2b23d0ba | 99 | * Receives data and stores the source address in address if address | 
| marcozecchini | 0:9fca2b23d0ba | 100 | * is not NULL. Returns the number of bytes received into the buffer. | 
| marcozecchini | 0:9fca2b23d0ba | 101 | * | 
| marcozecchini | 0:9fca2b23d0ba | 102 | * By default, recvfrom blocks until data is sent. If socket is set to | 
| marcozecchini | 0:9fca2b23d0ba | 103 | * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned | 
| marcozecchini | 0:9fca2b23d0ba | 104 | * immediately. | 
| marcozecchini | 0:9fca2b23d0ba | 105 | * | 
| marcozecchini | 0:9fca2b23d0ba | 106 | * @param address Destination for the source address or NULL | 
| marcozecchini | 0:9fca2b23d0ba | 107 | * @param data Destination buffer for data received from the host | 
| marcozecchini | 0:9fca2b23d0ba | 108 | * @param size Size of the buffer in bytes | 
| marcozecchini | 0:9fca2b23d0ba | 109 | * @return Number of received bytes on success, negative error | 
| marcozecchini | 0:9fca2b23d0ba | 110 | * code on failure | 
| marcozecchini | 0:9fca2b23d0ba | 111 | */ | 
| marcozecchini | 0:9fca2b23d0ba | 112 | nsapi_size_or_error_t recvfrom(SocketAddress *address, | 
| marcozecchini | 0:9fca2b23d0ba | 113 | void *data, nsapi_size_t size); | 
| marcozecchini | 0:9fca2b23d0ba | 114 | |
| marcozecchini | 0:9fca2b23d0ba | 115 | protected: | 
| marcozecchini | 0:9fca2b23d0ba | 116 | virtual nsapi_protocol_t get_proto(); | 
| marcozecchini | 0:9fca2b23d0ba | 117 | virtual void event(); | 
| marcozecchini | 0:9fca2b23d0ba | 118 | |
| marcozecchini | 0:9fca2b23d0ba | 119 | volatile unsigned _pending; | 
| marcozecchini | 0:9fca2b23d0ba | 120 | rtos::EventFlags _event_flag; | 
| marcozecchini | 0:9fca2b23d0ba | 121 | }; | 
| marcozecchini | 0:9fca2b23d0ba | 122 | |
| marcozecchini | 0:9fca2b23d0ba | 123 | |
| marcozecchini | 0:9fca2b23d0ba | 124 | #endif | 
| marcozecchini | 0:9fca2b23d0ba | 125 | |
| marcozecchini | 0:9fca2b23d0ba | 126 | /** @}*/ |