Network Multithreading in Love2d

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
sundays
Prole
Posts: 2
Joined: Sun Sep 02, 2018 8:33 pm

Network Multithreading in Love2d

Post by sundays » Sun Sep 02, 2018 8:42 pm

I'm writing a simple game that has online multiplayer. I'm not sure how exactly networking is done in love2d. I'm using blocking sockets. My plan was to handle everything but reading/writing to the socket in the main thread. I would send messages from the client on the main thread, and read messages in an infinite loop on the network thread, pushing them into a channel as they are read.

There seems to be no way of pushing custom userdata like a socket to a thread, though. It seems like the solution is to let the network thread handle both reading and writing, rather than sending messages from the main thread.

But since I'm using blocking sockets, then my client won't be able to send any messages while the receive loop is blocking. I would only be able to check the channel for outgoing messages once a full server message has been read.

Am I supposed to be using asynchronous sockets with love2d to avoid this? It would be easy if I had a way of passing a reference to my socket from the main thread to the net thread so it can read from it.

Thanks.

edit:

I've fixed this by using non-blocking sockets. During the update loop, I read bytes into a buffer and, when there are enough bytes available, construct my messages from that and call the appropriate handler.

It's more code than having a separate thread, but it works.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 8 guests