diff --git a/microscript/ILibDuktape_Polyfills.c b/microscript/ILibDuktape_Polyfills.c index 0873ffd..4ed8e26 100644 --- a/microscript/ILibDuktape_Polyfills.c +++ b/microscript/ILibDuktape_Polyfills.c @@ -1813,63 +1813,17 @@ void ILibDuktape_ChainViewer_PostSelect(void* object, int slct, fd_set *readset, { duk_context *ctx = (duk_context*)((void**)((ILibTransport*)object)->ChainLink.ExtraMemoryPtr)[0]; void *hptr = ((void**)((ILibTransport*)object)->ChainLink.ExtraMemoryPtr)[1]; - int i; ILibDuktape_EventEmitter_SetupEmit(ctx, hptr, "PostSelect"); // [emit][this][name] duk_push_int(ctx, slct); // [emit][this][name][select] - duk_push_array(ctx); // [emit][this][name][select][readset - for (i = 0; i < 4096; ++i) - { - if (FD_ISSET(i, readset)) - { - duk_push_int(ctx, i); - duk_put_prop_index(ctx, -2, duk_get_length(ctx, -1)); // [emit][this][name][select][readset] - } - } - duk_push_array(ctx); // [emit][this][name][select][readset][writeset] - for (i = 0; i < 4096; ++i) - { - if (FD_ISSET(i, writeset)) - { - duk_push_int(ctx, i); - duk_put_prop_index(ctx, -2, duk_get_length(ctx, -1)); // [emit][this][name][select][readset][writeset] - } - } - duk_push_array(ctx); // [emit][this][name][select][readset][writeset][errorset] - for (i = 0; i < 4096; ++i) - { - if (FD_ISSET(i, errorset)) - { - duk_push_int(ctx, i); - duk_put_prop_index(ctx, -2, duk_get_length(ctx, -1)); // [emit][this][name][select][readset][writeset][errorset] - } - } - if (duk_pcall_method(ctx, 5) != 0) { ILibDuktape_Process_UncaughtExceptionEx(ctx, "ChainViewer.emit('PostSelect'): Error "); } + + char *m = ILibChain_GetMetaDataFromDescriptorSet(Duktape_GetChain(ctx), readset, writeset, errorset); + duk_push_string(ctx, m); + if (duk_pcall_method(ctx, 3) != 0) { ILibDuktape_Process_UncaughtExceptionEx(ctx, "ChainViewer.emit('PostSelect'): Error "); } duk_pop(ctx); } -duk_ret_t ILibDuktape_ChainViewer_GetDescriptorInfo(duk_context *ctx) -{ - int fd = duk_require_int(ctx, 0); - void *chain = Duktape_GetChain(ctx); - void *module = ILibChain_GetObjectForDescriptor(chain, fd); - duk_push_object(ctx); - if (module != NULL) - { - duk_push_pointer(ctx, module); - duk_put_prop_string(ctx, -2, "_ptr"); - duk_push_string(ctx, Duktape_GetStashKey(module)); - duk_put_prop_string(ctx, -2, "pointer"); - if (((ILibChain_Link*)module)->MetaData != NULL) - { - duk_push_string(ctx, ((ILibChain_Link*)module)->MetaData); - duk_put_prop_string(ctx, -2, "moduleType"); - } - } - - - return(1); -} +extern void ILibPrependToChain(void *Chain, void *object); void ILibDuktape_ChainViewer_Push(duk_context *ctx, void *chain) { duk_push_object(ctx); // [viewer] @@ -1881,9 +1835,7 @@ void ILibDuktape_ChainViewer_Push(duk_context *ctx, void *chain) ((void**)t->ChainLink.ExtraMemoryPtr)[1] = duk_get_heapptr(ctx, -1); ILibDuktape_EventEmitter *emitter = ILibDuktape_EventEmitter_Create(ctx); ILibDuktape_EventEmitter_CreateEventEx(emitter, "PostSelect"); - - ILibDuktape_CreateInstanceMethod(ctx, "GetDescriptorInfo", ILibDuktape_ChainViewer_GetDescriptorInfo, 1); - ILibAddToChain(chain, (void*)t); + ILibPrependToChain(chain, (void*)t); } void ILibDuktape_Polyfills_Init(duk_context *ctx) diff --git a/microstack/ILibParsers.c b/microstack/ILibParsers.c index 158bf2f..3494e69 100644 --- a/microstack/ILibParsers.c +++ b/microstack/ILibParsers.c @@ -1829,6 +1829,15 @@ void ILibAddToChain(void *Chain, void *object) ILibLinkedList_AddTail(((ILibBaseChain*)Chain)->Links, object); ((ILibChain_Link*)object)->ParentChain = Chain; } +void ILibPrependToChain(void *Chain, void *object) +{ + // + // Add link to the front of the chain (Linked List) + // + ILibLinkedList_AddHead(((ILibBaseChain*)Chain)->Links, object); + ((ILibChain_Link*)object)->ParentChain = Chain; + +} //! Return the base timer for this chain. Most of the time, new timers probably don't need to be created /*! @@ -2625,6 +2634,58 @@ void ILibChain_DisableWatchDog(void *chain) ((ILibBaseChain*)chain)->nowatchdog = 1; } +char *ILibChain_GetMetaDataFromDescriptorSet(void *chain, fd_set *inr, fd_set *inw, fd_set *ine) +{ + char *ret = NULL; + ILibChain_Link *module; + void *node = ILibLinkedList_GetNode_Head(((ILibBaseChain*)chain)->Links); + int selectTimeout = UPNP_MAX_WAIT * 1000; + + fd_set readset; + fd_set errorset; + fd_set writeset; + fd_set emptyset; FD_ZERO(&emptyset); + struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; + int len = 0; + + + while (node != NULL && (module = (ILibChain_Link*)ILibLinkedList_GetDataFromNode(node)) != NULL) + { + if (module->PreSelectHandler != NULL) + { + FD_ZERO(&readset); + FD_ZERO(&errorset); + FD_ZERO(&writeset); + + module->PreSelectHandler(module, &readset, &writeset, &errorset, &selectTimeout); + if (memcmp(&readset, &emptyset, sizeof(fd_set)) != 0 || memcmp(&writeset, &emptyset, sizeof(fd_set)) != 0 || memcmp(&errorset, &emptyset, sizeof(fd_set)) != 0) + { + // Descriptors were added, lets test them + if (select(FD_SETSIZE, &readset, &writeset, &errorset, &tv) > 0) + { + len += sprintf_s(ILibScratchPad + len, sizeof(ILibScratchPad) - len, "%s ", ((ILibChain_Link*)module)->MetaData); + } + } + } + node = ILibLinkedList_GetNextNode(node); + } + + if (len > 0) { ret = ILibScratchPad; } + if (ret == NULL) + { +#if defined(WIN32) + if (FD_ISSET(((ILibBaseChain*)chain)->TerminateSock, ine) || FD_ISSET(((ILibBaseChain*)chain)->TerminateSock, inr)) { ret = "ILibForceUnblockChain"; } +#else + if (FD_ISSET(fileno(((ILibBaseChain*)chain)->TerminateReadPipe), inr)) { ret = "ILibForceUnblockChain"; } +#endif + if (ret == NULL) + { + ret = "UNKNOWN"; + } + } + return(ret); +} + void *ILibChain_GetObjectForDescriptor(void *chain, int fd) { void *ret = NULL; @@ -6469,6 +6530,7 @@ void *ILibCreateLifeTime(void *Chain) if ((RetVal = (struct ILibLifeTime*)malloc(sizeof(struct ILibLifeTime))) == NULL) ILIBCRITICALEXIT(254); memset(RetVal,0,sizeof(struct ILibLifeTime)); + RetVal->ChainLink.MetaData = "ILibLifeTime"; RetVal->ObjectList = ILibLinkedList_Create(); RetVal->ChainLink.PreSelectHandler = &ILibLifeTime_Check; RetVal->ChainLink.DestroyHandler = &ILibLifeTime_Destroy; diff --git a/microstack/ILibParsers.h b/microstack/ILibParsers.h index 8482a6a..bbc9a04 100644 --- a/microstack/ILibParsers.h +++ b/microstack/ILibParsers.h @@ -924,6 +924,7 @@ int ILibIsRunningOnChainThread(void* chain); void ILibChain_DestroyEx(void *chain); void ILibChain_DisableWatchDog(void *chain); void *ILibChain_GetObjectForDescriptor(void *chain, int fd); + char *ILibChain_GetMetaDataFromDescriptorSet(void *chain, fd_set *inr, fd_set *inw, fd_set *ine); ILibExportMethod void ILibStartChain(void *chain); ILibExportMethod void ILibStopChain(void *chain); ILibExportMethod void ILibChain_Continue(void *chain, ILibChain_Link **modules, int moduleCount, int maxTimeout);