X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fshared%2FCommandBuffer.hpp;fp=src%2Fshared%2FCommandBuffer.hpp;h=316fe6bb43585cb07d2fd7686b3657960ef2412c;hb=b07f3c123fff221edeffb4864bab7db88d0d1f4d;hp=0000000000000000000000000000000000000000;hpb=20d0a76d2519c71009c3b3babec0df27529f8142;p=blank.git diff --git a/src/shared/CommandBuffer.hpp b/src/shared/CommandBuffer.hpp new file mode 100644 index 0000000..316fe6b --- /dev/null +++ b/src/shared/CommandBuffer.hpp @@ -0,0 +1,46 @@ +#ifndef BLANK_SHARED_COMMANDBUFFER_HPP_ +#define BLANK_SHARED_COMMANDBUFFER_HPP_ + +#include "CLIContext.hpp" +#include "../net/tcp.hpp" + +#include + + +namespace blank { + +/// Turns a tcp stream into commands and writes their +/// output back to the stream. +/// Instances delete themselves when OnRemove(tcp::Socket &) +/// is called, so make sure it was either allocated with new +/// and isn't dereferenced after removal or OnRemove is never +/// called. +class CommandBuffer +: public CLIContext +, public tcp::IOHandler { + +public: + explicit CommandBuffer(CLI &); + ~CommandBuffer() override; + + // CLIContext implementation + void Error(const std::string &) override; + void Message(const std::string &) override; + void Broadcast(const std::string &) override; + + /// IOHandler implementation + void OnSend(tcp::Socket &) override; + void OnRecv(tcp::Socket &) override; + void OnRemove(tcp::Socket &) noexcept override; + +private: + CLI &cli; + std::string write_buffer; + std::string read_buffer; + std::size_t head; + +}; + +} + +#endif