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.
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
UDPSocket.h
00001 /* UDPSocket 00002 * Copyright (c) 2015 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef UDPSOCKET_H 00018 #define UDPSOCKET_H 00019 00020 #include "network-socket/Socket.h" 00021 #include "network-socket/NetworkStack.h" 00022 #include "network-socket/NetworkInterface.h" 00023 #include "rtos/Semaphore.h" 00024 00025 00026 /** UDP socket 00027 */ 00028 class UDPSocket : public Socket { 00029 public: 00030 /** Create an uninitialized socket 00031 * 00032 * Must call open to initialize the socket on a network stack. 00033 */ 00034 UDPSocket(); 00035 00036 /** Create a socket on a network interface 00037 * 00038 * Creates and opens a socket on the network stack of the given 00039 * network interface. 00040 * 00041 * @param stack Network stack as target for socket 00042 */ 00043 template <typename S> 00044 UDPSocket(S *stack) 00045 : _pending(0), _read_sem(0), _write_sem(0), 00046 _read_in_progress(false), _write_in_progress(false) 00047 { 00048 open(stack); 00049 } 00050 00051 /** Destroy a socket 00052 * 00053 * Closes socket if the socket is still open 00054 */ 00055 virtual ~UDPSocket(); 00056 00057 /** Send a packet over a UDP socket 00058 * 00059 * Sends data to the specified address specified by either a domain name 00060 * or an IP address and port. Returns the number of bytes sent from the 00061 * buffer. 00062 * 00063 * By default, sendto blocks until data is sent. If socket is set to 00064 * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned 00065 * immediately. 00066 * 00067 * @param host Hostname of the remote host 00068 * @param port Port of the remote host 00069 * @param data Buffer of data to send to the host 00070 * @param size Size of the buffer in bytes 00071 * @return Number of sent bytes on success, negative error 00072 * code on failure 00073 */ 00074 int sendto(const char *host, uint16_t port, const void *data, unsigned size); 00075 00076 /** Send a packet over a UDP socket 00077 * 00078 * Sends data to the specified address. Returns the number of bytes 00079 * sent from the buffer. 00080 * 00081 * By default, sendto blocks until data is sent. If socket is set to 00082 * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned 00083 * immediately. 00084 * 00085 * @param address The SocketAddress of the remote host 00086 * @param data Buffer of data to send to the host 00087 * @param size Size of the buffer in bytes 00088 * @return Number of sent bytes on success, negative error 00089 * code on failure 00090 */ 00091 int sendto(const SocketAddress &address, const void *data, unsigned size); 00092 00093 /** Receive a packet over a UDP socket 00094 * 00095 * Receives data and stores the source address in address if address 00096 * is not NULL. Returns the number of bytes received into the buffer. 00097 * 00098 * By default, recvfrom blocks until data is sent. If socket is set to 00099 * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned 00100 * immediately. 00101 * 00102 * @param address Destination for the source address or NULL 00103 * @param data Destination buffer for data received from the host 00104 * @param size Size of the buffer in bytes 00105 * @return Number of received bytes on success, negative error 00106 * code on failure 00107 */ 00108 int recvfrom(SocketAddress *address, void *data, unsigned size); 00109 00110 protected: 00111 virtual nsapi_protocol_t get_proto(); 00112 virtual void event(); 00113 00114 volatile unsigned _pending; 00115 rtos::Semaphore _read_sem; 00116 rtos::Semaphore _write_sem; 00117 bool _read_in_progress; 00118 bool _write_in_progress; 00119 }; 00120 00121 00122 #endif
Generated on Tue Jul 12 2022 12:28:57 by
1.7.2
