Socket library for MTS Socket Modem Arduino Shield devices from Multi-Tech Systems

Dependencies:   HTTPClient-SSL

Dependents:   mtsas mtsas mtsas mtsas_lat3

Committer:
Mike Fiore
Date:
Mon May 19 12:36:11 2014 -0500
Revision:
1:096f484f3ae6
Child:
2:ebc6129de4e8
add socket code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Mike Fiore 1:096f484f3ae6 1 #include "TCPSocketConnection.h"
Mike Fiore 1:096f484f3ae6 2 #include <algorithm>
Mike Fiore 1:096f484f3ae6 3
Mike Fiore 1:096f484f3ae6 4 TCPSocketConnection::TCPSocketConnection()
Mike Fiore 1:096f484f3ae6 5 {
Mike Fiore 1:096f484f3ae6 6 }
Mike Fiore 1:096f484f3ae6 7
Mike Fiore 1:096f484f3ae6 8 int TCPSocketConnection::connect(const char* host, const int port)
Mike Fiore 1:096f484f3ae6 9 {
Mike Fiore 1:096f484f3ae6 10 if (!ip->open(host, port, IPStack::TCP)) {
Mike Fiore 1:096f484f3ae6 11 return -1;
Mike Fiore 1:096f484f3ae6 12 }
Mike Fiore 1:096f484f3ae6 13 return 0;
Mike Fiore 1:096f484f3ae6 14 }
Mike Fiore 1:096f484f3ae6 15
Mike Fiore 1:096f484f3ae6 16 bool TCPSocketConnection::is_connected(void)
Mike Fiore 1:096f484f3ae6 17 {
Mike Fiore 1:096f484f3ae6 18 return ip->isOpen();
Mike Fiore 1:096f484f3ae6 19 }
Mike Fiore 1:096f484f3ae6 20
Mike Fiore 1:096f484f3ae6 21 int TCPSocketConnection::send(char* data, int length)
Mike Fiore 1:096f484f3ae6 22 {
Mike Fiore 1:096f484f3ae6 23 Timer tmr;
Mike Fiore 1:096f484f3ae6 24
Mike Fiore 1:096f484f3ae6 25 if (!_blocking) {
Mike Fiore 1:096f484f3ae6 26 tmr.start();
Mike Fiore 1:096f484f3ae6 27 while (tmr.read_ms() < _timeout) {
Mike Fiore 1:096f484f3ae6 28 if (ip->writeable())
Mike Fiore 1:096f484f3ae6 29 break;
Mike Fiore 1:096f484f3ae6 30 }
Mike Fiore 1:096f484f3ae6 31 if (tmr.read_ms() >= _timeout) {
Mike Fiore 1:096f484f3ae6 32 return -1;
Mike Fiore 1:096f484f3ae6 33 }
Mike Fiore 1:096f484f3ae6 34 }
Mike Fiore 1:096f484f3ae6 35 return ip->write(data, length, 0);
Mike Fiore 1:096f484f3ae6 36 }
Mike Fiore 1:096f484f3ae6 37
Mike Fiore 1:096f484f3ae6 38 // -1 if unsuccessful, else number of bytes written
Mike Fiore 1:096f484f3ae6 39 int TCPSocketConnection::send_all(char* data, int length)
Mike Fiore 1:096f484f3ae6 40 {
Mike Fiore 1:096f484f3ae6 41 if (_blocking) {
Mike Fiore 1:096f484f3ae6 42 return ip->write(data, length, -1);
Mike Fiore 1:096f484f3ae6 43 } else {
Mike Fiore 1:096f484f3ae6 44 return ip->write(data, length, _timeout);
Mike Fiore 1:096f484f3ae6 45 }
Mike Fiore 1:096f484f3ae6 46 }
Mike Fiore 1:096f484f3ae6 47
Mike Fiore 1:096f484f3ae6 48 // -1 if unsuccessful, else number of bytes received
Mike Fiore 1:096f484f3ae6 49 int TCPSocketConnection::receive(char* data, int length)
Mike Fiore 1:096f484f3ae6 50 {
Mike Fiore 1:096f484f3ae6 51 Timer tmr;
Mike Fiore 1:096f484f3ae6 52 int time = -1;
Mike Fiore 1:096f484f3ae6 53
Mike Fiore 1:096f484f3ae6 54 if (!_blocking) {
Mike Fiore 1:096f484f3ae6 55 tmr.start();
Mike Fiore 1:096f484f3ae6 56 while (time < _timeout + 20) {
Mike Fiore 1:096f484f3ae6 57 if (ip->readable()) {
Mike Fiore 1:096f484f3ae6 58 break;
Mike Fiore 1:096f484f3ae6 59 }
Mike Fiore 1:096f484f3ae6 60 time = tmr.read_ms();
Mike Fiore 1:096f484f3ae6 61 }
Mike Fiore 1:096f484f3ae6 62 if (time >= _timeout + 20) {
Mike Fiore 1:096f484f3ae6 63 return -1;
Mike Fiore 1:096f484f3ae6 64 }
Mike Fiore 1:096f484f3ae6 65 } else {
Mike Fiore 1:096f484f3ae6 66 while(!ip->readable());
Mike Fiore 1:096f484f3ae6 67 }
Mike Fiore 1:096f484f3ae6 68
Mike Fiore 1:096f484f3ae6 69 return ip->read(data, length, 0);
Mike Fiore 1:096f484f3ae6 70 }
Mike Fiore 1:096f484f3ae6 71
Mike Fiore 1:096f484f3ae6 72
Mike Fiore 1:096f484f3ae6 73 // -1 if unsuccessful, else number of bytes received
Mike Fiore 1:096f484f3ae6 74 int TCPSocketConnection::receive_all(char* data, int length)
Mike Fiore 1:096f484f3ae6 75 {
Mike Fiore 1:096f484f3ae6 76 if (_blocking) {
Mike Fiore 1:096f484f3ae6 77 return ip->read(data, length, -1);
Mike Fiore 1:096f484f3ae6 78 } else {
Mike Fiore 1:096f484f3ae6 79 return ip->read(data, length, _timeout);
Mike Fiore 1:096f484f3ae6 80 }
Mike Fiore 1:096f484f3ae6 81 }