1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-27 01:56:14 +01:00

Ethernet UDP working

This commit is contained in:
Gregor Baues 2020-11-09 18:13:11 +01:00
parent 6d7d01b1b5
commit 34fe1256e7
5 changed files with 23 additions and 14 deletions

View File

@ -82,7 +82,7 @@ void setup()
eth1.setup(ETHERNET, UDPR); // ETHERNET, UDP on Port 2560 eth1.setup(ETHERNET, UDPR); // ETHERNET, UDP on Port 2560
// eth1.setup(ETHERNET, TCP); // ETHERNET, UDP on Port 2560 // eth1.setup(ETHERNET, TCP); // ETHERNET, UDP on Port 2560
eth2.setup(ETHERNET, TCP, 23); // ETHERNET, TCP on Port 23 for the CLI // eth2.setup(ETHERNET, TCP, 23); // ETHERNET, TCP on Port 23 for the CLI
// eth1.setup(ETHERNET, TCP, 8888); // ETHERNET, TCP on Port 8888 // eth1.setup(ETHERNET, TCP, 8888); // ETHERNET, TCP on Port 8888
// wifi.setup(WIFI, TCP); // WIFI on Port 2560 // wifi.setup(WIFI, TCP); // WIFI on Port 2560
// eth1.setHttpCallback(httpRequestHandler); // HTTP callback // eth1.setHttpCallback(httpRequestHandler); // HTTP callback

View File

@ -73,7 +73,7 @@ byte EthernetSetup::setup()
} }
else else
{ {
ERR(F("\nUDP failed to start")); ERR(F("UDP failed to start"));
connected = false; connected = false;
} }
break; break;

View File

@ -27,12 +27,15 @@ extern uint8_t diagNetworkClient;
template<class S, class C, class U> template<class S, class C, class U>
bool Transport<S,C,U>::setup(NetworkInterface *nw) { bool Transport<S,C,U>::setup(NetworkInterface *nw) {
t = new TransportProcessor();
if (protocol == TCP) { if (protocol == TCP) {
connectionPool(server); // server should have started here so create the connection pool only for TCP though connectionPool(server); // server should have started here so create the connection pool only for TCP though
t->udp = 0;
} else { } else {
connectionPool(udp); connectionPool(udp);
t->udp = udp;
} }
t = new TransportProcessor();
t->nwi = nw; // The TransportProcessor needs to know which Interface he is connected to t->nwi = nw; // The TransportProcessor needs to know which Interface he is connected to
connected = true; // server & clients which will recieve/send data have all e setup and are available connected = true; // server & clients which will recieve/send data have all e setup and are available
return true; return true;
@ -49,7 +52,7 @@ void Transport<S,C,U>::loop() {
}; };
case TCP: case TCP:
{ {
DBG(F("Transport: %s\n"), this->transport == WIFI ? "WIFI" : "ETHERNET"); DBG(F("Transport: %s"), this->transport == WIFI ? "WIFI" : "ETHERNET");
tcpSessionHandler(server); tcpSessionHandler(server);
}; };
case MQTT: case MQTT:
@ -69,7 +72,7 @@ void Transport<S, C, U>::connectionPool(S *server)
connections[i].client = &clients[i]; connections[i].client = &clients[i];
memset(connections[i].overflow, 0, MAX_OVERFLOW); memset(connections[i].overflow, 0, MAX_OVERFLOW);
connections[i].id = i; connections[i].id = i;
TRC(F("\nTCP Connection pool: [%d:%x]"), i, connections[i].client); TRC(F("TCP Connection pool: [%d:%x]"), i, connections[i].client);
} }
} }
template<class S, class C, class U> template<class S, class C, class U>
@ -82,7 +85,7 @@ void Transport<S, C, U>::connectionPool(U *udp)
memset(connections[i].overflow, 0, MAX_OVERFLOW); memset(connections[i].overflow, 0, MAX_OVERFLOW);
connections[i].id = i; connections[i].id = i;
TRC(F("\nUDP Connection pool: [%d:%x]"), i, connections[i].client); TRC(F("UDP Connection pool: [%d:%x]"), i, udp);
} }
} }
/** /**
@ -99,7 +102,7 @@ void Transport<S, C, U>::udpHandler(U* udp)
int packetSize = udp->parsePacket(); int packetSize = udp->parsePacket();
if (packetSize > 0) if (packetSize > 0)
{ {
TRC(F("\nReceived packet of size:[%d]"), packetSize); TRC(F("Received packet of size:[%d]"), packetSize);
IPAddress remote = udp->remoteIP(); IPAddress remote = udp->remoteIP();
char portBuffer[6]; char portBuffer[6];
TRC(F("From: [%d.%d.%d.%d: %s]"), remote[0], remote[1], remote[2], remote[3], utoa(udp->remotePort(), portBuffer, 10)); // DIAG has issues with unsigend int's so go through utoa TRC(F("From: [%d.%d.%d.%d: %s]"), remote[0], remote[1], remote[2], remote[3], utoa(udp->remotePort(), portBuffer, 10)); // DIAG has issues with unsigend int's so go through utoa

View File

@ -59,23 +59,28 @@ RingStream streamer(512); // buffer into which to feed the commands for handling
void sendWiThrottleToDCC(Connection *c, TransportProcessor *t, bool blocking) void sendWiThrottleToDCC(Connection *c, TransportProcessor *t, bool blocking)
{ {
// streamer.printStream();
byte *_buffer = streamer.getBuffer(); byte *_buffer = streamer.getBuffer();
memset(_buffer, 0, 512); // clear out the _buffer memset(_buffer, 0, 512); // clear out the _buffer
WiThrottle *wt = WiThrottle::getThrottle(c->id); // get a throttle for the Connection; will be created if it doesn't exist WiThrottle *wt = WiThrottle::getThrottle(c->id); // get a throttle for the Connection; will be created if it doesn't exist
// STRINGIFY(__FILE__); TRC(F("WiThrottle [%x:%x] parsing: [%e]"), wt, _buffer, t->command);
DBG(F("WiThrottle [%x:%x] parsing: [%e]"), wt, _buffer, t->command);
wt->parse(&streamer, (byte *)t->command); // get the response; not all commands will produce a reply wt->parse(&streamer, (byte *)t->command); // get the response; not all commands will produce a reply
if (streamer.count() != -1) if (streamer.count() != -1)
{ {
dumpRingStreamBuffer(_buffer, 512); dumpRingStreamBuffer(_buffer, 512);
if (c->client->connected()) TRC(F("UDP %x"), t->udp);
{
if ( t->udp != 0) {
TRC(F("Sending UDP WiThrottle response ..."));
t->udp->beginPacket(t->udp->remoteIP(), t->udp->remotePort());
t->udp->write(_buffer, strlen((char *)_buffer));
t->udp->endPacket();
} else if (c->client->connected()) {
c->client->write(_buffer, strlen((char *)_buffer)); c->client->write(_buffer, strlen((char *)_buffer));
} }
} }
// streamer.printStream();
streamer.resetStream(); streamer.resetStream();
} }

View File

@ -36,8 +36,9 @@ private:
void sendToDCC(Connection *c, TransportProcessor* t, bool blocking); void sendToDCC(Connection *c, TransportProcessor* t, bool blocking);
#endif #endif
public:
public:
UDP *udp; // need to carry the single UDP server instance over to the processor for sending packest
NetworkInterface *nwi; NetworkInterface *nwi;
uint8_t buffer[MAX_ETH_BUFFER]; uint8_t buffer[MAX_ETH_BUFFER];
char command[MAX_JMRI_CMD]; char command[MAX_JMRI_CMD];