A Simple TCP & UDP Socket Library
Dependents: SimpleSocketExamples 1.0
DatagramSocket.cpp@0:3eb1ce25eac4, 2012-06-05 (annotated)
- Committer:
- yamaguch
- Date:
- Tue Jun 05 05:56:49 2012 +0000
- Revision:
- 0:3eb1ce25eac4
1.1.3; fixed 0x00 reception bug
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yamaguch | 0:3eb1ce25eac4 | 1 | /* |
yamaguch | 0:3eb1ce25eac4 | 2 | Copyright (c) 2011, Senio Networks, Inc. |
yamaguch | 0:3eb1ce25eac4 | 3 | |
yamaguch | 0:3eb1ce25eac4 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
yamaguch | 0:3eb1ce25eac4 | 5 | of this software and associated documentation files (the "Software"), to deal |
yamaguch | 0:3eb1ce25eac4 | 6 | in the Software without restriction, including without limitation the rights |
yamaguch | 0:3eb1ce25eac4 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
yamaguch | 0:3eb1ce25eac4 | 8 | copies of the Software, and to permit persons to whom the Software is |
yamaguch | 0:3eb1ce25eac4 | 9 | furnished to do so, subject to the following conditions: |
yamaguch | 0:3eb1ce25eac4 | 10 | |
yamaguch | 0:3eb1ce25eac4 | 11 | The above copyright notice and this permission notice shall be included in |
yamaguch | 0:3eb1ce25eac4 | 12 | all copies or substantial portions of the Software. |
yamaguch | 0:3eb1ce25eac4 | 13 | |
yamaguch | 0:3eb1ce25eac4 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
yamaguch | 0:3eb1ce25eac4 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
yamaguch | 0:3eb1ce25eac4 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
yamaguch | 0:3eb1ce25eac4 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
yamaguch | 0:3eb1ce25eac4 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
yamaguch | 0:3eb1ce25eac4 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
yamaguch | 0:3eb1ce25eac4 | 20 | THE SOFTWARE. |
yamaguch | 0:3eb1ce25eac4 | 21 | */ |
yamaguch | 0:3eb1ce25eac4 | 22 | |
yamaguch | 0:3eb1ce25eac4 | 23 | #include "EthernetNetIf.h" |
yamaguch | 0:3eb1ce25eac4 | 24 | #include "SimpleSocket.h" |
yamaguch | 0:3eb1ce25eac4 | 25 | #include <stdarg.h> |
yamaguch | 0:3eb1ce25eac4 | 26 | |
yamaguch | 0:3eb1ce25eac4 | 27 | DatagramSocket::DatagramSocket(int port, bool debug) : bufsize(512), debug(debug) { |
yamaguch | 0:3eb1ce25eac4 | 28 | host = Host(IpAddr(), port); |
yamaguch | 0:3eb1ce25eac4 | 29 | createDatagramSocket(); |
yamaguch | 0:3eb1ce25eac4 | 30 | } |
yamaguch | 0:3eb1ce25eac4 | 31 | |
yamaguch | 0:3eb1ce25eac4 | 32 | DatagramSocket::DatagramSocket(Host host, bool debug) : host(host), bufsize(512), debug(debug) { |
yamaguch | 0:3eb1ce25eac4 | 33 | createDatagramSocket(); |
yamaguch | 0:3eb1ce25eac4 | 34 | } |
yamaguch | 0:3eb1ce25eac4 | 35 | |
yamaguch | 0:3eb1ce25eac4 | 36 | DatagramSocket::DatagramSocket(IpAddr ip, int port, bool debug) : host(ip, port), bufsize(512), debug(debug) { |
yamaguch | 0:3eb1ce25eac4 | 37 | createDatagramSocket(); |
yamaguch | 0:3eb1ce25eac4 | 38 | } |
yamaguch | 0:3eb1ce25eac4 | 39 | |
yamaguch | 0:3eb1ce25eac4 | 40 | DatagramSocket::~DatagramSocket() { |
yamaguch | 0:3eb1ce25eac4 | 41 | delete[] buf; |
yamaguch | 0:3eb1ce25eac4 | 42 | } |
yamaguch | 0:3eb1ce25eac4 | 43 | |
yamaguch | 0:3eb1ce25eac4 | 44 | void DatagramSocket::createDatagramSocket() { |
yamaguch | 0:3eb1ce25eac4 | 45 | udpSocket.setOnEvent(this, &DatagramSocket::onUDPSocketEvent); |
yamaguch | 0:3eb1ce25eac4 | 46 | UDPSocketErr err = udpSocket.bind(host); |
yamaguch | 0:3eb1ce25eac4 | 47 | DBG(DatagramError(err)); |
yamaguch | 0:3eb1ce25eac4 | 48 | buf = new char[bufsize + 1]; |
yamaguch | 0:3eb1ce25eac4 | 49 | } |
yamaguch | 0:3eb1ce25eac4 | 50 | |
yamaguch | 0:3eb1ce25eac4 | 51 | int DatagramSocket::write(char *buf, int length) { |
yamaguch | 0:3eb1ce25eac4 | 52 | if (length > bufsize) length = bufsize; |
yamaguch | 0:3eb1ce25eac4 | 53 | this->length = length; |
yamaguch | 0:3eb1ce25eac4 | 54 | memcpy(this->buf, buf, length); |
yamaguch | 0:3eb1ce25eac4 | 55 | return length; |
yamaguch | 0:3eb1ce25eac4 | 56 | } |
yamaguch | 0:3eb1ce25eac4 | 57 | |
yamaguch | 0:3eb1ce25eac4 | 58 | int DatagramSocket::printf(const char* format, ...) { |
yamaguch | 0:3eb1ce25eac4 | 59 | va_list argp; |
yamaguch | 0:3eb1ce25eac4 | 60 | va_start(argp, format); |
yamaguch | 0:3eb1ce25eac4 | 61 | int len = vsnprintf(buf, bufsize, format, argp); |
yamaguch | 0:3eb1ce25eac4 | 62 | va_end(argp); |
yamaguch | 0:3eb1ce25eac4 | 63 | if (len > 0) |
yamaguch | 0:3eb1ce25eac4 | 64 | length = len; |
yamaguch | 0:3eb1ce25eac4 | 65 | return len; |
yamaguch | 0:3eb1ce25eac4 | 66 | } |
yamaguch | 0:3eb1ce25eac4 | 67 | |
yamaguch | 0:3eb1ce25eac4 | 68 | void DatagramSocket::send(Host& host) { |
yamaguch | 0:3eb1ce25eac4 | 69 | DBG("Host = %d.%d.%d.%d:%d\n", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort()); |
yamaguch | 0:3eb1ce25eac4 | 70 | Net::poll(); |
yamaguch | 0:3eb1ce25eac4 | 71 | int err = udpSocket.sendto(buf, length, &host); |
yamaguch | 0:3eb1ce25eac4 | 72 | DBG(DatagramError((UDPSocketErr) (err < 0 ? err : 0))); |
yamaguch | 0:3eb1ce25eac4 | 73 | } |
yamaguch | 0:3eb1ce25eac4 | 74 | |
yamaguch | 0:3eb1ce25eac4 | 75 | void DatagramSocket::send(IpAddr ip, int port) { |
yamaguch | 0:3eb1ce25eac4 | 76 | DBG("IP = %d.%d.%d.%d, Port = %d\n", ip[0], ip[1], ip[2], ip[3], port); |
yamaguch | 0:3eb1ce25eac4 | 77 | Host host(ip, port); |
yamaguch | 0:3eb1ce25eac4 | 78 | send(host); |
yamaguch | 0:3eb1ce25eac4 | 79 | } |
yamaguch | 0:3eb1ce25eac4 | 80 | |
yamaguch | 0:3eb1ce25eac4 | 81 | void DatagramSocket::send(char *name, int port) { |
yamaguch | 0:3eb1ce25eac4 | 82 | DBG("Name = %s, Port = %d\n", name, port); |
yamaguch | 0:3eb1ce25eac4 | 83 | Resolver resolver; |
yamaguch | 0:3eb1ce25eac4 | 84 | IpAddr ip = resolver.resolve(name, debug); |
yamaguch | 0:3eb1ce25eac4 | 85 | send(ip, port); |
yamaguch | 0:3eb1ce25eac4 | 86 | } |
yamaguch | 0:3eb1ce25eac4 | 87 | |
yamaguch | 0:3eb1ce25eac4 | 88 | int DatagramSocket::read(char *buf, int size) { |
yamaguch | 0:3eb1ce25eac4 | 89 | int len = length < size ? length : size; |
yamaguch | 0:3eb1ce25eac4 | 90 | if (len > 0) |
yamaguch | 0:3eb1ce25eac4 | 91 | memcpy(buf, this->buf, len); |
yamaguch | 0:3eb1ce25eac4 | 92 | return len; |
yamaguch | 0:3eb1ce25eac4 | 93 | } |
yamaguch | 0:3eb1ce25eac4 | 94 | |
yamaguch | 0:3eb1ce25eac4 | 95 | int DatagramSocket::scanf(const char* format, ...) { |
yamaguch | 0:3eb1ce25eac4 | 96 | va_list argp; |
yamaguch | 0:3eb1ce25eac4 | 97 | va_start(argp, format); |
yamaguch | 0:3eb1ce25eac4 | 98 | buf[length] = '\0'; |
yamaguch | 0:3eb1ce25eac4 | 99 | int ret = vsscanf(buf, format, argp); |
yamaguch | 0:3eb1ce25eac4 | 100 | va_end(argp); |
yamaguch | 0:3eb1ce25eac4 | 101 | return ret; |
yamaguch | 0:3eb1ce25eac4 | 102 | } |
yamaguch | 0:3eb1ce25eac4 | 103 | |
yamaguch | 0:3eb1ce25eac4 | 104 | int DatagramSocket::receive(Host *host, float timeout) { |
yamaguch | 0:3eb1ce25eac4 | 105 | timer.reset(); |
yamaguch | 0:3eb1ce25eac4 | 106 | timer.start(); |
yamaguch | 0:3eb1ce25eac4 | 107 | readable = false; |
yamaguch | 0:3eb1ce25eac4 | 108 | length = 0; |
yamaguch | 0:3eb1ce25eac4 | 109 | |
yamaguch | 0:3eb1ce25eac4 | 110 | do { |
yamaguch | 0:3eb1ce25eac4 | 111 | Net::poll(); |
yamaguch | 0:3eb1ce25eac4 | 112 | if (readable) { |
yamaguch | 0:3eb1ce25eac4 | 113 | Host host2; |
yamaguch | 0:3eb1ce25eac4 | 114 | length = udpSocket.recvfrom(buf, bufsize, &host2); |
yamaguch | 0:3eb1ce25eac4 | 115 | if (length > 0 && host) |
yamaguch | 0:3eb1ce25eac4 | 116 | *host = host2; |
yamaguch | 0:3eb1ce25eac4 | 117 | break; |
yamaguch | 0:3eb1ce25eac4 | 118 | } |
yamaguch | 0:3eb1ce25eac4 | 119 | } while (timer.read() < timeout); |
yamaguch | 0:3eb1ce25eac4 | 120 | |
yamaguch | 0:3eb1ce25eac4 | 121 | return length; |
yamaguch | 0:3eb1ce25eac4 | 122 | } |
yamaguch | 0:3eb1ce25eac4 | 123 | |
yamaguch | 0:3eb1ce25eac4 | 124 | void DatagramSocket::setDebug(bool debug) { |
yamaguch | 0:3eb1ce25eac4 | 125 | this->debug = debug; |
yamaguch | 0:3eb1ce25eac4 | 126 | } |
yamaguch | 0:3eb1ce25eac4 | 127 | |
yamaguch | 0:3eb1ce25eac4 | 128 | void DatagramSocket::onUDPSocketEvent(UDPSocketEvent e) { |
yamaguch | 0:3eb1ce25eac4 | 129 | DBG(DatagramEvent(e)); |
yamaguch | 0:3eb1ce25eac4 | 130 | |
yamaguch | 0:3eb1ce25eac4 | 131 | switch (e) { |
yamaguch | 0:3eb1ce25eac4 | 132 | case UDPSOCKET_READABLE: //The only event for now |
yamaguch | 0:3eb1ce25eac4 | 133 | readable = true; |
yamaguch | 0:3eb1ce25eac4 | 134 | break; |
yamaguch | 0:3eb1ce25eac4 | 135 | } |
yamaguch | 0:3eb1ce25eac4 | 136 | } |