Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check tcp clients #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#define MAX_SRV_CLIENTS 4
#define RXBUFFERSIZE 1024
#define TXBUFFERSIZE 512
#define STACK_PROTECTOR 512 // bytes
#define HOSTNAME "esp-eBus"
#define RESET_PIN 3
Expand All @@ -29,6 +30,8 @@ WiFiClient serverClientsRO[MAX_SRV_CLIENTS];

unsigned long last_comms;

unsigned char txBuf[TXBUFFERSIZE];

int random_ch(){
#ifdef ESP32
return 6;
Expand Down Expand Up @@ -188,9 +191,18 @@ void loop() {

//check TCP clients for data
for (int i = 0; i < MAX_SRV_CLIENTS; i++){
while (serverClients[i].available() && Serial.availableForWrite() > 0) {
// working char by char is not very efficient
Serial.write(serverClients[i].read());
size_t client_data_len = serverClients[i].available();
size_t authorized_len = (client_data_len > TXBUFFERSIZE) ? TXBUFFERSIZE : client_data_len;

while (authorized_len && Serial.availableForWrite() > 0) {
size_t ret = serverClients[i].readBytes(txBuf, authorized_len);
if (ret < authorized_len)
// Error
continue;
Serial.write(txBuf, authorized_len);

client_data_len = serverClients[i].available();
authorized_len = (client_data_len > TXBUFFERSIZE) ? TXBUFFERSIZE : client_data_len;
}
}

Expand Down