1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2026-01-03 17:13:16 +00:00

1. Updated Logging capability

2. Updated Windows unwindhandler() to delete HANDLE when signaled
This commit is contained in:
Bryan Roe
2021-11-22 11:44:27 -08:00
parent 25cdbfbda4
commit 0420400920
5 changed files with 98 additions and 10 deletions

View File

@@ -134,6 +134,7 @@ void* gILibChain = NULL; // Global Chain Instance used for Remote Logging when
ILibSemaphoreTrack_Handler ILibSemaphoreTrack_func = NULL;
void* ILibSemaphoreTrack_user = NULL;
uint32_t _g_logMessageX = 0;
#define ILibChain_SIGMAX 32
ILibLinkedList g_signalHandlers[ILibChain_SIGMAX] = { NULL };
@@ -2192,8 +2193,13 @@ int ILibChain_WindowsSelect(void *chain, fd_set *readset, fd_set *writeset, fd_s
// FALSE means to remove tha HANDLE
if (((ILibBaseChain*)chain)->currentHandle != NULL && ILibMemory_CanaryOK(info))
{
ILIBLOGMESSAGEX2(LOGEX_OVERLAPPED_IO, " * ILibChain_WindowsSelect(%p) [REMOVED]", ((ILibBaseChain*)chain)->currentHandle);
ILibLinkedList_Remove(info->node);
}
else
{
ILIBLOGMESSAGEX2(LOGEX_OVERLAPPED_IO, " * ILibChain_WindowsSelect(%p) [???] => %p, ", ((ILibBaseChain*)chain)->currentHandle);
}
}
}
((ILibBaseChain*)chain)->currentHandle = NULL;
@@ -3548,14 +3554,17 @@ BOOL ILibChain_ReadEx_Sink(void *chain, HANDLE h, ILibWaitHandle_ErrorStatus sta
DWORD bytesRead = 0;
DWORD err;
if (status == ILibWaitHandle_ErrorStatus_REMOVED)
{
ILIBLOGMESSAGEX2(LOGEX_OVERLAPPED_IO, "ILibChain_ReadEx_Sink(%p) [REMOVED]", h);
ILibMemory_Free(data);
return(FALSE);
}
if (GetOverlappedResult(data->fileHandle, data->p, &bytesRead, FALSE) && bytesRead > 0)
{
ILIBLOGMESSAGEX2(LOGEX_OVERLAPPED_IO, "ILibChain_ReadEx_Sink(%p) [bytesRead: %lu]", h, bytesRead);
if (data->handler != NULL) { data->handler(chain, data->fileHandle, ILibWaitHandle_ErrorStatus_NONE, data->buffer, bytesRead, data->user); }
ILibMemory_Free(data);
return(FALSE);
@@ -3564,12 +3573,14 @@ BOOL ILibChain_ReadEx_Sink(void *chain, HANDLE h, ILibWaitHandle_ErrorStatus sta
{
if ((err=GetLastError()) == ERROR_IO_PENDING)
{
ILIBLOGMESSAGEX2(LOGEX_OVERLAPPED_IO, "ILibChain_ReadEx_Sink(%p) [PENDING]", h);
// Still pending, so wait for another callback
return(TRUE);
}
else
{
// ERROR
ILIBLOGMESSAGEX2(LOGEX_OVERLAPPED_IO, "ILibChain_ReadEx_Sink(%p) [ERROR]", h);
if (data->handler != NULL) { data->handler(chain, data->fileHandle, ILibWaitHandle_ErrorStatus_IO_ERROR, data->buffer, 0, data->user); }
ILibMemory_Free(data);
return(FALSE);
@@ -3614,8 +3625,25 @@ ILibTransport_DoneState ILibChain_WriteEx2(void *chain, HANDLE h, OVERLAPPED *p,
void ILibChain_ReadEx2_UnwindHandler(void *chain, void *user)
{
ILibChain_ReadEx_data *state = (ILibChain_ReadEx_data*)user;
state->handler(chain, state->fileHandle, ILibWaitHandle_ErrorStatus_NONE, state->buffer, (int)(uintptr_t)state->p, state->user);
ILIBLOGMESSAGEX2(LOGEX_OVERLAPPED_IO, " + ILibChain_ReadEx2_UnwindHandler(%p)", state->reserved);
if (state->handler(chain, state->fileHandle, ILibWaitHandle_ErrorStatus_NONE, state->buffer, (int)(uintptr_t)state->p, state->user) == FALSE)
{
ILIBLOGMESSAGEX2(LOGEX_OVERLAPPED_IO, " -> ILibChain_ReadEx2_UnwindHandler() [Try Remove: %p]", state->reserved);
// Need to remove HANDLE from list
void *node = ILibLinkedList_GetNode_Search(((ILibBaseChain*)chain)->auxSelectHandles, NULL, state->reserved);
if (node != NULL)
{
ILIBLOGMESSAGEX2(LOGEX_OVERLAPPED_IO, " -> Removing Handle [%p]", state->reserved);
ILibChain_WaitHandleInfo *info = (ILibChain_WaitHandleInfo*)ILibMemory_Extra(node);
ILibChain_ReadEx_data *data = (ILibChain_ReadEx_data*)info->user;
ILibMemory_Free(data);
ILibLinkedList_Remove(node);
}
}
free(state);
ILIBLOGMESSAGEX2(LOGEX_OVERLAPPED_IO, " -> ILibChain_ReadEx2_UnwindHandler() [END]");
}
void ILibChain_ReadEx2(void *chain, HANDLE h, OVERLAPPED *p, char *buffer, DWORD bufferLen, ILibChain_ReadEx_Handler handler, void *user, char *metadata)
{
@@ -3654,7 +3682,7 @@ void ILibChain_ReadEx2(void *chain, HANDLE h, OVERLAPPED *p, char *buffer, DWORD
state->fileHandle = h;
state->user = user;
state->p = (void*)(uintptr_t)bytesRead;
state->reserved = p != NULL ? p->hEvent : NULL;
ILibChain_RunOnMicrostackThreadEx2(chain, ILibChain_ReadEx2_UnwindHandler, state, 1);
}
}
@@ -3790,6 +3818,8 @@ void ILibChain_AddWaitHandleEx(void *chain, HANDLE h, int msTIMEOUT, ILibChain_W
if (((ILibBaseChain*)chain)->currentHandle != h)
{
ILIBLOGMESSAGEX2(LOGEX_OVERLAPPED_IO, "-- ILibChain_AddWaitHandlesEx(): %p", h);
void *node = ILibLinkedList_AddTailEx(((ILibBaseChain*)chain)->auxSelectHandles, h, metadata==NULL?1:1+strnlen_s(metadata, 1024));
info = (ILibChain_WaitHandleInfo*)ILibMemory_Extra(node);
info->node = node;
@@ -10745,6 +10775,25 @@ void ILIBLOGMESSAGEX(char *format, ...)
ILIBLOGMESSSAGE(dest);
}
void ILIBLOGMESSAGEX2(uint32_t mask, char *format, ...)
{
if ((_g_logMessageX & mask) == mask)
{
char dest[4096];
int len = 0;
va_list argptr;
va_start(argptr, format);
len += vsnprintf(dest + len, sizeof(dest) - len, format, argptr);
va_end(argptr);
ILIBLOGMESSSAGE(dest);
}
}
void ILIBLOGMESSAGEX2_SetMask(uint32_t m)
{
_g_logMessageX = m;
}
#if defined(__APPLE__) || defined(ILIB_NO_TIMEDJOIN)
typedef struct ILibThread_AppleThread

View File

@@ -52,6 +52,10 @@ extern "C" {
extern char* ILibCriticalLogFilename;
#define LOGEX_OVERLAPPED_IO 0x01
#define LOGEX_PROCESSPIPE 0x02
#ifdef _WIN32_WCE
#define REQUIRES_MEMORY_ALIGNMENT
#define errno 0
@@ -1081,6 +1085,7 @@ int ILibIsRunningOnChainThread(void* chain);
HANDLE fileHandle;
OVERLAPPED *p;
void *user;
void *reserved;
}ILibChain_ReadEx_data;
typedef struct ILibChain_WriteEx_data
{
@@ -1592,6 +1597,8 @@ int ILibIsRunningOnChainThread(void* chain);
#define ILIBCRITICALEXITMSG(code, msg) {printf("%s", ILibCriticalLog(msg, __FILE__, __LINE__, 0, 0)); exit(code);}
#define ILIBLOGMESSSAGE(msg) ILibCriticalLog(msg, __FILE__, __LINE__, 0, 0)
void ILIBLOGMESSAGEX(char *format, ...);
void ILIBLOGMESSAGEX2(uint32_t m, char *format, ...);
void ILIBLOGMESSAGEX2_SetMask(uint32_t m);
#if defined(WIN32)
#define ILIBCRITICALERREXIT(ex) { ILibCriticalLog(NULL, __FILE__, __LINE__, GetLastError(), 0); exit(ex); }

View File

@@ -1173,14 +1173,15 @@ void ILibProcessPipe_Pipe_ResumeEx(ILibProcessPipe_PipeObject* p)
ILibChain_AddWaitHandle(p->manager->ChainLink.ParentChain, p->mOverlapped->hEvent, -1, ILibProcessPipe_Process_ReadHandler, p);
p->PAUSED = 0;
return;
#endif
#else
ILibProcessPipe_Pipe_ResumeEx_ContinueProcessing(p);
if (p->PAUSED == 0)
{
ILibLifeTime_Add(ILibGetBaseTimer(p->manager->ChainLink.ParentChain), p, 0, &ILibProcessPipe_Process_StartPipeReaderWriterEx, NULL); // Need to context switch to Chain Thread
}
#endif
}
#ifdef WIN32
BOOL ILibProcessPipe_Process_Pipe_ReadExHandler(void *chain, HANDLE h, ILibWaitHandle_ErrorStatus status, char *buffer, DWORD bytesRead, void* user);
#endif
@@ -1273,13 +1274,20 @@ BOOL ILibProcessPipe_Process_Pipe_ReadExHandler(void *chain, HANDLE h, ILibWaitH
size_t consumed = 0;
if (status == ILibWaitHandle_ErrorStatus_NONE)
{
ILIBLOGMESSAGEX2(LOGEX_PROCESSPIPE, "ReadExHandler[%p](%p) -> TotalRead: %llu, bytesRead: %llu", h, buffer, pipeObject->totalRead, bytesRead);
pipeObject->totalRead += bytesRead;
do
{
handler(pipeObject->buffer + pipeObject->readOffset, pipeObject->totalRead, &consumed, pipeObject->user1, pipeObject->user2);
pipeObject->readOffset += consumed;
pipeObject->totalRead -= consumed;
if (pipeObject->PAUSED == 0)
{
ILIBLOGMESSAGEX2(LOGEX_PROCESSPIPE, " ReadExHandler(%p, %llu, %llu); [%llu]", pipeObject->buffer + pipeObject->readOffset, pipeObject->totalRead, consumed, pipeObject->readOffset);
handler(pipeObject->buffer + pipeObject->readOffset, pipeObject->totalRead, &consumed, pipeObject->user1, pipeObject->user2);
pipeObject->readOffset += consumed;
pipeObject->totalRead -= consumed;
ILIBLOGMESSAGEX2(LOGEX_PROCESSPIPE, " -> readOffset: %llu, totalRead: %llu, consumed: %llu, PAUSE: %d", pipeObject->readOffset, pipeObject->totalRead, consumed, pipeObject->PAUSED);
}
} while (pipeObject->PAUSED == 0 && consumed != 0 && pipeObject->totalRead > 0);
if (pipeObject->totalRead == 0) { pipeObject->readOffset = 0; }
if (pipeObject->PAUSED == 0)
{
@@ -1293,11 +1301,13 @@ BOOL ILibProcessPipe_Process_Pipe_ReadExHandler(void *chain, HANDLE h, ILibWaitH
ILibMemory_ReallocateRaw(&(pipeObject->buffer), pipeObject->bufferSize * 2);
pipeObject->bufferSize = pipeObject->bufferSize * 2;
}
ILIBLOGMESSAGEX2(LOGEX_PROCESSPIPE, "ILibChain_ReadEx2() => (%p, %llu, %llu)", pipeObject->buffer, pipeObject->readOffset + pipeObject->totalRead, pipeObject->bufferSize - pipeObject->totalRead);
ILibChain_ReadEx2(chain, h, pipeObject->mOverlapped, pipeObject->buffer + pipeObject->readOffset + pipeObject->totalRead, (DWORD)(pipeObject->bufferSize - pipeObject->totalRead), ILibProcessPipe_Process_Pipe_ReadExHandler, pipeObject, pipeObject->metadata);
return(TRUE);
}
else
{
ILIBLOGMESSAGEX2(LOGEX_PROCESSPIPE, "HANDLE will get removed");
return(FALSE);
}
}