Does anyone know about networking?

Ask here for technical assistance with installing, running, etc. Forum related issues should be reported here, too.
MildewMan1
Colonist
Colonist
Posts: 30
Joined: Sat Jun 27, 2020 3:02 pm
Favourite Race: Tarth

Re: Does anyone know about networking?

Post by MildewMan1 »

Here's some of the problems that I've noticed so far that I'm trying to work on and/or figure out what is causing it.

At the start of each turn, the host game experiences a decent waiting time where the cursor is the waiting cursor and the UI is unresponsive... At least this happens with 7 players (5 AI and the connected player). I'm not sure if this time would be reduced with a lower number of players. I believe the waiting is because the host has to finish processing all of the other player's net messages from the previous turn before the game will let the host play. I'm going to try and figure out if I can stop this from happening or try to speed up the processing of net messages.

If the slave ends their turn first, sometimes this slave will not immediately receive the end turn message from the host, so he will be stuck on the "Waiting for other players to finish their turn" dialog window for a few minutes even though the host is already finished with its wait period and doing new turn stuff. I put something in where certain messages, such as end turn notifications, get pushed to the front of the message deque, so that they get processed first, which helped with this problem some, but it still happens sometimes. My next attempt is to hold all end turn messages and not let the game process them until all of the players have received the end turn message for the current turn and then process them at the same time on all machines.

