Jump to content
  • entries
    943
  • comments
    5,899
  • views
    923,763

Peer-to-peer Networking and the Floating Server


Josh

5,313 views

 Share

I have Steam peer-to-peer networking commands implemented into Leadwerks 4.6. Here are the commands. Note this complex system has been boiled down to just three simple commands:

class P2P
{
public:
#ifdef LEADWERKS_5
	static bool Send(uint64 steamid, const int messageid, shared_ptr<BankStream> data, const int channel = 0, const int flags = 0);
	static bool Send(uint64 steamid, const int messageid, shared_ptr<Bank> data, const int channel = 0, const int flags = 0);
	static shared_ptr<Message> Receive(uint64 steamid, const int channel = 0);
#else
	static bool Send(uint64 steamid, const int messageid, BankStream* data, const int channel = 0, const int flags = 0);
	static bool Send(uint64 steamid, const int messageid, Bank* data, const int channel = 0, const int flags = 0);
	static Message* Receive(uint64 steamid, const int channel = 0);
#endif
	static bool Send(uint64 steamid, const int messageid, const int channel = 0, const int flags = 0);
	static bool Send(uint64 steamid, const int messageid, std::string& data, const int channel = 0, const int flags = 0);
	static bool Send(uint64 steamid, const int messageid, const void* data, const int size, const int channel = 0, const int flags = 0);
	static bool Disconnect(const uint64 steamid);
};

There isn't really any concept of "making a connection" here. You just start sending packets to any Steam user you want, and when you are done you can call Disconnect() to clean up some stuff. This system is completely peer-to-peer, so any player in a game can directly message any other player, although you probably want one central computer in charge of the game. NAT punch-through is supported, and in the event that it fails then messages will automatically be sent through a relay server.

This is only part of the solution though. We still need a publicly readable list of what games are available to join.

In a system like ENet, which is what our existing networking is built on, you have one server and multiple clients. The server can be hosted on one of the player's machines, or it can be a dedicated server. A dedicated server is a computer running the game that doesn't have a player on it. It will also skip rendering graphics so that it can relay messages between players faster. A dedicated server is always on, but these take a lot of extra work to develop and maintain, and if the dedicated server ever goes down the game is unplayable.

A game hosted on one of the player's machines is nice because you don't have to set up a dedicated server for your game. Players themselves can create a new game with whatever settings they want, and other players can join them. However, when the player that created the game leaves, the game is terminated.

Lobbies

The Steam networking API supports a feature called lobbies. In games like Left 4 Dead, players will congregate in a lobby until it is full. A dedicated server is selected. The lobby is destroyed and the game moves over to a dedicated server to play. Lobbies can be selected by a player either through automatic matchmaking (I'm not a fan of this) or a list of lobbies can be retrieved.

maxresdefault.thumb.jpg.381e92d453f352b8fd064e6022b2d3f4.jpg

The name "lobby" is a little misleading because that is only one possible way this feature can be used. There is no reason a lobby can't be left open while the game is playing, in which case they function more like a regular public game server list. One interesting feature is that if the lobby creator leaves the game, a new lobby leader is automatically selected and the lobby lives on. This means that one user can create a game, other players can join it, and the original creator can leave the game without it shutting down. The new lobby leader can be used as the server that synchronizes the game state between all players, creating a sort of "floating server" that only disappears when all players leave the game.

This is a huge improvement over both dedicated and hosted servers. A floating server isn't tied to a single machine, whether that be one player's computer or a dedicated server that has to be maintained constantly. This is the best solution to quickly get your multiplayer game up and running, and it will be available in Leadwerks Game Engine 4.6.

  • Like 5
 Share

1 Comment


Recommended Comments

I saw a lot of potential in Steam allowing lobbies and connecting with your friends for free, before you were even Greenlit (at the time) or signed up as a developer with them, just using their free Spacewar ID.  After all, who doesn't want to make a multiplayer game to play with their friends?  It's what inspired me to delve into it and also make a little tutorial for the community years ago because I enjoyed it so much.

  • Like 1
Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...