#include <cinttypes>
#include <iostream>
#include <vector>
#include <stdexcept>
#include <cstring>

using namespace std;

struct IReceiver {
    virtual void Receive(const char* data, unsigned int size) = 0;
    virtual ~IReceiver() {};
};

struct ICallback {
    virtual void BinaryPacket(const char* data, unsigned int size) = 0;
    virtual void TextPacket(const char* data, unsigned int size) = 0;
    virtual ~ICallback() {}
};

class CallbackImpl : public ICallback {
public:
    CallbackImpl(std::ostream& out)
            : m_out(out)
            , m_packetsReceived(0) {}
    
    void BinaryPacket(const char* data, unsigned int size) override {
        m_out << "Binary packet received (" << size << " bytes)" << std::endl;
        ++m_packetsReceived;
    }
    void TextPacket(const char* data, unsigned int size) override {
        m_out << "Text packet received (" << size << " characters)" << std::endl;
        ++m_packetsReceived;
    }
    uint32_t packetsReceived() const {return m_packetsReceived;}

private:
    std::ostream& m_out;
    uint32_t m_packetsReceived;
};

class ReceiverImpl : public IReceiver {
private:
    enum PacketType { None, Binary, Text };
    
public:
    ReceiverImpl(ICallback* callback)
            : m_callback(callback)
            , m_currentPacketType(None)
            , m_binaryDataSize(-1)
            , m_checkBeginIndex(-1) {

        m_inputBuffer.reserve(4096);
    }

    void Receive(const char* data, unsigned int size) override {
        if (!data || size == 0) {
            return;
        }
        m_inputBuffer.insert(m_inputBuffer.end(), data, data + size);
        while (processData());
    }

private:
    int processData() {
        switch (m_currentPacketType) {
            case None: {
                if (m_inputBuffer.size() == 0) {
                    return 0;
                }
                if (m_inputBuffer[0] == 0x24) {
                    m_currentPacketType = Binary;
                    m_binaryDataSize = -1;
                } else {
                    m_currentPacketType = Text;
                    m_checkBeginIndex = 0;
                }
                return 1;
            }
            case Binary: {
                constexpr uint32_t SizeBytesCount = (uint32_t) sizeof(uint32_t);
                uint32_t size = (uint32_t) m_inputBuffer.size();
                if (m_binaryDataSize == (uint32_t) -1) {
                    if (size < SizeBytesCount + 1) {
                        return 0;
                    }
                    memcpy(&m_binaryDataSize, &m_inputBuffer[1], SizeBytesCount);
                }
                if (size > m_binaryDataSize + SizeBytesCount) {
                    m_callback->BinaryPacket(&m_inputBuffer[SizeBytesCount + 1], m_binaryDataSize);
                    finishPacket(m_binaryDataSize + SizeBytesCount + 1);
                    return 1;
                }
                return 0;
            }
            case Text: {
                const uint32_t eolIndex = findEol();
                if (eolIndex == (uint32_t) -1) {
                    return 0;
                }
                m_callback->TextPacket(&m_inputBuffer[0], eolIndex);
                finishPacket(eolIndex + 4);
                return 1;
            }
            default: {
                break;
            }
        }
        throw std::runtime_error("What are you doing here?");
    }
    uint32_t findEol() {
        if (m_inputBuffer.size() < 4) {
            return (uint32_t) -1;
        }

        const char* const begin = m_inputBuffer.data();
        const char* const end = begin + m_inputBuffer.size() - 3;
        const char* it = begin + m_checkBeginIndex;
        while (it != end) {
            const char* const eolPtr = (const char*) memchr(it, '\r', end - it);
            if (!eolPtr) {
                break;
            }
            if (eolPtr[1] == '\n' && eolPtr[2] == '\r' && eolPtr[3] == '\n') {
                return (uint32_t) (eolPtr - begin);
            }
            it = eolPtr + 1;
        }
        m_checkBeginIndex = (uint32_t) (end - begin);
        return (uint32_t) -1;
    }
    void finishPacket(uint32_t packetSize) {
        m_inputBuffer.assign(m_inputBuffer.begin() + packetSize, m_inputBuffer.end());
        m_currentPacketType = None;
    }

private:
    ICallback* m_callback;
    std::vector<char> m_inputBuffer;
    PacketType m_currentPacketType;
    uint32_t m_binaryDataSize;
    uint32_t m_checkBeginIndex;
};

class DataProvider {
public:
    DataProvider() : m_currentPos(0) {}
    void prepareData(uint32_t packetsCount) {
        srand(298);
        m_data.clear();
        for (; packetsCount; --packetsCount) {
            const uint32_t packetSize = 128 + rand() % 16000;
            std::vector<char> data;
            if (rand() & 0x01) {
                data = generateBinaryPacket(packetSize);
            } else {
                data = generateTextPacket(packetSize);
            }
            m_data.insert(m_data.end(), data.begin(), data.end());
        }
    }
    void sendData(IReceiver* receiver) {
        m_currentPos = 0;
        const uint32_t size = (uint32_t) m_data.size();
        while (m_currentPos < size) {
            uint32_t sendSize = 500 + rand() % 2000;
            if (m_currentPos + sendSize > size) {
                sendSize = size - m_currentPos;
            }
            receiver->Receive(&m_data[m_currentPos], sendSize);
            m_currentPos += sendSize;
        }
    }

private:
    static std::vector<char> generateBinaryPacket(uint32_t size) {
        constexpr uint32_t SizeBytesCount = sizeof(uint32_t);
        std::vector<char> data(size + SizeBytesCount + 1, 0);
        data[0] = 0x24;
        memcpy(&data[1], &size, SizeBytesCount);
        for (uint32_t i = SizeBytesCount + 1, n = (uint32_t) data.size(); i < n; ++i) {
            data[i] = (char) rand();
        }
        return data;
    }
    static std::vector<char> generateTextPacket(uint32_t size) {
        std::vector<char> data(size + 4, ' ');
        for (uint32_t i = 0; i < size; ++i) {
            do {
                data[i] = (char) (32 + rand() % 95);
            } while (data[i] == 0x24);
        }
        memcpy(&data[size], "\r\n\r\n", 4);
        return data;
    }
private:
    std::vector<char> m_data;
    uint32_t m_currentPos;
};


int main() {
    CallbackImpl* callback = nullptr;
    ReceiverImpl* receiver = nullptr;
    try {
        callback = new CallbackImpl(std::cout);
        receiver = new ReceiverImpl(callback);

        constexpr uint32_t TestPacketsCount = 256;
        DataProvider dataProvider;

        std::cout << "Prepearing data..." << std::endl;
        dataProvider.prepareData(TestPacketsCount);
        std::cout << "OK." << std::endl;

        std::cout << "Sending data..." << std::endl;
        dataProvider.sendData(receiver);
        std::cout << "OK." << std::endl;
        
        std::cout << "Packets received " << callback->packetsReceived() << " of " << TestPacketsCount << std::endl;

    } catch (std::exception& exc) {
        std::cerr << "Exception caught: " << exc.what() << std::endl;
    }
    delete callback;
    delete receiver;

    return 0;
}