use GwBuffer::fillData for udp receive

This commit is contained in:
andreas 2024-11-11 20:28:57 +01:00
parent b4eaad4dbf
commit 098b9ba558
3 changed files with 12 additions and 22 deletions

View File

@ -33,16 +33,6 @@ size_t GwBuffer::freeSpace()
}
return readPointer - writePointer - 1;
}
size_t GwBuffer::continousSpace() const{
if (readPointer <= writePointer){
return bufferSize-offset(writePointer);
}
return readPointer-writePointer-1;
}
void GwBuffer::moveWp(size_t offset){
if (offset > continousSpace()) return;
writePointer+=offset;
}
size_t GwBuffer::usedSpace()
{
if (readPointer <= writePointer)

View File

@ -18,9 +18,9 @@ class GwMessageFetcher{
* buffer to safely inserte data if it fits
* and to write out data if possible
*/
typedef size_t (*GwBufferHandleFunction)(uint8_t *buffer, size_t len, void *param);
class GwBuffer{
public:
using GwBufferHandleFunction=std::function<size_t(uint8_t *buffer, size_t len, void *param)>;
static const size_t TX_BUFFER_SIZE=1620; // app. 20 NMEA messages
static const size_t RX_BUFFER_SIZE=600; // enough for 1 NMEA message or actisense message or seasmart message
typedef enum {
@ -54,9 +54,6 @@ class GwBuffer{
* find the first occurance of x in the buffer, -1 if not found
*/
int findChar(char x);
uint8_t *getWp(){return writePointer;}
size_t continousSpace() const; //free space from wp
void moveWp(size_t offset); //move the wp forward
};
#endif

View File

@ -142,15 +142,18 @@ void GwUdpReader::readMessages(GwMessageFetcher *writer)
if (fd < 0) return;
//we expect one NMEA message in one UDP packet
buffer->reset();
struct sockaddr_in from;
socklen_t fromLen=sizeof(from);
ssize_t res=recvfrom(fd,buffer->getWp(),buffer->continousSpace(),MSG_DONTWAIT,
size_t rd=buffer->fillData(buffer->freeSpace(),
[this](uint8_t *rcvb,size_t rcvlen,void *param)->size_t{
struct sockaddr_in from;
socklen_t fromLen=sizeof(from);
ssize_t res=recvfrom(fd,rcvb,rcvlen,MSG_DONTWAIT,
(struct sockaddr*)&from,&fromLen);
if (res <= 0) return;
if (GwSocketHelper::equals(from.sin_addr,apAddr)) return;
if (!currentStationIp.isEmpty() && (GwSocketHelper::equals(from.sin_addr,staAddr))) return;
buffer->moveWp(res);
LOG_DEBUG(GwLog::DEBUG,"UDPR: received %d bytes",res);
if (res <= 0) return 0;
if (GwSocketHelper::equals(from.sin_addr,apAddr)) return 0;
if (!currentStationIp.isEmpty() && (GwSocketHelper::equals(from.sin_addr,staAddr))) return 0;
return res;
},this);
if (buffer->usedSpace() > 0)(GwLog::DEBUG,"UDPR: received %d bytes",buffer->usedSpace());
writer->handleBuffer(buffer);
}
size_t GwUdpReader::sendToClients(const char *buf, int source,bool partial)