1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-23 12:51:24 +01:00

STM32 ethernet client handling fix

This commit is contained in:
pmantoine 2024-07-22 08:34:05 +08:00
parent 0babe2876f
commit 14cc1e463c
3 changed files with 79 additions and 54 deletions

View File

@ -215,16 +215,23 @@ bool EthernetInterface::checkLink() {
// gets disconnected and connected again // gets disconnected and connected again
if(!outboundRing) if(!outboundRing)
outboundRing=new RingStream(OUTBOUND_RING_SIZE); outboundRing=new RingStream(OUTBOUND_RING_SIZE);
// Clear out the clients
for (byte socket = 0; socket < MAX_SOCK_NUM; socket++) {
clients[socket].inUse = false;
}
} }
return true; return true;
} else { // LinkOFF } else { // LinkOFF
if (connected) { // Were connected, but no longer without a LINK! if (connected) { // Were connected, but no longer without a LINK!
DIAG(F("Ethernet cable disconnected")); DIAG(F("Ethernet cable disconnected"));
connected=false; connected=false;
//clean up any client //clean up any clients
for (byte socket = 0; socket < MAX_SOCK_NUM; socket++) { for (byte socket = 0; socket < MAX_SOCK_NUM; socket++) {
if(clients[socket].connected()) if (clients[socket].inUse && clients[socket].client.connected())
clients[socket].stop(); {
clients[socket].client.flush();
clients[socket].client.stop();
}
} }
mdns.removeServiceRecord(IP_PORT, MDNSServiceTCP); mdns.removeServiceRecord(IP_PORT, MDNSServiceTCP);
// tear down server // tear down server
@ -251,57 +258,68 @@ void EthernetInterface::loop2() {
// check for new client // check for new client
if (client) if (client)
{ {
byte socket; byte socket;
bool sockfound = false; bool sockfound = false;
for (socket = 0; socket < MAX_SOCK_NUM; socket++) { for (socket = 0; socket < MAX_SOCK_NUM; socket++)
if (clients[socket] && (clients[socket] == client)) { {
sockfound = true; if (clients[socket].inUse && (client == clients[socket].client))
if (Diag::ETHERNET) DIAG(F("Ethernet: Old client socket %d"),socket); {
break; sockfound = true;
} if (Diag::ETHERNET)
} DIAG(F("Ethernet: Old client socket %d"), socket);
if (!sockfound) { // new client break;
for (socket = 0; socket < MAX_SOCK_NUM; socket++) {
if (!clients[socket]) {
// On accept() the EthernetServer doesn't track the client anymore
// so we store it in our client array
clients[socket] = client;
if (Diag::ETHERNET) DIAG(F("Ethernet: New client socket %d"),socket);
break;
}
}
} }
if (socket==MAX_SOCK_NUM) DIAG(F("new Ethernet OVERFLOW")); }
if (!sockfound)
{ // new client
for (socket = 0; socket < MAX_SOCK_NUM; socket++)
{
if (!clients[socket].inUse)
{
// On accept() the EthernetServer doesn't track the client anymore
// so we store it in our client array
clients[socket].client = client;
clients[socket].inUse = true;
if (Diag::ETHERNET)
DIAG(F("Ethernet: New client socket %d"), socket);
break;
}
}
}
if (socket == MAX_SOCK_NUM)
DIAG(F("new Ethernet OVERFLOW"));
} }
// check for incoming data from all possible clients // check for incoming data from all possible clients
for (byte socket = 0; socket < MAX_SOCK_NUM; socket++) for (byte socket = 0; socket < MAX_SOCK_NUM; socket++)
{ {
if (clients[socket]) { if (clients[socket].inUse)
if (!clients[socket].connected()) { // stop any clients which disconnect {
CommandDistributor::forget(socket); if (!clients[socket].client.connected())
clients[socket].stop(); { // stop any clients which disconnect
#if defined(ARDUINO_ARCH_AVR) CommandDistributor::forget(socket);
clients[socket]=NULL; clients[socket].client.flush();
#else clients[socket].client.stop();
clients[socket]=(EthernetClient)nullptr; clients[socket].inUse = false;
#endif if (Diag::ETHERNET)
//if (Diag::ETHERNET) DIAG(F("Ethernet: disconnect %d "), socket);
DIAG(F("Ethernet: disconnect %d "), socket); return; // Trick: So that we do not continue in this loop with client that is NULL
return; // Trick: So that we do not continue in this loop with client that is NULL }
}
int available = clients[socket].client.available();
int available=clients[socket].available(); if (available > 0)
if (available > 0) { {
if (Diag::ETHERNET) DIAG(F("Ethernet: available socket=%d,avail=%d"), socket, available); if (Diag::ETHERNET)
// read bytes from a client DIAG(F("Ethernet: available socket=%d,avail=%d"), socket, available);
int count = clients[socket].read(buffer, MAX_ETH_BUFFER); // read bytes from a client
buffer[count] = '\0'; // terminate the string properly int count = clients[socket].client.read(buffer, MAX_ETH_BUFFER);
if (Diag::ETHERNET) DIAG(F(",count=%d:%e"), socket,buffer); buffer[count] = '\0'; // terminate the string properly
// execute with data going directly back if (Diag::ETHERNET)
CommandDistributor::parse(socket,buffer,outboundRing); DIAG(F(",count=%d:%e"), socket, buffer);
return; // limit the amount of processing that takes place within 1 loop() cycle. // execute with data going directly back
} CommandDistributor::parse(socket, buffer, outboundRing);
return; // limit the amount of processing that takes place within 1 loop() cycle.
}
} }
} }
@ -315,9 +333,10 @@ void EthernetInterface::loop2() {
DIAG(F("Ethernet outboundRing socket=%d error"), socketOut); DIAG(F("Ethernet outboundRing socket=%d error"), socketOut);
} else if (socketOut >= 0) { } else if (socketOut >= 0) {
int count=outboundRing->count(); int count=outboundRing->count();
if (Diag::ETHERNET) DIAG(F("Ethernet reply socket=%d, count=:%d"), socketOut,count); if (Diag::ETHERNET)
for(;count>0;count--) clients[socketOut].write(outboundRing->read()); DIAG(F("Ethernet reply socket=%d, count=:%d"), socketOut,count);
clients[socketOut].flush(); //maybe for(;count>0;count--) clients[socketOut].client.write(outboundRing->read());
clients[socketOut].client.flush(); //maybe
} }
} }
#endif #endif

