Skip to content

Commit

Permalink
added server.isConnected() with very very basic checks to prevent cra…
Browse files Browse the repository at this point in the history
…shing. #156
  • Loading branch information
hoeken committed Aug 11, 2024
1 parent 53fd2db commit 2cc61fe
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
23 changes: 16 additions & 7 deletions examples/platformio/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,12 @@ void timeAvailable(struct timeval* t)

bool connectToWifi()
{
// dual client and AP mode
WiFi.mode(WIFI_AP_STA);
// WiFi.mode(WIFI_AP); // ap only mode
// WiFi.mode(WIFI_STA); // client only mode
WiFi.mode(WIFI_AP_STA); // ap and client

// Configure SoftAP
// dual client and AP mode
WiFi.softAPConfig(softap_ip, softap_ip, IPAddress(255, 255, 255, 0)); // subnet FF FF FF 00
WiFi.softAP(softap_ssid, softap_password);
IPAddress myIP = WiFi.softAPIP();
Expand Down Expand Up @@ -268,12 +270,11 @@ void setup()
#endif
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2);

// set up our esp32 to listen on the local_hostname.local domain
if (!MDNS.begin(local_hostname)) {
// set up our esp32 to listen on the psychic.local domain
if (MDNS.begin(local_hostname))
MDNS.addService("http", "tcp", 80);
else
Serial.println("Error starting mDNS");
return;
}
MDNS.addService("http", "tcp", 80);

if (!LittleFS.begin()) {
Serial.println("LittleFS Mount Failed. Do Platform -> Build Filesystem Image and Platform -> Upload Filesystem Image from VSCode");
Expand Down Expand Up @@ -697,4 +698,12 @@ void loop()

lastUpdate = millis();
}

// just some dev code to test that starting / stopping the server works okay.
// delay(5000);
// Serial.println("Stopping Server");
// server.stop();
// delay(5000);
// Serial.println("Starting Server");
// server.start();
}
44 changes: 44 additions & 0 deletions src/PsychicHttpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include "PsychicWebHandler.h"
#include "PsychicWebSocket.h"
#include "WiFi.h"
#ifdef PSY_ENABLE_ETHERNET
#include "ETH.h"
#endif

PsychicHttpServer::PsychicHttpServer(uint16_t port) : _onOpen(NULL),
_onClose(NULL)
Expand Down Expand Up @@ -72,11 +75,32 @@ void PsychicHttpServer::setPort(uint16_t port)
this->config.server_port = port;
}

bool PsychicHttpServer::isConnected()
{
if (WiFi.softAPIP())
return true;
if (WiFi.localIP())
return true;

#ifdef PSY_ENABLE_ETHERNET
if (ETH.localIP())
return true;
#endif

return false;
}

esp_err_t PsychicHttpServer::start()
{
if (_running)
return ESP_OK;

// starting without network will crash us.
if (!isConnected()) {
ESP_LOGE(PH_TAG, "Server start failed - no network.");
return ESP_FAIL;
}

esp_err_t ret;

#ifdef ENABLE_ASYNC
Expand Down Expand Up @@ -140,6 +164,26 @@ esp_err_t PsychicHttpServer::stop()
if (!_running)
return ESP_OK;

// some handlers (aka websockets) need actual endpoints in esp-idf http_server
for (auto& endpoint : _esp_idf_endpoints) {
ESP_LOGD(PH_TAG, "Removing endpoint %s | %s", endpoint.uri, http_method_str((http_method)endpoint.method));

// Unregister endpoint with ESP-IDF server
esp_err_t ret = httpd_unregister_uri_handler(this->server, endpoint.uri, endpoint.method);
if (ret != ESP_OK)
ESP_LOGE(PH_TAG, "Removal of endpoint failed (%s)", esp_err_to_name(ret));
}

// Unregister a handler for each http_method method - it will match all requests with that URI/method
for (auto& method : supported_methods) {
ESP_LOGD(PH_TAG, "Removing %s meta endpoint", http_method_str((http_method)method));

// Unregister endpoint with ESP-IDF server
esp_err_t ret = httpd_unregister_uri_handler(this->server, "*", method);
if (ret != ESP_OK)
ESP_LOGE(PH_TAG, "Removal of endpoint failed (%s)", esp_err_to_name(ret));
}

esp_err_t ret = _stopServer();
if (ret != ESP_OK) {
ESP_LOGE(PH_TAG, "Server stop failed (%s)", esp_err_to_name(ret));
Expand Down
4 changes: 2 additions & 2 deletions src/PsychicHttpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
#include <regex>
#endif

enum PsychicHttpMethod
{
enum PsychicHttpMethod {
HTTP_ANY = 99
};

Expand Down Expand Up @@ -60,6 +59,7 @@ class PsychicHttpServer

virtual void setPort(uint16_t port);

bool isConnected();
bool isRunning() { return _running; }
esp_err_t begin() { return start(); }
esp_err_t end() { return stop(); }
Expand Down

0 comments on commit 2cc61fe

Please sign in to comment.