aqhome: started rewriting message code, start using new event2 lib.
This commit is contained in:
149
aqhome/ipc2/ipcmsgreader.c
Normal file
149
aqhome/ipc2/ipcmsgreader.c
Normal file
@@ -0,0 +1,149 @@
|
||||
/****************************************************************************
|
||||
* 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 "./ipcmsgreader.h"
|
||||
#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_HEADER_SIZE 4
|
||||
#define AQH_MSG_READER_MINMSGSIZE 12
|
||||
#define AQH_MSG_READER_MAXMSGSIZE 10240
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* forward declarations
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
static int _readMsg(AQH_OBJECT *o);
|
||||
static int _readHeaderFromRingbuffer(AQH_MSG_READER *xo);
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* implementation
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
AQH_OBJECT *AQH_IpcMsgReader_new(AQH_EVENT_LOOP *eventLoop, AQH_OBJECT *fdObject)
|
||||
{
|
||||
AQH_OBJECT *o;
|
||||
|
||||
o=AQH_MsgReader_new(eventLoop, fdObject);
|
||||
AQH_MsgReader_SetReadMsgFn(o, _readMsg);
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _readMsg(AQH_OBJECT *o)
|
||||
{
|
||||
AQH_MSG_READER *xo;
|
||||
|
||||
xo=AQH_MsgReader_GetData(o);
|
||||
if (xo) {
|
||||
int rv;
|
||||
|
||||
if (xo->bytesReceived<AQH_MSG_READER_HEADER_SIZE) {
|
||||
rv=_readHeaderFromRingbuffer(xo);
|
||||
if (rv<0) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
if (xo->bytesReceived>=AQH_MSG_READER_HEADER_SIZE) {
|
||||
/* reading remainder of msg directly into allocated buffer */
|
||||
rv=AQH_MsgReader_ReadRemainderFromRingbuffer(o);
|
||||
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;
|
||||
}
|
||||
return GWEN_ERROR_GENERIC;
|
||||
}
|
||||
|
||||
|
||||
|
||||
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_HEADER_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, "Ringbuffer empty");
|
||||
return 0;
|
||||
}
|
||||
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_HEADER_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); /* +4 because of msg len (4 bytes) */
|
||||
memmove(xo->currentMsgBuf, xo->headerBuffer, xo->bytesReceived);
|
||||
xo->bytesLeft=(msgLen+4)-xo->bytesReceived;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user