How does applicationcommand("downloadhttprequest") work?

How does applicationcommand("downloadhttprequest") work?

yue_y
Not applicable
32 Views
7 Replies
Message 1 of 8

How does applicationcommand("downloadhttprequest") work?

yue_y
Not applicable

[ FlexSim 19.1.0 ]

From this post https://answers.flexsim.com/questions/67068/how-to-make-asynchronous-call-to-web-service.html, I know this command: applicationcommand("downloadhttprequest", verb, server, object, data, silent, filePath, callbackNode) is asynchronous and the callbackNode will be executed after the request has complete. Could someone help me understand how does this function work under the hood? Since FlexSim is single threaded, I assume this function is executed in a separate thread and once it finishes, it will push the callbackNode on top of FlexSim's event queue and get scheduled next. However, in this post https://answers.flexsim.com/questions/67556/is-flexsim-thread-safe.html it says FlexSim's event loop is not thread safe, then how is the callbackNode safely added to the event queue?

Also, not sure if it's related, in the cpp code provided in this post:https://answers.flexsim.com/questions/67556/is-flexsim-thread-safe.html

There is a line:

PostMessage(window, FLEXSIM_MESSAGE_USER_NODEFUNCTION, (WPARAM)registernode, NULL)

Does this line force the registernode to be executed immediately? If so, how does this line interact with FlexSim event loop? Thank you!

0 Likes
Accepted solutions (1)
33 Views
7 Replies
Replies (7)
Message 2 of 8

philboboADSK
Autodesk
Autodesk
Accepted solution

applicationcommand("downloadhttprequest") passes the flag WINHTTP_FLAG_ASYNC to the WinHttpOpen function. Then it passes a callback to the WinHttpSetStatusCallback function. That callback uses PostMessage() to execute the passed treenode callbackNode on FlexSim's main thread, and the http communications happen asynchronously on a separate thread.

PostMessage(window, FLEXSIM_MESSAGE_USER_NODEFUNCTION, (WPARAM)registernode, NULL) posts a message to FlexSim's main window message loop. It doesn't execute immediately (you use nodefunction() directly to execute immediately); FlexSim's processing of its Windows messages will execute that node function when it executes that Windows message. A posted window message will happen soon, but not immediately. That line doesn't interact with FlexSim's simulation event loop; it interacts with FlexSim's windows message loop.



Phil BoBo
Sr. Manager, Software Development
0 Likes
Message 3 of 8

yue_y
Not applicable

For applicationcommand("downloadhttprequest"), how does FlexSim main thread know the action is complete and it's time to execute the callback? The way I understand FlexSim is that there is a event loop executing event in a queue in the order of time. Since it is single thread, how does the callback function gets in the middle of the loop? Does FlexSim frequently checking a flag or does the call back function pushed to FlexSim event queue?

0 Likes
Message 4 of 8

yue_y
Not applicable

When will FlexSim processes its Windows messages?

0 Likes
Message 5 of 8

philboboADSK
Autodesk
Autodesk

FlexSim is always processing its Windows messages. It is literally a while(1) loop. See the example in Windows message loop.



Phil BoBo
Sr. Manager, Software Development
0 Likes
Message 6 of 8

philboboADSK
Autodesk
Autodesk

FlexSim doesn't have to do anything special to handle it. It is a Windows API command called from a Windows API application. Windows handles it.

If you want to know how it works, contact Microsoft. You don't need to know how the Windows API handles it behind the scenes in order to use it.



Phil BoBo
Sr. Manager, Software Development
0 Likes
Message 7 of 8

yue_y
Not applicable

Many thanks for your help!

0 Likes
Message 8 of 8

philboboADSK
Autodesk
Autodesk

Re-reading your question, I need to clarify:

The callback passed to WinHttpOpen is a C++ function defined in the FlexSim engine.

That C++ callback function uses PostMessage() to execute the callback treenode function on the main thread.

The execution of the treenode callback is handled by FlexSim; the execution of the C++ callback is handled by the Windows API.



Phil BoBo
Sr. Manager, Software Development