NETWORKING ELEMENTS IN uLINK

NETWORKING ELEMENTS IN uLINK

uLink supports everything discussed in the Networking Concepts section. Server creation and client connection, sharing data between connected clients, determining which player controls which objects, and punching through network configuration variations are all supported out of the box. This page will walk you through the uLink-specific implementation of these networking practices.

UDP

uLink is based on UDP network traffic only. uLink never uses TCP, as UDP is the fastest option in our opinion. TCP has built-in mechanism for reducing the network traffic speed when network packets are lost, while UDP has no speed reduction. However, unlike TCP, UDP has no built-in reliability, but uLink itself keeps track of packet numbers internally and can guarantee reliability and in-order transmission (if that is needed in the game).

Communicating using Network Views

The uLinkNetworkView is a component that sends and receives data across the network. The uLinkNetworkView components make your GameObject capable of sending data using RPC calls and state synchronization. The way you use uLinkNetworkView will determine how your game's networking behaviors will work. Network views in uLink have few options, but they are incredibly important for your networked game.
For more information on using uLink's network views, please read the Network View section of this manual.

Remote procedure calls

Remote procedure calls (RPCs) are functions declared in scripts that are attached to a GameObject that contains a uLinkNetworkView. The RPC function can be called from any script within a GameObject that has a uLinkNetworkView component.
For more information on using RPCs in uLink, please read the RPC Details section.

State synchronization

State synchronization is the continual sharing of data across all game clients. This way a player's position can be synchronized with all clients, making it appear as if it's controlled locally when data is actually being delivered over a network. To synchronize the state within a GameObject you just need to add a uLinkNetworkView to that object and tell it what to observe. The observed data is then synchronized to all clients in the game.
For more information on using state synchronization in uLink, please read the State Synchronization section.

The uLink.Network.Instantiate() method

uLink.Network.Instantiate() lets you instantiate a prefab on all clients in a natural and easy way. Essentially this is a normal Instantiate() call in Unity, but it performs the instantiation on all clients and on the server.
Internally uLink.Network.Instantiate() is simply a buffered RPC call which is executed on all clients (also locally). It allocates a unique uLink.NetworkViewID and assigns it to the instantiated prefab which makes sure it synchronizes across all clients correctly.
For more information, please read the Network Instantiate section.

Master server

The master server helps you match games. When you start a server you connect to the master server, and it provides a list of all the active servers.
The master server is a meeting place for game servers and clients where servers are listed and compatible clients can connect to running games. This prevents the need for fiddling with IP addresses or hostnames for all parties involved. It can even help users to host games without them having to reconfigure their routers where, under normal circumstances, that would be required. It can help clients bypass the server's firewall and get to private IP addresses which are normally not accessible through the public internet. This is done with help from a facilitator which facilitates connection establishment.
For more information, please read the Master Server section.

Network message callbacks

To make it easy to build both clients and servers in the same Unity project uLink provides several network message callbacks that are easy to use by implementing them in your scripts.
Below is a list of message callbacks handling uLink network connections between clients and servers. The list is an example of convenient message callbacks for handling normal network connections and failing network connections.
void uLink_OnPlayerConnected(uLink.NetworkPlayer player); void uLink_OnConnectedToServer(System.Net.IPEndPoint server); void uLink_OnPlayerDisconnected(uLink.NetworkPlayer player); void uLink_OnDisconnectedFromServer(uLink.NetworkDisconnection mode); void uLink_OnFailedToConnect(uLink.NetworkConnectionError error);
As you can see these callbacks cover some error situations. They are convenient to have when there is a network issue. For example, a game client can get a power failure at any time and then uLink will timeout this client's connection on the server side and send the message callback uLink_OnPlayerDisconnected() on the server. If you want to do custom coding in this situation (like saving the position of the player's avatar to the database in an MMORPG game) it is easy to implement this custom code in a script on the server.
There are many more message callbacks to use in uLink, a list of all of them is provided in Appendix A in this manual. All of them start with the prefix uLink_, that is how to separate them from other message callbacks in Unity. Code examples for the most common uLink message callbacks is provided in the API documentation for the Network class.

Use uLink classes only

Since uLink replaces all network classes in the built-in Unity network library it is important not to use the Unity classes by mistake. To make sure you are using uLink classes only, write the namespace in front of all uLink classes like this example:
uLink.Network.sendRate = 15;
But it is easy to sometimes forget to write the namespace uLink and the result is frustrating bugs at runtime when testing because you are using the Unity built-in classes by mistake and the error messages can be very strange and misguiding. Avoid this problem and save lots of time by using the compiler as a helper.
In C# scripts with network code, make sure you have both these code lines:
using uLink; using UnityEngine;
This will make the compiler in the Unity editor indicate an "ambiguous reference" error whenever you forget the uLink namespace in front of a class.

Debugging network games

uLink comes with several facilities to help you debug your networked game.
  1. Using the Hierarchy View in Unity effectively you can track object creation and inspect them.
  2. You can launch Unity two times on the same machine, and open different projects in each (just make identical copies of the project folder). On Windows, this can be done by just launching another Unity instance and opening the project from the project wizard. On Mac OS X, multiple Unity instances can be opened from the terminal, and a -projectPath argument can be specified:
    /Applications/Unity/Unity.app/Contents/MacOS/Unity
    -projectPath "/Users/MyUser/MyProjectFolder/"
    /Applications/Unity/Unity.app/Contents/MacOS/Unity
    -projectPath "/Users/MyUser/MyOtherProjectFolder/"
Make sure you don't accidentally use Unity's built-in networking API or components.
  • Make sure all network-related API calls and references includes the uLink namespace.
  • Make sure any network message callback has the uLink_ prefix.
  • Make sure all network script components like uLinkNetworkView, used in game objects and prefabs, start with uLink.
  • Use the utility script uLinkStatisticsGUI to get visual information about bandwidth usage and more.
  • Read the log file for the standalone builds you do. Network errors will be logged there. The log file is named output_log.txt for standalone builds. Read more in the Server Operations manual chapter about this log file.