Files
grbl-sender/grbl_communication.h
T

48 lines
1.0 KiB
C++
Raw Normal View History

2023-04-27 15:53:44 +03:00
#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);
};
}