1
0
mirror of https://github.com/thooge/esp32-nmea2000-obp60.git synced 2025-12-12 13:33:06 +01:00

Merge branch 'wellenvogel:master' into master

This commit is contained in:
norbert-walter
2022-01-13 10:50:30 +01:00
committed by GitHub
7 changed files with 69 additions and 16 deletions

View File

@@ -128,9 +128,11 @@ void GwChannel::updateCounter(const char *msg, bool out)
}
}
bool GwChannel::canSendOut(const char *buffer){
bool GwChannel::canSendOut(const char *buffer, bool isSeasmart){
if (! enabled || ! impl) return false;
if (! NMEAout || readActisense) return false;
if (readActisense) return false;
if (! isSeasmart && ! NMEAout) return false;
if (isSeasmart && ! seaSmartOut) return false;
if (writeFilter && ! writeFilter->canPass(buffer)) return false;
return true;
}
@@ -177,9 +179,9 @@ void GwChannel::readMessages(GwChannel::NMEA0183Handler handler){
receiver->setHandler(handler);
impl->readMessages(receiver);
}
void GwChannel::sendToClients(const char *buffer, int sourceId){
void GwChannel::sendToClients(const char *buffer, int sourceId, bool isSeasmart){
if (! impl) return;
if (canSendOut(buffer)){
if (canSendOut(buffer,isSeasmart)){
if(impl->sendToClients(buffer,sourceId)){
updateCounter(buffer,true);
}

View File

@@ -56,7 +56,7 @@ class GwChannel{
}
bool isEnabled(){return enabled;}
bool shouldRead(){return enabled && NMEAin;}
bool canSendOut(const char *buffer);
bool canSendOut(const char *buffer, bool isSeasmart);
bool canReceive(const char *buffer);
bool sendSeaSmart(){ return seaSmartOut;}
bool sendToN2K(){return toN2k;}
@@ -67,7 +67,7 @@ class GwChannel{
void loop(bool handleRead, bool handleWrite);
typedef std::function<void(const char *buffer, int sourceid)> NMEA0183Handler;
void readMessages(NMEA0183Handler handler);
void sendToClients(const char *buffer, int sourceId);
void sendToClients(const char *buffer, int sourceId, bool isSeasmart=false);
typedef std::function<void(const tN2kMsg &msg, int sourceId)> N2kHandler ;
void parseActisense(N2kHandler handler);
void sendActisense(const tN2kMsg &msg, int sourceId);

View File

@@ -0,0 +1,20 @@
#pragma once
#include <lwip/sockets.h>
class GwSocketHelper{
public:
static bool setKeepAlive(int socket, bool noDelay=true){
int val=1;
if (noDelay){
if (setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(int)) != ESP_OK) return false;
}
if (setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, (char *)&val, sizeof(int)) != ESP_OK) return false;
val = 10; // seconds of idleness before first keepalive probe
if (setsockopt(socket, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof(val)) != ESP_OK) return false;
val = 5; // interval between first and subsequent keepalives
if (setsockopt(socket, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof(val)) != ESP_OK) return false;
val = 4; // number of lost keepalives before we close the socket
if (setsockopt(socket, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof(val)) != ESP_OK) return false;
return true;
}
};

View File

@@ -3,6 +3,7 @@
#include <lwip/sockets.h>
#include "GwBuffer.h"
#include "GwSocketConnection.h"
#include "GwSocketHelper.h"
GwSocketServer::GwSocketServer(const GwConfigHandler *config, GwLog *logger, int minId)
{
@@ -62,11 +63,13 @@ int GwSocketServer::available()
if (client_sock >= 0)
{
int val = 1;
if (setsockopt(client_sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&val, sizeof(int)) == ESP_OK)
if (! GwSocketHelper::setKeepAlive(client_sock,true)){
LOG_DEBUG(GwLog::ERROR,"unable to set keepalive, nodelay on socket");
}
else
{
if (setsockopt(client_sock, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(int)) == ESP_OK)
fcntl(client_sock, F_SETFL, O_NONBLOCK);
return client_sock;
fcntl(client_sock, F_SETFL, O_NONBLOCK);
return client_sock;
}
close(client_sock);
}

View File

@@ -1,6 +1,7 @@
#include "GwTcpClient.h"
#include <functional>
#include <ESPmDNS.h>
#include "GwSocketHelper.h"
class ResolveArgs{
public:
@@ -72,6 +73,12 @@ void GwTcpClient::startConnection()
LOG_DEBUG(GwLog::ERROR,"unable to create socket: %d", errno);
return;
}
if (! GwSocketHelper::setKeepAlive(sockfd,true)){
error="unable to set keepalive, nodelay on socket";
LOG_DEBUG(GwLog::ERROR,"%s",error.c_str());
close(sockfd);
return;
}
fcntl( sockfd, F_SETFL, fcntl( sockfd, F_GETFL, 0 ) | O_NONBLOCK );
int res = lwip_connect_r(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
if (res < 0 ) {