FastLED 3.10.8
Loading...
Searching...
No Matches
tcp.h
Go to the documentation of this file.
1#pragma once
2
3// TCP socket, acceptor, and endpoint types — Asio-compatible shapes.
4// Requires native socket APIs (Windows or POSIX).
5// On embedded platforms (STM32, AVR, etc.) this file compiles to nothing.
6
9#include "fl/stl/span.h"
10#include "fl/stl/stdint.h"
11#include "fl/stl/string.h"
12#include "fl/stl/noexcept.h"
13
14namespace fl {
15namespace asio {
16namespace ip {
17namespace tcp {
18
21struct endpoint {
22 fl::string host; // hostname or IP address
23 u16 port;
24
26 endpoint(const fl::string &h, u16 p) FL_NO_EXCEPT : host(h), port(p) {}
27 endpoint(const char *h, u16 p) FL_NO_EXCEPT : host(h), port(p) {}
28
29 bool operator==(const endpoint &o) const FL_NO_EXCEPT {
30 return port == o.port && host == o.host;
31 }
32 bool operator!=(const endpoint &o) const FL_NO_EXCEPT { return !(*this == o); }
33};
34
35#ifdef FASTLED_HAS_NETWORKING
36
42class socket {
43 public:
44 socket() FL_NO_EXCEPT;
45 ~socket() FL_NO_EXCEPT;
46
47 // Movable
48 socket(socket &&other) FL_NO_EXCEPT;
49 socket &operator=(socket &&other) FL_NO_EXCEPT;
50
51 // Not copyable
52 socket(const socket &) FL_NO_EXCEPT = delete;
53 socket &operator=(const socket &) FL_NO_EXCEPT = delete;
54
56 bool is_open() const FL_NO_EXCEPT;
57
59 error_code connect(const endpoint &ep) FL_NO_EXCEPT;
60
62 void close() FL_NO_EXCEPT;
63
65 void shutdown() FL_NO_EXCEPT;
66
69 size_t read_some(fl::span<u8> buffer, error_code &ec) FL_NO_EXCEPT;
70
72 size_t write_some(fl::span<const u8> buffer, error_code &ec) FL_NO_EXCEPT;
73
75 void async_read_some(fl::span<u8> buffer, io_handler handler) FL_NO_EXCEPT;
76
78 void async_write_some(fl::span<const u8> buffer, io_handler handler) FL_NO_EXCEPT;
79
81 void async_connect(const endpoint &ep, connect_handler handler) FL_NO_EXCEPT;
82
84 void set_non_blocking(bool mode) FL_NO_EXCEPT;
85
87 bool is_non_blocking() const FL_NO_EXCEPT;
88
90 int native_handle() const FL_NO_EXCEPT;
91
93 void assign(int fd) FL_NO_EXCEPT;
94
95 private:
96 int mFd;
97 bool mNonBlocking;
98
99 void close_fd() FL_NO_EXCEPT;
100};
101
106class acceptor {
107 public:
108 acceptor() FL_NO_EXCEPT;
109 ~acceptor() FL_NO_EXCEPT;
110
111 // Not copyable or movable (owns listening socket)
112 acceptor(const acceptor &) FL_NO_EXCEPT = delete;
113 acceptor &operator=(const acceptor &) FL_NO_EXCEPT = delete;
114
116 error_code open(u16 port) FL_NO_EXCEPT;
117
119 error_code listen(int backlog = 5) FL_NO_EXCEPT;
120
123 error_code accept(socket &peer) FL_NO_EXCEPT;
124
126 void async_accept(socket &peer, connect_handler handler) FL_NO_EXCEPT;
127
129 void close() FL_NO_EXCEPT;
130
132 bool is_open() const FL_NO_EXCEPT;
133
135 int native_handle() const FL_NO_EXCEPT;
136
138 u16 port() const FL_NO_EXCEPT;
139
140 private:
141 int mFd;
142 u16 mPort;
143};
144
145#endif // FASTLED_HAS_NETWORKING
146
147} // namespace tcp
148} // namespace ip
149} // namespace asio
150} // namespace fl
fl::UICheckbox listen("Listen", true)
fl::function< void(const error_code &ec, size_t bytes_transferred)> io_handler
Asio-style I/O completion handler signature.
fl::function< void(const error_code &ec)> connect_handler
Asio-style connect completion handler signature.
unsigned char u8
Definition stdint.h:131
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
Base definition for an LED controller.
Definition crgb.hpp:179
Asio-compatible error code: numeric code + optional human-readable message.
Definition error_code.h:31
endpoint(const char *h, u16 p) FL_NO_EXCEPT
Definition tcp.h:27
endpoint() FL_NO_EXCEPT
Definition tcp.h:25
bool operator!=(const endpoint &o) const FL_NO_EXCEPT
Definition tcp.h:32
bool operator==(const endpoint &o) const FL_NO_EXCEPT
Definition tcp.h:29
endpoint(const fl::string &h, u16 p) FL_NO_EXCEPT
Definition tcp.h:26