mirror of
https://github.com/thooge/esp32-nmea2000-obp60.git
synced 2025-12-13 05:53:06 +01:00
TCP server separated
This commit is contained in:
@@ -97,13 +97,18 @@ bool GwConfigHandler::reset(bool save){
|
||||
if (!save) return true;
|
||||
return saveConfig();
|
||||
}
|
||||
String GwConfigHandler::getString(const String name){
|
||||
GwConfigItem *i=findConfig(name);
|
||||
String GwConfigHandler::getString(const String name) const{
|
||||
GwConfigInterface *i=getConfigItem(name,false);
|
||||
if (!i) return String();
|
||||
return i->asString();
|
||||
}
|
||||
bool GwConfigHandler::getBool(const String name){
|
||||
GwConfigItem *i=findConfig(name);
|
||||
bool GwConfigHandler::getBool(const String name) const{
|
||||
GwConfigInterface *i=getConfigItem(name,false);
|
||||
if (!i) return false;
|
||||
return i->asBoolean();
|
||||
}
|
||||
int GwConfigHandler::getInt(const String name) const{
|
||||
GwConfigInterface *i=getConfigItem(name,false);
|
||||
if (!i) return 0;
|
||||
return i->asInt();
|
||||
}
|
||||
@@ -9,6 +9,7 @@ class GwConfigInterface{
|
||||
virtual String asString() const=0;
|
||||
virtual const char * asCString() const =0;
|
||||
virtual bool asBoolean() const = 0;
|
||||
virtual int asInt() const = 0;
|
||||
};
|
||||
class GwConfigItem: public GwConfigInterface{
|
||||
private:
|
||||
@@ -33,6 +34,9 @@ class GwConfigItem: public GwConfigInterface{
|
||||
virtual bool asBoolean() const{
|
||||
return strcasecmp(value.c_str(),"true") == 0;
|
||||
}
|
||||
virtual int asInt() const{
|
||||
return (int)value.toInt();
|
||||
}
|
||||
String getName() const{
|
||||
return name;
|
||||
}
|
||||
@@ -59,6 +63,8 @@ class GwConfigHandler{
|
||||
const String wifiClient="wifiClient";
|
||||
const String wifiPass="wifiPass";
|
||||
const String wifiSSID="wifiSSID";
|
||||
const String serverPort="serverPort";
|
||||
const String maxClients="maxClients";
|
||||
GwConfigHandler(GwLog *logger);
|
||||
bool loadConfig();
|
||||
bool saveConfig();
|
||||
@@ -67,20 +73,24 @@ class GwConfigHandler{
|
||||
bool reset(bool save);
|
||||
String toString() const;
|
||||
String toJson() const;
|
||||
String getString(const String name);
|
||||
bool getBool(const String name);
|
||||
String getString(const String name) const;
|
||||
bool getBool(const String name) const ;
|
||||
int getInt(const String name) const;
|
||||
GwConfigItem * findConfig(const String name, bool dummy=false);
|
||||
GwConfigInterface * getConfigItem(const String name, bool dummy=false) const;
|
||||
private:
|
||||
GwConfigItem* configs[5]={
|
||||
GwConfigItem* configs[7]={
|
||||
new GwConfigItem(sendUsb,"true"),
|
||||
new GwConfigItem (receiveUsb,"false"),
|
||||
new GwConfigItem (wifiClient,"false"),
|
||||
new GwConfigItem (wifiSSID,""),
|
||||
new GwConfigItem (wifiPass,"")
|
||||
new GwConfigItem (wifiPass,""),
|
||||
new GwConfigItem (serverPort,"2222"),
|
||||
new GwConfigItem (maxClients, "10")
|
||||
|
||||
};
|
||||
int getNumConfig() const{
|
||||
return 5;
|
||||
return 7;
|
||||
}
|
||||
};
|
||||
#endif
|
||||
58
lib/socketserver/GwSocketServer.cpp
Normal file
58
lib/socketserver/GwSocketServer.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include "GwSocketServer.h"
|
||||
|
||||
GwSocketServer::GwSocketServer(const GwConfigHandler *config,GwLog *logger){
|
||||
this->config=config;
|
||||
this->logger=logger;
|
||||
}
|
||||
void GwSocketServer::begin(){
|
||||
server=new WiFiServer(config->getInt(config->serverPort),config->getInt(config->maxClients));
|
||||
server->begin();
|
||||
logger->logString("Socket server created, port=%d",
|
||||
config->getInt(config->serverPort));
|
||||
|
||||
}
|
||||
void GwSocketServer::loop()
|
||||
{
|
||||
WiFiClient client = server->available(); // listen for incoming clients
|
||||
|
||||
if (client){
|
||||
logger->logString("new client connected from %s",
|
||||
client.remoteIP().toString().c_str());
|
||||
clients.push_back(wiFiClientPtr(new WiFiClient(client)));
|
||||
}
|
||||
for (auto it = clients.begin(); it != clients.end();it++)
|
||||
{
|
||||
if ((*it) != NULL)
|
||||
{
|
||||
if (!(*it)->connected())
|
||||
{
|
||||
logger->logString("client disconnect ");
|
||||
(*it)->stop();
|
||||
clients.erase(it);
|
||||
}
|
||||
else
|
||||
{
|
||||
while ((*it)->available())
|
||||
{
|
||||
char c = (*it)->read();
|
||||
//TODO: read data
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
it = clients.erase(it); // Should have been erased by StopClient
|
||||
}
|
||||
}
|
||||
}
|
||||
void GwSocketServer::sendToClients(const char *buf){
|
||||
for (auto it = clients.begin() ; it != clients.end(); it++) {
|
||||
if ( (*it) != NULL && (*it)->connected() ) {
|
||||
(*it)->println(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int GwSocketServer::numClients(){
|
||||
return clients.size();
|
||||
}
|
||||
23
lib/socketserver/GwSocketServer.h
Normal file
23
lib/socketserver/GwSocketServer.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef _GWSOCKETSERVER_H
|
||||
#define _GWSOCKETSERVER_H
|
||||
#include "GWConfig.h"
|
||||
#include "GwLog.h"
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <WiFi.h>
|
||||
|
||||
using wiFiClientPtr = std::shared_ptr<WiFiClient>;
|
||||
class GwSocketServer{
|
||||
private:
|
||||
const GwConfigHandler *config;
|
||||
GwLog *logger;
|
||||
std::list<wiFiClientPtr> clients;
|
||||
WiFiServer *server=NULL;
|
||||
public:
|
||||
GwSocketServer(const GwConfigHandler *config,GwLog *logger);
|
||||
void begin();
|
||||
void loop();
|
||||
void sendToClients(const char *buf);
|
||||
int numClients();
|
||||
};
|
||||
#endif
|
||||
Reference in New Issue
Block a user