/*
 * Copyright (C) 2020-2021 Aloysius Indrayanto
 *
 * This file is part of the eCxx library, see LICENSE file for the license details.
 */


#ifndef ECXX_PLATFORM_NODEMCU_TCP_CONSOLE_H
#define ECXX_PLATFORM_NODEMCU_TCP_CONSOLE_H


#include <eCxx/CTL/BasicOutputWriter.h>


//
// Forward declaration of WiFi client and server classes
//
class WiFiClient;
class WiFiServer;


namespace eCxx {


//
// A simple TCP console
//
// WARNING : All functions derived from BasicOutputWriter<T> that accept parameters with data types
//           'PGMCharPointer' and 'PGMCharPointerFar' expect the pointers to come from program
//           memory exclusively!
//
// NOTE    : Whenever possible, use the versions that accept near program pointers because those that
//           accept the far program pointers may have more overhead and restrictions.
//
class TCPConsole : public BasicOutputWriter<TCPConsole> {
    public:
        // # The default port for the console   server is port 23
        // # The default port for the dummy web server is port 80 (use 0 to disable the dummy web server)
        TCPConsole(uint16_t portConsole = 23, uint16_t portDummyWeb = 80);
        ~TCPConsole();

        void begin();
        void end();

        void handle();

        bool canRead() const;
        bool read(uint8_t& value);
        bool peek(uint8_t& value) const;

        bool canWrite() const;
        bool write(uint8_t value);
        void flush();

    private:
        WiFiServer* _tcpDummyWebServer;
        WiFiServer* _tcpConsoleServer;
        WiFiClient* _tcpConsoleClient;
};


} // namespace eCxx


#endif
