Nicolas Borla
/
BBR_1Ebene
BBR 1 Ebene
mbed-os/features/netsocket/UDPSocket.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 | /* UDPSocket |
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 UDPSOCKET_H |
borlanic | 0:fbdae7e6d805 | 21 | #define UDPSOCKET_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 | /** UDP socket |
borlanic | 0:fbdae7e6d805 | 30 | */ |
borlanic | 0:fbdae7e6d805 | 31 | class UDPSocket : 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 | UDPSocket(); |
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 | UDPSocket(S *stack) |
borlanic | 0:fbdae7e6d805 | 48 | : _pending(0), _event_flag(0) |
borlanic | 0:fbdae7e6d805 | 49 | { |
borlanic | 0:fbdae7e6d805 | 50 | open(stack); |
borlanic | 0:fbdae7e6d805 | 51 | } |
borlanic | 0:fbdae7e6d805 | 52 | |
borlanic | 0:fbdae7e6d805 | 53 | /** Destroy a socket |
borlanic | 0:fbdae7e6d805 | 54 | * |
borlanic | 0:fbdae7e6d805 | 55 | * Closes socket if the socket is still open |
borlanic | 0:fbdae7e6d805 | 56 | */ |
borlanic | 0:fbdae7e6d805 | 57 | virtual ~UDPSocket(); |
borlanic | 0:fbdae7e6d805 | 58 | |
borlanic | 0:fbdae7e6d805 | 59 | /** Send a packet over a UDP socket |
borlanic | 0:fbdae7e6d805 | 60 | * |
borlanic | 0:fbdae7e6d805 | 61 | * Sends data to the specified address specified by either a domain name |
borlanic | 0:fbdae7e6d805 | 62 | * or an IP address and port. Returns the number of bytes sent from the |
borlanic | 0:fbdae7e6d805 | 63 | * buffer. |
borlanic | 0:fbdae7e6d805 | 64 | * |
borlanic | 0:fbdae7e6d805 | 65 | * By default, sendto blocks until data is sent. If socket is set to |
borlanic | 0:fbdae7e6d805 | 66 | * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned |
borlanic | 0:fbdae7e6d805 | 67 | * immediately. |
borlanic | 0:fbdae7e6d805 | 68 | * |
borlanic | 0:fbdae7e6d805 | 69 | * @param host Hostname of the remote host |
borlanic | 0:fbdae7e6d805 | 70 | * @param port Port of the remote host |
borlanic | 0:fbdae7e6d805 | 71 | * @param data Buffer of data to send to the host |
borlanic | 0:fbdae7e6d805 | 72 | * @param size Size of the buffer in bytes |
borlanic | 0:fbdae7e6d805 | 73 | * @return Number of sent bytes on success, negative error |
borlanic | 0:fbdae7e6d805 | 74 | * code on failure |
borlanic | 0:fbdae7e6d805 | 75 | */ |
borlanic | 0:fbdae7e6d805 | 76 | nsapi_size_or_error_t sendto(const char *host, uint16_t port, |
borlanic | 0:fbdae7e6d805 | 77 | const void *data, nsapi_size_t size); |
borlanic | 0:fbdae7e6d805 | 78 | |
borlanic | 0:fbdae7e6d805 | 79 | /** Send a packet over a UDP socket |
borlanic | 0:fbdae7e6d805 | 80 | * |
borlanic | 0:fbdae7e6d805 | 81 | * Sends data to the specified address. Returns the number of bytes |
borlanic | 0:fbdae7e6d805 | 82 | * sent from the buffer. |
borlanic | 0:fbdae7e6d805 | 83 | * |
borlanic | 0:fbdae7e6d805 | 84 | * By default, sendto blocks until data is sent. If socket is set to |
borlanic | 0:fbdae7e6d805 | 85 | * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned |
borlanic | 0:fbdae7e6d805 | 86 | * immediately. |
borlanic | 0:fbdae7e6d805 | 87 | * |
borlanic | 0:fbdae7e6d805 | 88 | * @param address The SocketAddress of the remote host |
borlanic | 0:fbdae7e6d805 | 89 | * @param data Buffer of data to send to the host |
borlanic | 0:fbdae7e6d805 | 90 | * @param size Size of the buffer in bytes |
borlanic | 0:fbdae7e6d805 | 91 | * @return Number of sent bytes on success, negative error |
borlanic | 0:fbdae7e6d805 | 92 | * code on failure |
borlanic | 0:fbdae7e6d805 | 93 | */ |
borlanic | 0:fbdae7e6d805 | 94 | nsapi_size_or_error_t sendto(const SocketAddress &address, |
borlanic | 0:fbdae7e6d805 | 95 | const void *data, nsapi_size_t size); |
borlanic | 0:fbdae7e6d805 | 96 | |
borlanic | 0:fbdae7e6d805 | 97 | /** Receive a datagram over a UDP socket |
borlanic | 0:fbdae7e6d805 | 98 | * |
borlanic | 0:fbdae7e6d805 | 99 | * Receives a datagram and stores the source address in address if address |
borlanic | 0:fbdae7e6d805 | 100 | * is not NULL. Returns the number of bytes written into the buffer. If the |
borlanic | 0:fbdae7e6d805 | 101 | * datagram is larger than the buffer, the excess data is silently discarded. |
borlanic | 0:fbdae7e6d805 | 102 | * |
borlanic | 0:fbdae7e6d805 | 103 | * By default, recvfrom blocks until a datagram is received. If socket is set to |
borlanic | 0:fbdae7e6d805 | 104 | * non-blocking or times out with no datagram, NSAPI_ERROR_WOULD_BLOCK |
borlanic | 0:fbdae7e6d805 | 105 | * is returned. |
borlanic | 0:fbdae7e6d805 | 106 | * |
borlanic | 0:fbdae7e6d805 | 107 | * @param address Destination for the source address or NULL |
borlanic | 0:fbdae7e6d805 | 108 | * @param data Destination buffer for datagram received from the host |
borlanic | 0:fbdae7e6d805 | 109 | * @param size Size of the buffer in bytes |
borlanic | 0:fbdae7e6d805 | 110 | * @return Number of received bytes on success, negative error |
borlanic | 0:fbdae7e6d805 | 111 | * code on failure |
borlanic | 0:fbdae7e6d805 | 112 | */ |
borlanic | 0:fbdae7e6d805 | 113 | nsapi_size_or_error_t recvfrom(SocketAddress *address, |
borlanic | 0:fbdae7e6d805 | 114 | void *data, nsapi_size_t size); |
borlanic | 0:fbdae7e6d805 | 115 | |
borlanic | 0:fbdae7e6d805 | 116 | protected: |
borlanic | 0:fbdae7e6d805 | 117 | virtual nsapi_protocol_t get_proto(); |
borlanic | 0:fbdae7e6d805 | 118 | virtual void event(); |
borlanic | 0:fbdae7e6d805 | 119 | |
borlanic | 0:fbdae7e6d805 | 120 | volatile unsigned _pending; |
borlanic | 0:fbdae7e6d805 | 121 | rtos::EventFlags _event_flag; |
borlanic | 0:fbdae7e6d805 | 122 | }; |
borlanic | 0:fbdae7e6d805 | 123 | |
borlanic | 0:fbdae7e6d805 | 124 | |
borlanic | 0:fbdae7e6d805 | 125 | #endif |
borlanic | 0:fbdae7e6d805 | 126 | |
borlanic | 0:fbdae7e6d805 | 127 | /** @}*/ |