So, I've been experimenting with networking using kryonet, and I have written a really simple 2-client demo, basically just giving each other coordinates as they move.
My question is, in what way should I write my client-server architecture?
Should I convert to UDP and use datagrams, or should I stick with TCP and write a player object with all the information needed inside, and send that?
Do I send updates at the same rate as I render, less often, etc? (updates are sent on the render thread every 6 frames through the client object, but it receives in it's own thread, I know I could write this based on a timer in the client thread or in it's own send thread as well, but I don't know how necessary that is)
All I really need are some good resources. Feel free to post examples if you have written network code before. (I'm not interesting in interpolation or anything as of right now, I just want something usable, I'm more interested in the networking side, rather than what a user would see, as the only user is myself)
UDP is for when things such as message order isn't critical and when missing messages on occasion won't break your networking model. For example, sending entity coordinates every time they change would be UDP since missing a coordinate doesn't really matter when the next one is milliseconds away.
With Java you can very easily serialize data across the network and use things such as Remote Method Invocation (Provided by Kryonet and the JDK) So you should take advantage of that if you can. You should also take advantage of the TCP\IP protocol if you can as well since it is much less error prone.
Anyway, when it comes to some general hints for networking your game:
- Most of the time, your bottlenecks will be bandwidth and network speed, so don't be afraid to spend some CPU time compressing the data your are transmitting across to clients\peers.
- Abstract the networking to as high of a level as possible. You talked about constantly sending X\Y coordinates? Well, it would be more useful to the client if you sent the velocity, acceleration and position and then you can send the coordinates much less frequently and implement some sensible client-side prediction. If your game is more predictable in terms of player movement (i.e, tile based) you can abstract it much further and notify clients when an entity is moving to another tile and have them perform the remainder of the logic client-side.
The basic principal for a centralized server (I'm not sure which model your after, but this one is very common) is to do something like the following:
1. Server thread waits for incoming connections, upon request for connection, it is accepted.
2. Server spawns another thread, that acts as a sort of 'sub-server' for just that accepted connection. The subserver will handle all communications from just that particular client. Since the subserver runs in a different thread, the server is now free to go back and repeat step one (listen for more clients)
3. The subserver querys the state of the game from the server, and helps initialize its respective remote client.
4. When the spawned subserver receives a command from the client it will notify the server which spawned it that its corresponding client has issued a command (i.e picked up item, moved somewhere) and the server will broadcast that command to all other spawned sub-servers.
It is a little more involved than that, and not being very into networking myself I might have used the improper terminology there (others are welcome to correct me) but that is the basic gist of it.