REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
tcp.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef ARDUINO
4
5#include "QNEthernet.h"
6#include "websockets/common.h"
7
8// Forward declaration to avoid circular reference
9namespace web {
10class LucidacWebsocketsClient;
11}
12
13namespace websockets {
14namespace network {
15using qindesign::network::EthernetClient;
16using qindesign::network::EthernetServer;
18
19struct TcpClient {
21 EthernetClient *client;
22
23 // Formerly, this was a kind-of-socket
24 // TcpClient(EthernetClient* client) : client(client) {}
25 // But now we have the full client context
27
29
30 bool connect(std::string host, int port) { return client && client->connect(host.c_str(), port); }
31
32 bool poll() {
33 return client && client->available() >
34 0; // Returns the amount of data available, whether the connection is closed or not.
35 }
36
37 bool available() {
38 return (bool)client; // Returns whether connected (at least in QNEthernet).
39 }
40
41 void send(std::string data) {
42 client && client->writeFully(reinterpret_cast<uint8_t *>(const_cast<char *>(data.c_str())), data.size());
43 }
44
45 void send(uint8_t *data, uint32_t len) { client && client->writeFully(data, len); }
46
47 std::string readLine() {
48 int val;
49 std::string line;
50 do {
51 val = client->read();
52 if (val < 0)
53 continue;
54 line += (char)val;
55 } while (val != '\n');
56 if (!available())
57 close();
58 return line;
59 }
60
61 int read(uint8_t *buffer, uint32_t len) { return client->read(buffer, len); }
62
63 void close() {
64 if (client)
65 client->stop();
66 }
67
68 ~TcpClient() { client->stop(); }
69};
70
71#define DUMMY_PORT 0
72
73class TcpServer {
74 EthernetServer server;
75
76public:
77 TcpServer() : server(DUMMY_PORT) {}
78
79 bool poll() { return server.availableForWrite(); }
80
81 bool listen(uint16_t port) {
82 server.begin(port);
83 return available();
84 }
85
87 while (available()) {
88 auto client = server.available();
89 // FIXME if we want to use the Server.
90 // if(client) return new TcpClient(client);
91 }
92 return new TcpClient;
93 }
94
95 bool available() { return server.availableForWrite(); }
96
97 void close() { server.end(); }
98
100 if (available())
101 close();
102 }
103};
104
105} // namespace network
106} // namespace websockets
107
108#endif // ARDUINO
TcpClient * accept()
Definition tcp.h:86
bool listen(uint16_t port)
Definition tcp.h:81
uint32_t
Definition flasher.cpp:195
Definition assets.h:11
This structure collects all relevant context about a running Websocket connection.
Definition server.h:24
std::string readLine()
Definition tcp.h:47
bool connect(std::string host, int port)
Definition tcp.h:30
EthernetClient * client
Definition tcp.h:21
void send(std::string data)
Definition tcp.h:41
LucidacWebsocketsClient * context
Definition tcp.h:20
int read(uint8_t *buffer, uint32_t len)
Definition tcp.h:61
void send(uint8_t *data, uint32_t len)
Definition tcp.h:45
#define DUMMY_PORT
Definition tcp.h:71