Fix socket bugs, timeout for connection

This commit is contained in:
Christoph Hagen 2023-12-05 22:54:47 +01:00
parent 4c23565b9c
commit 6f8838c32b
3 changed files with 31 additions and 16 deletions

View File

@ -84,6 +84,8 @@ private:
void disconnect();
bool shouldReconnect = true;
bool isConnecting = false;
uint32_t connectionTimeout = 0;
uint32_t nextReconnectAttemptMs = 0;
void didDisconnect();

View File

@ -22,20 +22,21 @@ void SesameController::configure(ServoConfiguration servoConfig, ServerConfigura
Ethernet.init(ethernetConfig.spiPinSS);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("[ERROR] Ethernet shield not found.");
} else if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("[ERROR] Ethernet cable is not connected.");
} else if (Ethernet.linkStatus() == Unknown) {
Serial.println("[ERROR] Ethernet cable status unknown.");
} else if (Ethernet.linkStatus() == LinkON) {
Serial.println("[INFO] Ethernet cable is connected.");
if (Ethernet.begin(ethernetConfig.macAddress, ethernetConfig.dhcpLeaseTimeoutMs, ethernetConfig.dhcpLeaseResponseTimeoutMs) == 1) {
Serial.print("[INFO] DHCP assigned IP ");
Serial.println(Ethernet.localIP());
ethernetIsConfigured = true;
} else {
if (Ethernet.begin(ethernetConfig.macAddress, ethernetConfig.dhcpLeaseTimeoutMs, ethernetConfig.dhcpLeaseResponseTimeoutMs) == 1) {
Serial.print("[INFO] DHCP assigned IP ");
Serial.println(Ethernet.localIP());
ethernetIsConfigured = true;
} else {
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("[ERROR] Ethernet shield not found.");
} else if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("[ERROR] Ethernet cable is not connected.");
} else if (Ethernet.linkStatus() == Unknown) {
Serial.println("[ERROR] Ethernet cable status unknown.");
} else if (Ethernet.linkStatus() == LinkON) {
Serial.println("[INFO] Ethernet cable is connected.");
// Try to configure using IP address instead of DHCP
Ethernet.begin(ethernetConfig.macAddress, ethernetConfig.manualIp, ethernetConfig.manualDnsAddress);
Serial.print("[WARNING] DHCP failed, using self-assigned IP ");

View File

@ -20,6 +20,9 @@ void ServerConnection::connect() {
return;
}
isConnecting = true;
Serial.printf("[INFO] Connecting to %s:%d%s\n", configuration.url, configuration.port, configuration.path);
connectionTimeout = currentTime + configuration.socketHeartbeatIntervalMs;
webSocket.begin(configuration.url, configuration.port, configuration.path);
webSocket.setAuthorization(configuration.key);
@ -31,7 +34,7 @@ void ServerConnection::connect() {
}
void ServerConnection::didDisconnect() {
if (shouldReconnect) {
if (shouldReconnect || isConnecting) {
return; // Disconnect already registered.
}
Serial.println("[INFO] Socket disconnected");
@ -40,6 +43,7 @@ void ServerConnection::didDisconnect() {
}
void ServerConnection::didConnect() {
isConnecting = false;
Serial.println("[INFO] Socket connected");
webSocket.sendTXT(configuration.key);
webSocket.enableHeartbeat(configuration.socketHeartbeatIntervalMs, configuration.socketHeartbeatTimeoutMs, configuration.socketHeartbeatFailureReconnectCount);
@ -52,10 +56,16 @@ void ServerConnection::disconnect() {
void ServerConnection::loop(uint32_t millis) {
currentTime = millis;
webSocket.loop();
if (shouldReconnect) {
if (shouldReconnect && !isConnecting) {
shouldReconnect = false;
connect();
}
if (isConnecting && millis > connectionTimeout) {
Serial.println("[INFO] Failed to connect");
disconnect();
shouldReconnect = true;
isConnecting = false;
}
}
void ServerConnection::webSocketEventHandler(WStype_t type, uint8_t * payload, size_t length) {
@ -75,11 +85,13 @@ switch(type) {
case WStype_PONG:
break;
case WStype_PING:
break;
case WStype_ERROR:
case WStype_FRAGMENT_TEXT_START:
case WStype_FRAGMENT_BIN_START:
case WStype_FRAGMENT:
case WStype_FRAGMENT_FIN:
Serial.printf("[WARN] Unexpected socket event %d\n", type);
controller->sendServerError(MessageResult::UnexpectedSocketEvent);
break;
}