RiscOS way to make non-blocking asynchronous HTTP requests
Sergey Lentsov (8268) 63 posts |
Hello, I’m a newbie in RiscOS programming. I plan to make main event loop like this pseudo-code:
But as far as I know this way will make additional load on CPU. Is there better way? And second question: |
Michael Grunditz (467) 531 posts |
You can see what happens if you do while(1) {}, complete stop! Someone may correct me, but wimp_poll is the one that drives wimp multitasking. If you do a simple c program with a loop, and start it from filer , you will see the same thing. (wow post 467 and am user 467! :) ) |
Andrew Rawnsley (492) 1445 posts |
I’ve never actually needed to sit down and write socket code, but my understanding is that the basic socket swis block by default. However, there are non-blocking versions (I believe you mark the socket as non-blocking before first use). You can read a bit of info here if you search for “non-blocking”: http://www.riscos.com/support/developers/prm/internet.html I don’t know how your curl-port will work in that context. I’d imagine that unixlib handles that automatically, but I have no experience of that. Hopefully there will be folks on here that will be able to provide advice on using non-blocking sockets specifically, and how that fits into curl. |
Steve Fryatt (216) 2105 posts |
Yes, but
You should be calling NetSurf does exactly this, and has a lot of code dedicated to such a scheduler – this is one of the areas where RISC OS doesn’t help you at all, unfortunately. |
Rick Murray (539) 13840 posts |
Just as a guess, as I use DeskLib, but I’d imagine something like… #define POLL_DELAY 50 event_set_mask(1); while (!quit) { poll_time = os_read_monotonic_time() + POLL_DELAY; event_poll_idle (&event_code, &poll_block, poll_time, NULL ); if ( netconn.data_available() ) { netconn.process_data(); } // and handle wimp events! :-) } This will return with a null event every 50cs unless something else happens in the meantime.
Yes, that’s what blocking means. You need to specify the socket as non blocking with: socketioctl(sock_handle, FIONBIO, &flag); Where flag is an integer set to 1. Call this between creating the socket and listening or accepting connections. Note that this means you will need to handle “soft” errors such as EINPROGRESS and EWOULDBLOCK, which basically mean “hang on, in doing it!”.
Indeed. You just get to (usually) come back from Wimp polls periodically. It’s then up to you to decide what to do and how to do it. In Manga, I just look to see if data is available, and if it is I transfer it. Rinse and repeat until done. |
Sergey Lentsov (8268) 63 posts |
Great thanks to all authors! |
Chris Johns (8262) 242 posts |
From a very vague memory – isn’t there an internet event you can use to tell you when there’s something on the socket? |
nemo (145) 2546 posts | |
Rick Murray (539) 13840 posts |
But note that events are not something you can reasonably use from application code… |
nemo (145) 2546 posts |
A few lines of code in the RMA sets your wimp poll word. Bob’s your uncle. |