Update NetworkManager.cs

adds extra checks for null and improves SendPacket, also adds a new function: NetworkInvaildWarning to handle passing to the log as debug level.

why: In some cases the network can disconnect from the sim in a weird way so the client trys to send data out but the outbox / inbox are gone. these extra checks
will help stop it from outright killing itself.
This commit is contained in:
Madpeterz 2020-12-19 23:44:24 +00:00
parent c7826595cd
commit bdab4f14a0

View file

@ -348,6 +348,8 @@ namespace OpenMetaverse
private Timer DisconnectTimer;
private bool connected;
private long lastpacketwarning = 0;
/// <summary>
/// Default constructor
/// </summary>
@ -450,26 +452,7 @@ namespace OpenMetaverse
/// <param name="packet">Packet to send</param>
public void SendPacket(Packet packet)
{
// try CurrentSim, however directly after login this will
// be null, so if it is, we'll try to find the first simulator
// we're connected to in order to send the packet.
Simulator simulator = CurrentSim;
if (simulator == null && Client.Network.Simulators.Count >= 1)
{
Logger.DebugLog("CurrentSim object was null, using first found connected simulator", Client);
simulator = Client.Network.Simulators[0];
}
if (simulator != null && simulator.Connected)
{
simulator.SendPacket(packet);
}
else
{
//throw new NotConnectedException("Packet received before simulator packet processing threads running, make certain you are completely logged in");
Logger.Log("Packet received before simulator packet processing threads running, make certain you are completely logged in.", Helpers.LogLevel.Error);
}
SendPacket(packet, CurrentSim);
}
/// <summary>
@ -479,13 +462,18 @@ namespace OpenMetaverse
/// <param name="simulator">Simulator to send the packet to</param>
public void SendPacket(Packet packet, Simulator simulator)
{
if (simulator == null && Client.Network.Simulators.Count >= 1)
{
Logger.DebugLog("simulator object was null, using first found connected simulator", Client);
simulator = Client.Network.Simulators[0];
}
if (simulator != null)
{
simulator.SendPacket(packet);
}
else
{
Logger.Log("Packet received before simulator packet processing threads running, make certain you are completely logged in", Helpers.LogLevel.Error);
NetworkInvaildWarning("simulator", "SendPacket");
}
}
@ -494,20 +482,52 @@ namespace OpenMetaverse
/// </summary>
/// <param name="packet">Incoming packet to process</param>
public void EnqueueIncoming(IncomingPacket packet)
{
if (_packetInbox != null)
{
if (_packetInbox.Writer.TryWrite(packet))
Interlocked.Increment(ref _packetInboxCount);
}
else
{
NetworkInvaildWarning("_packetInbox", "EnqueueIncoming");
}
}
/// <summary>
/// adds a debug message when you try to access a network item
/// while they are still null
/// </summary>
/// <param name="source">what</param>
/// <param name="function">where</param>
protected void NetworkInvaildWarning(string source,string function)
{
long now = DateTimeOffset.Now.ToUnixTimeSeconds();
long dif = lastpacketwarning - now;
if (dif > 10)
{
lastpacketwarning = now;
Logger.Log(source+" is null (Are we disconnected?) - from: "+ function,
Helpers.LogLevel.Debug);
}
}
/// <summary>
/// Add a packet to the Inbox queue to process
/// </summary>
/// <param name="packet">Incoming packet to process</param>
public void EnqueueOutgoing(OutgoingPacket packet)
{
if (_packetOutbox != null)
{
if (_packetOutbox.Writer.TryWrite(packet))
Interlocked.Increment(ref _packetOutboxCount);
}
else
{
NetworkInvaildWarning("_packetOutbox", "EnqueueOutgoing");
}
}
/// <summary>
/// Connect to a simulator
@ -774,7 +794,7 @@ namespace OpenMetaverse
}
else
{
Logger.Log("DisconnectSim() called with a null Simulator reference", Helpers.LogLevel.Warning, Client);
NetworkInvaildWarning("simulator", "DisconnectSim");
}
}
@ -895,6 +915,8 @@ namespace OpenMetaverse
}
private async Task OutgoingPacketHandler()
{
if (_packetOutbox != null)
{
var reader = _packetOutbox.Reader;
@ -921,8 +943,16 @@ namespace OpenMetaverse
}
}
}
else
{
NetworkInvaildWarning("_packetOutbox", "OutgoingPacketHandler");
}
}
private async Task IncomingPacketHandler()
{
if (_packetInbox != null)
{
var reader = _packetInbox.Reader;
@ -950,6 +980,11 @@ namespace OpenMetaverse
}
}
}
else
{
NetworkInvaildWarning("_packetInbox", "IncomingPacketHandler");
}
}
private void SetCurrentSim(Simulator simulator, string seedcaps)
{