1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-23 21:01:25 +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
@ -253,53 +260,64 @@ void EthernetInterface::loop2() {
{ {
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)) { {
if (clients[socket].inUse && (client == clients[socket].client))
{
sockfound = true; sockfound = true;
if (Diag::ETHERNET) DIAG(F("Ethernet: Old client socket %d"),socket); if (Diag::ETHERNET)
DIAG(F("Ethernet: Old client socket %d"), socket);
break; break;
} }
} }
if (!sockfound) { // new client if (!sockfound)
for (socket = 0; socket < MAX_SOCK_NUM; socket++) { { // new client
if (!clients[socket]) { for (socket = 0; socket < MAX_SOCK_NUM; socket++)
{
if (!clients[socket].inUse)
{
// On accept() the EthernetServer doesn't track the client anymore // On accept() the EthernetServer doesn't track the client anymore
// so we store it in our client array // so we store it in our client array
clients[socket] = client; clients[socket].client = client;
if (Diag::ETHERNET) DIAG(F("Ethernet: New client socket %d"),socket); clients[socket].inUse = true;
if (Diag::ETHERNET)
DIAG(F("Ethernet: New client socket %d"), socket);
break; break;
} }
} }
} }
if (socket==MAX_SOCK_NUM) DIAG(F("new Ethernet OVERFLOW")); 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 {
if (!clients[socket].client.connected())
{ // stop any clients which disconnect
CommandDistributor::forget(socket); CommandDistributor::forget(socket);
clients[socket].stop(); clients[socket].client.flush();
#if defined(ARDUINO_ARCH_AVR) clients[socket].client.stop();
clients[socket]=NULL; clients[socket].inUse = false;
#else if (Diag::ETHERNET)
clients[socket]=(EthernetClient)nullptr;
#endif
//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].available(); int available = clients[socket].client.available();
if (available > 0) { if (available > 0)
if (Diag::ETHERNET) DIAG(F("Ethernet: available socket=%d,avail=%d"), socket, available); {
if (Diag::ETHERNET)
DIAG(F("Ethernet: available socket=%d,avail=%d"), socket, available);
// read bytes from a client // read bytes from a client
int count = clients[socket].read(buffer, MAX_ETH_BUFFER); int count = clients[socket].client.read(buffer, MAX_ETH_BUFFER);
buffer[count] = '\0'; // terminate the string properly buffer[count] = '\0'; // terminate the string properly
if (Diag::ETHERNET) DIAG(F(",count=%d:%e"), socket,buffer); if (Diag::ETHERNET)
DIAG(F(",count=%d:%e"), socket, buffer);
// execute with data going directly back // execute with data going directly back
CommandDistributor::parse(socket,buffer,outboundRing); CommandDistributor::parse(socket, buffer, outboundRing);
return; // limit the amount of processing that takes place within 1 loop() cycle. 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;