]> git.localhorst.tv Git - blank.git/blob - src/shared/CommandBuffer.hpp
add TCP based CLI
[blank.git] / src / shared / CommandBuffer.hpp
1 #ifndef BLANK_SHARED_COMMANDBUFFER_HPP_
2 #define BLANK_SHARED_COMMANDBUFFER_HPP_
3
4 #include "CLIContext.hpp"
5 #include "../net/tcp.hpp"
6
7 #include <string>
8
9
10 namespace blank {
11
12 /// Turns a tcp stream into commands and writes their
13 /// output back to the stream.
14 /// Instances delete themselves when OnRemove(tcp::Socket &)
15 /// is called, so make sure it was either allocated with new
16 /// and isn't dereferenced after removal or OnRemove is never
17 /// called.
18 class CommandBuffer
19 : public CLIContext
20 , public tcp::IOHandler {
21
22 public:
23         explicit CommandBuffer(CLI &);
24         ~CommandBuffer() override;
25
26         // CLIContext implementation
27         void Error(const std::string &) override;
28         void Message(const std::string &) override;
29         void Broadcast(const std::string &) override;
30
31         /// IOHandler implementation
32         void OnSend(tcp::Socket &) override;
33         void OnRecv(tcp::Socket &) override;
34         void OnRemove(tcp::Socket &) noexcept override;
35
36 private:
37         CLI &cli;
38         std::string write_buffer;
39         std::string read_buffer;
40         std::size_t head;
41
42 };
43
44 }
45
46 #endif