Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: ThingPlug_Ethernet_Example
Fork of GMMP_mbed by
Network/TCPclient/Client.cpp@0:7e575e5f88ec, 2015-08-09 (annotated)
- Committer:
 - lesmin
 - Date:
 - Sun Aug 09 14:11:35 2015 +0000
 - Revision:
 - 0:7e575e5f88ec
 
forked from GMMP
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| lesmin | 0:7e575e5f88ec | 1 | #include "Client.h" | 
| lesmin | 0:7e575e5f88ec | 2 | #include "mbed.h" | 
| lesmin | 0:7e575e5f88ec | 3 | |
| lesmin | 0:7e575e5f88ec | 4 | #include <stdint.h> | 
| lesmin | 0:7e575e5f88ec | 5 | |
| lesmin | 0:7e575e5f88ec | 6 | Client::Client() : _len(0), _sock() { | 
| lesmin | 0:7e575e5f88ec | 7 | } | 
| lesmin | 0:7e575e5f88ec | 8 | |
| lesmin | 0:7e575e5f88ec | 9 | Client::~Client() { | 
| lesmin | 0:7e575e5f88ec | 10 | } | 
| lesmin | 0:7e575e5f88ec | 11 | |
| lesmin | 0:7e575e5f88ec | 12 | int Client::connect(const char *host, uint16_t port) { | 
| lesmin | 0:7e575e5f88ec | 13 | return _sock.connect(host, port) == 0; | 
| lesmin | 0:7e575e5f88ec | 14 | } | 
| lesmin | 0:7e575e5f88ec | 15 | |
| lesmin | 0:7e575e5f88ec | 16 | size_t Client::write(uint8_t b) { | 
| lesmin | 0:7e575e5f88ec | 17 | //return write(&b, 1); | 
| lesmin | 0:7e575e5f88ec | 18 | return write((char*)&b, 1); //lesmin | 
| lesmin | 0:7e575e5f88ec | 19 | } | 
| lesmin | 0:7e575e5f88ec | 20 | |
| lesmin | 0:7e575e5f88ec | 21 | //size_t Client::write(const uint8_t *buf, size_t size) { | 
| lesmin | 0:7e575e5f88ec | 22 | size_t Client::write(char *buf, size_t size) { | 
| lesmin | 0:7e575e5f88ec | 23 | _sock.set_blocking(false, 1500); | 
| lesmin | 0:7e575e5f88ec | 24 | // NOTE: we know it's dangerous to cast from (const uint8_t *) to (char *), | 
| lesmin | 0:7e575e5f88ec | 25 | // but we are trying to maintain a stable interface between the Arduino | 
| lesmin | 0:7e575e5f88ec | 26 | // one and the mbed one. What's more, while TCPSocketConnection has no | 
| lesmin | 0:7e575e5f88ec | 27 | // intention of modifying the data here, it requires us to send a (char *) | 
| lesmin | 0:7e575e5f88ec | 28 | // typed data. So we belive it's safe to do the cast here. | 
| lesmin | 0:7e575e5f88ec | 29 | //return _sock.send_all(const_cast<char*>((const char*) buf), size); | 
| lesmin | 0:7e575e5f88ec | 30 | return _sock.send_all(buf, size); | 
| lesmin | 0:7e575e5f88ec | 31 | } | 
| lesmin | 0:7e575e5f88ec | 32 | |
| lesmin | 0:7e575e5f88ec | 33 | int Client::available() { | 
| lesmin | 0:7e575e5f88ec | 34 | if (_len > 0) { return 1; } | 
| lesmin | 0:7e575e5f88ec | 35 | int ret = read(_buf, 1); | 
| lesmin | 0:7e575e5f88ec | 36 | if (ret <= 0) { return 0; } | 
| lesmin | 0:7e575e5f88ec | 37 | _len = ret; | 
| lesmin | 0:7e575e5f88ec | 38 | return 1; | 
| lesmin | 0:7e575e5f88ec | 39 | } | 
| lesmin | 0:7e575e5f88ec | 40 | |
| lesmin | 0:7e575e5f88ec | 41 | int Client::read() { | 
| lesmin | 0:7e575e5f88ec | 42 | if (_len > 0) { | 
| lesmin | 0:7e575e5f88ec | 43 | _len = 0; | 
| lesmin | 0:7e575e5f88ec | 44 | return _buf[0]; | 
| lesmin | 0:7e575e5f88ec | 45 | } | 
| lesmin | 0:7e575e5f88ec | 46 | return -1; | 
| lesmin | 0:7e575e5f88ec | 47 | } | 
| lesmin | 0:7e575e5f88ec | 48 | |
| lesmin | 0:7e575e5f88ec | 49 | int Client::read(uint8_t *buf, size_t size) { | 
| lesmin | 0:7e575e5f88ec | 50 | return _sock.receive_all((char*) buf, size); | 
| lesmin | 0:7e575e5f88ec | 51 | } | 
| lesmin | 0:7e575e5f88ec | 52 | |
| lesmin | 0:7e575e5f88ec | 53 | void Client::flush() { | 
| lesmin | 0:7e575e5f88ec | 54 | // does nothing, TCP stack takes care of this | 
| lesmin | 0:7e575e5f88ec | 55 | } | 
| lesmin | 0:7e575e5f88ec | 56 | |
| lesmin | 0:7e575e5f88ec | 57 | void Client::stop() { | 
| lesmin | 0:7e575e5f88ec | 58 | _sock.close(); | 
| lesmin | 0:7e575e5f88ec | 59 | } | 
| lesmin | 0:7e575e5f88ec | 60 | |
| lesmin | 0:7e575e5f88ec | 61 | uint8_t Client::connected() { | 
| lesmin | 0:7e575e5f88ec | 62 | return _sock.is_connected(); | 
| lesmin | 0:7e575e5f88ec | 63 | } | 
