How to do it

Terminology

message - the smallest unit of data you want to send. For example, a string, or a list of numbers, or a dict.

packet - a bundle of messages; the item that is actually sent between the server and the client. A packet may contain 1 or more messages, so long as it is smaller than 1400 bytes.

Creating an ordered protocol

The first thing to do is to turn UDP into an ordered protocol, so that we get messages that are guaranteed to be in order. Here is how.

The sender puts the number in the header of the packet.

Send: 1, 2, 3, 4, 5

On the client side, you may get the packets out of order - but they have the numbers in them, so you can tell whether they're out of order.

Get: 2, 5, 1, 3, 4

Drop the packets that are out of order.

Have: 2, 3, 4

We have turned an unreliable, unordered protocol into an unreliable, ordered protocol. We have, ironically, made a protocol better by making it more unreliable.

Creating a reliable protocol

The next thing we need to do is to create a way to make sure the messages we want to get through, get through. We'll do this using something called an ACK(nowledgment) message. Basically, we'll have the sender of the message keep yelling the message to the recipient until the sender gets a message from the recipient to OKAY OKAY SHUT UP ALREADY I GET THE IDEA.

The message gets put into the sender's "to send" list.

The sender sends the message.

Send: Message ID # 5 - "Are you awake?"

The sender sends the message again.

The sender sends the message yet again!

The sender continues to send this message (i.e. this message remains on the sender's "to send" list) - and possibly others, but it'll keep sending this message...

The recipient gets the message at some point along this cycle.

The recipient goes "okay, okay, I got it!" and sends a message back to the sender saying basically that.

Reply: Message ID # 42 - Re: Message ID #5 - "I'm FINE. Be QUIET."

(Note that this reply may not reach the sender right away. If the sender doesn't hear and keeps obliviously sending data, the recipient keeps ACKing back at the sender to tell it that thank you very much, but it has already gotten the message and no, really, you can stop now.)

Eventually, the sender gets the ACK.

The sender deletes the message (message ID #5) from its "to send" list.

The message has been delivered. Everybody is happy.

In short, the way to make sure your messages get through is to keep on yelling until you hear the other person telling you that they have.

Making an API

Using the notes above and copious amounts of documentation about python sockets, you should be able to make a simple communications API for your games.

Your API might end up looking something like this when it's used.

#### On the server |#### On the client
|
## Initialize everything |## Initialize everything
# create an instance of the server obj. |# pick a server, and create a client instance
server = Server() |picked_server = NiceGuiServerPicker()
|client = Client(picked_server)
|
## update events |# get events
# Pretend that the Server object has |# Pretend that the Client object has
# an attribute called "events," which |# an attribute called "server"
# it keeps track of, modifies, etc. |# and that you can call a method in it
# you could call things such as |# called get_events() which returns
server.del_event(bar) |# a list of events.
server.add_event(foo) |events = Client.server.get_events()
|
## send messages |## send messages
# you can send messages to everyone |# you can only send messages to the server
server.send(packet) |client.send(packet)
# you can also message a specific client|
server.send_to(specific_client, packet) |
|
# And so on... |# And so on...

Keep-alive

Since internet connections (or mesh connections, or whatever) tend to go funky and flicker in and out sometimes, you may want to periodically make sure you're still connected.

An easy way of doing this is to have the client send a ping every 5 seconds or so and require the server to ACK back. If you miss 4 ACKs in a row (somewhere around 20 seconds of no response) you're probably offline and the game should act as such.

HOWEVER, the general OLPC development policy is to not do keep-alives unless the player is active (i.e in the middle of a game, they need messages flying back and forth, etc). in order to conserve resources. What this means is that players staring at a "Game Over" or "Lobby" screen not doing anything in particular should not... be sending keep-alives.

packet structure

You need to specify some sort of structure for the data packets you're passing so that your program will know where to put in and look for what kinds of information. Your packet structure might look something like this:

name of kid / who to send it to

validation something

number of messages in packet (total: n messages)

message 1

message 2

message 3

...

message n

message structure

Your messages also need some sort of structure. It might look like this...

id

action

the actual message

packet/message structure example

Using the example structures for packets and messages above, you could have packets and messages like this:

Making your game

Once your API is done, you can make a game. Presumably you want to make a game with this at some point.

Use MVC

You want to use MVC for sanity's sake, so that your game's logic is abstracted from its UI. In other words, you want to write code like...

g = Game()
# The game logic state is entirely contained within the Game object.
# There is no message handling. There are no graphics.

Client and Server instances

Have separate Game() instances for servers and clients so that you don't have multithreaded nastiness trying to work with the same object simultaneously.

In other words, this is a good thing:

game_on_server = Game()
game_on_client = Game()

This is not.

game_on_server = game_on_client = Game()

Note: Galcon does #2. This makes Phil cry.

Client and server on the same XO

It's a really good idea to have your server code and your client code running in separate threads. This way, you have no special case of a pseudo-server-client user/software - just client software and server software. All players are clients. One user just happens to be simultaneously running the server software - separately - on their computer.

Things you may want to think about someday

Authentication

You may also want to authenticate users. Each XO has a public key (of a private/public key pair). You'll probably want to make note of this, and find out that key, because kids using the XO laptop will usually be hopping through meshes, school servers, routers, etc. and hopping-between/sharing/etc. IP addresses.

You'll want to include an identifier in the packets you send out, as well as some proof that you're the person that you say you are (in other words, your public key). Do this last after it's all working, though.

Broadcast

This refers to one-to-many messages. You have to send broadcast networks to everyone in your network and tell them which games are available.

Timestamps

Can we put timestamps in our messages so we can find out how old they are? Yeah, you could.

Optimizing bandwidth

You often don't need to pass as much information back and forth as you might think. This is great. Bandwidth is precious. For instance, instead of sending huge packets with the state of the entire game each time, you can send the initial state at start-up, and then send diffs thereafter.

Implementation

Coderanger has some code up online, not necessarily related to this document