-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTcpServer.cpp
67 lines (63 loc) · 1.54 KB
/
TcpServer.cpp
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
57
58
59
60
61
62
63
64
65
66
67
#include "LcdTcp.h"
//how many clients should be able to telnet to this ESP8266
#define MAX_SRV_CLIENTS 1
#define SRV_PORT 2400
WiFiServer server(SRV_PORT);
WiFiClient serverClients[MAX_SRV_CLIENTS];
void TcpServerInit(void)
{
Serial.println("TCP SERVER BEGIN WITH:");
server.begin();
server.setNoDelay(true);
Serial.print("IP:");
Serial.println(WiFi.localIP());
}
void TcpServerProc(void)
{
uint8_t i;
//check if there are any new clients
if (server.hasClient())
{
for (i = 0; i < MAX_SRV_CLIENTS; i++)
{
//find free/disconnected spot
if (!serverClients[i] || !serverClients[i].connected())
{
if (serverClients[i])
serverClients[i].stop();
serverClients[i] = server.available();
Serial.print("New client Id:");
Serial.print(i);
Serial.println("");
continue;
}
}
//no free/disconnected spot so reject
WiFiClient serverClient = server.available();
serverClient.stop();
}
//check clients for data
for (i = 0; i < MAX_SRV_CLIENTS; i++)
{
if (serverClients[i] && serverClients[i].connected())
{
if (serverClients[i].available())
{
ParseEventFrameStream(&serverClients[i]);
}
}
}
//check UART for data
// if(Serial.available()){
// size_t len = Serial.available();
// uint8_t sbuf[len];
// Serial.readBytes(sbuf, len);
// //push UART data to all connected telnet clients
// for(i = 0; i < MAX_SRV_CLIENTS; i++){
// if (serverClients[i] && serverClients[i].connected()){
// serverClients[i].write(sbuf, len);
// delay(1);
// }
// }
// }
}