We should create proper demo(s) for all this, and some tutorials, ... As a lot of other things
To make it simple, here's a short explanation about Raydium network API:
- You want to send an event ? Use a
netcall. Basically, it's a simple RPC (remote procedure call), allowing you to call, from a client, a function on the server. This function can pass a buffer of data, with whatever-you-want-until-it's-too-big in it. You can then use the server to
broadcast this packet over all clients, if you want, or send it to a particular client.
- You want to have a "state" synchronized over all clients ? Use
propags. It allows you to have some local data, and to make sure that every client on the network have the same. Each time a client is modifying this data, he must call raydium_network_propag_refresh() to update this data on all other clients.
If you look carefully at tests.c, it's a very simple program. Here's how it works:
A function, broad(), is defined to send a packet to all other clients. In facts, this function does nothing else than calling raydium_network_broadcast(). It has even the same signature ...
Then, tests creates it's main loop, where the only thing it did is reading the network. Every packet received is given to broad() ... so every client receive it (including the sender itself, note it). In almost every cases, the ONLY data here is ODENet data : element positions, creation, ... Nothing else: Raydium deals with netcalls and propags internally.
Speaking of which ... you can see netcalls registering in the main() of tests. Again, the idea is simple: if the server receive a netcall from a client ... it does nothing else that spreading it to all clients. And that's it.
So, to create a very basic "chat" with Raydium, here's a guideline: (it's not some real code, but close)
On the clients:
Code:
// 10, or 11, or 30 ... anything you want over 0 and under 127
#define NETCALL_CHAT (RAYDIUM_NETWORK_PACKET_BASE+10)
// we'll call this when we want to send some text to other clients ...
void chat_send(char *txt)
{
char buff[RAYDIUM_NETWORK_PACKET_SIZE];
// let's create the packet ... we only have offset our data so
// the header can be created by network_write() ...
strcpy(buff+RAYDIUM_NETWORK_PACKET_OFFSET],txt); // strNcpy is needed here ! Don't mess with network ...
raydium_network_write(NULL,raydium_network_uid,NETCALL_CHAT,buff);
}
// we do want we want with received text ...
void chat_get(char *txt)
{
printf("message: %s\n");
}
// will be called "by the network"
void netcall_chat_get(int type, char *buff)
{
// just get rid of the packet header:
chat_get(buff+RAYDIUM_NETWORK_PACKET_OFFSET);
}
int main(...)
{
...
// let's register our "network function" (netcall)
raydium_network_netcall_add(netcall_chat_get,NETCALL_CHAT,1);
// connect to the server ...
if(!raydium_network_client_connect_to("foobar.org"))
// ...
}
... that's it. The server will look like:
Code:
#define RAYDIUM_NETWORK_ONLY
#include "raydium/index.c"
// an C header could be better ... :)
#define NETCALL_CHAT (RAYDIUM_NETWORK_PACKET_BASE+10)
char str[RAYDIUM_NETWORK_PACKET_SIZE];
void loop(void)
{
signed char type;
int id;
if(raydium_network_read_flushed(&id, &type, str)==RAYDIUM_NETWORK_DATA_OK)
{
// will never be called until using ODENet ...
raydium_network_broadcast(type,str);
}
}
int main(int argc, char **argv)
{
raydium_network_only_init(argc,argv);
raydium_network_netcall_add(raydium_network_broadcast,NETCALL_CHAT,1);
do{
loop();
// a small delay (like usleep(1) for Linux) is needed here,
// or you'll overburn CPU and make network stack turn crazy
}while(1);
}
I write this "from" head, so I didn't check anything, but you've a quite complete view of this API here. Let me know what's not crystal clear here
Warning: if the client only do "chatting", you'll quickly end up with timeouts (see RAYDIUM_NETWORK_TIMEOUT)