Socket library for MTS Socket Modem Arduino Shield devices from Multi-Tech Systems
Dependencies: HTTPClient-SSL
Dependents: mtsas mtsas mtsas mtsas_lat3
UDPSocket.cpp@43:2b78cadc0894, 2015-06-25 (annotated)
- Committer:
- mfiore
- Date:
- Thu Jun 25 13:56:41 2015 +0000
- Revision:
- 43:2b78cadc0894
- Parent:
- 9:b2e3862705fc
update to MultiTech official version of HTTPClient-SSL library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfiore | 9:b2e3862705fc | 1 | #include "UDPSocket.h" |
mfiore | 9:b2e3862705fc | 2 | #include <string> |
mfiore | 9:b2e3862705fc | 3 | #include <algorithm> |
mfiore | 9:b2e3862705fc | 4 | |
mfiore | 9:b2e3862705fc | 5 | UDPSocket::UDPSocket() |
mfiore | 9:b2e3862705fc | 6 | { |
mfiore | 9:b2e3862705fc | 7 | } |
mfiore | 9:b2e3862705fc | 8 | |
mfiore | 9:b2e3862705fc | 9 | int UDPSocket::init(void) |
mfiore | 9:b2e3862705fc | 10 | { |
mfiore | 9:b2e3862705fc | 11 | return 0; |
mfiore | 9:b2e3862705fc | 12 | } |
mfiore | 9:b2e3862705fc | 13 | |
mfiore | 9:b2e3862705fc | 14 | // Server initialization |
mfiore | 9:b2e3862705fc | 15 | int UDPSocket::bind(int port) |
mfiore | 9:b2e3862705fc | 16 | { |
mfiore | 9:b2e3862705fc | 17 | if (ip->bind(port)) { |
mfiore | 9:b2e3862705fc | 18 | return 0; |
mfiore | 9:b2e3862705fc | 19 | } else { |
mfiore | 9:b2e3862705fc | 20 | return -1; |
mfiore | 9:b2e3862705fc | 21 | } |
mfiore | 9:b2e3862705fc | 22 | } |
mfiore | 9:b2e3862705fc | 23 | |
mfiore | 9:b2e3862705fc | 24 | // -1 if unsuccessful, else number of bytes written |
mfiore | 9:b2e3862705fc | 25 | int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) |
mfiore | 9:b2e3862705fc | 26 | { |
mfiore | 9:b2e3862705fc | 27 | if (!ip->open(remote.get_address(), remote.get_port(), IPStack::UDP)) { |
mfiore | 9:b2e3862705fc | 28 | return -1; |
mfiore | 9:b2e3862705fc | 29 | } |
mfiore | 9:b2e3862705fc | 30 | |
mfiore | 9:b2e3862705fc | 31 | if (_blocking) { |
mfiore | 9:b2e3862705fc | 32 | return ip->write(packet, length, -1); |
mfiore | 9:b2e3862705fc | 33 | } else { |
mfiore | 9:b2e3862705fc | 34 | return ip->write(packet, length, _timeout); |
mfiore | 9:b2e3862705fc | 35 | } |
mfiore | 9:b2e3862705fc | 36 | } |
mfiore | 9:b2e3862705fc | 37 | |
mfiore | 9:b2e3862705fc | 38 | // -1 if unsuccessful, else number of bytes received |
mfiore | 9:b2e3862705fc | 39 | int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) |
mfiore | 9:b2e3862705fc | 40 | { |
mfiore | 9:b2e3862705fc | 41 | if (!ip->open(remote.get_address(), remote.get_port(), IPStack::UDP)) { |
mfiore | 9:b2e3862705fc | 42 | return -1; |
mfiore | 9:b2e3862705fc | 43 | } |
mfiore | 9:b2e3862705fc | 44 | |
mfiore | 9:b2e3862705fc | 45 | if (_blocking) { |
mfiore | 9:b2e3862705fc | 46 | return ip->read(buffer, length, -1); |
mfiore | 9:b2e3862705fc | 47 | } else { |
mfiore | 9:b2e3862705fc | 48 | return ip->read(buffer, length, _timeout); |
mfiore | 9:b2e3862705fc | 49 | } |
mfiore | 9:b2e3862705fc | 50 | } |