View File

@ -36,7 +36,7 @@
#include <NativeEthernet.h> //TEENSY Ethernet Treiber #include <NativeEthernet.h> //TEENSY Ethernet Treiber
#include <NativeEthernetUdp.h> #include <NativeEthernetUdp.h>
#define MAX_SOCK_NUM 4 #define MAX_SOCK_NUM 4
#elif defined (ARDUINO_NUCLEO_F429ZI) || defined (ARDUINO_NUCLEO_F439ZI) #elif defined (ARDUINO_NUCLEO_F429ZI) || defined (ARDUINO_NUCLEO_F439ZI) || defined (ARDUINO_NUCLEO_F4X9ZI)
#include <LwIP.h> #include <LwIP.h>
// #include "STM32lwipopts.h" // #include "STM32lwipopts.h"
#include <STM32Ethernet.h> #include <STM32Ethernet.h>
@ -73,7 +73,12 @@ class EthernetInterface {
void loop2(); void loop2();
bool checkLink(); bool checkLink();
EthernetServer * server = NULL; EthernetServer * server = NULL;
EthernetClient clients[MAX_SOCK_NUM]; // accept up to MAX_SOCK_NUM client connections at the same time; This depends on the chipset used on the Shield struct {
EthernetClient client;
bool inUse;
} clients[MAX_CLIENT];
// accept up to MAX_SOCK_NUM client connections at the same time; This depends on the chipset used on the Shield
uint8_t buffer[MAX_ETH_BUFFER+1]; // buffer used by TCP for the recv uint8_t buffer[MAX_ETH_BUFFER+1]; // buffer used by TCP for the recv
RingStream * outboundRing = NULL; RingStream * outboundRing = NULL;
}; };

View File

@ -39,7 +39,8 @@
#if defined(I2C_USE_INTERRUPTS) && defined(ARDUINO_ARCH_STM32) #if defined(I2C_USE_INTERRUPTS) && defined(ARDUINO_ARCH_STM32)
#if defined(ARDUINO_NUCLEO_F401RE) || defined(ARDUINO_NUCLEO_F411RE) || defined(ARDUINO_NUCLEO_F446RE) \ #if defined(ARDUINO_NUCLEO_F401RE) || defined(ARDUINO_NUCLEO_F411RE) || defined(ARDUINO_NUCLEO_F446RE) \
|| defined(ARDUINO_NUCLEO_F412ZG) || defined(ARDUINO_NUCLEO_F413ZH) \ || defined(ARDUINO_NUCLEO_F412ZG) || defined(ARDUINO_NUCLEO_F413ZH) \
|| defined(ARDUINO_NUCLEO_F429ZI) || defined(ARDUINO_NUCLEO_F439ZI) || defined(ARDUINO_NUCLEO_F446ZE) || defined(ARDUINO_NUCLEO_F429ZI) || defined(ARDUINO_NUCLEO_F439ZI) || defined(ARDUINO_NUCLEO_F4X9ZI) \
|| defined(ARDUINO_NUCLEO_F446ZE)
// Assume I2C1 for now - default I2C bus on Nucleo-F411RE and likely all Nucleo-64 // Assume I2C1 for now - default I2C bus on Nucleo-F411RE and likely all Nucleo-64
// and Nucleo-144 variants // and Nucleo-144 variants
I2C_TypeDef *s = I2C1; I2C_TypeDef *s = I2C1;