DEFINE(Armagetron Advanced, Armagetron)

Introduction | Layer One | Layer Two | Layer Three

# Layer one: protocol abstraction

Skip this layer if you really want to use Armagetron's networking system fast; it contains details you do not need to know from the start.

This layer is responsible for sending raw data packets to other computers. The files `net_anet.h,net_sysdep.C,net_udp.C` and `net_wins.C` are responsible for it; they are written in pure C. `net_anet.h` is the header file with the declarations visible to layer two, and `net_systep.C` just includes the real implementation from `net_udp.C` or `net_wins.C` depending on the system Armagetron is compiled for.

## Data structures

#### `struct sockaddr:`

Everything the network protocol needs to identify a connection to another computer. In the case of UDP, it contains the IP address and port number.

#### `Sockets:`

represented by integers: in analogy to file descriptors, all network IO is done through sockets. Every client has one socket where all the messages to/from the server are sent/received through. The server has one listening socket for the messages from new clients, and one IO socket for each client. (With the UDP protocol, all these sockets are really identical, as there is no such thing as a connection and no need/possibility to assign a connection to the sockets.)

## Function list

(only the functions layer two actually uses are mentioned)

#### `Socket ANET_Init();`

Initialises the network system, returns the control socket. All future IO is done through that socket.

#### `void ANET_Shutdown (void);`

Closes the network system.

#### `void ANET_Listen(bool state);`

If state is true, the listening socket is initialised (listening at the port `net_hostport=4532`) and can be queried by `ANET_CheckNewConnections`.

#### `Socket ANET_CheckNewConnections (void);`

On the server, this checks if there are any new connections from clients wanting to join the game. Returns a socket ready to receive the newcomer if there is one, or -1 if not. In case of the UDP protocol, even the messages from the known clients are caught by this function (as there is no way to distinguish new and old connections with UDP), so layer two needs to do some extra checks with this function.

#### `int ANET_GetAddrFromName (const char *name, struct sockaddr *addr);`

Fills `*addr` with the information necessary to reach the server `name` (containing the IP address in numerical notation or the hostname); the port number is set to `net_hostport=4532` to match the port the server listens on.

#### `ANET_Connect(Socket s,struct sockaddr *addr);`

Opens a connection to the computer given by `addr` through the socket `s`. As UDP is a connectionless protocol, this does not do anything currently.

#### `int ANET_Write (Socket sock, const byte *buf, int len, struct sockaddr *addr);`

Sends `len` bytes of data from `buf` through the socket `sock` to the peer identified with `*addr`. A connection has to be established before with `ANET_Connect()`.

#### `int ANET_Read (Socket sock, byte *buf, int len, struct sockaddr *addr);`

Reads up to `len` bytes from the connection associated to socket `sock` and stores them in the buffer `buf`. (If a connectionless protocol like UDP is used and the socket is a listening socket, this can mean ANY data coming in at the port the socket listens on...) The sender's address is stored in `*addr`, the number of actually read bytes is returned (Zero means no data was received.)

#### `char *ANET_AddrToString (const struct sockaddr *addr);`

Returns the information from `addr` in human readable form.

#### `int ANET_AddrCompare (struct sockaddr *addr1, struct sockaddr *addr2);`

Checks whether `*addr1` and `*addr2` are the same computer (ignoring the port number). If they are, 0 is returned, -1 if not.

#### `int ANET_GetSocketPort (struct sockaddr *addr)`

Returns the port number from `*addr`.

This Page was created by Manuel Moos(
).

Last modification: Fri Apr 17 18:01:08 UTC 2009

Introduction | Layer One | Layer Two | Layer Three