branch with improvemnts

Fork of M2XStreamClient by AT&T M2X Team

Committer:
mazgch
Date:
Sat Sep 13 15:49:53 2014 +0000
Revision:
11:5c71c2948812
Parent:
0:f479e4f4db0e
use local buffers and the flush or fill

Who changed what in which revision?

UserRevisionLine numberNew 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
mazgch 11:5c71c2948812 6 Client::Client() : _incnt(0), _outcnt(0), _sock() {
mazgch 11:5c71c2948812 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) {
mazgch 11:5c71c2948812 22 size_t cnt = 0;
mazgch 11:5c71c2948812 23 while (size) {
mazgch 11:5c71c2948812 24 int tmp = sizeof(_outbuf) - _outcnt;
mazgch 11:5c71c2948812 25 if (tmp > size) tmp = size;
mazgch 11:5c71c2948812 26 memcpy(_outbuf + _outcnt, buf, tmp);
mazgch 11:5c71c2948812 27 _outcnt += tmp;
mazgch 11:5c71c2948812 28 buf += tmp;
mazgch 11:5c71c2948812 29 size -= tmp;
mazgch 11:5c71c2948812 30 cnt += tmp;
mazgch 11:5c71c2948812 31 // if no space flush it
mazgch 11:5c71c2948812 32 if (_outcnt == sizeof(_outbuf))
mazgch 11:5c71c2948812 33 _flushout();
mazgch 11:5c71c2948812 34 }
mazgch 11:5c71c2948812 35 return cnt;
mazgch 11:5c71c2948812 36 }
mazgch 11:5c71c2948812 37
mazgch 11:5c71c2948812 38 void Client::_flushout(void)
mazgch 11:5c71c2948812 39 {
mazgch 11:5c71c2948812 40 if (_outcnt > 0) {
mazgch 11:5c71c2948812 41 // NOTE: we know it's dangerous to cast from (const uint8_t *) to (char *),
mazgch 11:5c71c2948812 42 // but we are trying to maintain a stable interface between the Arduino
mazgch 11:5c71c2948812 43 // one and the mbed one. What's more, while TCPSocketConnection has no
mazgch 11:5c71c2948812 44 // intention of modifying the data here, it requires us to send a (char *)
mazgch 11:5c71c2948812 45 // typed data. So we belive it's safe to do the cast here.
mazgch 11:5c71c2948812 46 _sock.send_all(const_cast<char*>((const char*) _outbuf), _outcnt);
mazgch 11:5c71c2948812 47 _outcnt = 0;
mazgch 11:5c71c2948812 48 }
mazgch 11:5c71c2948812 49 }
mazgch 11:5c71c2948812 50
mazgch 11:5c71c2948812 51 void Client::_fillin(void)
mazgch 11:5c71c2948812 52 {
mazgch 11:5c71c2948812 53 int tmp = sizeof(_inbuf) - _incnt;
mazgch 11:5c71c2948812 54 if (tmp) {
mazgch 11:5c71c2948812 55 tmp = _sock.receive_all((char*)_inbuf + _incnt, tmp);
mazgch 11:5c71c2948812 56 if (tmp > 0)
mazgch 11:5c71c2948812 57 _incnt += tmp;
mazgch 11:5c71c2948812 58 }
mazgch 11:5c71c2948812 59 }
mazgch 11:5c71c2948812 60
mazgch 11:5c71c2948812 61 void Client::flush() {
mazgch 11:5c71c2948812 62 _flushout();
jb8414 0:f479e4f4db0e 63 }
jb8414 0:f479e4f4db0e 64
jb8414 0:f479e4f4db0e 65 int Client::available() {
mazgch 11:5c71c2948812 66 if (_incnt == 0) {
mazgch 11:5c71c2948812 67 _flushout();
mazgch 11:5c71c2948812 68 _fillin();
mazgch 11:5c71c2948812 69 }
mazgch 11:5c71c2948812 70 return (_incnt > 0) ? 1 : 0;
jb8414 0:f479e4f4db0e 71 }
jb8414 0:f479e4f4db0e 72
jb8414 0:f479e4f4db0e 73 int Client::read() {
mazgch 11:5c71c2948812 74 uint8_t ch;
mazgch 11:5c71c2948812 75 return (read(&ch, 1) == 1) ? ch : -1;
jb8414 0:f479e4f4db0e 76 }
jb8414 0:f479e4f4db0e 77
mazgch 11:5c71c2948812 78 int Client::read(uint8_t *buf, size_t size) {
mazgch 11:5c71c2948812 79 int cnt = 0;
mazgch 11:5c71c2948812 80 while (size) {
mazgch 11:5c71c2948812 81 // need more
mazgch 11:5c71c2948812 82 if (size > _incnt) {
mazgch 11:5c71c2948812 83 _flushout();
mazgch 11:5c71c2948812 84 _fillin();
mazgch 11:5c71c2948812 85 }
mazgch 11:5c71c2948812 86 if (_incnt > 0) {
mazgch 11:5c71c2948812 87 int tmp = _incnt;
mazgch 11:5c71c2948812 88 if (tmp > size) tmp = size;
mazgch 11:5c71c2948812 89 memcpy(buf, _inbuf, tmp);
mazgch 11:5c71c2948812 90 if (tmp != _incnt)
mazgch 11:5c71c2948812 91 memmove(_inbuf, _inbuf + tmp, _incnt - tmp);
mazgch 11:5c71c2948812 92 _incnt -= tmp;
mazgch 11:5c71c2948812 93 size -= tmp;
mazgch 11:5c71c2948812 94 buf += tmp;
mazgch 11:5c71c2948812 95 cnt += tmp;
mazgch 11:5c71c2948812 96 } else // no data
mazgch 11:5c71c2948812 97 break;
mazgch 11:5c71c2948812 98 }
mazgch 11:5c71c2948812 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() {
mazgch 11:5c71c2948812 107 return _sock.is_connected() ? 1 : 0;
jb8414 0:f479e4f4db0e 108 }