The last problem that I'm running into is whenever I try to shut the network down or disconnect a socket, the game freezes up. I'm not sure if this is because of a deadlock in my network code or something that the game is doing based on what my code is doing (i.e. trying to access some pointers from the original dll that aren't there, etc.)

I think once I get these worked out, I'll be ready to test this with other people if there's anyone willing to help me test it. I still don't currently know how my network code will respond to more than one online player at the same time.

User avatar
Tggtt
Mighty Colony Leader
Mighty Colony Leader
Posts: 226
Joined: Sun Oct 06, 2013 8:08 pm

Re: Does anyone know about networking?

Post by Tggtt »

Hello MildewMan1, at first, many congrats on your partial successes!
MildewMan1 wrote:
Sat Sep 19, 2020 7:32 pm
I'm not sure if this is because of a deadlock in my network code
Pun intended? :lol:

About the issues you are encountering, you need to remember that TCP uses blocking calls.
Yet, I admit I have never played a 7 player game ever. I have no idea if that bug was present in the original game.
This means that if the game expects a different sequence for the response sequence (or you invoked them in an incorrect order), that will cause a deadlock.
Probably you need to reorder some calls or delay some of them to avoid them blocking before an earlier message is expected.

Still, it is usually easier to use TCP for the internet despite the risk of deadlocks.

Remember to keep these in mind:
  1. All messages are received in the same order they are sent. Obviously it is not really true in real life, but the protocol layer only unblocks the caller only after the correct sequence is received.
  2. Usual applications that use networking, often have a worker thread or an async callback, maybe they did not separate the callback and it gets stuck in a block.
  3. You might need to add some sort of a new kind of message between the hosts to help to resync.
  4. Despite TCP managing their correct order, the original game developers might have missed some detail. You might need to create a thread that adds the received messages to a queue and only dispatch them to the caller in the correct order.
  5. Every package has an underlying confirmation (acknowledge and sync signals) where the receiver confirms once they receive the message. The receiver may block the call return until the confirmation is actually sent. If you close a socket before a message is confirmed, it is presumed not sent.
  6. You need to add code to implement what to do once a socket is broken. I assume you are using winsock2. There are several error messages you need to be aware of when trying to send or receive a packet. Example: https://docs.microsoft.com/en-us/window ... or-codes-2 namely: WSAECONNABORTED, WSAECONNRESET, WSAENOTCONN, WSAESHUTDOWN and WSAETIMEDOUT
If you decide to use UDP, it might work fine on a LAN or localhost, but unfortunatelly, the internet very complicated to get it to work well.

Regardless, do not let your small frustrations to destroy your great successes.
Best of luck!
Best 20x20 Map is elected.
Follow @OpenDeadlock.
Please help Deadlock Wikis by documenting buildings.
Note: OpenDeadlock Knowledge Gathering has been updated. Translation and Build Tools might fail. Send me a PM if you need help.

MildewMan1
Colonist
Colonist
Posts: 30
Joined: Sat Jun 27, 2020 3:02 pm
Favourite Race: Tarth

Re: Does anyone know about networking?

Post by MildewMan1 »

Haha, no pun intended!

I think I maybe figured out what was causing that problem. At some point I must have accidentally removed some code that sets the player numbers of slaves, and they were getting set to 0 (player 1). I believe the freezing problem was because the code was messing with the wrong player number values in memory. I fixed that earlier, and it didn't freeze when I disconnected the slave. I just need to add code to assign a new master player and code to either destroy the disconnected player's colony or change it to AI controlled.

I have a detached worker thread that receives from the sockets, sends outgoing messages in their queues, and then dispatches received messages (3 different functions called over and over while network is active). I am going to try putting all 3 of those in their own threads, which will be spawned from the main detached thread and then join them once they finish their tasks to see if that speeds things up. I don't think they used multithreading in the original Deadlock game, although it looks like they did in Deadlock 2 from the brief time that I've looked at that code.

I am using the SFML library for the sockets, which is built on winsock2. You can set them to work in blocking or non blocking mode. They are in blocking mode but I am using their socket selector class with a 400 ms wait time, so they don't hang things up. If the selector says that the socket is not ready, then the function returns.

I have added a few custom message values already to handle a few other things, so it shouldn't be a problem if I need to add another one to sync everything up.

Honestly I don't think anyone would have known that the host was on the next turn while you were stuck on the waiting screen unless you were sitting right next to the other person. The slave players probably just thought that the host was still playing their turn. I only played multiplayer a few times back in the day, so I don't remember. Could be that they fixed whatever caused this in patch 1.31.

User avatar
Ubergeneral Grunt
Site Admin
Site Admin
Posts: 183
Joined: Fri Nov 19, 2010 10:20 am
Favourite Race: Tarth
Location: Perth, Western Australia
Contact:

Re: Does anyone know about networking?

Post by Ubergeneral Grunt »

Impressive so far. However, please ensure you can get this working with version 1.31 before testing this with other people (like Tggtt and myself). Not having the ability to save and reload multiplayer saves will be painful. Also, I'm sure the original documentation says that multiplayer games with 2 to 5 players are more stable than ones with 6 or 7. Although this may have just been the Mac version, I don't remember.
Tarth cooks make best strudel, barbecue, bean dip, fish, cat food, smelt, piston rings, tofu and cam shafts...

MildewMan1
Colonist
Colonist
Posts: 30
Joined: Sat Jun 27, 2020 3:02 pm
Favourite Race: Tarth

Re: Does anyone know about networking?

Post by MildewMan1 »

That shouldn't be a problem. Just have to get the corresponding memory addresses for the things I am using. I'll start working on that so I can see if the game plays differently.

I looked at the patch notes for the windows version, and you were right about the number of players. It also says not to play with AI, and I see why....they send a massive amount of net messages, which bogs down the network. Might be something I can work on improving later on by combining multiple net messages into a single packet.

I also noticed an interesting bug that I'm not sure anyone was aware of. When playing with 7 players, if a human player is not the ChCh't or Uva Mosk, sometimes their location on the slave's map are reversed from what it is on the master's map. Only seems to affect those 2 races. I'd guess it has something to do with going over the array boundaries on one side causing it to roll over the other way.

MildewMan1
Colonist
Colonist
Posts: 30
Joined: Sat Jun 27, 2020 3:02 pm
Favourite Race: Tarth

Re: Does anyone know about networking?

Post by MildewMan1 »

Just an update: I've got all of the code that was working on 1.20 working for version 1.31.

Right now I'm working on getting the load multiplayer game code working. The game normally sends the save file in like ~65 byte chunks, which takes a really long time to send for big maps. I've got it sending it in 4 kb chunks, which only takes a few seconds. I will probably increase the chunk size, but I need to do a little more testing on it to make sure it's working correctly first.

I need to work on player disconnect code. Right now it works fine if someone disconnects in the game or while selecting landing sites, but it causes the game to lock up otherwise. I don't think this problem will be difficult to fix; it's just not high on my priority list at the moment.

Lastly I need to test what happens with more than 2 human players playing, which I will work on after I get the load game stuff working.

MildewMan1
Colonist
Colonist
Posts: 30
Joined: Sat Jun 27, 2020 3:02 pm
Favourite Race: Tarth

Re: Does anyone know about networking?

Post by MildewMan1 »

Ok so this is the first time I've ever used multithreading, and I learned something new... I was about to pull my hair out with the load save game thing. It kept freezing the UI when it reloaded the world, and I finally figured out why. You are not supposed to update a UI from any thread except the one that created it.

The way I was handling the urgent net messages was to dispatch them immediately from the detached thread handling the socket instead of queueing them, which was causing the main window to get updated by the detached thread. I changed it so that urgent messages get added to a different deque and setup a window timer on the main thread that dispatches them. That fixed the load game problem, and it wouldn't surprise if this was also the reason why the game would freeze when people disconnected when not in the game.

Hopefully should be ready for some help testing soon.

MildewMan1
Colonist
Colonist
Posts: 30
Joined: Sat Jun 27, 2020 3:02 pm
Favourite Race: Tarth

Re: Does anyone know about networking?

Post by MildewMan1 »

I am ready to let people test out the new multiplayer framework for Deadlock 1!

To use this, copy the 4 DLLs from my github (https://github.com/MildewMan1/Deadlock_Multiplayer) into your Deadlock folder and start Deadlock. If you are using the GOG version, you will need to use the multiplayer patch that Tggtt created (viewtopic.php?f=4&t=351). Take a look at the Read Me on the github page as well.

The new system uses TCP sockets to connect, so for those that are joining a game, you will need to know the IP address and the port of the host player.

If you are hosting a new game, Deadlock will display your local IP, public IP, and listening port. If you are playing locally over a LAN, use the local IP, otherwise use the public IP. You must give the IP and port to the other players for them to connect.

I haven't been able to test the code with someone at a remote location. All of my testing has been from my home PC to a Windows 10 Virtual Box on my PC. Most likely you will have to configure Windows to allow Deadlock to have incoming and outgoing connections, and you will almost definitely have to do some port forwarding if you are connected to a router.

Please try this out if you can and let me know if you run into any bugs. If you do find a bug, please give me as much detail as possible about what happened, what you were currently doing, etc., so I can try to reproduce the problem.

User avatar
Tggtt
Mighty Colony Leader
Mighty Colony Leader
Posts: 226
Joined: Sun Oct 06, 2013 8:08 pm

Re: Does anyone know about networking?

Post by Tggtt »

That's great news MildewMan1,

I am sorry I am too busy to test it correctly right now.

I just want to ask if there is any way to save the game as soon as the host disconnects.
Best 20x20 Map is elected.
Follow @OpenDeadlock.
Please help Deadlock Wikis by documenting buildings.
Note: OpenDeadlock Knowledge Gathering has been updated. Translation and Build Tools might fail. Send me a PM if you need help.

MildewMan1
Colonist
Colonist
Posts: 30
Joined: Sat Jun 27, 2020 3:02 pm
Favourite Race: Tarth

Re: Does anyone know about networking?

Post by MildewMan1 »

As long as the host does not enable the Kill AI code, then I believe it should be possible. I will test it out and verify though.

I am already working on some improvements to it though including adding a dll version number, which should have been added in the first one, to prevent people with mismatched dll versions from playing together. Current version only verifies the Deadlock version.

Post Reply