Thanks for the insightful replies.
I was under the gross misapprehension that the serial event handler was called in direct response to a character arriving at the serial port, but I now see that it's not that at all.
No wonder the code didn't make any sense to me.
It looks like I'm going to need to write my own replacement for the Serial1 ISR, so that the code can get notified as soon as a character arrives.
Btw.
I didn't mention what I am doing..
I've written some web server code that works with a Serial Wifi module.
I,e it parses the incoming GET request and sends back the appropriate response data.
However the issue I'm having is if I send back a page, which contains references to other pages e.g. Style sheets which are also on the Arduino. That most browsers seem to parse the page on the fly and submit requests for the other pages and images etc before the initial page has finished being transferred.
As the GET requests from browsers are around 400 characters long, the subsequent request overflow the incoming serial buffer, with the result that the subsequent requests get lost, because my send function is still pushing data to the wifi module while the GET requests are coming in.
I don't think this can be solved correctly by increasing the serial buffer size, and anyway that would require people to modify their Arduino installation, and I was hoping to write generic code ( a version is already on GitHub and is being used by other people)
Adding code to the send function, to sporadically check for incoming data, would probably resolve the problem, but it would be messy, as I'd need to put a call to the check function in a few places.
I was hoping to split out the data input and parsing from the output, using something like SerialEvent.
That is, have some code that just operates off the serial ISR in and parses the incoming data and determines what pages ( or other data ) needs to be returned to the wifi module.
Then put these requests in an queue (array ), the have the page generation and output function operate from the main loop, I.e. poll the queue and send pages one by one until the queue was empty.
In this case the queue only needs to store what page needs to be returned e.e index.html. Along with any URL parameters e.g. DigitalWrite.html?pin=13&value=LOW
In fact I'd not use quite as verbose a name or parameters as this, but it gives an example.
Anyway, the ram taken by the queue wouldn't need to be too big as long as the pages didn't have lots and lots of links to other pages on the Arduino,
Anyway, I guess my next task is to write a replacement serial ISR and see if it gets called instead of the existing one.
Note I'm using serial1 on a pro micro, so this won't interfere with normal operations.