www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - VIbe-d fiber question

reply Tristan Brice Velloza Kildaire <deavmi redxen.eu> writes:
I have a piece of server-side software I am writing using Vibed's 
web socket support.

I initially setup an HTTP server of which listens on a given 
port, with the provided `HTTPServerSettings`

```d
// Setup where to listen
this.httpSettings = new HTTPServerSettings();
httpSettings.port = bindPort;
httpSettings.bindAddresses = bindAddresses;
```

I then later continue to setup a handler for web socket 
connections using:

```d
// Setup a websocket negotiater with a handler attached
this.websocketNegotiater = handleWebSockets(&websocketHandler);
```

Then lastly I attach the web socket handler to a given route 
which must allow web socket connections to it:

```d
// Handle `/` as the web socket path
this.router = new URLRouter();
router.get("/", this.websocketNegotiater);
```

All of this is pretty stock-standard.

---

However, here is the snatch (and consequently where my question 
comes in). Because we are accepting connections by virtue of a 
web socket handler which is called on web socket connections to 
the `/` route, I can obviously handle each client by doing some 
work in a loop, then when I am done calling `this.yield()` where 
`this` is a `Connection : Fiber` (custom type of mine), this then 
saving the stack context, the instruction address etc. for later 
resumption with `conn1.call()`.

My question though, is, if there is a way to hook a 
`pre-connection-accept()` function in, because currently the only 
way I am able to propgress the other fibers is by yieling away 
from the websocket connection handler, loop through all the 
current fibers and calling `.call()` on them - this works but 
then after which I have to exit the `websocketHandler()` method 
such that the code can progress to the processing of any possible 
new connections - but in such a case where there are none I 
effectively hang on waiting for such an event instead of doing 
something along the lines of:

```d
if(noNewConnections)
{
     // Loop through all fibers and .call them
}
else
{
     // Process new connection as per `websocketHandler()`
}
```

---

I hope I made my problem/scenario clear and would be interested 
to hear what you guys have to say.
Mar 12 2023
parent WebFreak001 <d.forum webfreak.org> writes:
On Monday, 13 March 2023 at 06:24:15 UTC, Tristan Brice Velloza 
Kildaire wrote:
 [...]

 However, here is the snatch (and consequently where my question 
 comes in). Because we are accepting connections by virtue of a 
 web socket handler which is called on web socket connections to 
 the `/` route, I can obviously handle each client by doing some 
 work in a loop, then when I am done calling `this.yield()` 
 where `this` is a `Connection : Fiber` (custom type of mine), 
 this then saving the stack context, the instruction address 
 etc. for later resumption with `conn1.call()`.

 [...]
you should use vibe.d's fiber's (runTask / new task is default for every connection handler) and then use vibe.d's `yield` function instead. It also yields on all vibe.d I/O APIs, e.g. also websocket send/receive. Instead of pre-connection-accept, you simply do you work before your actual code. You can use runTask if you need to branch off things, but it's easier to just do all the work in your websocket handler.
Mar 13 2023