Janus Lidgren Modifications
From EQUIS Lab Wiki
Janus and Lidgren both run a separate thread for network communication. To improve the efficiency of the toolkit these two thread were combined in the TimeLineNetwork class.
Note: in the TimeLineServer, Lidgren.Network.dll is used in its original form. The modified version Lidgren.Network.Janus.dll is used by the TimeLineNetwork class in the Janus project.
The following is a summary of the modification that were made to Lidgren.Network for the Janus toolkit:
Make a copy of the Lidgren.Network folder and rename it to Lidgren.Network.Janus.
Inside this folder, rename Lidgren.Network.csproj to Lidgren.Network.Janus.csproj.
Open Lidgren.Network.Janus.csproj
Modify the project properties as follows:
- Change the AssemblyTitle to Lidgren.Network.Janus
- Change the AssemblyProduct to Lidgren.Network.Janus
- Change the AssemblyName to Lidgren.Network.Janus
- Leave the default namespace Lidgren.Network
There are two C# files that require minor changes:
- NetPeer.cs
- NetPeerInternal.cs
NetPeer.cs
Add a new method StartFromJanus. This method is identical to the Start method except that the line m_networkThread.Start(); is commented out.
/// <summary> /// Binds to socket and does not start thread as the thread is run from Janus /// </summary> public void StartFromJanus() { if (m_status != NetPeerStatus.NotRunning) { // already running! Just ignore... LogWarning("Start() called on already running NetPeer - ignoring."); return; } m_status = NetPeerStatus.Starting; // fix network thread name if (m_configuration.NetworkThreadName == "Lidgren network thread") { int pc = Interlocked.Increment(ref s_initializedPeersCount); m_configuration.NetworkThreadName = "Lidgren network thread " + pc.ToString(); } InitializeNetwork(); // start network thread m_networkThread = new Thread(new ThreadStart(NetworkLoop)); m_networkThread.Name = m_configuration.NetworkThreadName; m_networkThread.IsBackground = true; // m_networkThread.Start(); // send upnp discovery if (m_upnp != null) m_upnp.Discover(this); // allow some time for network thread to start up in case they call Connect() or UPnP calls immediately Thread.Sleep(50); }
NetPeerInternal.cs
Three members of this class must be made public so that they can be accessed from Janus:
- public NetPeerStatus m_status;
- public void ExecutePeerShutdown()
- public void Heartbeat()
Finally, the exception thrown in VerifyNetworkThread() must be commented out as shown below
internal void VerifyNetworkThread() { Thread ct = Thread.CurrentThread; // if (Thread.CurrentThread != m_networkThread) // throw new NetException("Executing on wrong thread! Should be library system thread (is " + ct.Name + " mId " + ct.ManagedThreadId + ")"); }
NetPeerConfiguration.cs
For both Lidgren.Network and Lidgren.Network.Janus, increase the size of the send and receive buffers. The original size was 131071. So far testing indicates that 1310710 is sufficient. However this may need to be larger for high bandwidth applications or if there is significant variation in the latency.
m_receiveBufferSize = 1310710; m_sendBufferSize = 1310710;
After these changes have been made rebuild the Lidgren.Network.Janus project and copy Lidgren.Network.Janus.dll to the JanusToolkit Janus folder.