Improved avatar responsiveness.

This commit is contained in:
CasperW
2009-11-21 15:36:38 +01:00
committed by Melanie
parent 0844e5951c
commit 0149265ee8
6 changed files with 164 additions and 49 deletions

View File

@@ -29,6 +29,7 @@ using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections.Generic;
using log4net;
namespace OpenMetaverse
@@ -52,12 +53,25 @@ namespace OpenMetaverse
/// <summary>Local IP address to bind to in server mode</summary>
protected IPAddress m_localBindAddress;
/// <summary>
/// Standard queue for our outgoing SyncBeginPrioritySend
/// </summary>
private List<UDPPacketBuffer> m_standardQueue = new List<UDPPacketBuffer>();
/// <summary>
/// Prioritised queue for our outgoing SyncBeginPrioritySend
/// </summary>
private List<UDPPacketBuffer> m_priorityQueue = new List<UDPPacketBuffer>();
/// <summary>UDP socket, used in either client or server mode</summary>
private Socket m_udpSocket;
/// <summary>Flag to process packets asynchronously or synchronously</summary>
private bool m_asyncPacketHandling;
/// <summary>Are we currently sending data asynchronously?</summary>
private volatile bool m_sendingData = false;
/// <summary>The all important shutdown flag</summary>
private volatile bool m_shutdownFlag = true;
@@ -246,25 +260,48 @@ namespace OpenMetaverse
}
}
public void SyncBeginSend(UDPPacketBuffer buf)
public void SyncBeginPrioritySend(UDPPacketBuffer buf, int Priority)
{
if (!m_shutdownFlag)
{
try
if (!m_sendingData)
{
m_udpSocket.SendTo(
buf.Data,
0,
buf.DataLength,
SocketFlags.None,
buf.RemoteEndPoint);
m_sendingData = true;
try
{
AsyncBeginSend(buf);
}
catch (SocketException) { }
catch (ObjectDisposedException) { }
}
else
{
if (Priority == 2)
{
lock (m_priorityQueue)
{
m_priorityQueue.Add(buf);
}
}
else
{
lock (m_standardQueue)
{
if (Priority != 0)
{
m_standardQueue.Insert(0, buf);
}
else
{
m_standardQueue.Add(buf);
}
}
}
}
catch (SocketException) { }
catch (ObjectDisposedException) { }
}
}
public void AsyncBeginSend(UDPPacketBuffer buf)
private void AsyncBeginSend(UDPPacketBuffer buf)
{
if (!m_shutdownFlag)
{
@@ -288,8 +325,36 @@ namespace OpenMetaverse
{
try
{
// UDPPacketBuffer buf = (UDPPacketBuffer)result.AsyncState;
m_udpSocket.EndSendTo(result);
if (m_sendingData)
{
lock (m_priorityQueue)
{
if (m_priorityQueue.Count > 0)
{
UDPPacketBuffer buf = m_priorityQueue[0];
m_priorityQueue.RemoveAt(0);
AsyncBeginSend(buf);
}
else
{
lock (m_standardQueue)
{
if (m_standardQueue.Count > 0)
{
UDPPacketBuffer buf = m_standardQueue[0];
m_standardQueue.RemoveAt(0);
AsyncBeginSend(buf);
}
else
{
m_sendingData = false;
}
}
}
}
}
}
catch (SocketException) { }
catch (ObjectDisposedException) { }