Forked version
Fork of M2XStreamClient by
Client.cpp@12:debf4b2f7960, 2014-09-26 (annotated)
- Committer:
- citrusbyte
- Date:
- Fri Sep 26 09:35:10 2014 +0000
- Revision:
- 12:debf4b2f7960
- Parent:
- 0:f479e4f4db0e
Use new buffered client implementation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jb8414 | 0:f479e4f4db0e | 1 | #include "Client.h" |
jb8414 | 0:f479e4f4db0e | 2 | #include "mbed.h" |
jb8414 | 0:f479e4f4db0e | 3 | |
jb8414 | 0:f479e4f4db0e | 4 | #include <stdint.h> |
jb8414 | 0:f479e4f4db0e | 5 | |
citrusbyte | 12:debf4b2f7960 | 6 | Client::Client() : _incnt(0), _outcnt(0), _sock() { |
citrusbyte | 12:debf4b2f7960 | 7 | _sock.set_blocking(false, 1500); |
jb8414 | 0:f479e4f4db0e | 8 | } |
jb8414 | 0:f479e4f4db0e | 9 | |
jb8414 | 0:f479e4f4db0e | 10 | Client::~Client() { |
jb8414 | 0:f479e4f4db0e | 11 | } |
jb8414 | 0:f479e4f4db0e | 12 | |
jb8414 | 0:f479e4f4db0e | 13 | int Client::connect(const char *host, uint16_t port) { |
jb8414 | 0:f479e4f4db0e | 14 | return _sock.connect(host, port) == 0; |
jb8414 | 0:f479e4f4db0e | 15 | } |
jb8414 | 0:f479e4f4db0e | 16 | |
jb8414 | 0:f479e4f4db0e | 17 | size_t Client::write(uint8_t b) { |
jb8414 | 0:f479e4f4db0e | 18 | return write(&b, 1); |
jb8414 | 0:f479e4f4db0e | 19 | } |
jb8414 | 0:f479e4f4db0e | 20 | |
jb8414 | 0:f479e4f4db0e | 21 | size_t Client::write(const uint8_t *buf, size_t size) { |
citrusbyte | 12:debf4b2f7960 | 22 | size_t cnt = 0; |
citrusbyte | 12:debf4b2f7960 | 23 | while (size) { |
citrusbyte | 12:debf4b2f7960 | 24 | int tmp = sizeof(_outbuf) - _outcnt; |
citrusbyte | 12:debf4b2f7960 | 25 | if (tmp > size) tmp = size; |
citrusbyte | 12:debf4b2f7960 | 26 | memcpy(_outbuf + _outcnt, buf, tmp); |
citrusbyte | 12:debf4b2f7960 | 27 | _outcnt += tmp; |
citrusbyte | 12:debf4b2f7960 | 28 | buf += tmp; |
citrusbyte | 12:debf4b2f7960 | 29 | size -= tmp; |
citrusbyte | 12:debf4b2f7960 | 30 | cnt += tmp; |
citrusbyte | 12:debf4b2f7960 | 31 | // if no space flush it |
citrusbyte | 12:debf4b2f7960 | 32 | if (_outcnt == sizeof(_outbuf)) |
citrusbyte | 12:debf4b2f7960 | 33 | _flushout(); |
citrusbyte | 12:debf4b2f7960 | 34 | } |
citrusbyte | 12:debf4b2f7960 | 35 | return cnt; |
citrusbyte | 12:debf4b2f7960 | 36 | } |
citrusbyte | 12:debf4b2f7960 | 37 | |
citrusbyte | 12:debf4b2f7960 | 38 | void Client::_flushout(void) |
citrusbyte | 12:debf4b2f7960 | 39 | { |
citrusbyte | 12:debf4b2f7960 | 40 | if (_outcnt > 0) { |
citrusbyte | 12:debf4b2f7960 | 41 | // NOTE: we know it's dangerous to cast from (const uint8_t *) to (char *), |
citrusbyte | 12:debf4b2f7960 | 42 | // but we are trying to maintain a stable interface between the Arduino |
citrusbyte | 12:debf4b2f7960 | 43 | // one and the mbed one. What's more, while TCPSocketConnection has no |
citrusbyte | 12:debf4b2f7960 | 44 | // intention of modifying the data here, it requires us to send a (char *) |
citrusbyte | 12:debf4b2f7960 | 45 | // typed data. So we belive it's safe to do the cast here. |
citrusbyte | 12:debf4b2f7960 | 46 | _sock.send_all(const_cast<char*>((const char*) _outbuf), _outcnt); |
citrusbyte | 12:debf4b2f7960 | 47 | _outcnt = 0; |
citrusbyte | 12:debf4b2f7960 | 48 | } |
citrusbyte | 12:debf4b2f7960 | 49 | } |
citrusbyte | 12:debf4b2f7960 | 50 | |
citrusbyte | 12:debf4b2f7960 | 51 | void Client::_fillin(void) |
citrusbyte | 12:debf4b2f7960 | 52 | { |
citrusbyte | 12:debf4b2f7960 | 53 | int tmp = sizeof(_inbuf) - _incnt; |
citrusbyte | 12:debf4b2f7960 | 54 | if (tmp) { |
citrusbyte | 12:debf4b2f7960 | 55 | tmp = _sock.receive_all((char*)_inbuf + _incnt, tmp); |
citrusbyte | 12:debf4b2f7960 | 56 | if (tmp > 0) |
citrusbyte | 12:debf4b2f7960 | 57 | _incnt += tmp; |
citrusbyte | 12:debf4b2f7960 | 58 | } |
citrusbyte | 12:debf4b2f7960 | 59 | } |
citrusbyte | 12:debf4b2f7960 | 60 | |
citrusbyte | 12:debf4b2f7960 | 61 | void Client::flush() { |
citrusbyte | 12:debf4b2f7960 | 62 | _flushout(); |
jb8414 | 0:f479e4f4db0e | 63 | } |
jb8414 | 0:f479e4f4db0e | 64 | |
jb8414 | 0:f479e4f4db0e | 65 | int Client::available() { |
citrusbyte | 12:debf4b2f7960 | 66 | if (_incnt == 0) { |
citrusbyte | 12:debf4b2f7960 | 67 | _flushout(); |
citrusbyte | 12:debf4b2f7960 | 68 | _fillin(); |
citrusbyte | 12:debf4b2f7960 | 69 | } |
citrusbyte | 12:debf4b2f7960 | 70 | return (_incnt > 0) ? 1 : 0; |
jb8414 | 0:f479e4f4db0e | 71 | } |
jb8414 | 0:f479e4f4db0e | 72 | |
jb8414 | 0:f479e4f4db0e | 73 | int Client::read() { |
citrusbyte | 12:debf4b2f7960 | 74 | uint8_t ch; |
citrusbyte | 12:debf4b2f7960 | 75 | return (read(&ch, 1) == 1) ? ch : -1; |
jb8414 | 0:f479e4f4db0e | 76 | } |
jb8414 | 0:f479e4f4db0e | 77 | |
citrusbyte | 12:debf4b2f7960 | 78 | int Client::read(uint8_t *buf, size_t size) { |
citrusbyte | 12:debf4b2f7960 | 79 | int cnt = 0; |
citrusbyte | 12:debf4b2f7960 | 80 | while (size) { |
citrusbyte | 12:debf4b2f7960 | 81 | // need more |
citrusbyte | 12:debf4b2f7960 | 82 | if (size > _incnt) { |
citrusbyte | 12:debf4b2f7960 | 83 | _flushout(); |
citrusbyte | 12:debf4b2f7960 | 84 | _fillin(); |
citrusbyte | 12:debf4b2f7960 | 85 | } |
citrusbyte | 12:debf4b2f7960 | 86 | if (_incnt > 0) { |
citrusbyte | 12:debf4b2f7960 | 87 | int tmp = _incnt; |
citrusbyte | 12:debf4b2f7960 | 88 | if (tmp > size) tmp = size; |
citrusbyte | 12:debf4b2f7960 | 89 | memcpy(buf, _inbuf, tmp); |
citrusbyte | 12:debf4b2f7960 | 90 | if (tmp != _incnt) |
citrusbyte | 12:debf4b2f7960 | 91 | memmove(_inbuf, _inbuf + tmp, _incnt - tmp); |
citrusbyte | 12:debf4b2f7960 | 92 | _incnt -= tmp; |
citrusbyte | 12:debf4b2f7960 | 93 | size -= tmp; |
citrusbyte | 12:debf4b2f7960 | 94 | buf += tmp; |
citrusbyte | 12:debf4b2f7960 | 95 | cnt += tmp; |
citrusbyte | 12:debf4b2f7960 | 96 | } else // no data |
citrusbyte | 12:debf4b2f7960 | 97 | break; |
citrusbyte | 12:debf4b2f7960 | 98 | } |
citrusbyte | 12:debf4b2f7960 | 99 | return cnt; |
jb8414 | 0:f479e4f4db0e | 100 | } |
jb8414 | 0:f479e4f4db0e | 101 | |
jb8414 | 0:f479e4f4db0e | 102 | void Client::stop() { |
jb8414 | 0:f479e4f4db0e | 103 | _sock.close(); |
jb8414 | 0:f479e4f4db0e | 104 | } |
jb8414 | 0:f479e4f4db0e | 105 | |
jb8414 | 0:f479e4f4db0e | 106 | uint8_t Client::connected() { |
citrusbyte | 12:debf4b2f7960 | 107 | return _sock.is_connected() ? 1 : 0; |
jb8414 | 0:f479e4f4db0e | 108 | } |