branch with improvemnts
Fork of M2XStreamClient by
Client.cpp@5:ea68c8980ad8, 2013-10-24 (annotated)
- Committer:
- citrusbyte
- Date:
- Thu Oct 24 12:22:33 2013 +0000
- Revision:
- 5:ea68c8980ad8
Initial commit for M2X mbed client library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
citrusbyte | 5:ea68c8980ad8 | 1 | #include "Client.h" |
citrusbyte | 5:ea68c8980ad8 | 2 | #include "mbed.h" |
citrusbyte | 5:ea68c8980ad8 | 3 | |
citrusbyte | 5:ea68c8980ad8 | 4 | #include <stdint.h> |
citrusbyte | 5:ea68c8980ad8 | 5 | |
citrusbyte | 5:ea68c8980ad8 | 6 | Client::Client() : _len(0), _sock() { |
citrusbyte | 5:ea68c8980ad8 | 7 | } |
citrusbyte | 5:ea68c8980ad8 | 8 | |
citrusbyte | 5:ea68c8980ad8 | 9 | Client::~Client() { |
citrusbyte | 5:ea68c8980ad8 | 10 | } |
citrusbyte | 5:ea68c8980ad8 | 11 | |
citrusbyte | 5:ea68c8980ad8 | 12 | int Client::connect(const char *host, uint16_t port) { |
citrusbyte | 5:ea68c8980ad8 | 13 | return _sock.connect(host, port) == 0; |
citrusbyte | 5:ea68c8980ad8 | 14 | } |
citrusbyte | 5:ea68c8980ad8 | 15 | |
citrusbyte | 5:ea68c8980ad8 | 16 | size_t Client::write(uint8_t b) { |
citrusbyte | 5:ea68c8980ad8 | 17 | return write(&b, 1); |
citrusbyte | 5:ea68c8980ad8 | 18 | } |
citrusbyte | 5:ea68c8980ad8 | 19 | |
citrusbyte | 5:ea68c8980ad8 | 20 | size_t Client::write(const uint8_t *buf, size_t size) { |
citrusbyte | 5:ea68c8980ad8 | 21 | _sock.set_blocking(false, 15000); |
citrusbyte | 5:ea68c8980ad8 | 22 | // NOTE: we know it's dangerous to cast from (const uint8_t *) to (char *), |
citrusbyte | 5:ea68c8980ad8 | 23 | // but we are trying to maintain a stable interface between the Arduino |
citrusbyte | 5:ea68c8980ad8 | 24 | // one and the mbed one. What's more, while TCPSocketConnection has no |
citrusbyte | 5:ea68c8980ad8 | 25 | // intention of modifying the data here, it requires us to send a (char *) |
citrusbyte | 5:ea68c8980ad8 | 26 | // typed data. So we belive it's safe to do the cast here. |
citrusbyte | 5:ea68c8980ad8 | 27 | return _sock.send_all(const_cast<char*>((const char*) buf), size); |
citrusbyte | 5:ea68c8980ad8 | 28 | } |
citrusbyte | 5:ea68c8980ad8 | 29 | |
citrusbyte | 5:ea68c8980ad8 | 30 | int Client::available() { |
citrusbyte | 5:ea68c8980ad8 | 31 | if (_len > 0) { return 1; } |
citrusbyte | 5:ea68c8980ad8 | 32 | int ret = read(_buf, 1); |
citrusbyte | 5:ea68c8980ad8 | 33 | if (ret <= 0) { return 0; } |
citrusbyte | 5:ea68c8980ad8 | 34 | _len = ret; |
citrusbyte | 5:ea68c8980ad8 | 35 | return 1; |
citrusbyte | 5:ea68c8980ad8 | 36 | } |
citrusbyte | 5:ea68c8980ad8 | 37 | |
citrusbyte | 5:ea68c8980ad8 | 38 | int Client::read() { |
citrusbyte | 5:ea68c8980ad8 | 39 | if (_len > 0) { |
citrusbyte | 5:ea68c8980ad8 | 40 | _len = 0; |
citrusbyte | 5:ea68c8980ad8 | 41 | return _buf[0]; |
citrusbyte | 5:ea68c8980ad8 | 42 | } |
citrusbyte | 5:ea68c8980ad8 | 43 | return -1; |
citrusbyte | 5:ea68c8980ad8 | 44 | } |
citrusbyte | 5:ea68c8980ad8 | 45 | |
citrusbyte | 5:ea68c8980ad8 | 46 | int Client::read(uint8_t *buf, size_t size) { |
citrusbyte | 5:ea68c8980ad8 | 47 | return _sock.receive_all((char*) buf, size); |
citrusbyte | 5:ea68c8980ad8 | 48 | } |
citrusbyte | 5:ea68c8980ad8 | 49 | |
citrusbyte | 5:ea68c8980ad8 | 50 | void Client::flush() { |
citrusbyte | 5:ea68c8980ad8 | 51 | // does nothing, TCP stack takes care of this |
citrusbyte | 5:ea68c8980ad8 | 52 | } |
citrusbyte | 5:ea68c8980ad8 | 53 | |
citrusbyte | 5:ea68c8980ad8 | 54 | void Client::stop() { |
citrusbyte | 5:ea68c8980ad8 | 55 | _sock.close(); |
citrusbyte | 5:ea68c8980ad8 | 56 | } |
citrusbyte | 5:ea68c8980ad8 | 57 | |
citrusbyte | 5:ea68c8980ad8 | 58 | uint8_t Client::connected() { |
citrusbyte | 5:ea68c8980ad8 | 59 | return _sock.is_connected(); |
citrusbyte | 5:ea68c8980ad8 | 60 | } |
citrusbyte | 5:ea68c8980ad8 | 61 |