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.
UDPSocket.cpp
00001 /* Copyright (C) 2013 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #include "UDPSocket.h" 00020 00021 #include <string> 00022 #include <algorithm> 00023 00024 UDPSocket::UDPSocket() { 00025 00026 } 00027 00028 int UDPSocket::init(void) { 00029 return init_socket(SOCK_DGRAM, IPPROTO_UDP); 00030 } 00031 00032 int UDPSocket::bind(int port) { 00033 if (init_socket(SOCK_DGRAM, IPPROTO_UDP) < 0) { 00034 return -1; 00035 } 00036 00037 sockaddr_in localHost; 00038 std::memset(&localHost, 0, sizeof(sockaddr_in)); 00039 00040 localHost.sin_family = AF_INET; 00041 localHost.sin_port = htons(port); 00042 localHost.sin_addr.s_addr = 0; 00043 00044 if (_cc3000_module->_socket.bind(_sock_fd, (sockaddr *)&localHost, sizeof(sockaddr_in)) != 0) { 00045 DBG_SOCKET("Failed to bind a socket (udp). Closing socket"); 00046 _cc3000_module->_socket.closesocket(_sock_fd); 00047 _sock_fd = -1; 00048 return -1; 00049 } 00050 00051 return 0; 00052 } 00053 00054 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) 00055 { 00056 if (_sock_fd < 0) { 00057 return -1; 00058 } 00059 // TODO - seems to be a bug, waiting for TI to respond 00060 // if (!_blocking) { 00061 // TimeInterval timeout(_timeout); 00062 // if (wait_writable(timeout) != 0) { 00063 // DBG_SOCKET("The socket is not writeable. _sock_fd: %d", _sock_fd); 00064 // return 0; 00065 // } 00066 // } 00067 00068 return _cc3000_module->_socket.sendto(_sock_fd, packet, length, 0, (sockaddr *)&remote._remote_host, sizeof(sockaddr)); 00069 } 00070 00071 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) 00072 { 00073 if (_sock_fd < 0) { 00074 return -1; 00075 } 00076 00077 if (!_blocking) { 00078 TimeInterval timeout(_timeout); 00079 if (wait_readable(timeout) != 0) { 00080 DBG_SOCKET("The socket is not readable. _sock_fd: %d", _sock_fd); 00081 return 0; 00082 } 00083 } 00084 00085 remote.reset_address(); 00086 socklen_t remote_host_length = sizeof(remote._remote_host); 00087 00088 return _cc3000_module->_socket.recvfrom(_sock_fd, buffer, length, 0, (sockaddr *)&remote._remote_host, &remote_host_length); 00089 } 00090 00091 00092
Generated on Tue Jul 12 2022 18:55:02 by
