RaydiumWikiNi

ApiNetworkIntro

PagePrincipale :: DerniersChangements :: ParametresUtilisateur :: Vous êtes ec2-3-144-36-141.us-east-2.compute.amazonaws.com

28.1 Introduction to network.c


Raydium supports networking via UDP/IP, providing high level functions for multiplayer game development.

Raydium servers are limited to 256 clients for now.

You will find in network.c a set of functions and vars dedicated to networked games: players names, event callbacks, UDP sockets, broadcasts, ... All this is ready to use. As it's not done in the introduction of this guide, We will explain here some variables defined in index.c file.

1. define RAYDIUM_NETWORK_PORT 29104

1. define RAYDIUM_NETWORK_PACKET_SIZE 230

1. define RAYDIUM_NETWORK_TIMEOUT 5

1. define RAYDIUM_NETWORK_PACKET_OFFSET 4

1. define RAYDIUM_NETWORK_MAX_CLIENTS 8

1. define RAYDIUM_NETWORK_MODE_NONE 0

1. define RAYDIUM_NETWORK_MODE_CLIENT 1

1. define RAYDIUM_NETWORK_MODE_SERVER 2

Here, we can find network port declaration (Raydium will use only one port, allowing easy forwarding if needed), default timeout (unit: second), and the three mode possible for a Raydium application (none no network is used?, client and server).

But there is also two other very important defines: packet size (unit: byte) and max number of clients.. This is important because Raydium uses UDP sockets, and UDP sockets required fixed length packets, and as you need to set packet size as small as possible (for obvious speed reasons), you must calculate you maximum information packet size (players position, for example), multiply it by RAYDIUM_NETWORK_MAX_CLIENTS,and add RAYDIUM_NETWORK_PACKET_OFFSET wich represent the required header of the packet.

It's more easy than it seems, look:

My game will support 8 players.

I will send players state with 3 floats (x,y,z).

My packet size must be: 8*3*sizeof(float)+RAYDIUM_NETWORK_PACKET_OFFSET = 100 bytes.

Please, do not change packet offset size, since Raydium will use it for packet header.

1. define RAYDIUM_NETWORK_DATA_OK 1

1. define RAYDIUM_NETWORK_DATA_NONE 0

1. define RAYDIUM_NETWORK_DATA_ERROR -1

This three defines are used as network functions result:

if(raydium_network_read_flushed(&id,&type,buff)==RAYDIUM_NETWORK_DATA_OK)

{

...?

1. define RAYDIUM_NETWORK_PACKET_BASE 10

In most network functions, you will find a ``type argument, used to determine packet ``goal. This type is 8 bits long (256 possible values), but Raydium already uses some of them. So you can use RAYDIUM_NETWORK_PACKET_BASE as a base for your own types:

1. define NORMAL_DATA RAYDIUM_NETWORK_PACKET_BASE

1. define BALL_TAKEN (DATA+1)

1. define SCORE_INFO (DATA+2)

1. define HORN (DATA+3)

...?

Your own player id (0<= id <= RAYDIUM_NETWORK_MAX_CLIENTS). Read only:

int raydium_network_uid;

Current network mode (none, client, server). Read only:

char raydium_network_mode;

Boolean used to determine client state (connected or not), read only:

char raydium_network_clientRAYDIUM_NETWORK_MAX_CLIENTS?;

example: if(raydium_network_client[4]) draw_player(4);

Can be used by a server to send data to his clients. Read only:

struct sockaddr raydium_network_client_addrRAYDIUM_NETWORK_MAX_CLIENTS?;

Players names, read only:

char raydium_network_nameRAYDIUM_NETWORK_MAX_CLIENTS?RAYDIUM_MAX_NAME_LEN?;

OnConnect?? and OnDisconnect?? events (server only):

void * raydium_network_on_connect;

void * raydium_network_on_disconnect;

You can place your owns callbacks void(int)? on these events, as in this example:

void new_client(int client)

{

raydium_log(``New player: %s'', raydium_network_nameclient?);

}

...?

int main(int argc, char **argv)

{

...?

raydium_network_on_connect=new_client;

...?





Return to RaydiumApiReference index.