parallel delta queue processing

thread safe packet construction, compilation, compression, gump compilation, sending, coalescing, and many other fixes
TODO:  delta queue recursion fixups, ipooledenumerable fixups/generics
This commit is contained in:
Mark Sturgill 2013-10-11 00:01:05 -07:00
parent 9404651774
commit ddcc4e7a20
16 changed files with 619 additions and 575 deletions

View file

@ -1315,7 +1315,8 @@ namespace Server.Items
int inPack = 1; int inPack = 1;
foreach ( Mobile m in defender.GetMobilesInRange( 1 ) ) IPooledEnumerable eable = defender.GetMobilesInRange( 1 );
foreach ( Mobile m in eable )
{ {
if ( m != attacker && m is BaseCreature ) if ( m != attacker && m is BaseCreature )
{ {
@ -1333,6 +1334,7 @@ namespace Server.Items
++inPack; ++inPack;
} }
} }
eable.Free();
if ( inPack >= 5 ) if ( inPack >= 5 )
return 100; return 100;
@ -1365,7 +1367,8 @@ namespace Server.Items
{ {
Clone bc; Clone bc;
foreach ( Mobile m in defender.GetMobilesInRange( 4 ) ) IPooledEnumerable eable = defender.GetMobilesInRange( 4 );
foreach ( Mobile m in eable)
{ {
bc = m as Clone; bc = m as Clone;
@ -1384,6 +1387,7 @@ namespace Server.Items
break; break;
} }
} }
eable.Free();
} }
PlaySwingAnimation( attacker ); PlaySwingAnimation( attacker );
@ -1912,11 +1916,13 @@ namespace Server.Items
int range = Core.ML ? 5 : 10; int range = Core.ML ? 5 : 10;
foreach ( Mobile m in from.GetMobilesInRange( range ) ) IPooledEnumerable eable = from.GetMobilesInRange(range);
foreach ( Mobile m in eable )
{ {
if ( from != m && defender != m && SpellHelper.ValidIndirectTarget( from, m ) && from.CanBeHarmful( m, false ) && ( !Core.ML || from.InLOS( m ) ) ) if ( from != m && defender != m && SpellHelper.ValidIndirectTarget( from, m ) && from.CanBeHarmful( m, false ) && ( !Core.ML || from.InLOS( m ) ) )
list.Add( m ); list.Add( m );
} }
eable.Free();
if ( list.Count == 0 ) if ( list.Count == 0 )
return; return;

View file

@ -365,6 +365,8 @@ namespace Server.Misc
} }
} }
/* Must be thread-safe */
public static int MobileNotoriety( Mobile source, Mobile target ) public static int MobileNotoriety( Mobile source, Mobile target )
{ {
if ( Core.AOS && ( target.Blessed || ( target is BaseCreature && ( (BaseCreature)target ).IsInvulnerable ) || target is PlayerVendor || target is TownCrier ) ) if ( Core.AOS && ( target.Blessed || ( target is BaseCreature && ( (BaseCreature)target ).IsInvulnerable ) || target is PlayerVendor || target is TownCrier ) )

View file

@ -2632,7 +2632,8 @@ namespace Server.Mobiles
if (srcSkill <= 0) if (srcSkill <= 0)
return; return;
foreach (Mobile trg in m_Mobile.GetMobilesInRange(m_Mobile.RangePerception)) IPooledEnumerable eable = m_Mobile.GetMobilesInRange(m_Mobile.RangePerception);
foreach (Mobile trg in eable)
{ {
if (trg != m_Mobile && trg.Player && trg.Alive && trg.Hidden && trg.AccessLevel == AccessLevel.Player && m_Mobile.InLOS(trg)) if (trg != m_Mobile && trg.Player && trg.Alive && trg.Hidden && trg.AccessLevel == AccessLevel.Player && m_Mobile.InLOS(trg))
{ {
@ -2655,6 +2656,7 @@ namespace Server.Mobiles
} }
} }
} }
eable.Free();
} }
public virtual void Deactivate() public virtual void Deactivate()

View file

@ -5429,7 +5429,8 @@ namespace Server.Mobiles
{ {
Corpse toRummage = null; Corpse toRummage = null;
foreach ( Item item in this.GetItemsInRange( 2 ) ) IPooledEnumerable eable = this.GetItemsInRange(2);
foreach ( Item item in eable )
{ {
if ( item is Corpse && item.Items.Count > 0 ) if ( item is Corpse && item.Items.Count > 0 )
{ {
@ -5437,6 +5438,7 @@ namespace Server.Mobiles
break; break;
} }
} }
eable.Free();
if ( toRummage == null ) if ( toRummage == null )
return false; return false;

View file

@ -1210,27 +1210,17 @@ namespace Server.Mobiles
m_IsStealthing = false; // IsStealthing should be moved to Server.Mobiles m_IsStealthing = false; // IsStealthing should be moved to Server.Mobiles
} }
[CommandProperty( AccessLevel.GameMaster )] public override void OnHiddenChanged()
public override bool Hidden
{ {
get RemoveBuff(BuffIcon.Invisibility); //Always remove, default to the hiding icon EXCEPT in the invis spell where it's explicitly set
if (!Hidden)
{ {
return base.Hidden; RemoveBuff(BuffIcon.HidingAndOrStealth);
} }
set else// if( !InvisibilitySpell.HasTimer( this ) )
{ {
base.Hidden = value; BuffInfo.AddBuff(this, new BuffInfo(BuffIcon.HidingAndOrStealth, 1075655)); //Hidden/Stealthing & You Are Hidden
RemoveBuff( BuffIcon.Invisibility ); //Always remove, default to the hiding icon EXCEPT in the invis spell where it's explicitly set
if( !Hidden )
{
RemoveBuff( BuffIcon.HidingAndOrStealth );
}
else// if( !InvisibilitySpell.HasTimer( this ) )
{
BuffInfo.AddBuff( this, new BuffInfo( BuffIcon.HidingAndOrStealth, 1075655 ) ); //Hidden/Stealthing & You Are Hidden
}
} }
} }

View file

