I've been working a lot on multiplayer over the last couple of days and have some things to discuss here. I am attempting to keep it as general as possible at this point. The idea here is that the users can look at what's in place and add on top of it.
Basic Architecture
There are a lot of different methods to implement Server-Client communication, but I just went with the simple, typical Java based method which is over sockets. The server starts and listens on a socket, and for every connection it receives it starts a new thread to handle all the communication with that client.
To send data back and forth, I created a packet exchange system. This was the first thing that came to mind, and I just went for it to test it out. Each packet has a specific use case that it is responsible for serializing/deserializing to/from a byte stream. This byte stream is written/read over the socket.
An example: A client moves their player, a packet is then sent to the server with the client's player entity's id and the new location. This is encoded as a byte stream of (id, x, y) and sent over the socket to the server. The server reads the packet by reading from the stream three ints in the same order as they were written by the client.
Obviously this has some fairly significant flaws and doesn't really do much of any validation between client/server, but that's a hurdle for another time.
Moving on, whenever the server receives a packet from a client, it forwards that packet to all other clients if it's something that all clients need to be aware of.
Things To Consider
- Serializing & Deserializing: One of the packets implemented now is for sending
Entity information over the sockets. To do so, there must be some way to serialize them to a byte stream. Currently I see three ways of doing this.
- Add methods to the
Entity class and override them where need be. This would look like:
// in Entity.java
public byte[] serialize(DataOutputStream dos) throws IOException {
dos.writeInt(getEntityID());
dos.writeInt(getY());
dos.writeInt(getX());
return // bytes
}
// in LivingEntity.java
@Override
public byte[] serialize(DataOutputStream dos) throws IOException {
super(dos);
dos.writeFloat(getHealth());
dos.writeDouble(getSpeed());
return //the bytes
}
// etc...
public void deserialize(DataInputStream dis) ...
- Add an interface like
NetworkSerializable with serialize and deserialize methods to anything that should be able to be sent over the network. My gripe with this method is that it would just be hierarchy hell. And could end up to requiring different types of entities for single player vs multiplayer (annoying).
- Use annotations like
@Serialize & @Deserialize. This is my personal favorite solution. The benefit of this is that any method could be marked and be used as long as the correct return type and parameters are used. The user would be free to have any hierarchy they desire and could just slap this down anywhere. It does require them to manage more state, though. The major drawback is annotation based stuff is all going to be reflection which can get slow. This could be offset somewhat by doing class path scanning or build registries to avoid scanning during general runtime for the methods.
- The nasty pile of threads: There are a lot of threads for the server using this method. There's the starting thread, then the game thread, then the server thread, and another thread for each client. It could be really beneficial to use some existing libraries for this stuff to ensure thread safety, something like Akka perhaps.
- Is this going to even be useful for the general case? The more I work on it, the more it feels like I'm designing something that I find useful, but may not be useful to the average game.
- Probably more stuff that I can't think of right now.
Conclusion
There's still a lot to be learned in this process, but I wanted to share what i have so far. I've been doing this in my fork so as to not create noise in the main repo. You can check it out here. Only 1300 lines of code! Phew.
Thoughts?
I've been working a lot on multiplayer over the last couple of days and have some things to discuss here. I am attempting to keep it as general as possible at this point. The idea here is that the users can look at what's in place and add on top of it.
Basic Architecture
There are a lot of different methods to implement Server-Client communication, but I just went with the simple, typical Java based method which is over sockets. The server starts and listens on a socket, and for every connection it receives it starts a new thread to handle all the communication with that client.
To send data back and forth, I created a packet exchange system. This was the first thing that came to mind, and I just went for it to test it out. Each packet has a specific use case that it is responsible for serializing/deserializing to/from a
bytestream. Thisbytestream is written/read over the socket.An example: A client moves their player, a packet is then sent to the server with the client's player entity's id and the new location. This is encoded as a
bytestream of (id, x, y) and sent over the socket to the server. The server reads the packet by reading from the stream threeints in the same order as they were written by the client.Obviously this has some fairly significant flaws and doesn't really do much of any validation between client/server, but that's a hurdle for another time.
Moving on, whenever the server receives a packet from a client, it forwards that packet to all other clients if it's something that all clients need to be aware of.
Things To Consider
Entityinformation over the sockets. To do so, there must be some way to serialize them to abytestream. Currently I see three ways of doing this.Entityclass and override them where need be. This would look like:NetworkSerializablewithserializeanddeserializemethods to anything that should be able to be sent over the network. My gripe with this method is that it would just be hierarchy hell. And could end up to requiring different types of entities for single player vs multiplayer (annoying).@Serialize&@Deserialize. This is my personal favorite solution. The benefit of this is that any method could be marked and be used as long as the correct return type and parameters are used. The user would be free to have any hierarchy they desire and could just slap this down anywhere. It does require them to manage more state, though. The major drawback is annotation based stuff is all going to be reflection which can get slow. This could be offset somewhat by doing class path scanning or build registries to avoid scanning during general runtime for the methods.Conclusion
There's still a lot to be learned in this process, but I wanted to share what i have so far. I've been doing this in my fork so as to not create noise in the main repo. You can check it out here. Only 1300 lines of code! Phew.
Thoughts?