# Comet Play protocol, version 1 How a game talks to a Comet Play server. Two connections: - **TCP**, lines of text: signing in, the lobby, tables, chat, and game data that must arrive (moves, turns). Port 7778 with TLS, or 7777 without. - **UDP**, small binary packets: voice, video and game data that is better late-dropped than late (positions in a race). Port 7777. The server does not know the rules of any game. It seats players at a table and passes what one sends to the others. ## Lines Each message is one line of UTF-8 text ending in a newline (`\n`; a `\r` before it is ignored). A line is at most 4096 bytes. Words are separated by single spaces. The last part of a line may contain spaces if it starts with ` :` (space, colon): everything after the colon, exactly as sent, is one value. SAY :good luck, have fun OPEN racing 4 :Friday laps Commands are upper case. To every command but `SAY` and `DATA` the server answers with one line, after any lines the answer consists of: OK [words...] [:text] ERR : `SAY` and `DATA` are answered only when they fail. Other lines (events) can arrive at any time, between a command and its answer too; tell them apart by their first word. When it connects, a client receives COMETPLAY 1 : and has 30 seconds to sign in. After that it must send something at least every 120 seconds (`PING` will do) or it is dropped. A client that is being dropped is sent `BYE :` first. Names are 2 to 16 letters, digits, `-` and `_`, starting with a letter; `Ann` and `ann` are the same account. Passwords are 6 to 128 characters. ## Signing in | Client sends | Server answers | |---|---| | `REGISTER :` | `OK REGISTER `: the account is made and signed in | | `LOGIN :` | `OK LOGIN ` | | `GAMES` | one `GAME :` per game, then `OK GAMES `. Allowed before signing in | | `PING [:text]` | `PONG [:text]` | | `QUIT` | `BYE :goodbye` | `` is 16 hex digits: this connection's key for UDP (below). `` is 1 if players may join after the game has started. `MOTD :` may follow a sign-in. Signing in to an account that is connected already ends the older connection. Error codes: `invalid` (name or password not allowed), `taken`, `closed` (no new accounts), `denied` (wrong name or password), `banned`, `wait` (too many tries; later), `full` (server full). ## The lobby A signed-in player who is not at a table is in the lobby and receives its events. Ask for the whole picture with `LOBBY` after signing in and again after leaving a table (events are not sent to players at tables). | Client sends | Server answers | |---|---| | `LOBBY` | a `PLAYER` line per player and a `TABLE` line per table, then `OK LOBBY ` | | `SEEK [,...] [:note]` | `OK SEEK `: says which games you want to play; replaces what you said before | | `SEEK -` | `OK SEEK -`: you are no longer asking | | `SAY :` | nothing; everyone in the lobby, you included, gets the `CHAT` line | | `TELL :` | `OK TELL `; only that player gets it | Events: PLAYER : someone arrived or changed GONE someone left the server TABLE : CLOSED <id> a table emptied and is gone CHAT lobby <name> :<text> CHAT tell <name> :<text> a private message to you INVITE <name> <table> <game> asks you to join a table Sitting down at a table clears what you were seeking. ## Tables A table is one game's worth of seats, numbered from 0. Whoever opens it is its host; if the host leaves, the player in the lowest seat becomes host. A table closes when its last player leaves. | Client sends | Server answers | |---|---| | `OPEN <game> [seats] [:title]` | `OK OPEN <table> <seat> <game> <state> <seats> :<title>`. `seats` may be anything the game allows; left out, it is the game's most | | `JOIN <table>` | `OK JOIN <table> <seat> <game> <state> <seats> :<title>` | | `LEAVE` | `OK LEAVE`; you are back in the lobby | | `INVITE <name>` | `OK INVITE <name>`; the player (who must be in the lobby) gets `INVITE` | | `START` (host) | `OK START`; everyone gets `STARTED` | | `END` (host) | `OK END`; everyone gets `ENDED`, the table is open again | | `KICK <seat>` (host) | `OK KICK <seat>`; that player gets `KICKED <table>` and is in the lobby | | `SAY :<text>` | nothing; everyone at the table gets `CHAT table <name> :<text>` | | `DATA * :<payload>` | nothing; every other player at the table gets `DATA <your seat> :<payload>` | | `DATA <seat> :<payload>` | nothing; only that seat gets it | Straight after `OK OPEN` or `OK JOIN` come a `SEAT` line for everyone at the table (you too), a `HOST` line, and `STARTED` if the game is under way. Events at a table: SEAT <seat> <name> someone sat down LEFT <seat> <name> :<why> someone left or lost their connection HOST <seat> who the host is now STARTED <seed> the game has begun ENDED the game is over; the table is open again DATA <seat> :<payload> game data from that seat CHAT table <name> :<text> `<seed>` is a number below 2^32, the same for everyone at the table: seed a random-number generator with it and every player shuffles the same deck. A `DATA` payload is whatever the game likes, as text: `e2e4`, `lap 3 41.207`, or base64 when it has bytes to send. It arrives in the order sent and exactly as sent. Chat, notes and titles are different: the server strips control characters and cuts them at 300 characters (60 for a title). Error codes: `state` (not possible where you are), `game` (no such game), `seats` (the game does not allow that many), `absent` (no such table, seat or player), `full`, `playing` (started, and the game does not let players join late), `host` (only the host may), `players` (too few to start), `busy`, `slow` (talking too fast), `usage`. ## UDP: voice, video, fast game data After signing in, a client may send datagrams to `<udp-port>` on the server. Each starts with a 12-byte header: byte 0-1 'C' 'P' byte 2 1 (the version) byte 3 the channel byte 4-11 the token, as 8 bytes (the 16 hex digits of the sign-in answer) byte 12- the payload, at most 1400 bytes What the server sends has a 6-byte header: byte 0-1 'C' 'P' byte 2 1 byte 3 the channel byte 4 the seat it came from (255: the server) byte 5 0 byte 6- the payload | Channel | For | The server | |---|---|---| | 0 | hello | sends the first 64 bytes of the payload back to you | | 1 | voice | passes it to everyone else at your table | | 2 | video | the same | | 3 | game data | the same | Send a hello first and again every 15 seconds or so: it tells the server where to send your table's packets (and keeps a home router's door open), and the answer tells you UDP gets through. If no answer comes, fall back to `DATA` lines and do without voice. The server passes payloads on untouched and in no promised order; packets can be lost. Put a sequence number in your own payload. A player may send 256 KiB a second (the server's setting); more is dropped. The token is a secret: use the TLS port so nobody can read it on the way. ### Voice, as games are asked to send it So that every game can hear every other, voice payloads are: byte 0-1 sequence number, low byte first, one more for each packet byte 2 format: 1 byte 3- sound Format 1 is 20 milliseconds of 16 kHz, one channel, 16-bit signed samples, low byte first: 640 bytes, 50 packets a second, 32 KiB a second for each person talking. To play it on Comet OS, repeat each sample three times on both channels (48 kHz stereo) and write it to an audio stream. Send nothing while the player is silent. ## The web side For web pages and tools, the same picture as JSON over HTTP(S): GET /api/lobby players (name, table, seeking, note) and tables (game, state, seats, host, title) GET /api/games the games and how many can play each GET /api/status version, uptime and counters ## An example S: COMETPLAY 1 :Comet Play C: LOGIN Ann :correct horse S: OK LOGIN Ann 7777 9f3c2a1b00e4d571 C: LOBBY S: PLAYER Ann 0 - : S: PLAYER Ben 0 chess :anyone? S: OK LOBBY 2 0 C: OPEN chess S: OK OPEN 1 0 chess open 2 : S: SEAT 0 Ann S: HOST 0 C: INVITE Ben S: OK INVITE Ben S: SEAT 1 Ben C: START S: OK START S: STARTED 2914402113 C: DATA * :e2e4 S: DATA 1 :e7e5 S: CHAT table Ben :nice opening