-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
andreas
committed
Nov 4, 2024
1 parent
d0dee36
commit 490a5b9
Showing
6 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include "GwUdpWriter.h" | ||
#include <ESPmDNS.h> | ||
#include <errno.h> | ||
#include "GwBuffer.h" | ||
#include "GwSocketConnection.h" | ||
#include "GwSocketHelper.h" | ||
|
||
GwUdpWriter::GwUdpWriter(const GwConfigHandler *config, GwLog *logger, int minId) | ||
{ | ||
this->config = config; | ||
this->logger = logger; | ||
this->minId = minId; | ||
} | ||
|
||
void GwUdpWriter::begin() | ||
{ | ||
fd=socket(AF_INET,SOCK_DGRAM,IPPROTO_IP); | ||
if (fd < 0){ | ||
LOG_ERROR("unable to create udp socket"); | ||
return; | ||
} | ||
int enable = 1; | ||
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)); | ||
setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &enable, sizeof(int)); | ||
port=config->getInt(GwConfigDefinitions::udpwPort); | ||
//TODO: check port | ||
address=config->getString(GwConfigDefinitions::udpwAddress); | ||
LOG_INFO("UDP writer created, address=%s, port=%d", | ||
address.c_str(),port); | ||
inet_pton(AF_INET, address.c_str(), &destination.sin_addr); | ||
destination.sin_family = AF_INET; | ||
destination.sin_port = htons(port); | ||
} | ||
|
||
void GwUdpWriter::loop(bool handleRead, bool handleWrite) | ||
{ | ||
|
||
} | ||
|
||
void GwUdpWriter::readMessages(GwMessageFetcher *writer) | ||
{ | ||
|
||
} | ||
size_t GwUdpWriter::sendToClients(const char *buf, int source,bool partial) | ||
{ | ||
if (source == minId) return 0; | ||
size_t len=strlen(buf); | ||
ssize_t err = sendto(fd,buf,len,0,(struct sockaddr *)&destination, sizeof(destination)); | ||
if (err < 0){ | ||
LOG_DEBUG(GwLog::DEBUG,"UDP writer error sending: %d",errno); | ||
return 0; | ||
} | ||
return err; | ||
} | ||
|
||
|
||
GwUdpWriter::~GwUdpWriter() | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#ifndef _GWUDPWRITER_H | ||
#define _GWUDPWRITER_H | ||
#include "GWConfig.h" | ||
#include "GwLog.h" | ||
#include "GwBuffer.h" | ||
#include "GwChannelInterface.h" | ||
#include <memory> | ||
#include <sys/socket.h> | ||
#include <arpa/inet.h> | ||
|
||
class GwUdpWriter: public GwChannelInterface{ | ||
private: | ||
const GwConfigHandler *config; | ||
GwLog *logger; | ||
int fd=-1; | ||
int minId; | ||
int port; | ||
String address; | ||
struct sockaddr_in destination; | ||
public: | ||
GwUdpWriter(const GwConfigHandler *config,GwLog *logger,int minId); | ||
~GwUdpWriter(); | ||
void begin(); | ||
virtual void loop(bool handleRead=true,bool handleWrite=true); | ||
virtual size_t sendToClients(const char *buf,int sourceId, bool partialWrite=false); | ||
virtual void readMessages(GwMessageFetcher *writer); | ||
}; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters