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