jhj0411jhj / torchlight2_lan_game_server_tool

A message forwarding tool for Torchlight 2 lan game.
16 stars 3 forks source link

Request For Information #1

Open Phanjam opened 4 months ago

Phanjam commented 4 months ago

Hello Huaijun Jiang.

This is not really an issue - it's just a request for more information about your tool, so I hope you don't mind I opened this issue. Please feel free to delete it if you would rather not have a discussion.

You already know that gamers who want to play multiplayer Torchlight 2 are always having trouble making and keeping a good connection with other clients. This tool of yours looks like a solution for those troubles (actually, it looks like a gift from heaven!), but I'm not a programmer and I don't know anything about net coding for internet co-operative games, so I'm not sure how to describe it correctly to other players.

My request is if you could please describe with more detail how your tool works. If you can also mention why it works, that would be so great. Like, what the problems were with the old code, and how your tool addresses those problems.

Thank you for making this tool for Torchlight 2!

jhj0411jhj commented 4 months ago

Hi @Phanjam,

I'm glad to hear that my tool is helping someone else. I'm happy to answer your questions and hope it will assist gamers in playing old games, not just Torchlight 2. I'll start by explaining how Torchlight 2 works on LAN, followed by a description of how my tool operates.

How Torchlight 2 Works on LAN

From the player's perspective, a host creates a room on LAN, and other clients find and join the room. You may wonder how this actually works. After some exploration (which I will explain later), I figured out the method Torchlight 2 uses for room discovery on LAN.

Torchlight 2 uses UDP port 4549 for LAN game connection. Once a Torchlight 2 game is hosted on LAN, it starts broadcasting its room information (e.g., the room name) to other computers on LAN (destination IP = 255.255.255.255). When a client receives this broadcast, the room appears in the Torchlight 2 LAN game room list. The player can then click the connect button, and the game handles the rest.

Specifically, the host will broadcast the 'room information' message to 255.255.255.255 under the following conditions:

  1. Every 30 seconds.
  2. When the host receives a 'room request' message from clients.

A client can broadcast a 'room request' message (length = 18) on the LAN to request room information from the host. The client will broadcast a 'room request' message if:

  1. The LAN game room list page is opened.
  2. The player clicks on the refresh button.

The Problem When Using VLAN

If the room information message is not correctly delivered from host to client, the client will not be able to find the room and establish a connection with the host. The primary reason preventing clients from receiving the message is that, during the room discovery stage, all network messages are sent to the ambiguous broadcast IP 255.255.255.255 instead of direct IPs. The broadcast message may fail to pass through the VLAN due to the following reasons:

  1. Some VLAN software (e.g., Pugongying) simply blocks the broadcast message.
  2. The broadcast message is sent to the wrong network due to network priority. For example, the message might be broadcast to your home Wi-Fi instead of the VLAN. (Since zerotier allows broadcast messages, this is typically the reason for zerotier users.)

How My Tool Works

To ensure the 'room information' message is delivered from host to client, my tool operates as follows:

  1. The tool listens on UDP port 4549 on the host machine.
  2. Once the host creates a room, the tool will capture the broadcast 'room information' message sent by the host game within 30 seconds.
  3. Even if the broadcast message doesn't pass through the VLAN, the tool ensures delivery by sending the 'room information' message directly to the clients' IPs. The clients' IPs should be provided by the host player in the beginning.

Please note:

  1. There are two types of messages broadcast on LAN: the room information message (sent by the host) and the room request message (sent by the client, length = 18). The room information message is longer. To distinguish between them, I set a length threshold of 30 bytes. If the captured broadcast message is longer than 30 bytes, the tool will consider it a room information message. Otherwise, the message will be ignored.
  2. Once all clients are connected, game data packets will be delivered directly between the host and clients, rendering the tool unnecessary. You can close the tool at this point and reopen it if a new client wants to join.

An Alternative Method for Zerotier Users

Zerotier allows broadcast messages, but users may still encounter issues due to network priority. To check the network priority, a user can open cmd.exe and type route print. For the destination 255.255.255.255, there might be multiple entries. The broadcast message will be sent to the interface with the lowest Metric (hops). If that interface isn't the Zerotier network, the broadcast message won't be delivered correctly.

To fix this, one way is to use my tool. The other way is to modify the network metric. Here’s how:

  1. Open Windows Network Connections.
  2. Right-click the network interface and select Properties.
  3. Click on Internet Protocol Version 4 (TCP/IPv4), then select Properties.
  4. On the General tab, click Advanced.
  5. In the IP Settings tab, clear the Automatic metric check box. Enter a lower metric in the Interface Metric field (e.g., enter 1).

By setting the Zerotier network to the lowest metric, it will become the preferred network for broadcast messages, ensuring they pass through successfully.

Exploration on Torchlight 2 LAN Game Room Discovery

In this section, I'll briefly explain how I figured out the Torchlight 2 LAN game room discovery mechanism. Although different games have unique network discovery methods, I hope this provides some inspiration for other players of older games.

  1. Find the Network Port Torchlight 2 Uses: Open cmd.exe and combine the commands tasklist, netstat -nao, and findstr to find the process ID and network port that Torchlight 2 uses. I discovered that when I created a LAN game, UDP port 4549 was always occupied.
  2. Analyze Network Packets with Wireshark: Use Wireshark to monitor and analyze network packets. Set the filter condition to udp.port == 4549 to inspect packets related to the LAN game. Don't forget to interact with the game to observe changes in the network traffic.

BTW, I'm not a professional network programmer. I developed this tool as a practice exercise in network knowledge. It's not very difficult, and I believe anyone can learn and develop a tool like this.