@ -176,7 +176,8 @@ namespace Server.Regions
{ {
BaseGuard useGuard = null; BaseGuard useGuard = null;
foreach ( Mobile m in focus.GetMobilesInRange( 8 ) ) IPooledEnumerable eable = focus.GetMobilesInRange( 8 );
foreach ( Mobile m in eable)
{ {
if ( m is BaseGuard ) if ( m is BaseGuard )
{ {
@ -190,6 +191,8 @@ namespace Server.Regions
} }
} }
eable.Free();
if ( useGuard == null ) if ( useGuard == null )
{ {
m_GuardParams[0] = focus; m_GuardParams[0] = focus;

View file

@ -20,7 +20,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using System.Threading;
using System.IO; using System.IO;
namespace Server.Diagnostics { namespace Server.Diagnostics {
@ -65,11 +67,8 @@ namespace Server.Diagnostics {
} }
} }
[MethodImpl(MethodImplOptions.Synchronized)]
public static PacketSendProfile Acquire( Type type ) { public static PacketSendProfile Acquire( Type type ) {
if ( !Core.Profiling ) {
return null;
}
PacketSendProfile prof; PacketSendProfile prof;
if ( !_profiles.TryGetValue( type, out prof ) ) { if ( !_profiles.TryGetValue( type, out prof ) ) {
@ -81,13 +80,8 @@ namespace Server.Diagnostics {
private long _created; private long _created;
public long Created { public void Increment() {
get { Interlocked.Increment(ref _created);
return _created;
}
set {
_created = value;
}
} }
public PacketSendProfile( Type type ) public PacketSendProfile( Type type )
@ -97,7 +91,7 @@ namespace Server.Diagnostics {
public override void WriteTo( TextWriter op ) { public override void WriteTo( TextWriter op ) {
base.WriteTo( op ); base.WriteTo( op );
op.Write( "\t{0,12:N0}", Created ); op.Write( "\t{0,12:N0}", _created );
} }
} }
@ -110,11 +104,8 @@ namespace Server.Diagnostics {
} }
} }
[MethodImpl(MethodImplOptions.Synchronized)]
public static PacketReceiveProfile Acquire( int packetId ) { public static PacketReceiveProfile Acquire( int packetId ) {
if ( !Core.Profiling ) {
return null;
}
PacketReceiveProfile prof; PacketReceiveProfile prof;
if ( !_profiles.TryGetValue( packetId, out prof ) ) { if ( !_profiles.TryGetValue( packetId, out prof ) ) {

View file

@ -22,6 +22,10 @@ using System;
using System.IO; using System.IO;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
#if Framework_4_0
using System.Linq;
using System.Threading.Tasks;
#endif
using Server.Network; using Server.Network;
using Server.Items; using Server.Items;
using Server.ContextMenus; using Server.ContextMenus;
@ -1635,14 +1639,17 @@ namespace Server
} }
} }
private object _opll = new object();
public Packet OPLPacket public Packet OPLPacket
{ {
get get
{ {
if ( m_OPLPacket == null ) lock (_opll) {
{ if ( m_OPLPacket == null ) {
m_OPLPacket = new OPLInfo( PropertyList ); m_OPLPacket = new OPLInfo( PropertyList );
m_OPLPacket.SetStatic(); m_OPLPacket.SetStatic();
}
} }
return m_OPLPacket; return m_OPLPacket;
@ -1713,6 +1720,10 @@ namespace Server
} }
} }
private object _wpl = new object();
private object _wplsa = new object();
private object _wplhs = new object();
public Packet WorldPacket public Packet WorldPacket
{ {
get get
@ -1725,10 +1736,11 @@ namespace Server
// - Packet Flags // - Packet Flags
// - Direction // - Direction
if ( m_WorldPacket == null ) lock (_wpl) {
{ if ( m_WorldPacket == null ) {
m_WorldPacket = new WorldItem( this ); m_WorldPacket = new WorldItem( this );
m_WorldPacket.SetStatic(); m_WorldPacket.SetStatic();
}
} }
return m_WorldPacket; return m_WorldPacket;
@ -1747,10 +1759,11 @@ namespace Server
// - Packet Flags // - Packet Flags
// - Direction // - Direction
if ( m_WorldPacketSA == null ) lock (_wplsa) {
{ if ( m_WorldPacketSA == null ) {
m_WorldPacketSA = new WorldItemSA( this ); m_WorldPacketSA = new WorldItemSA( this );
m_WorldPacketSA.SetStatic(); m_WorldPacketSA.SetStatic();
}
} }
return m_WorldPacketSA; return m_WorldPacketSA;
@ -1769,10 +1782,11 @@ namespace Server
// - Packet Flags // - Packet Flags
// - Direction // - Direction
if ( m_WorldPacketHS == null ) lock (_wplhs) {
{ if ( m_WorldPacketHS == null ) {
m_WorldPacketHS = new WorldItemHS( this ); m_WorldPacketHS = new WorldItemHS( this );
m_WorldPacketHS.SetStatic(); m_WorldPacketHS.SetStatic();
}
} }
return m_WorldPacketHS; return m_WorldPacketHS;
@ -3012,6 +3026,8 @@ namespace Server
{ {
SetFlag( ImplFlag.InQueue, true ); SetFlag( ImplFlag.InQueue, true );
if (_processing)
Console.WriteLine(new System.Diagnostics.StackTrace());
m_DeltaQueue.Add( this ); m_DeltaQueue.Add( this );
} }
@ -3026,6 +3042,8 @@ namespace Server
{ {
SetFlag( ImplFlag.InQueue, false ); SetFlag( ImplFlag.InQueue, false );
if (_processing)
Console.WriteLine(new System.Diagnostics.StackTrace());
m_DeltaQueue.Remove( this ); m_DeltaQueue.Remove( this );
} }
} }
@ -3124,41 +3142,44 @@ namespace Server
if ( openers != null ) if ( openers != null )
{ {
for ( int i = 0; i < openers.Count; ++i ) lock (openers)
{ {
Mobile mob = openers[i]; for (int i = 0; i < openers.Count; ++i)
int range = GetUpdateRange( mob );
if ( mob.Map != map || !mob.InRange( worldLoc, range ) )
{ {
openers.RemoveAt( i-- ); Mobile mob = openers[i];
}
else
{
if ( mob == rootParent || mob == tradeRecip )
continue;
NetState ns = mob.NetState; int range = GetUpdateRange(mob);
if ( ns != null ) if (mob.Map != map || !mob.InRange(worldLoc, range))
{ {
if ( mob.CanSee( this ) ) openers.RemoveAt(i--);
{ }
if ( ns.ContainerGridLines ) else
ns.Send( new ContainerContentUpdate6017( this ) ); {
else if (mob == rootParent || mob == tradeRecip)
ns.Send( new ContainerContentUpdate( this ) ); continue;
if ( ObjectPropertyList.Enabled ) NetState ns = mob.NetState;
ns.Send( OPLPacket );
if (ns != null)
{
if (mob.CanSee(this))
{
if (ns.ContainerGridLines)
ns.Send(new ContainerContentUpdate6017(this));
else
ns.Send(new ContainerContentUpdate(this));
if (ObjectPropertyList.Enabled)
ns.Send(OPLPacket);
}
} }
} }
} }
}
if ( openers.Count == 0 ) if (openers.Count == 0)
contParent.Openers = null; contParent.Openers = null;
}
} }
return; return;
} }
@ -3168,10 +3189,15 @@ namespace Server
{ {
Packet p = null; Packet p = null;
Point3D worldLoc = GetWorldLocation(); Point3D worldLoc = GetWorldLocation();
object equipUpdateLock = new object();
IPooledEnumerable eable = map.GetClientsInRange( worldLoc, GetMaxUpdateRange() ); IPooledEnumerable eable = map.GetClientsInRange( worldLoc, GetMaxUpdateRange() );
#if Framework_4_0
Parallel.ForEach( eable.Cast<NetState>(), state => {
#else
foreach ( NetState state in eable ) { foreach ( NetState state in eable ) {
#endif
Mobile m = state.Mobile; Mobile m = state.Mobile;
if ( m.CanSee( this ) && m.InRange( worldLoc, GetUpdateRange( m ) ) ) { if ( m.CanSee( this ) && m.InRange( worldLoc, GetUpdateRange( m ) ) ) {
@ -3185,8 +3211,11 @@ namespace Server
else else
state.Send( new ContainerContentUpdate( this ) ); state.Send( new ContainerContentUpdate( this ) );
} else if ( m_Parent is Mobile ) { } else if ( m_Parent is Mobile ) {
p = new EquipUpdate( this ); lock (equipUpdateLock) {
p.Acquire(); p = new EquipUpdate(this);
p.Acquire();
}
state.Send( p ); state.Send( p );
} }
} else { } else {
@ -3199,6 +3228,9 @@ namespace Server
} }
} }
} }
#if Framework_4_0
);
#endif
if ( p != null ) if ( p != null )
Packet.Release( p ); Packet.Release( p );
@ -3212,11 +3244,15 @@ namespace Server
{ {
Packet p = null; Packet p = null;
Point3D worldLoc = GetWorldLocation(); Point3D worldLoc = GetWorldLocation();
object equipPacketLock = new object();
IPooledEnumerable eable = map.GetClientsInRange( worldLoc, GetMaxUpdateRange() ); IPooledEnumerable eable = map.GetClientsInRange( worldLoc, GetMaxUpdateRange() );
foreach ( NetState state in eable ) #if Framework_4_0
{ Parallel.ForEach( eable.Cast<NetState>(), state => {
#else
foreach ( NetState state in eable ) {
#endif
Mobile m = state.Mobile; Mobile m = state.Mobile;
if ( m.CanSee( this ) && m.InRange( worldLoc, GetUpdateRange( m ) ) ) if ( m.CanSee( this ) && m.InRange( worldLoc, GetUpdateRange( m ) ) )
@ -3224,8 +3260,9 @@ namespace Server
//if ( sendOPLUpdate ) //if ( sendOPLUpdate )
// state.Send( RemovePacket ); // state.Send( RemovePacket );
if ( p == null ) lock (equipPacketLock)
p = Packet.Acquire( new EquipUpdate( this ) ); if ( p == null )
p = Packet.Acquire( new EquipUpdate( this ) );
state.Send( p ); state.Send( p );
@ -3233,6 +3270,9 @@ namespace Server
state.Send( OPLPacket ); state.Send( OPLPacket );
} }
} }
#if Framework_4_0
);
#endif
Packet.Release( p ); Packet.Release( p );
@ -3246,33 +3286,45 @@ namespace Server
Point3D worldLoc = GetWorldLocation(); Point3D worldLoc = GetWorldLocation();
IPooledEnumerable eable = map.GetClientsInRange( worldLoc, GetMaxUpdateRange() ); IPooledEnumerable eable = map.GetClientsInRange( worldLoc, GetMaxUpdateRange() );
foreach ( NetState state in eable ) #if Framework_4_0
{ Parallel.ForEach( eable.Cast<NetState>(), state => {
#else
foreach ( NetState state in eable ) {
#endif
Mobile m = state.Mobile; Mobile m = state.Mobile;
if ( m.CanSee( this ) && m.InRange( worldLoc, GetUpdateRange( m ) ) ) if ( m.CanSee( this ) && m.InRange( worldLoc, GetUpdateRange( m ) ) )
state.Send( OPLPacket ); state.Send( OPLPacket );
} }
#if Framework_4_0
);
#endif
eable.Free(); eable.Free();
} }
} }
} }
private static bool _processing = false;
public static void ProcessDeltaQueue() public static void ProcessDeltaQueue()
{ {
#if Framework_4_0
_processing = true;
Parallel.ForEach( m_DeltaQueue, i => i.ProcessDelta() );
m_DeltaQueue.Clear();
_processing = false;
#else
int count = m_DeltaQueue.Count; int count = m_DeltaQueue.Count;
for ( int i = 0; i < m_DeltaQueue.Count; ++i ) for (int i = 0; i < m_DeltaQueue.Count; ++i) {
{
m_DeltaQueue[i].ProcessDelta(); m_DeltaQueue[i].ProcessDelta();
if ( i >= count ) if (i >= count)
break; break;
} }
m_DeltaQueue.Clear();
if ( m_DeltaQueue.Count > 0 ) #endif
m_DeltaQueue.Clear();
} }
public virtual void OnDelete() public virtual void OnDelete()

