shown in other instead.
Network Principles of Operation
The principles of network module operation is as follows:
- At the low level, a 3rd party network library RakNet 4.04 is used.
- The network functionality is provided by a plugin library Network that extends the standard set of functions available in UnigineScript. It can be found under <UnigineSDK>/lib folder. (See how to load it).
Inside, the Network plugin consists of the following modules:
- Network — the main logic of Network plugin.
- NetworkNode — basic network functions, such as opening and closing network connection, sending and receiving network packets.
- NetworkInterperter — export of plugin functions into UnigineScript.
Handling Network Messages
The following diagram shows how network messages are handled in Network classes.
Network messages are handled in two stages:
- Update is for the initial, low-level handling of received network packets. If some of the received packets require a high-level processing (i.e. passing them to the script), they form network messages, which are placed in a queue.
1.1. The current time is checked. Network packets are received at regular intervals (by default it is 30 times per second). For that, the time passed since the last update of NetworkNode is calculated. 1.2. If the update time came, NetworkNode::update function is called. 1.3. Packets with network messages are get from RakNet. 1.4. RakNet packets are parsed, a packet type is determined and if a packet message is relevant, it is passed to NetworkNode. 1.5. If necessary, instances of NetworkEvent are generated and placed in a queue. Network events are network messages that the application needs to be notified about (see the details). 1.6. sleep() function is called to let other processes to be executed on the CPU.
- Sync is to call script handlers for network messages. (Handlers need to be registered in the script first).
2.1. NetworkEvent are taken from the queue one by one. 2.2. Handlers for these network events are executed, if registered.
Adding Custom Network Code
By initialization of the network module, NETWORK extern define is exported. This define can be used in the script code to check whether network module is used at the moment.
#ifdef NETWORK // do something network specific here #else // do something single-player specific here #endif
Examples of network plugin usage can be found in <UnigineSDK>/data/network directory.
See also a step-by-step example how to add Network functionality to your project.