315 lines
7.7 KiB
C
315 lines
7.7 KiB
C
/****************************************************************************
|
|
* This file is part of the project AqHome.
|
|
* AqHome (c) by 2025 Martin Preuss, all rights reserved.
|
|
*
|
|
* The license for this file can be found in the file COPYING which you
|
|
* should have received along with this file.
|
|
****************************************************************************/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
# include <config.h>
|
|
#endif
|
|
|
|
#include "./msgreader_p.h"
|
|
#include <aqhome/events2/fdobject.h>
|
|
|
|
#include <gwenhywfar/inherit.h>
|
|
#include <gwenhywfar/debug.h>
|
|
#include <gwenhywfar/endianfns.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
#define AQH_MSG_READER_MINMSGSIZE 12
|
|
#define AQH_MSG_READER_MAXMSGSIZE 10240
|
|
|
|
enum {
|
|
AQH_MSGREADER_SLOT_SOCKETREADY=1
|
|
};
|
|
|
|
|
|
|
|
GWEN_INHERIT(AQH_OBJECT, AQH_MSG_READER)
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
* forward declarations
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
static void GWENHYWFAR_CB _freeData(void *bp, void *p);
|
|
|
|
static int _handleSignal(AQH_OBJECT *o, uint32_t slotId, AQH_OBJECT *senderObject, int param1, void *param2);
|
|
static int _handleSocketReady(AQH_OBJECT *o);
|
|
static int _fillRingbuffer(AQH_OBJECT *o);
|
|
static int _readMsgFromRingbuffer(AQH_OBJECT *o, AQH_MSG_READER *xo);
|
|
static int _readHeaderFromRingbuffer(AQH_MSG_READER *xo);
|
|
static int _readRemainderFromRingbuffer(AQH_MSG_READER *xo);
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
* implementation
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
AQH_OBJECT *AQH_MsgReader_new(AQH_EVENT_LOOP *eventLoop, int fd)
|
|
{
|
|
AQH_OBJECT *o;
|
|
AQH_MSG_READER *xo;
|
|
|
|
o=AQH_Object_new(eventLoop);
|
|
GWEN_NEW_OBJECT(AQH_MSG_READER, xo);
|
|
GWEN_INHERIT_SETDATA(AQH_OBJECT, AQH_MSG_READER, o, xo, _freeData);
|
|
xo->fdSocket=fd;
|
|
xo->ringBuffer=GWEN_RingBuffer_new(AQH_MSG_READER_RINGBUFFER_SIZE);
|
|
|
|
AQH_Object_SetSignalHandlerFn(o, _handleSignal);
|
|
|
|
/* create object for readable socket, connect to THIS, enable */
|
|
xo->fdObject=AQH_FdObject_new(AQH_Object_GetEventLoop(o), fd, AQH_FDOBJECT_FDMODE_READ);
|
|
AQH_Object_AddLink(xo->fdObject, AQH_FDOBJECT_SIGNAL_ISREADY, AQH_MSGREADER_SLOT_SOCKETREADY, o);
|
|
AQH_Object_Enable(xo->fdObject);
|
|
|
|
return o;
|
|
}
|
|
|
|
|
|
|
|
void GWENHYWFAR_CB _freeData(GWEN_UNUSED void *bp, void *p)
|
|
{
|
|
AQH_MSG_READER *xo;
|
|
|
|
xo=(AQH_MSG_READER*) p;
|
|
if (xo->fdObject) {
|
|
AQH_Object_Disable(xo->fdObject);
|
|
AQH_Object_free(xo->fdObject);
|
|
}
|
|
GWEN_FREE_OBJECT(xo);
|
|
}
|
|
|
|
|
|
|
|
int _handleSignal(AQH_OBJECT *o,
|
|
uint32_t slotId,
|
|
GWEN_UNUSED AQH_OBJECT *senderObject,
|
|
GWEN_UNUSED int param1,
|
|
GWEN_UNUSED void *param2)
|
|
{
|
|
switch(slotId) {
|
|
case AQH_MSGREADER_SLOT_SOCKETREADY: return _handleSocketReady(o);
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return 0; /* not handled */
|
|
}
|
|
|
|
|
|
|
|
int _handleSocketReady(AQH_OBJECT *o)
|
|
{
|
|
AQH_MSG_READER *xo;
|
|
|
|
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_MSG_READER, o);
|
|
if (xo) {
|
|
int rv;
|
|
|
|
/* read available data into ringbuffer */
|
|
rv=_fillRingbuffer(o);
|
|
if (rv<0) {
|
|
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
|
|
AQH_Object_EmitSignal(o, AQH_MSG_READER_SIGNAL_ERROR, rv, NULL);
|
|
xo->fdSocket=-1;
|
|
return 1;
|
|
}
|
|
|
|
/* read messages from ring buffer until buffer empty */
|
|
do {
|
|
rv=_readMsgFromRingbuffer(o, xo);
|
|
} while (rv==1);
|
|
if (rv<0) {
|
|
AQH_Object_EmitSignal(o, AQH_MSG_READER_SIGNAL_ERROR, rv, NULL);
|
|
xo->fdSocket=-1;
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
int _fillRingbuffer(AQH_OBJECT *o)
|
|
{
|
|
AQH_MSG_READER *xo;
|
|
|
|
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_MSG_READER, o);
|
|
if (xo && xo->fdSocket!=-1) {
|
|
uint32_t len;
|
|
|
|
/* read data into ringbuffer */
|
|
len=GWEN_RingBuffer_GetMaxUnsegmentedWrite(xo->ringBuffer);
|
|
if (len>0) {
|
|
int rv;
|
|
|
|
rv=AQH_FdObject_Read(xo->fdObject, (uint8_t*) GWEN_RingBuffer_GetWritePointer(xo->ringBuffer), len);
|
|
if (rv<0) {
|
|
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
|
|
xo->fdSocket=-1;
|
|
return rv;
|
|
}
|
|
else if (rv==0) {
|
|
DBG_INFO(AQH_LOGDOMAIN, "EOF met");
|
|
xo->fdSocket=-1;
|
|
return 0;
|
|
}
|
|
else {
|
|
/* bytes received */
|
|
GWEN_RingBuffer_SkipBytesWrite(xo->ringBuffer, rv);
|
|
return rv;
|
|
}
|
|
}
|
|
else {
|
|
DBG_INFO(AQH_LOGDOMAIN, "Ringbuffer full");
|
|
return GWEN_ERROR_BUFFER_OVERFLOW;
|
|
}
|
|
}
|
|
else {
|
|
DBG_INFO(AQH_LOGDOMAIN, "fd inactive (previous error or EOF?)");
|
|
return GWEN_ERROR_INVALID;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int _readMsgFromRingbuffer(AQH_OBJECT *o, AQH_MSG_READER *xo)
|
|
{
|
|
int rv;
|
|
|
|
if (xo->bytesReceived<AQH_MSG_READER_HEADERBUFFER_SIZE) {
|
|
rv=_readHeaderFromRingbuffer(xo);
|
|
if (rv<0) {
|
|
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
|
|
return rv;
|
|
}
|
|
}
|
|
|
|
if (xo->bytesReceived>=AQH_MSG_READER_HEADERBUFFER_SIZE) {
|
|
/* reading remainder of msg directly into allocated buffer */
|
|
rv=_readRemainderFromRingbuffer(xo);
|
|
if (rv<0) {
|
|
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
|
|
return rv;
|
|
}
|
|
else if (rv==1) {
|
|
int msgLen;
|
|
uint8_t *msgPtr;
|
|
|
|
msgLen=xo->bytesReceived;
|
|
msgPtr=xo->currentMsgBuf;
|
|
|
|
xo->bytesReceived=0;
|
|
xo->bytesLeft=0;
|
|
xo->currentMsgBuf=NULL;
|
|
|
|
rv=AQH_Object_EmitSignal(o, AQH_MSG_READER_SIGNAL_MSGRECVD, msgLen, (void*) msgPtr);
|
|
if (rv==0) {
|
|
DBG_INFO(AQH_LOGDOMAIN, "Received message ignored");
|
|
free(msgPtr);
|
|
}
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
int _readHeaderFromRingbuffer(AQH_MSG_READER *xo)
|
|
{
|
|
uint32_t remaining;
|
|
int rv;
|
|
uint32_t xferSize;
|
|
uint32_t bytesInBuffer;
|
|
|
|
bytesInBuffer=GWEN_RingBuffer_GetUsedBytes(xo->ringBuffer);
|
|
|
|
/* still reading header */
|
|
remaining=AQH_MSG_READER_HEADERBUFFER_SIZE-xo->bytesReceived;
|
|
if (bytesInBuffer<remaining)
|
|
remaining=bytesInBuffer;
|
|
xferSize=remaining;
|
|
rv=GWEN_RingBuffer_ReadBytes(xo->ringBuffer, (char*) (xo->headerBuffer+xo->bytesReceived), &xferSize);
|
|
if (rv<0) {
|
|
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
|
|
return rv;
|
|
}
|
|
if (xferSize<remaining) {
|
|
DBG_ERROR(AQH_LOGDOMAIN, "Read fewer bytes than available?");
|
|
return GWEN_ERROR_GENERIC;
|
|
}
|
|
xo->bytesReceived+=xferSize;
|
|
if (xo->bytesReceived==AQH_MSG_READER_HEADERBUFFER_SIZE) {
|
|
uint32_t msgLen;
|
|
|
|
/* full size received, parse msg size, allocate buffer */
|
|
msgLen=GWEN_ENDIAN_LE32TOH(*((const uint32_t*)(xo->headerBuffer)));
|
|
if (msgLen<AQH_MSG_READER_MINMSGSIZE || msgLen>AQH_MSG_READER_MAXMSGSIZE) {
|
|
DBG_ERROR(AQH_LOGDOMAIN, "Bad message size(%lu)", (unsigned long int) msgLen);
|
|
return GWEN_ERROR_GENERIC;
|
|
}
|
|
xo->currentMsgBuf=(uint8_t*) malloc(msgLen+4);
|
|
memmove(xo->currentMsgBuf, xo->headerBuffer, xo->bytesReceived);
|
|
xo->bytesLeft=(msgLen+4)-xo->bytesReceived;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
int _readRemainderFromRingbuffer(AQH_MSG_READER *xo)
|
|
{
|
|
uint32_t bytesInBuffer;
|
|
uint32_t bytesToRead;
|
|
int rv;
|
|
|
|
bytesInBuffer=GWEN_RingBuffer_GetUsedBytes(xo->ringBuffer);
|
|
|
|
/* still reading header */
|
|
bytesToRead=xo->bytesLeft;
|
|
if (bytesInBuffer<bytesToRead)
|
|
bytesToRead=bytesInBuffer;
|
|
if (bytesToRead) {
|
|
uint32_t xferSize;
|
|
|
|
xferSize=bytesToRead;
|
|
rv=GWEN_RingBuffer_ReadBytes(xo->ringBuffer, (char*) (xo->currentMsgBuf+xo->bytesReceived), &xferSize);
|
|
if (rv<0) {
|
|
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
|
|
return rv;
|
|
}
|
|
if (xferSize<bytesToRead) {
|
|
DBG_ERROR(AQH_LOGDOMAIN, "Read fewer bytes than available?");
|
|
return GWEN_ERROR_GENERIC;
|
|
}
|
|
xo->bytesReceived+=xferSize;
|
|
xo->bytesLeft-=xferSize;
|
|
if (xo->bytesLeft==0) {
|
|
/* msg finished */
|
|
DBG_INFO(AQH_LOGDOMAIN, "Message complete");
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|