View file

@ -1098,30 +1098,9 @@ namespace Server
{ {
private IPooledEnumerator m_Enumerator; private IPooledEnumerator m_Enumerator;
private static Queue<PooledEnumerable> m_InstancePool = new Queue<PooledEnumerable>();
private static int m_Depth = 0;
public static PooledEnumerable Instantiate( IPooledEnumerator etor ) public static PooledEnumerable Instantiate( IPooledEnumerator etor )
{ {
++m_Depth; PooledEnumerable e = new PooledEnumerable( etor );
if ( m_Depth >= 5 )
Console.WriteLine( "Warning: Make sure to call .Free() on pooled enumerables." );
PooledEnumerable e;
if ( m_InstancePool.Count > 0 )
{
e = m_InstancePool.Dequeue();
e.m_Enumerator = etor;
}
else
{
e = new PooledEnumerable( etor );
}
etor.Enumerable = e;
return e; return e;
} }
@ -1133,7 +1112,7 @@ namespace Server
public IEnumerator GetEnumerator() public IEnumerator GetEnumerator()
{ {
if ( m_Enumerator == null ) if ( m_Enumerator == null )
throw new ObjectDisposedException( "PooledEnumerable", "GetEnumerator() called after Free()" ); throw new ObjectDisposedException("PooledEnumerable", "GetEnumerator() called after Free()");
return m_Enumerator; return m_Enumerator;
} }
@ -1142,12 +1121,8 @@ namespace Server
{ {
if ( m_Enumerator != null ) if ( m_Enumerator != null )
{ {
m_InstancePool.Enqueue( this );
m_Enumerator.Free(); m_Enumerator.Free();
m_Enumerator = null; m_Enumerator = null;
--m_Depth;
} }
} }
@ -1182,27 +1157,10 @@ namespace Server
private SectorEnumeratorType m_Type; private SectorEnumeratorType m_Type;
private object m_Current; private object m_Current;
private static Queue<TypedEnumerator> m_InstancePool = new Queue<TypedEnumerator>(); public static TypedEnumerator Instantiate( Map map, Rectangle2D bounds, SectorEnumeratorType type ) {
TypedEnumerator e = new TypedEnumerator(map, bounds, type);
public static TypedEnumerator Instantiate( Map map, Rectangle2D bounds, SectorEnumeratorType type )
{
TypedEnumerator e;
if ( m_InstancePool.Count > 0 )
{
e = m_InstancePool.Dequeue();
e.m_Map = map;
e.m_Bounds = bounds;
e.m_Type = type;
e.Reset();
}
else
{
e = new TypedEnumerator( map, bounds, type );
}
e.Reset();
return e; return e;
} }
@ -1211,18 +1169,13 @@ namespace Server
if ( m_Map == null ) if ( m_Map == null )
return; return;
m_InstancePool.Enqueue( this );
m_Map = null;
if ( m_Enumerator != null ) if ( m_Enumerator != null )
{ {
m_Enumerator.Free(); m_Enumerator.Free();
m_Enumerator = null; m_Enumerator = null;
} }
if ( m_Enumerable != null ) m_Map = null;
m_Enumerable.Free();
} }
public TypedEnumerator( Map map, Rectangle2D bounds, SectorEnumeratorType type ) public TypedEnumerator( Map map, Rectangle2D bounds, SectorEnumeratorType type )
@ -1230,22 +1183,22 @@ namespace Server
m_Map = map; m_Map = map;
m_Bounds = bounds; m_Bounds = bounds;
m_Type = type; m_Type = type;
Reset();
} }
public object Current public object Current {
{ get { return m_Current; }
get
{
return m_Current;
}
} }
public bool MoveNext() public bool MoveNext()
{ {
while ( true ) while ( true )
{ {
if (m_Enumerator == null)
{
Console.WriteLine("hmm m_Enumerator null??");
return false;
}
if ( m_Enumerator.MoveNext() ) if ( m_Enumerator.MoveNext() )
{ {
object o; object o;
@ -1256,6 +1209,7 @@ namespace Server
} }
catch catch
{ {
Console.WriteLine("typed Enum current null");
continue; continue;
} }
@ -1293,10 +1247,6 @@ namespace Server
else else
{ {
m_Current = null; m_Current = null;
m_Enumerator.Free();
m_Enumerator = null;
return false; return false;
} }
} }
@ -1305,11 +1255,7 @@ namespace Server
public void Reset() public void Reset()
{ {
m_Current = null; m_Current = null;
m_Enumerator = SectorEnumerator.Instantiate( m_Map, m_Bounds, m_Type );
if ( m_Enumerator != null )
m_Enumerator.Free();
m_Enumerator = SectorEnumerator.Instantiate( m_Map, m_Bounds, m_Type );//new SectorEnumerator( m_Map, m_Origin, m_Type, m_Range );
} }
public void Dispose() public void Dispose()
@ -1333,26 +1279,11 @@ namespace Server
private object m_Current; private object m_Current;
private int m_Index; private int m_Index;
private static Queue<MultiTileEnumerator> m_InstancePool = new Queue<MultiTileEnumerator>();
public static MultiTileEnumerator Instantiate( Sector sector, Point2D loc ) public static MultiTileEnumerator Instantiate( Sector sector, Point2D loc )
{ {
MultiTileEnumerator e; MultiTileEnumerator e = new MultiTileEnumerator( sector, loc );
if ( m_InstancePool.Count > 0 )
{
e = m_InstancePool.Dequeue();
e.m_List = sector.Multis;
e.m_Location = loc;
e.Reset();
}
else
{
e = new MultiTileEnumerator( sector, loc );
}
e.Reset();
return e; return e;
} }
@ -1360,14 +1291,10 @@ namespace Server
{ {
m_List = sector.Multis; m_List = sector.Multis;
m_Location = loc; m_Location = loc;
Reset();
} }
public object Current public object Current {
{ get {
get
{
return m_Current; return m_Current;
} }
} }
@ -1415,12 +1342,7 @@ namespace Server
if ( m_List == null ) if ( m_List == null )
return; return;
m_InstancePool.Enqueue( this );
m_List = null; m_List = null;
if ( m_Enumerable != null )
m_Enumerable.Free();
} }
public void Reset() public void Reset()
@ -1451,26 +1373,11 @@ namespace Server
private int m_Stage; // 0 = items, 1 = mobiles private int m_Stage; // 0 = items, 1 = mobiles
private object m_Current; private object m_Current;
private static Queue<ObjectEnumerator> m_InstancePool = new Queue<ObjectEnumerator>();
public static ObjectEnumerator Instantiate( Map map, Rectangle2D bounds ) public static ObjectEnumerator Instantiate( Map map, Rectangle2D bounds )
{ {
ObjectEnumerator e; ObjectEnumerator e = new ObjectEnumerator(map, bounds);
if ( m_InstancePool.Count > 0 )
{
e = m_InstancePool.Dequeue();
e.m_Map = map;
e.m_Bounds = bounds;
e.Reset();
}
else
{
e = new ObjectEnumerator( map, bounds );
}
e.Reset();
return e; return e;
} }
@ -1479,8 +1386,6 @@ namespace Server
if ( m_Map == null ) if ( m_Map == null )
return; return;
m_InstancePool.Enqueue( this );
m_Map = null; m_Map = null;
if ( m_Enumerator != null ) if ( m_Enumerator != null )
@ -1488,26 +1393,15 @@ namespace Server
m_Enumerator.Free(); m_Enumerator.Free();
m_Enumerator = null; m_Enumerator = null;
} }
if ( m_Enumerable != null )
m_Enumerable.Free();
} }
private ObjectEnumerator( Map map, Rectangle2D bounds ) private ObjectEnumerator( Map map, Rectangle2D bounds )
{ {
m_Map = map; m_Map = map;
m_Bounds = bounds; m_Bounds = bounds;
Reset();
} }
public object Current public object Current { get { return m_Current; } }
{
get
{
return m_Current;
}
}
public bool MoveNext() public bool MoveNext()
{ {
@ -1523,6 +1417,7 @@ namespace Server
} }
catch catch
{ {
Console.WriteLine("OBJ Enum current null");
continue; continue;
} }
@ -1608,25 +1503,10 @@ namespace Server
private static Queue<SectorEnumerator> m_InstancePool = new Queue<SectorEnumerator>(); private static Queue<SectorEnumerator> m_InstancePool = new Queue<SectorEnumerator>();
public static SectorEnumerator Instantiate( Map map, Rectangle2D bounds, SectorEnumeratorType type ) public static SectorEnumerator Instantiate( Map map, Rectangle2D bounds, SectorEnumeratorType type ) {
{ SectorEnumerator e = new SectorEnumerator(map, bounds, type);
SectorEnumerator e;
if ( m_InstancePool.Count > 0 )
{
e = m_InstancePool.Dequeue();
e.m_Map = map;
e.m_Bounds = bounds;
e.m_Type = type;
e.Reset();
}
else
{
e = new SectorEnumerator( map, bounds, type );
}
e.Reset();
return e; return e;
} }
@ -1635,12 +1515,7 @@ namespace Server
if ( m_Map == null ) if ( m_Map == null )
return; return;
m_InstancePool.Enqueue( this ); //m_Map = null;
m_Map = null;
if ( m_Enumerable != null )
m_Enumerable.Free();
} }
private SectorEnumerator( Map map, Rectangle2D bounds, SectorEnumeratorType type ) private SectorEnumerator( Map map, Rectangle2D bounds, SectorEnumeratorType type )
@ -1648,8 +1523,6 @@ namespace Server
m_Map = map; m_Map = map;
m_Bounds = bounds; m_Bounds = bounds;
m_Type = type; m_Type = type;
Reset();
} }
private IList GetListForSector( Sector sector ) private IList GetListForSector( Sector sector )
@ -1667,32 +1540,8 @@ namespace Server
} }
} }
public object Current public object Current {
{ get { return m_CurrentList[m_CurrentIndex]; }
get
{
return m_CurrentList[m_CurrentIndex];
/*try
{
return m_CurrentList[m_CurrentIndex];
}
catch
{
Console.WriteLine( "Warning: Object removed during enumeration. May not be recoverable" );
m_CurrentIndex = -1;
m_CurrentList = GetListForSector( m_Map.InternalGetSector( m_xSector, m_ySector ) );
if ( MoveNext() )
{
return Current;
}
else
{
throw new Exception( "Object disposed during enumeration. Was not recoverable." );
}
}*/
}
} }
public bool MoveNext() public bool MoveNext()
@ -1701,6 +1550,9 @@ namespace Server
{ {
++m_CurrentIndex; ++m_CurrentIndex;
if (m_CurrentList == null)
return false; // So much fail with PooledEnumerables ><
if ( m_CurrentIndex == m_CurrentList.Count ) if ( m_CurrentIndex == m_CurrentList.Count )
{ {
++m_ySector; ++m_ySector;

View file

@ -23,6 +23,10 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
#if Framework_4_0
using System.Linq;
using System.Threading.Tasks;
#endif
using Server; using Server;
using Server.Accounting; using Server.Accounting;
using Server.Commands; using Server.Commands;
@ -4913,7 +4917,7 @@ namespace Server
} }
} }
//eable.Free(); eable.Free();
object mutateContext = null; object mutateContext = null;
string mutatedText = text; string mutatedText = text;
@ -7988,7 +7992,7 @@ namespace Server
} }
[CommandProperty( AccessLevel.GameMaster )] [CommandProperty( AccessLevel.GameMaster )]
public virtual bool Hidden public bool Hidden
{ {
get get
{ {
@ -7998,49 +8002,54 @@ namespace Server
{ {
if( m_Hidden != value ) if( m_Hidden != value )
{ {
m_AllowedStealthSteps = 0;
m_Hidden = value; m_Hidden = value;
//Delta( MobileDelta.Flags ); //Delta( MobileDelta.Flags );
if( m_Map != null ) OnHiddenChanged();
}
}
}
public virtual void OnHiddenChanged()
{
m_AllowedStealthSteps = 0;
if (m_Map != null)
{
Packet p = null;
IPooledEnumerable eable = m_Map.GetClientsInRange(m_Location);
foreach (NetState state in eable)
{
if (!state.Mobile.CanSee(this))
{ {
Packet p = null; if (p == null)
p = this.RemovePacket;
IPooledEnumerable eable = m_Map.GetClientsInRange( m_Location ); state.Send(p);
}
else
{
if (state.StygianAbyss)
state.Send(new MobileIncoming(state.Mobile, this));
else
state.Send(new MobileIncomingOld(state.Mobile, this));
foreach( NetState state in eable ) if (IsDeadBondedPet)
state.Send(new BondedStatus(0, m_Serial, 1));
if (ObjectPropertyList.Enabled)
{ {
if( !state.Mobile.CanSee( this ) ) state.Send(OPLPacket);
{
if( p == null )
p = this.RemovePacket;
state.Send( p ); //foreach ( Item item in m_Items )
} // state.Send( item.OPLPacket );
else
{
if ( state.StygianAbyss )
state.Send( new MobileIncoming( state.Mobile, this ) );
else
state.Send( new MobileIncomingOld( state.Mobile, this ) );
if( IsDeadBondedPet )
state.Send( new BondedStatus( 0, m_Serial, 1 ) );
if( ObjectPropertyList.Enabled )
{
state.Send( OPLPacket );
//foreach ( Item item in m_Items )
// state.Send( item.OPLPacket );
}
}
} }
eable.Free();
} }
} }
eable.Free();
} }
} }
@ -9956,6 +9965,8 @@ namespace Server
{ {
m_InDeltaQueue = true; m_InDeltaQueue = true;
if (_processing)
Console.WriteLine(new System.Diagnostics.StackTrace());
m_DeltaQueue.Enqueue( this ); m_DeltaQueue.Enqueue( this );
} }
@ -10121,7 +10132,9 @@ namespace Server
sendPublicStats = true; sendPublicStats = true;
} }
if( (delta & (MobileDelta.WeaponDamage | MobileDelta.Resistances | MobileDelta.Stat | MobileDelta.Weight | MobileDelta.Gold | MobileDelta.Armor | MobileDelta.StatCap | MobileDelta.Followers | MobileDelta.TithingPoints | MobileDelta.Race)) != 0 ) if( (delta & (MobileDelta.WeaponDamage | MobileDelta.Resistances | MobileDelta.Stat |
MobileDelta.Weight | MobileDelta.Gold | MobileDelta.Armor | MobileDelta.StatCap |
MobileDelta.Followers | MobileDelta.TithingPoints | MobileDelta.Race)) != 0 )
{ {
sendPrivateStats = true; sendPrivateStats = true;
} }
@ -10142,6 +10155,9 @@ namespace Server
sendFacialHair = true; sendFacialHair = true;
} }
#if Framework_4_0
Packet[][] cache = new Packet[2][] { new Packet[8], new Packet[8] };
#else
Packet[][] cache = m_MovingPacketCache; Packet[][] cache = m_MovingPacketCache;
if( sendMoving || sendNonlocalMoving || sendHealthbarPoison || sendHealthbarYellow ) if( sendMoving || sendNonlocalMoving || sendHealthbarPoison || sendHealthbarYellow )
@ -10150,6 +10166,7 @@ namespace Server
for( int j = 0; j < cache[i].Length; ++j ) for( int j = 0; j < cache[i].Length; ++j )
Packet.Release( ref cache[i][j] ); Packet.Release( ref cache[i][j] );
} }
#endif
NetState ourState = m.m_NetState; NetState ourState = m.m_NetState;
@ -10252,22 +10269,42 @@ namespace Server
{ {
Mobile beholder; Mobile beholder;
IPooledEnumerable eable = m.Map.GetClientsInRange( m.m_Location );
Packet hitsPacket = null; Packet hitsPacket = null;
Packet statPacketTrue = null, statPacketFalse = null; Packet statPacketTrue = null;
Packet statPacketFalse = null;
Packet deadPacket = null; Packet deadPacket = null;
Packet hairPacket = null, facialhairPacket = null; Packet hairPacket = null;
Packet hbpPacket = null, hbyPacket = null; Packet facialhairPacket = null;
Packet hbpPacket = null;
Packet hbyPacket = null;
foreach( NetState state in eable ) // 9 Separate Locks.. Feels Dirty
{ object hitsPacketLock = new object();
object statPacketTrueLock = new object();
object statPacketFalseLock = new object();
object deadPacketLock = new object();
object hairPacketLock = new object();
object facialhairPacketLock = new object();
object hbpPacketLock = new object();
object hbyPacketLock = new object();
object cacheSync = new object();
Packet removePacket = RemovePacket;
Packet oplPacket = OPLPacket;
IPooledEnumerable eable = m.Map.GetClientsInRange(m.m_Location);
#if Framework_4_0
Parallel.ForEach( eable.Cast<NetState>(), state => {
#else
foreach ( NetState state in eable ) {
#endif
beholder = state.Mobile; beholder = state.Mobile;
if( beholder != m && beholder.CanSee( m ) ) if( beholder != m && beholder.CanSee( m ) )
{ {
if( sendRemove ) if( sendRemove )
state.Send( m.RemovePacket ); state.Send(removePacket);
if( sendIncoming ) if( sendIncoming )
{ {
@ -10279,8 +10316,11 @@ namespace Server
if( m.IsDeadBondedPet ) if( m.IsDeadBondedPet )
{ {
if( deadPacket == null ) lock (deadPacketLock)
deadPacket = Packet.Acquire( new BondedStatus( 0, m.m_Serial, 1 ) ); {
if (deadPacket == null)
deadPacket = Packet.Acquire(new BondedStatus(0, m.m_Serial, 1));
}
state.Send( deadPacket ); state.Send( deadPacket );
} }
@ -10291,24 +10331,36 @@ namespace Server
{ {
int noto = Notoriety.Compute( beholder, m ); int noto = Notoriety.Compute( beholder, m );
Packet p = cache[0][noto]; Packet p;
if( p == null ) lock (cacheSync)
cache[0][noto] = p = Packet.Acquire( new MobileMoving( m, noto ) ); {
p = cache[0][noto];
if (p == null)
cache[0][noto] = p = Packet.Acquire(new MobileMoving(m, noto));
}
state.Send( p ); state.Send( p );
} }
if ( sendHealthbarPoison ) { if ( sendHealthbarPoison ) {
if ( hbpPacket == null ) lock (hbpPacketLock)
hbpPacket = Packet.Acquire( new HealthbarPoison( m ) ); {
if (hbpPacket == null)
hbpPacket = Packet.Acquire(new HealthbarPoison(m));
}
state.Send( hbpPacket ); state.Send( hbpPacket );
} }
if ( sendHealthbarYellow ) { if ( sendHealthbarYellow ) {
if ( hbyPacket == null ) lock (hbyPacketLock)
hbyPacket = Packet.Acquire( new HealthbarYellow( m ) ); {
if (hbyPacket == null)
hbyPacket = Packet.Acquire(new HealthbarYellow(m));
}
state.Send( hbyPacket ); state.Send( hbyPacket );
} }
} else { } else {
@ -10316,10 +10368,15 @@ namespace Server
{ {
int noto = Notoriety.Compute( beholder, m ); int noto = Notoriety.Compute( beholder, m );
Packet p = cache[1][noto]; Packet p;
if( p == null ) lock (cacheSync)
cache[1][noto] = p = Packet.Acquire( new MobileMovingOld( m, noto ) ); {
p = cache[1][noto];
if (p == null)
cache[1][noto] = p = Packet.Acquire(new MobileMovingOld(m, noto));
}
state.Send( p ); state.Send( p );
} }
@ -10329,35 +10386,45 @@ namespace Server
{ {
if( m.CanBeRenamedBy( beholder ) ) if( m.CanBeRenamedBy( beholder ) )
{ {
if( statPacketTrue == null ) lock (statPacketTrueLock)
statPacketTrue = Packet.Acquire( new MobileStatusCompact( true, m ) ); {
if (statPacketTrue == null)
statPacketTrue = Packet.Acquire(new MobileStatusCompact(true, m));
}
state.Send( statPacketTrue ); state.Send( statPacketTrue );
} }
else else
{ {
if( statPacketFalse == null ) lock (statPacketFalseLock)
statPacketFalse = Packet.Acquire( new MobileStatusCompact( false, m ) ); {
if (statPacketFalse == null)
statPacketFalse = Packet.Acquire(new MobileStatusCompact(false, m));
}
state.Send( statPacketFalse ); state.Send( statPacketFalse );
} }
} }
else if( sendHits ) else if( sendHits )
{ {
if( hitsPacket == null ) lock (hitsPacketLock)
hitsPacket = Packet.Acquire( new MobileHitsN( m ) ); {
if (hitsPacket == null)
hitsPacket = Packet.Acquire(new MobileHitsN(m));
}
state.Send( hitsPacket ); state.Send( hitsPacket );
} }
if( sendHair ) if( sendHair )
{ {
if( hairPacket == null ) lock (hairPacketLock) {
{ if (hairPacket == null) {
if( removeHair ) if (removeHair)
hairPacket = Packet.Acquire( new RemoveHair( m ) ); hairPacket = Packet.Acquire(new RemoveHair(m));
else else
hairPacket = Packet.Acquire( new HairEquipUpdate( m ) ); hairPacket = Packet.Acquire(new HairEquipUpdate(m));
}
} }
state.Send( hairPacket ); state.Send( hairPacket );
@ -10365,21 +10432,25 @@ namespace Server
if( sendFacialHair ) if( sendFacialHair )
{ {
if( facialhairPacket == null ) lock (facialhairPacketLock) {
{ if (facialhairPacket == null) {
if( removeFacialHair ) if (removeFacialHair)
facialhairPacket = Packet.Acquire( new RemoveFacialHair( m ) ); facialhairPacket = Packet.Acquire(new RemoveFacialHair(m));
else else
facialhairPacket = Packet.Acquire( new FacialHairEquipUpdate( m ) ); facialhairPacket = Packet.Acquire(new FacialHairEquipUpdate(m));
}
} }
state.Send( facialhairPacket ); state.Send( facialhairPacket );
} }
if( sendOPLUpdate ) if( sendOPLUpdate )
state.Send( OPLPacket ); state.Send(oplPacket);
} }
} }
#if Framework_4_0
);
#endif
Packet.Release( hitsPacket ); Packet.Release( hitsPacket );
Packet.Release( statPacketTrue ); Packet.Release( statPacketTrue );
@ -10396,18 +10467,27 @@ namespace Server
if( sendMoving || sendNonlocalMoving || sendHealthbarPoison || sendHealthbarYellow ) if( sendMoving || sendNonlocalMoving || sendHealthbarPoison || sendHealthbarYellow )
{ {
for( int i = 0; i < cache.Length; ++i ) for( int i = 0; i < cache.Length; ++i )
for( int j = 0; j < cache.Length; ++j ) for( int j = 0; j < cache[i].Length; ++j )
Packet.Release( ref cache[i][j] ); Packet.Release(ref cache[i][j]);
} }
} }
public static bool _processing = false;
public static void ProcessDeltaQueue() public static void ProcessDeltaQueue()
{ {
#if Framework_4_0
_processing = true;
Parallel.ForEach( m_DeltaQueue, m => m.ProcessDelta() );
m_DeltaQueue.Clear();
_processing = false;
#else
int count = m_DeltaQueue.Count; int count = m_DeltaQueue.Count;
int index = 0; int index = 0;
while( m_DeltaQueue.Count > 0 && index++ < count ) while( m_DeltaQueue.Count > 0 && index++ < count )
m_DeltaQueue.Dequeue().ProcessDelta(); m_DeltaQueue.Dequeue().ProcessDelta();
#endif
} }
[CommandProperty( AccessLevel.Counselor, AccessLevel.GameMaster )] [CommandProperty( AccessLevel.Counselor, AccessLevel.GameMaster )]

View file

@ -83,92 +83,104 @@ namespace Server.Network {
// If our input exceeds this length, we may potentially overflow the buffer // If our input exceeds this length, we may potentially overflow the buffer
private const int PossibleOverflow = ( ( BufferSize * 8 ) - TerminalCodeLength ) / MaximalCodeLength; private const int PossibleOverflow = ( ( BufferSize * 8 ) - TerminalCodeLength ) / MaximalCodeLength;
private static object _syncRoot = new object(); public unsafe static void Compress(byte[] input, int offset, int count, byte[] output, ref int length)
{
private static byte[] _outputBuffer = new byte[BufferSize]; if (input == null)
{
[Obsolete( "Use Compress( byte[], int, int, ref int ) instead.", false )] throw new ArgumentNullException("input");
public static void Compress( byte[] input, int length, out byte[] output, out int outputLength ) { }
outputLength = 0; else if (offset < 0 || offset >= input.Length)
output = Compress( input, 0, length, ref outputLength ); {
} throw new ArgumentOutOfRangeException("offset");
}
public unsafe static byte[] Compress( byte[] input, int offset, int count, ref int length ) { else if (count < 0 || count > input.Length)
if ( input == null ) { {
throw new ArgumentNullException( "input" ); throw new ArgumentOutOfRangeException("count");
} else if ( offset < 0 || offset >= input.Length ) { }
throw new ArgumentOutOfRangeException( "offset" ); else if ((input.Length - offset) < count)
} else if ( count < 0 || count > input.Length ) { {
throw new ArgumentOutOfRangeException( "count" );
} else if ( ( input.Length - offset ) < count ) {
throw new ArgumentException(); throw new ArgumentException();
} }
length = 0; length = 0;
if ( count > DefiniteOverflow ) { if (count > DefiniteOverflow)
return null; {
return;
} }
lock ( _syncRoot ) { int bitCount = 0;
int bitCount = 0; int bitValue = 0;
int bitValue = 0;
fixed ( int* pTable = _huffmanTable ) { fixed (int* pTable = _huffmanTable)
int* pEntry; {
int* pEntry;
fixed ( byte* pInputBuffer = input ) { fixed (byte* pInputBuffer = input)
byte* pInput = pInputBuffer + offset, pInputEnd = pInput + count; {
byte* pInput = pInputBuffer + offset, pInputEnd = pInput + count;
fixed ( byte* pOutputBuffer = _outputBuffer ) { fixed (byte* pOutputBuffer = output)
byte* pOutput = pOutputBuffer, pOutputEnd = pOutput + BufferSize; {
byte* pOutput = pOutputBuffer, pOutputEnd = pOutput + BufferSize;
while ( pInput < pInputEnd ) { while (pInput < pInputEnd)
pEntry = &pTable[*pInput++ << 1]; {
pEntry = &pTable[*pInput++ << 1];
bitCount += pEntry[CountIndex];
bitValue <<= pEntry[CountIndex];
bitValue |= pEntry[ValueIndex];
while ( bitCount >= 8 ) {
bitCount -= 8;
if ( pOutput < pOutputEnd ) {
*pOutput++ = ( byte ) ( bitValue >> bitCount );
} else {
return null;
}
}
}
// terminal code
pEntry = &pTable[0x200];
bitCount += pEntry[CountIndex]; bitCount += pEntry[CountIndex];
bitValue <<= pEntry[CountIndex]; bitValue <<= pEntry[CountIndex];
bitValue |= pEntry[ValueIndex]; bitValue |= pEntry[ValueIndex];
// align on byte boundary while (bitCount >= 8)
if ( ( bitCount & 7 ) != 0 ) { {
bitValue <<= ( 8 - ( bitCount & 7 ) );
bitCount += ( 8 - ( bitCount & 7 ) );
}
while ( bitCount >= 8 ) {
bitCount -= 8; bitCount -= 8;
if ( pOutput < pOutputEnd ) { if (pOutput < pOutputEnd)
*pOutput++ = ( byte ) ( bitValue >> bitCount ); {
} else { *pOutput++ = (byte)(bitValue >> bitCount);
return null; }
else
{
length = 0;
return;
} }
} }
length = ( int ) ( pOutput - pOutputBuffer );
return _outputBuffer;
} }
// terminal code
pEntry = &pTable[0x200];
bitCount += pEntry[CountIndex];
bitValue <<= pEntry[CountIndex];
bitValue |= pEntry[ValueIndex];
// align on byte boundary
if ((bitCount & 7) != 0)
{
bitValue <<= (8 - (bitCount & 7));
bitCount += (8 - (bitCount & 7));
}
while (bitCount >= 8)
{
bitCount -= 8;
if (pOutput < pOutputEnd)
{
*pOutput++ = (byte)(bitValue >> bitCount);
}
else
{
length = 0;
return;
}
}
length = (int)(pOutput - pOutputBuffer);
return;
} }
} }
} }

View file

@ -236,7 +236,9 @@ namespace Server.Network
return; return;
} }
PacketReceiveProfile prof = PacketReceiveProfile.Acquire( packetID ); PacketReceiveProfile prof = null;
if (Core.Profiling) prof = PacketReceiveProfile.Acquire( packetID );
if ( prof != null ) { if ( prof != null ) {
prof.Start(); prof.Start();

View file

@ -19,12 +19,14 @@
***************************************************************************/ ***************************************************************************/
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading; using System.Threading;
#if Framework_4_0
using System.Threading.Tasks;
#endif
using Server; using Server;
using Server.Accounting; using Server.Accounting;
using Server.Network; using Server.Network;
@ -588,14 +590,15 @@ namespace Server.Network {
} }
} }
private bool _sending;
private object _sendL = new object();
public virtual void Send( Packet p ) { public virtual void Send( Packet p ) {
if ( m_Socket == null || m_BlockAllPackets ) { if ( m_Socket == null || m_BlockAllPackets ) {
p.OnSend(); p.OnSend();
return; return;
} }
PacketSendProfile prof = PacketSendProfile.Acquire( p.GetType() );
int length; int length;
byte[] buffer = p.Compile( m_CompressionEnabled, out length ); byte[] buffer = p.Compile( m_CompressionEnabled, out length );
@ -605,6 +608,10 @@ namespace Server.Network {
return; return;
} }
PacketSendProfile prof = null;
if (Core.Profiling) prof = PacketSendProfile.Acquire(p.GetType());
if ( prof != null ) { if ( prof != null ) {
prof.Start(); prof.Start();
} }
@ -616,22 +623,27 @@ namespace Server.Network {
try { try {
SendQueue.Gram gram; SendQueue.Gram gram;
lock ( m_SendQueue ) { lock (_sendL) {
gram = m_SendQueue.Enqueue( buffer, length ); lock (m_SendQueue)
} gram = m_SendQueue.Enqueue(buffer, length);
if ( gram != null ) { if (gram != null) {
#if NewAsyncSockets #if NewAsyncSockets
m_SendEventArgs.SetBuffer( gram.Buffer, 0, gram.Length ); m_SendEventArgs.SetBuffer( gram.Buffer, 0, gram.Length );
Send_Start(); Send_Start();
#else #else
try { try {
m_Socket.BeginSend( gram.Buffer, 0, gram.Length, SocketFlags.None, m_OnSend, m_Socket ); if (!_sending) {
} catch ( Exception ex ) { _sending = true;
TraceException( ex ); m_Socket.BeginSend(gram.Buffer, 0, gram.Length, SocketFlags.None, m_OnSend, m_Socket);
Dispose( false ); }
} }
catch (Exception ex) {
TraceException(ex);
Dispose(false);
}
#endif #endif
}
} }
} catch ( CapacityExceededException ) { } catch ( CapacityExceededException ) {
Console.WriteLine( "Client: {0}: Too much data pending, disconnecting...", this ); Console.WriteLine( "Client: {0}: Too much data pending, disconnecting...", this );
@ -814,13 +826,15 @@ namespace Server.Network {
} }
public bool Flush() { public bool Flush() {
if ( m_Socket == null || !m_SendQueue.IsFlushReady ) { if ( m_Socket == null )
return false; return false;
}
SendQueue.Gram gram; SendQueue.Gram gram;
lock ( m_SendQueue ) { lock ( m_SendQueue ) {
if (!m_SendQueue.IsFlushReady)
return false;
gram = m_SendQueue.CheckFlushReady(); gram = m_SendQueue.CheckFlushReady();
} }
@ -914,23 +928,26 @@ namespace Server.Network {
m_NextCheckActivity = Core.TickCount + 90000; m_NextCheckActivity = Core.TickCount + 90000;
if ( m_CoalesceSleep >= 0 ) { if (m_CoalesceSleep >= 0) {
Thread.Sleep( m_CoalesceSleep ); Thread.Sleep(m_CoalesceSleep);
} }
SendQueue.Gram gram; SendQueue.Gram gram;
lock ( m_SendQueue ) { lock (m_SendQueue) {
gram = m_SendQueue.Dequeue(); gram = m_SendQueue.Dequeue();
} }
if ( gram != null ) { if (gram != null) {
try { try {
s.BeginSend( gram.Buffer, 0, gram.Length, SocketFlags.None, m_OnSend, s ); s.BeginSend(gram.Buffer, 0, gram.Length, SocketFlags.None, m_OnSend, s);
} catch ( Exception ex ) { } catch (Exception ex) {
TraceException( ex ); TraceException(ex);
Dispose( false ); Dispose(false);
} }
} else {
lock (_sendL)
_sending = false;
} }
} catch ( Exception ){ } catch ( Exception ){
Dispose( false ); Dispose( false );
@ -974,23 +991,31 @@ namespace Server.Network {
} }
public bool Flush() { public bool Flush() {
if ( m_Socket == null || !m_SendQueue.IsFlushReady ) { if (m_Socket == null)
return false; return false;
}
SendQueue.Gram gram; lock (_sendL) {
if (_sending)
return false;
lock ( m_SendQueue ) { SendQueue.Gram gram;
gram = m_SendQueue.CheckFlushReady();
}
if ( gram != null ) { lock (m_SendQueue) {
try { if (!m_SendQueue.IsFlushReady)
m_Socket.BeginSend( gram.Buffer, 0, gram.Length, SocketFlags.None, m_OnSend, m_Socket ); return false;
return true;
} catch ( Exception ex ) { gram = m_SendQueue.CheckFlushReady();
TraceException( ex ); }
Dispose( false );
if (gram != null) {
try {
_sending = true;
m_Socket.BeginSend(gram.Buffer, 0, gram.Length, SocketFlags.None, m_OnSend, m_Socket);
return true;
} catch (Exception ex) {
TraceException(ex);
Dispose(false);
}
} }
} }
@ -1007,11 +1032,13 @@ namespace Server.Network {
} }
public static void FlushAll() { public static void FlushAll() {
#if Framework_4_0
Parallel.ForEach( m_Instances, ns => ns.Flush() );
#else
for ( int i = 0; i < m_Instances.Count; ++i ) { for ( int i = 0; i < m_Instances.Count; ++i ) {
NetState ns = m_Instances[i]; m_Instances[i].Flush();
ns.Flush();
} }
#endif
} }
private static int m_CoalesceSleep = -1; private static int m_CoalesceSleep = -1;
@ -1109,10 +1136,11 @@ namespace Server.Network {
m_Running = false; m_Running = false;
m_Disposed.Enqueue( this ); lock (m_Disposed)
m_Disposed.Enqueue( this );
if ( /*!flush &&*/ !m_SendQueue.IsEmpty ) { lock (m_SendQueue)
lock ( m_SendQueue ) if ( /*!flush &&*/ !m_SendQueue.IsEmpty ) {
m_SendQueue.Clear(); m_SendQueue.Clear();
} }
} }
@ -1131,37 +1159,38 @@ namespace Server.Network {
} }
} }
private static Queue m_Disposed = Queue.Synchronized( new Queue() ); private static Queue<NetState> m_Disposed = new Queue<NetState>();
public static void ProcessDisposedQueue() { public static void ProcessDisposedQueue() {
int breakout = 0; lock (m_Disposed) {
int breakout = 0;
while ( breakout < 200 && m_Disposed.Count > 0 ) { while ( breakout < 200 && m_Disposed.Count > 0 ) {
++breakout; ++breakout;
NetState ns = m_Disposed.Dequeue();
NetState ns = ( NetState ) m_Disposed.Dequeue(); Mobile m = ns.m_Mobile;
IAccount a = ns.m_Account;
Mobile m = ns.m_Mobile; if ( m != null ) {
IAccount a = ns.m_Account; m.NetState = null;
ns.m_Mobile = null;
}
if ( m != null ) { ns.m_Gumps.Clear();
m.NetState = null; ns.m_Menus.Clear();
ns.m_Mobile = null; ns.m_HuePickers.Clear();
} ns.m_Account = null;
ns.m_ServerInfo = null;
ns.m_CityInfo = null;
ns.m_Gumps.Clear(); m_Instances.Remove( ns );
ns.m_Menus.Clear();
ns.m_HuePickers.Clear();
ns.m_Account = null;
ns.m_ServerInfo = null;
ns.m_CityInfo = null;
m_Instances.Remove( ns ); if ( a != null ) {
ns.WriteConsole( "Disconnected. [{0} Online] [{1}]", m_Instances.Count, a );
if ( a != null ) { } else {
ns.WriteConsole( "Disconnected. [{0} Online] [{1}]", m_Instances.Count, a ); ns.WriteConsole( "Disconnected. [{0} Online]", m_Instances.Count );
} else { }
ns.WriteConsole( "Disconnected. [{0} Online]", m_Instances.Count );
} }
} }
} }

View file

@ -97,7 +97,7 @@ namespace Server.Network
/// <summary> /// <summary>
/// Internal format buffer. /// Internal format buffer.
/// </summary> /// </summary>
private static byte[] m_Buffer = new byte[4]; private byte[] m_Buffer = new byte[4];
/// <summary> /// <summary>
/// Instantiates a new PacketWriter instance with the default capacity of 4 bytes. /// Instantiates a new PacketWriter instance with the default capacity of 4 bytes.

View file

@ -2475,7 +2475,8 @@ namespace Server.Network
PacketWriter.ReleaseInstance( m_Strings ); PacketWriter.ReleaseInstance( m_Strings );
} }
private static byte[] m_PackBuffer; private const int GumpBufferSize = 0x4000;
private static BufferPool m_PackBuffers = new BufferPool("Gump", 4, GumpBufferSize);
private void WritePacked( PacketWriter src ) private void WritePacked( PacketWriter src )
{ {
@ -2493,8 +2494,15 @@ namespace Server.Network
wantLength += 4095; wantLength += 4095;
wantLength &= ~4095; wantLength &= ~4095;
if ( m_PackBuffer == null || m_PackBuffer.Length < wantLength ) byte[] m_PackBuffer;
lock (m_PackBuffers)
m_PackBuffer = m_PackBuffers.AcquireBuffer();
if (m_PackBuffer.Length < wantLength)
{
Console.WriteLine("Notice: DisplayGumpPacked creating new {0} byte buffer", wantLength);
m_PackBuffer = new byte[wantLength]; m_PackBuffer = new byte[wantLength];
}
int packLength = m_PackBuffer.Length; int packLength = m_PackBuffer.Length;
@ -2503,6 +2511,9 @@ namespace Server.Network
m_Stream.Write( (int) ( 4 + packLength ) ); m_Stream.Write( (int) ( 4 + packLength ) );
m_Stream.Write( (int) length ); m_Stream.Write( (int) length );
m_Stream.Write( m_PackBuffer, 0, packLength ); m_Stream.Write( m_PackBuffer, 0, packLength );
lock (m_PackBuffers)
m_PackBuffers.ReleaseBuffer(m_PackBuffer);
} }
} }
@ -2517,6 +2528,8 @@ namespace Server.Network
public DisplayGumpFast( Gump g ) : base( 0xB0 ) public DisplayGumpFast( Gump g ) : base( 0xB0 )
{ {
m_Buffer[0] = (byte)' ';
EnsureCapacity( 4096 ); EnsureCapacity( 4096 );
m_Stream.Write( (int) g.Serial ); m_Stream.Write( (int) g.Serial );
@ -2532,12 +2545,7 @@ namespace Server.Network
private static byte[] m_BeginTextSeparator = Gump.StringToBuffer( " @" ); private static byte[] m_BeginTextSeparator = Gump.StringToBuffer( " @" );
private static byte[] m_EndTextSeparator = Gump.StringToBuffer( "@" ); private static byte[] m_EndTextSeparator = Gump.StringToBuffer( "@" );
private static byte[] m_Buffer = new byte[48]; private byte[] m_Buffer = new byte[48];
static DisplayGumpFast()
{
m_Buffer[0] = (byte)' ';
}
public void AppendLayout( bool val ) public void AppendLayout( bool val )
{ {
@ -4378,10 +4386,9 @@ namespace Server.Network
{ {
m_PacketID = packetID; m_PacketID = packetID;
PacketSendProfile prof = PacketSendProfile.Acquire( GetType() ); if (Core.Profiling) {
PacketSendProfile prof = PacketSendProfile.Acquire( GetType() );
if ( prof != null ) { prof.Increment();
prof.Created++;
} }
} }
@ -4400,10 +4407,9 @@ namespace Server.Network
m_Stream = PacketWriter.CreateInstance( length );// new PacketWriter( length ); m_Stream = PacketWriter.CreateInstance( length );// new PacketWriter( length );
m_Stream.Write( ( byte ) packetID ); m_Stream.Write( ( byte ) packetID );
PacketSendProfile prof = PacketSendProfile.Acquire( GetType() ); if (Core.Profiling) {
PacketSendProfile prof = PacketSendProfile.Acquire( GetType() );
if ( prof != null ) { prof.Increment();
prof.Created++;
} }
} }
@ -4415,6 +4421,9 @@ namespace Server.Network
} }
} }
private const int CompressorBufferSize = 0x10000;
private static BufferPool m_CompressorBuffers = new BufferPool("Compressor", 4, CompressorBufferSize);
private const int BufferSize = 4096; private const int BufferSize = 4096;
private static BufferPool m_Buffers = new BufferPool( "Compressed", 16, BufferSize ); private static BufferPool m_Buffers = new BufferPool( "Compressed", 16, BufferSize );
@ -4490,8 +4499,10 @@ namespace Server.Network
{ {
Core.Set(); Core.Set();
if ( (m_State & (State.Acquired | State.Static)) == 0 ) lock (this) {
Free(); if ((m_State & (State.Acquired | State.Static)) == 0)
Free();
}
} }
private void Free() private void Free()
@ -4499,8 +4510,8 @@ namespace Server.Network
if ( m_CompiledBuffer == null ) if ( m_CompiledBuffer == null )
return; return;
if ( (m_State & State.Buffered) != 0 ) if ((m_State & State.Buffered) != 0)
m_Buffers.ReleaseBuffer( m_CompiledBuffer ); m_Buffers.ReleaseBuffer(m_CompiledBuffer);
m_State &= ~(State.Static | State.Acquired | State.Buffered); m_State &= ~(State.Static | State.Acquired | State.Buffered);
@ -4509,7 +4520,7 @@ namespace Server.Network
public void Release() public void Release()
{ {
if ( (m_State & State.Acquired) != 0 ) if ((m_State & State.Acquired) != 0)
Free(); Free();
} }
@ -4518,43 +4529,46 @@ namespace Server.Network
public byte[] Compile( bool compress, out int length ) public byte[] Compile( bool compress, out int length )
{ {
if ( m_CompiledBuffer == null ) lock (this)
{ {
if ( (m_State & State.Accessed) == 0 ) if (m_CompiledBuffer == null)
{ {
m_State |= State.Accessed; if ((m_State & State.Accessed) == 0)
}
else
{
if ( (m_State & State.Warned) == 0 )
{ {
m_State |= State.Warned; m_State |= State.Accessed;
}
try else
{
if ((m_State & State.Warned) == 0)
{ {
using ( StreamWriter op = new StreamWriter( "net_opt.log", true ) ) m_State |= State.Warned;
try
{
using (StreamWriter op = new StreamWriter("net_opt.log", true))
{
op.WriteLine("Redundant compile for packet {0}, use Acquire() and Release()", this.GetType());
op.WriteLine(new System.Diagnostics.StackTrace());
}
}
catch
{ {
op.WriteLine( "Redundant compile for packet {0}, use Acquire() and Release()", this.GetType() );
op.WriteLine( new System.Diagnostics.StackTrace() );
} }
} }
catch
{ m_CompiledBuffer = new byte[0];
} m_CompiledLength = 0;
length = m_CompiledLength;
return m_CompiledBuffer;
} }
m_CompiledBuffer = new byte[0]; InternalCompile(compress);
m_CompiledLength = 0;
length = m_CompiledLength;
return m_CompiledBuffer;
} }
InternalCompile( compress ); length = m_CompiledLength;
return m_CompiledBuffer;
} }
length = m_CompiledLength;
return m_CompiledBuffer;
} }
private void InternalCompile( bool compress ) private void InternalCompile( bool compress )
@ -4578,41 +4592,49 @@ namespace Server.Network
m_CompiledBuffer = ms.GetBuffer(); m_CompiledBuffer = ms.GetBuffer();
int length = (int)ms.Length; int length = (int)ms.Length;
if ( compress ) if ( compress ) {
{ byte[] buffer;
m_CompiledBuffer = Compression.Compress( lock (m_CompressorBuffers)
m_CompiledBuffer, 0, length, buffer = m_CompressorBuffers.AcquireBuffer();
ref length
); Compression.Compress(m_CompiledBuffer, 0, length, buffer, ref length);
if ( m_CompiledBuffer == null ) if (length <= 0) {
{ Console.WriteLine("Warning: Compression buffer overflowed on packet 0x{0:X2} ('{1}') (length={2})", m_PacketID, GetType().Name, length);
Console.WriteLine( "Warning: Compression buffer overflowed on packet 0x{0:X2} ('{1}') (length={2})", m_PacketID, GetType().Name, length ); using (StreamWriter op = new StreamWriter("compression_overflow.log", true))
using ( StreamWriter op = new StreamWriter( "compression_overflow.log", true ) )
{ {
op.WriteLine("{0} Warning: Compression buffer overflowed on packet 0x{1:X2} ('{2}') (length={3})", DateTime.UtcNow, m_PacketID, GetType().Name, length); op.WriteLine("{0} Warning: Compression buffer overflowed on packet 0x{1:X2} ('{2}') (length={3})", DateTime.UtcNow, m_PacketID, GetType().Name, length);
op.WriteLine( new System.Diagnostics.StackTrace() ); op.WriteLine(new System.Diagnostics.StackTrace());
} }
} } else {
} m_CompiledLength = length;
if ( m_CompiledBuffer != null ) if (length > BufferSize || (m_State & State.Static) != 0) {
{ m_CompiledBuffer = new byte[length];
} else {
lock (m_Buffers)
m_CompiledBuffer = m_Buffers.AcquireBuffer();
m_State |= State.Buffered;
}
Buffer.BlockCopy(buffer, 0, m_CompiledBuffer, 0, length);
lock (m_CompressorBuffers)
m_CompressorBuffers.ReleaseBuffer(buffer);
}
} else if (length > 0) {
byte[] old = m_CompiledBuffer;
m_CompiledLength = length; m_CompiledLength = length;
byte[] old = m_CompiledBuffer; if ( length > BufferSize || (m_State & State.Static) != 0 ) {
if ( length > BufferSize || (m_State & State.Static) != 0 )
{
m_CompiledBuffer = new byte[length]; m_CompiledBuffer = new byte[length];
} } else {
else lock (m_Buffers)
{ m_CompiledBuffer = m_Buffers.AcquireBuffer();
m_CompiledBuffer = m_Buffers.AcquireBuffer();
m_State |= State.Buffered; m_State |= State.Buffered;
} }
Buffer.BlockCopy( old, 0, m_CompiledBuffer, 0, length ); Buffer.BlockCopy(old, 0, m_CompiledBuffer, 0, length);
} }
PacketWriter.ReleaseInstance( m_Stream ); PacketWriter.ReleaseInstance( m_Stream );

View file

@ -104,22 +104,27 @@ namespace Server.Network {
if ( m_CoalesceBufferSize == value ) if ( m_CoalesceBufferSize == value )
return; return;
if ( m_UnusedBuffers != null ) BufferPool old = m_UnusedBuffers;
m_UnusedBuffers.Free();
m_CoalesceBufferSize = value; lock (old) {
m_UnusedBuffers = new BufferPool( "Coalesced", 2048, m_CoalesceBufferSize ); if ( m_UnusedBuffers != null )
m_UnusedBuffers.Free();
m_CoalesceBufferSize = value;
m_UnusedBuffers = new BufferPool( "Coalesced", 2048, m_CoalesceBufferSize );
}
} }
} }
public static byte[] AcquireBuffer() { public static byte[] AcquireBuffer() {
return m_UnusedBuffers.AcquireBuffer(); lock (m_UnusedBuffers)
return m_UnusedBuffers.AcquireBuffer();
} }
public static void ReleaseBuffer( byte[] buffer ) { public static void ReleaseBuffer( byte[] buffer ) {
if ( buffer != null && buffer.Length == m_CoalesceBufferSize ) { lock (m_UnusedBuffers)
m_UnusedBuffers.ReleaseBuffer( buffer ); if ( buffer != null && buffer.Length == m_CoalesceBufferSize )
} m_UnusedBuffers.ReleaseBuffer( buffer );
} }
private Queue<Gram> _pending; private Queue<Gram> _pending;
@ -143,15 +148,9 @@ namespace Server.Network {
} }
public Gram CheckFlushReady() { public Gram CheckFlushReady() {
Gram gram = null; Gram gram = _buffered;
_pending.Enqueue(_buffered);
if ( _pending.Count == 0 && _buffered != null ) { _buffered = null;
gram = _buffered;
_pending.Enqueue( _buffered );
_buffered = null;
}
return gram; return gram;
} }
@ -169,7 +168,7 @@ namespace Server.Network {
return gram; return gram;
} }
private const int PendingCap = 96 * 1024; private const int PendingCap = 128 * 1024;
public Gram Enqueue( byte[] buffer, int length ) { public Gram Enqueue( byte[] buffer, int length ) {
return Enqueue( buffer, 0, length ); return Enqueue( buffer, 0, length );