First TCP communicator
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <queue>
|
||||
|
||||
namespace grbl {
|
||||
|
||||
struct transport;
|
||||
|
||||
struct transport_callbacks {
|
||||
virtual void on_connected(transport*) = 0;
|
||||
virtual void on_disconnected(transport*) = 0;
|
||||
virtual void on_line_received(std::string line, transport* ) = 0;
|
||||
};
|
||||
|
||||
struct transport {
|
||||
virtual void open(transport_callbacks& cb) = 0;
|
||||
virtual void close() = 0;
|
||||
virtual void send(std::string line) = 0;
|
||||
};
|
||||
|
||||
struct tcp_transport : public transport {
|
||||
virtual ~tcp_transport();
|
||||
tcp_transport(std::string address, uint16_t port);
|
||||
|
||||
void open(transport_callbacks& cb) override;
|
||||
void close() override;
|
||||
void send(std::string line) override;
|
||||
|
||||
private:
|
||||
|
||||
void worker();
|
||||
|
||||
std::string ip;
|
||||
uint16_t port;
|
||||
transport_callbacks *listener = nullptr;
|
||||
int fd = -1;
|
||||
volatile bool is_connected = false;
|
||||
std::thread live_check_thread;
|
||||
std::queue<std::string> send_queue;
|
||||
volatile bool should_quit = false;
|
||||
bool is_empty_line(std::string line);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user