-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcomm_tcp_socket_client.h
More file actions
56 lines (43 loc) · 1.23 KB
/
Copy pathcomm_tcp_socket_client.h
File metadata and controls
56 lines (43 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// (C) 2024-2026 by Folkert van Heusden
// Released under MIT license
#include "gen.h"
#include <atomic>
#if !defined(BUILD_FOR_PICO2W)
#include <mutex>
#endif
#include <string>
#include <thread>
#include "comm.h"
#include "my_lock.h"
#include "utils.h"
#if defined(_WIN32)
#include <ws2tcpip.h>
#include <winsock2.h>
#else
#define SOCKET int
#define INVALID_SOCKET -1
#endif
class comm_tcp_socket_client: public comm
{
private:
const std::string host;
const int port { -1 };
std::atomic_bool stop_flag { false };
SOCKET cfd { INVALID_SOCKET };
my_lock cfd_lock;
std::thread *th { nullptr };
public:
comm_tcp_socket_client(const std::string & host, const int port);
virtual ~comm_tcp_socket_client();
bool begin() override;
#if IS_POSIX
JsonDocument serialize() const override;
static comm_tcp_socket_client *deserialize(const JsonVariantConst j);
#endif
std::string get_identifier() const override { return host + format(":%d", port) + " (client)"; }
bool is_connected() override;
bool has_data() override;
uint8_t get_byte() override;
void send_data(const uint8_t *const in, const size_t n) override;
void operator()();
};