4 Copyright (C) 2017 Marc Josef Pees. All rights reserved.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 See file LICENSE.txt for further informations on licensing terms.
13 Last updated July 10th, 2017
16 #ifndef ETHERNETSERVERSTREAM_H
17 #define ETHERNETSERVERSTREAM_H
23 //#define SERIAL_DEBUG
24 #include "firmataDebug.h"
26 class EthernetServerStream : public Stream
29 EthernetServerStream(IPAddress localip, uint16_t port);
34 size_t write(uint8_t);
35 void maintain(IPAddress localip);
38 EthernetClient client;
46 EthernetServer server = EthernetServer(3030);
47 bool listening = false;
48 bool connect_client();
53 * EthernetServerStream.cpp
54 * Copied here as a hack to linker issues with 3rd party board packages that don't properly
55 * implement the Arduino network APIs.
57 EthernetServerStream::EthernetServerStream(IPAddress localip, uint16_t port)
64 bool EthernetServerStream::connect_client()
68 if ( client && client.connected() ) return true;
72 EthernetClient newClient = server.available();
73 if ( !newClient ) return false;
76 DEBUG_PRINTLN("Connected");
81 EthernetServerStream::available()
83 return maintain() ? client.available() : 0;
87 EthernetServerStream::read()
89 return maintain() ? client.read() : -1;
93 EthernetServerStream::peek()
95 return maintain() ? client.peek() : -1;
98 void EthernetServerStream::flush()
105 EthernetServerStream::write(uint8_t c)
107 return maintain() ? client.write(c) : 0;
111 EthernetServerStream::maintain(IPAddress localip)
113 // ensure the local IP is updated in the case that it is changed by the DHCP server
114 if (this->localip != localip) {
115 this->localip = localip;
122 EthernetServerStream::stop()
132 EthernetServerStream::maintain()
134 if (connect_client()) return true;
140 server = EthernetServer(port);
147 #endif /* ETHERNETSERVERSTREAM_H */