FastLED 3.9.15
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) : host(h), port(p) {}
27 endpoint(const char *h, u16 p) : host(h), port(p) {}
28
29 bool operator==(const endpoint &o) const {
30 return port == o.port && host == o.host;
31 }
32 bool operator!=(const endpoint &o) const { return !(*this == o); }
33};
34
35#ifdef FASTLED_HAS_NETWORKING
36
42class socket {
43 public:
44 socket() FL_NOEXCEPT;
45 ~socket() FL_NOEXCEPT;
46
47 // Movable
48 socket(socket &&other) FL_NOEXCEPT;
49 socket &operator=(socket &&other) FL_NOEXCEPT;
50
51 // Not copyable
52 socket(const socket &) FL_NOEXCEPT = delete;
53 socket &operator=(const socket &) FL_NOEXCEPT = delete;
54
56 bool is_open() const;
57
59 error_code connect(const endpoint &ep);
60
62 void close();
63
65 void shutdown();
66
69 size_t read_some(fl::span<u8> buffer, error_code &ec);
70
72 size_t write_some(fl::span<const u8> buffer, error_code &ec);
73
75 void async_read_some(fl::span<u8> buffer, io_handler handler);
76
78 void async_write_some(fl::span<const u8> buffer, io_handler handler);
79
81 void async_connect(const endpoint &ep, connect_handler handler);
82
84 void set_non_blocking(bool mode);
85
87 bool is_non_blocking() const;
88
90 int native_handle() const;
91
93 void assign(int fd);
94
95 private:
96 int mFd;
97 bool mNonBlocking;
98
99 void close_fd();
100};
101
106class acceptor {
107 public:
108 acceptor() FL_NOEXCEPT;
109 ~acceptor() FL_NOEXCEPT;
110
111 // Not copyable or movable (owns listening socket)
112 acceptor(const acceptor &) FL_NOEXCEPT = delete;
113 acceptor &operator=(const acceptor &) FL_NOEXCEPT = delete;
114
116 error_code open(u16 port);
117
119 error_code listen(int backlog = 5);
120
123 error_code accept(socket &peer);
124
126 void async_accept(socket &peer, connect_handler handler);
127
129 void close();
130
132 bool is_open() const;
133
135 int native_handle() const;
136
138 u16 port() const;
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::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
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
Asio-compatible error code: numeric code + optional human-readable message.
Definition error_code.h:31
endpoint() FL_NOEXCEPT
Definition tcp.h:25
bool operator!=(const endpoint &o) const
Definition tcp.h:32
endpoint(const char *h, u16 p)
Definition tcp.h:27
endpoint(const fl::string &h, u16 p)
Definition tcp.h:26
bool operator==(const endpoint &o) const
Definition tcp.h:29