Skip to content

Commit

Permalink
Possibility to check current event count in epoll array
Browse files Browse the repository at this point in the history
  • Loading branch information
kala13x committed Nov 26, 2024
1 parent 11e4484 commit f4754b8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
26 changes: 14 additions & 12 deletions examples/async-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,24 @@ int handle_read(xapi_ctx_t *pCtx, xapi_data_t *pData)
xbyte_buffer_t *pBuffer = XAPI_GetRxBuff(pData);

xlogn("Received response: fd(%d), buff(%zu): %s",
(int)pData->sock.nFD, pBuffer->nUsed, (char*)pBuffer->pData);
(int)pData->sock.nFD, pBuffer->nUsed,
(const char*)pBuffer->pData);

return XAPI_DISCONNECT;
}

int handle_write(xapi_ctx_t *pCtx, xapi_data_t *pData)
{
xbyte_buffer_t *pBuffer = XAPI_GetTxBuff(pData);

XByteBuffer_AddFmt(pBuffer, "My simple request");
return XAPI_EnableEvent(pData, XPOLLOUT);
}

return XAPI_EnableEvent(pData, XPOLLOUT | XPOLLIN);
int send_complete(xapi_ctx_t *pCtx, xapi_data_t *pData)
{
xlogn("Request sent: fd(%d)", (int)pData->sock.nFD);
XAPI_DisableEvent(pData, XPOLLOUT);
return XAPI_EnableEvent(pData, XPOLLIN);
}

int init_data(xapi_ctx_t *pCtx, xapi_data_t *pData)
Expand All @@ -82,12 +88,11 @@ int service_callback(xapi_ctx_t *pCtx, xapi_data_t *pData)
return handle_write(pCtx, pData);
case XAPI_CB_CONNECTED:
return init_data(pCtx, pData);
case XAPI_CB_COMPLETE:
return send_complete(pCtx, pData);
case XAPI_CB_CLOSED:
xlogn("Connection closed: fd(%d)", (int)pData->sock.nFD);
return XAPI_DISCONNECT;
case XAPI_CB_COMPLETE:
xlogn("Request sent: fd(%d)", (int)pData->sock.nFD);
break;
case XAPI_CB_INTERRUPT:
if (g_nInterrupted) return XAPI_DISCONNECT;
break;
Expand Down Expand Up @@ -192,17 +197,14 @@ int main(int argc, char* argv[])
return XSTDERR;
}

xevent_status_t status;
do
while (!g_nInterrupted)
{
status = XAPI_Service(&api, 100);
xevent_status_t status = XAPI_Service(&api, 100);
if (status != XEVENT_STATUS_SUCCESS) break;

// Manualy break if no more events
xevents_t *pEvents = &api.events;
if (!pEvents->nEventCount) break;
if (!XAPI_GetEventCount(&api)) break;
}
while (status == XEVENT_STATUS_SUCCESS);

XAPI_Destroy(&api);
return 0;
Expand Down
2 changes: 1 addition & 1 deletion examples/async-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ int service_callback(xapi_ctx_t *pCtx, xapi_data_t *pData)
break;
case XAPI_CB_COMPLETE:
xlogn("Response sent: fd(%d)", (int)pData->sock.nFD);
return XAPI_DISCONNECT;
break;
case XAPI_CB_INTERRUPT:
if (g_nInterrupted) return XAPI_DISCONNECT;
break;
Expand Down
7 changes: 7 additions & 0 deletions src/net/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ static int XAPI_ClearEvent(xapi_t *pApi, xevent_data_t *pEvData)
return XAPI_StatusToEvent(pApi, nStatus);
}

size_t XAPI_GetEventCount(xapi_t *pApi)
{
XASSERT_RET((pApi != NULL), XSTDNON);
xevents_t *pEvents = &pApi->events;
return pEvents->nEventCount;
}

XSTATUS XAPI_SetEvents(xapi_data_t *pData, int nEvents)
{
XASSERT((pData && pData->pEvData), XSTDERR);
Expand Down
1 change: 1 addition & 0 deletions src/net/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ void XAPI_Destroy(xapi_t *pApi);
XSTATUS XAPI_DisableEvent(xapi_data_t *pData, int nEvent);
XSTATUS XAPI_EnableEvent(xapi_data_t *pData, int nEvent);
XSTATUS XAPI_SetEvents(xapi_data_t *pData, int nEvents);
size_t XAPI_GetEventCount(xapi_t *pApi);

XSTATUS XAPI_RespondHTTP(xapi_data_t *pApiData, int nCode, xapi_status_t eStatus);
XSTATUS XAPI_AuthorizeHTTP(xapi_data_t *pApiData, const char *pToken, const char *pKey);
Expand Down

0 comments on commit f4754b8

Please sign in to comment.