Onenet
Dependents: K64F_eCompass_OneNET_JW
Socket/UDPSocket.cpp@0:b2805b6888dc, 2016-06-20 (annotated)
- Committer:
- robert_jw
- Date:
- Mon Jun 20 01:40:20 2016 +0000
- Revision:
- 0:b2805b6888dc
ADS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
robert_jw | 0:b2805b6888dc | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
robert_jw | 0:b2805b6888dc | 2 | * |
robert_jw | 0:b2805b6888dc | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
robert_jw | 0:b2805b6888dc | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
robert_jw | 0:b2805b6888dc | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
robert_jw | 0:b2805b6888dc | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
robert_jw | 0:b2805b6888dc | 7 | * furnished to do so, subject to the following conditions: |
robert_jw | 0:b2805b6888dc | 8 | * |
robert_jw | 0:b2805b6888dc | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
robert_jw | 0:b2805b6888dc | 10 | * substantial portions of the Software. |
robert_jw | 0:b2805b6888dc | 11 | * |
robert_jw | 0:b2805b6888dc | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
robert_jw | 0:b2805b6888dc | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
robert_jw | 0:b2805b6888dc | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
robert_jw | 0:b2805b6888dc | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
robert_jw | 0:b2805b6888dc | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
robert_jw | 0:b2805b6888dc | 17 | */ |
robert_jw | 0:b2805b6888dc | 18 | |
robert_jw | 0:b2805b6888dc | 19 | #include "Socket/UDPSocket.h" |
robert_jw | 0:b2805b6888dc | 20 | |
robert_jw | 0:b2805b6888dc | 21 | #include <cstring> |
robert_jw | 0:b2805b6888dc | 22 | |
robert_jw | 0:b2805b6888dc | 23 | using std::memset; |
robert_jw | 0:b2805b6888dc | 24 | |
robert_jw | 0:b2805b6888dc | 25 | UDPSocket::UDPSocket() { |
robert_jw | 0:b2805b6888dc | 26 | } |
robert_jw | 0:b2805b6888dc | 27 | |
robert_jw | 0:b2805b6888dc | 28 | int UDPSocket::init(void) { |
robert_jw | 0:b2805b6888dc | 29 | return init_socket(SOCK_DGRAM); |
robert_jw | 0:b2805b6888dc | 30 | } |
robert_jw | 0:b2805b6888dc | 31 | |
robert_jw | 0:b2805b6888dc | 32 | // Server initialization |
robert_jw | 0:b2805b6888dc | 33 | int UDPSocket::bind(int port) { |
robert_jw | 0:b2805b6888dc | 34 | if (init_socket(SOCK_DGRAM) < 0) |
robert_jw | 0:b2805b6888dc | 35 | return -1; |
robert_jw | 0:b2805b6888dc | 36 | |
robert_jw | 0:b2805b6888dc | 37 | struct sockaddr_in localHost; |
robert_jw | 0:b2805b6888dc | 38 | std::memset(&localHost, 0, sizeof(localHost)); |
robert_jw | 0:b2805b6888dc | 39 | |
robert_jw | 0:b2805b6888dc | 40 | localHost.sin_family = AF_INET; |
robert_jw | 0:b2805b6888dc | 41 | localHost.sin_port = htons(port); |
robert_jw | 0:b2805b6888dc | 42 | localHost.sin_addr.s_addr = INADDR_ANY; |
robert_jw | 0:b2805b6888dc | 43 | |
robert_jw | 0:b2805b6888dc | 44 | if (lwip_bind(_sock_fd, (const struct sockaddr *) &localHost, sizeof(localHost)) < 0) { |
robert_jw | 0:b2805b6888dc | 45 | close(); |
robert_jw | 0:b2805b6888dc | 46 | return -1; |
robert_jw | 0:b2805b6888dc | 47 | } |
robert_jw | 0:b2805b6888dc | 48 | |
robert_jw | 0:b2805b6888dc | 49 | return 0; |
robert_jw | 0:b2805b6888dc | 50 | } |
robert_jw | 0:b2805b6888dc | 51 | |
robert_jw | 0:b2805b6888dc | 52 | int UDPSocket::join_multicast_group(const char* address) { |
robert_jw | 0:b2805b6888dc | 53 | struct ip_mreq mreq; |
robert_jw | 0:b2805b6888dc | 54 | |
robert_jw | 0:b2805b6888dc | 55 | // Set up group address |
robert_jw | 0:b2805b6888dc | 56 | mreq.imr_multiaddr.s_addr = inet_addr(address); |
robert_jw | 0:b2805b6888dc | 57 | mreq.imr_interface.s_addr = htonl(INADDR_ANY); |
robert_jw | 0:b2805b6888dc | 58 | |
robert_jw | 0:b2805b6888dc | 59 | return set_option(IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); |
robert_jw | 0:b2805b6888dc | 60 | } |
robert_jw | 0:b2805b6888dc | 61 | |
robert_jw | 0:b2805b6888dc | 62 | int UDPSocket::set_broadcasting(bool broadcast) { |
robert_jw | 0:b2805b6888dc | 63 | int option = (broadcast) ? (1) : (0); |
robert_jw | 0:b2805b6888dc | 64 | return set_option(SOL_SOCKET, SO_BROADCAST, &option, sizeof(option)); |
robert_jw | 0:b2805b6888dc | 65 | } |
robert_jw | 0:b2805b6888dc | 66 | |
robert_jw | 0:b2805b6888dc | 67 | // -1 if unsuccessful, else number of bytes written |
robert_jw | 0:b2805b6888dc | 68 | int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) { |
robert_jw | 0:b2805b6888dc | 69 | if (_sock_fd < 0) |
robert_jw | 0:b2805b6888dc | 70 | return -1; |
robert_jw | 0:b2805b6888dc | 71 | |
robert_jw | 0:b2805b6888dc | 72 | if (!_blocking) { |
robert_jw | 0:b2805b6888dc | 73 | TimeInterval timeout(_timeout); |
robert_jw | 0:b2805b6888dc | 74 | if (wait_writable(timeout) != 0) |
robert_jw | 0:b2805b6888dc | 75 | return 0; |
robert_jw | 0:b2805b6888dc | 76 | } |
robert_jw | 0:b2805b6888dc | 77 | |
robert_jw | 0:b2805b6888dc | 78 | return lwip_sendto(_sock_fd, packet, length, 0, (const struct sockaddr *) &remote._remoteHost, sizeof(remote._remoteHost)); |
robert_jw | 0:b2805b6888dc | 79 | } |
robert_jw | 0:b2805b6888dc | 80 | |
robert_jw | 0:b2805b6888dc | 81 | // -1 if unsuccessful, else number of bytes received |
robert_jw | 0:b2805b6888dc | 82 | int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) { |
robert_jw | 0:b2805b6888dc | 83 | if (_sock_fd < 0) |
robert_jw | 0:b2805b6888dc | 84 | return -1; |
robert_jw | 0:b2805b6888dc | 85 | |
robert_jw | 0:b2805b6888dc | 86 | if (!_blocking) { |
robert_jw | 0:b2805b6888dc | 87 | TimeInterval timeout(_timeout); |
robert_jw | 0:b2805b6888dc | 88 | if (wait_readable(timeout) != 0) |
robert_jw | 0:b2805b6888dc | 89 | return 0; |
robert_jw | 0:b2805b6888dc | 90 | } |
robert_jw | 0:b2805b6888dc | 91 | remote.reset_address(); |
robert_jw | 0:b2805b6888dc | 92 | socklen_t remoteHostLen = sizeof(remote._remoteHost); |
robert_jw | 0:b2805b6888dc | 93 | return lwip_recvfrom(_sock_fd, buffer, length, 0, (struct sockaddr*) &remote._remoteHost, &remoteHostLen); |
robert_jw | 0:b2805b6888dc | 94 | } |