Eric Jung / GMMP_mbed_Ethernet_Reinit

Dependents:   ThingPlug_Ethernet_Example

Fork of GMMP_mbed by Eric Jung

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Client.cpp Source File

Client.cpp

00001 #include "Client.h"
00002 #include "mbed.h"
00003 
00004 #include <stdint.h>
00005 
00006 Client::Client() : _len(0), _sock() {
00007 }
00008 
00009 Client::~Client() {
00010 }
00011 
00012 int Client::connect(const char *host, uint16_t port) {
00013   return _sock.connect(host, port) == 0;
00014 }
00015 
00016 size_t Client::write(uint8_t b) {
00017   //return write(&b, 1);
00018   return write((char*)&b, 1); //lesmin
00019 }
00020 
00021 //size_t Client::write(const uint8_t *buf, size_t size) {
00022 size_t Client::write(char *buf, size_t size) {
00023   _sock.set_blocking(false, 1500);
00024   // NOTE: we know it's dangerous to cast from (const uint8_t *) to (char *),
00025   // but we are trying to maintain a stable interface between the Arduino
00026   // one and the mbed one. What's more, while TCPSocketConnection has no
00027   // intention of modifying the data here, it requires us to send a (char *)
00028   // typed data. So we belive it's safe to do the cast here.
00029   //return _sock.send_all(const_cast<char*>((const char*) buf), size);
00030   return _sock.send_all(buf, size);
00031 }
00032 
00033 int Client::available() {
00034   if (_len > 0) { return 1; }
00035   int ret = read(_buf, 1);
00036   if (ret <= 0) { return 0; }
00037   _len = ret;
00038   return 1;
00039 }
00040 
00041 int Client::read() {
00042   if (_len > 0) {
00043     _len = 0;
00044     return _buf[0];
00045   }
00046   return -1;
00047 }
00048 
00049 int Client::read(uint8_t *buf, size_t size) {
00050   return _sock.receive_all((char*) buf, size);
00051 }
00052 
00053 void Client::flush() {
00054   // does nothing, TCP stack takes care of this
00055 }
00056 
00057 void Client::stop() {
00058   _sock.close();
00059 }
00060 
00061 uint8_t Client::connected() {
00062   return _sock.is_connected();
00063 }