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