aqhome: More work on endpoint system. Basically works.

This commit is contained in:
Martin Preuss
2023-02-22 17:54:17 +01:00
parent da1ea9c268
commit 7a1968e962
12 changed files with 242 additions and 61 deletions

View File

@@ -53,7 +53,7 @@ static int _setSocketNonBlocking(int fd);
AQH_MSG_ENDPOINT *AQH_MsgEndpoint_new(int fd, int groupId)
AQH_MSG_ENDPOINT *AQH_MsgEndpoint_new(int fd, int groupId, const char *name)
{
AQH_MSG_ENDPOINT *ep;
@@ -64,6 +64,7 @@ AQH_MSG_ENDPOINT *AQH_MsgEndpoint_new(int fd, int groupId)
ep->groupId=groupId;
ep->receivedMessageList=AQH_Msg_List_new();
ep->sendMessageList=AQH_Msg_List_new();
ep->name=name?strdup(name):"<unnamed>";
return ep;
}
@@ -99,6 +100,14 @@ void AQH_MsgEndpoint_SetFd(AQH_MSG_ENDPOINT *ep, int fd)
}
const char *AQH_MsgEndpoint_GetName(const AQH_MSG_ENDPOINT *ep)
{
return ep->name;
}
uint32_t AQH_MsgEndpoint_GetGroupId(const AQH_MSG_ENDPOINT *ep)
{
return ep->groupId;
@@ -424,12 +433,14 @@ int AQH_MsgEndpoint_DiscardInput(AQH_MSG_ENDPOINT *ep)
rv=read(ep->fd, buffer, sizeof(buffer));
} while( (rv>0 || (rv<0) && errno==EINTR));
if (rv<0 && errno!=EAGAIN && errno!=EWOULDBLOCK) {
DBG_ERROR(NULL, "Error on read(): %s (%d)", strerror(errno), errno);
DBG_ERROR(AQH_LOGDOMAIN, "Error on read(): %s (%d)", strerror(errno), errno);
return GWEN_ERROR_IO;
}
else if (rv==0) {
DBG_ERROR(NULL, "EOF met on read()");
DBG_ERROR(AQH_LOGDOMAIN, "EOF met on read()");
#if 0
return GWEN_ERROR_IO;
#endif
}
return 0;
}
@@ -443,17 +454,18 @@ int _internalHandleReadable(AQH_MSG_ENDPOINT *ep)
int len;
int i;
DBG_INFO(AQH_LOGDOMAIN, "Reading from endpoint %s", AQH_MsgEndpoint_GetName(ep));
do {
rv=read(ep->fd, buffer, sizeof(buffer));
} while( (rv<0) && errno==EINTR);
if (rv<0) {
if (errno==EAGAIN || errno==EWOULDBLOCK)
return GWEN_ERROR_TRY_AGAIN;
DBG_ERROR(NULL, "Error on read(): %s (%d)", strerror(errno), errno);
DBG_ERROR(AQH_LOGDOMAIN, "Error on read(): %s (%d)", strerror(errno), errno);
return GWEN_ERROR_IO;
}
else if (rv==0) {
DBG_ERROR(NULL, "EOF met on read()");
DBG_ERROR(AQH_LOGDOMAIN, "EOF met on read()");
return GWEN_ERROR_IO;
}
len=rv;
@@ -463,30 +475,30 @@ int _internalHandleReadable(AQH_MSG_ENDPOINT *ep)
ep->currentlyReceivedMsg=AQH_Msg_new();
rv=AQH_Msg_AddByte(ep->currentlyReceivedMsg, buffer[i]);
if (rv<0) {
DBG_ERROR(NULL, "here (%d)", rv);
DBG_ERROR(AQH_LOGDOMAIN, "here (%d)", rv);
return rv;
}
rv=AQH_Msg_IsMsgComplete(ep->currentlyReceivedMsg);
if (rv<0) {
/* invalid message */
DBG_ERROR(NULL, "Invalid message, discarding");
DBG_ERROR(AQH_LOGDOMAIN, "Invalid message, discarding");
AQH_Msg_free(ep->currentlyReceivedMsg);
ep->currentlyReceivedMsg=NULL;
rv=AQH_MsgEndpoint_DiscardInput(ep);
if (rv<0) {
DBG_ERROR(NULL, "here (%d)", rv);
DBG_ERROR(AQH_LOGDOMAIN, "here (%d)", rv);
return rv;
}
}
else if (rv>0) {
if (!AQH_Msg_IsChecksumValid(ep->currentlyReceivedMsg)) {
DBG_ERROR(NULL, "Invalid checksum, discarding message");
DBG_ERROR(AQH_LOGDOMAIN, "Invalid checksum, discarding message");
GWEN_Text_DumpString(AQH_Msg_GetBuffer(ep->currentlyReceivedMsg), AQH_Msg_GetBytesInBuffer(ep->currentlyReceivedMsg), 6);
AQH_Msg_free(ep->currentlyReceivedMsg);
ep->currentlyReceivedMsg=NULL;
rv=AQH_MsgEndpoint_DiscardInput(ep);
if (rv<0) {
DBG_ERROR(NULL, "here (%d)", rv);
DBG_ERROR(AQH_LOGDOMAIN, "here (%d)", rv);
return rv;
}
}
@@ -507,6 +519,7 @@ int _internalHandleWritable(AQH_MSG_ENDPOINT *ep, AQH_MSG_ENDPOINT_MGR *emgr)
{
AQH_MSG *msg;
DBG_INFO(AQH_LOGDOMAIN, "Writing to endpoint %s", AQH_MsgEndpoint_GetName(ep));
msg=AQH_Msg_List_First(ep->sendMessageList);
if (msg) {
uint8_t pos;
@@ -516,7 +529,7 @@ int _internalHandleWritable(AQH_MSG_ENDPOINT *ep, AQH_MSG_ENDPOINT_MGR *emgr)
rv=AQH_MsgEndpoint_CheckMsg(ep);
if (rv<0 || rv==1) {
DBG_ERROR(NULL, "Line busy, not sending");
DBG_ERROR(AQH_LOGDOMAIN, "Line busy, not sending");
usleep(100);
return 0;
}
@@ -527,7 +540,7 @@ int _internalHandleWritable(AQH_MSG_ENDPOINT *ep, AQH_MSG_ENDPOINT_MGR *emgr)
rv=AQH_MsgEndpoint_StartMsg(ep);
if (rv<0) {
DBG_ERROR(NULL, "here (%d)", rv);
DBG_ERROR(AQH_LOGDOMAIN, "here (%d)", rv);
return rv;
}
@@ -538,7 +551,7 @@ int _internalHandleWritable(AQH_MSG_ENDPOINT *ep, AQH_MSG_ENDPOINT_MGR *emgr)
if (rv<0) {
if (errno==EAGAIN || errno==EWOULDBLOCK)
return GWEN_ERROR_TRY_AGAIN;
DBG_ERROR(NULL, "Error on write(): %s (%d)", strerror(errno), errno);
DBG_ERROR(AQH_LOGDOMAIN, "Error on write(): %s (%d)", strerror(errno), errno);
return GWEN_ERROR_IO;
}
AQH_Msg_IncCurrentPos(msg, rv);
@@ -548,7 +561,7 @@ int _internalHandleWritable(AQH_MSG_ENDPOINT *ep, AQH_MSG_ENDPOINT_MGR *emgr)
AQH_Msg_List_Del(msg);
AQH_Msg_free(msg);
if (rv<0) {
DBG_ERROR(NULL, "here (%d)", rv);
DBG_ERROR(AQH_LOGDOMAIN, "here (%d)", rv);
return rv;
}
}