Added Gump.OnServerClose -> called instead of Gump.OnResponse from Mobile.CloseGump and Mobile.CloseAllGumps
NetState properties Gumps, HuePickers, and Menus are now exposed as IEnumerable Deprecated Mobile.CloseGump( Type, int ), Mobile.CloseGump( Type, int, bool ) Deprecated Mobile.CloseAllGumps( bool ) Deprecated Mobile.HasGump( Type, bool ) Mobile FindGump, CloseGump, and HasGump methods now support type inheritance Refactored SendQueue to provide a more concise implementation
This commit is contained in:
parent
ab373c4084
commit
6095f152cf
5 changed files with 665 additions and 762 deletions
|
|
@ -418,5 +418,8 @@ namespace Server.Gumps
|
||||||
public virtual void OnResponse( NetState sender, RelayInfo info )
|
public virtual void OnResponse( NetState sender, RelayInfo info )
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public virtual void OnServerClose( NetState owner ) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
202
Server/Mobile.cs
202
Server/Mobile.cs
|
|
@ -6435,222 +6435,138 @@ namespace Server
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Send( Packet p )
|
public bool Send( Packet p ) {
|
||||||
{
|
|
||||||
return Send( p, false );
|
return Send( p, false );
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Send( Packet p, bool throwOnOffline )
|
public bool Send( Packet p, bool throwOnOffline ) {
|
||||||
{
|
if ( m_NetState != null ) {
|
||||||
if( m_NetState != null )
|
|
||||||
{
|
|
||||||
m_NetState.Send( p );
|
m_NetState.Send( p );
|
||||||
return true;
|
return true;
|
||||||
}
|
} else if ( throwOnOffline ) {
|
||||||
else if( throwOnOffline )
|
|
||||||
{
|
|
||||||
throw new MobileNotConnectedException( this, "Packet could not be sent." );
|
throw new MobileNotConnectedException( this, "Packet could not be sent." );
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool SendHuePicker( HuePicker p )
|
public bool SendHuePicker( HuePicker p ) {
|
||||||
{
|
|
||||||
return SendHuePicker( p, false );
|
return SendHuePicker( p, false );
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool SendHuePicker( HuePicker p, bool throwOnOffline )
|
public bool SendHuePicker( HuePicker p, bool throwOnOffline ) {
|
||||||
{
|
if ( m_NetState != null ) {
|
||||||
if( m_NetState != null )
|
|
||||||
{
|
|
||||||
p.SendTo( m_NetState );
|
p.SendTo( m_NetState );
|
||||||
return true;
|
return true;
|
||||||
}
|
} else if ( throwOnOffline ) {
|
||||||
else if( throwOnOffline )
|
|
||||||
{
|
|
||||||
throw new MobileNotConnectedException( this, "Hue picker could not be sent." );
|
throw new MobileNotConnectedException( this, "Hue picker could not be sent." );
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Gump FindGump( Type type )
|
public Gump FindGump( Type type ) {
|
||||||
{
|
|
||||||
NetState ns = m_NetState;
|
NetState ns = m_NetState;
|
||||||
|
|
||||||
if( ns != null )
|
if ( ns != null ) {
|
||||||
{
|
foreach ( Gump gump in ns.Gumps ) {
|
||||||
List<Gump> gumps = ns.Gumps;
|
if ( type.IsAssignableFrom( gump.GetType() ) ) {
|
||||||
|
return gump;
|
||||||
for( int i = 0; i < gumps.Count; ++i )
|
}
|
||||||
{
|
|
||||||
if( gumps[i].GetType() == type )
|
|
||||||
return gumps[i];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static readonly ClientVersion m_SendsReponseOnForcedGumpClose = new ClientVersion( "4.0.9b" );
|
public bool CloseGump( Type type ) {
|
||||||
|
if ( m_NetState != null ) {
|
||||||
public bool CloseGump( Type type )
|
|
||||||
{
|
|
||||||
return CloseGump( type, 0, false );
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool CloseGump( Type type, int buttonID )
|
|
||||||
{
|
|
||||||
return CloseGump( type, buttonID, false );
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool CloseGump( Type type, int buttonID, bool throwOnOffline )
|
|
||||||
{
|
|
||||||
if( m_NetState != null )
|
|
||||||
{
|
|
||||||
if( HasGump( type ) )
|
|
||||||
m_NetState.Send( new CloseGump( Gump.GetTypeID( type ), buttonID ) );
|
|
||||||
|
|
||||||
Gump gump = FindGump( type );
|
Gump gump = FindGump( type );
|
||||||
|
|
||||||
if( gump != null )
|
if ( gump != null ) {
|
||||||
{
|
m_NetState.Send( new CloseGump( gump.TypeID, 0 ) );
|
||||||
m_NetState.Send( new CloseGump( Gump.GetTypeID( type ), buttonID ) );
|
|
||||||
|
|
||||||
if( m_NetState.Version > m_SendsReponseOnForcedGumpClose )
|
m_NetState.RemoveGump( gump );
|
||||||
{
|
|
||||||
m_NetState.Gumps.Remove( gump );
|
gump.OnServerClose( m_NetState );
|
||||||
gump.OnResponse( m_NetState, new RelayInfo( buttonID, new int[0], new TextRelay[0] ) );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
} else {
|
||||||
else if( throwOnOffline )
|
|
||||||
{
|
|
||||||
throw new MobileNotConnectedException( this, "Gump close packet could not be sent." );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool CloseAllGumps()
|
[Obsolete( "Use CloseGump( Type ) instead." )]
|
||||||
{
|
public bool CloseGump( Type type, int buttonID ) {
|
||||||
return CloseAllGumps( false );
|
return CloseGump( type );
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool CloseAllGumps( bool throwOnOffline )
|
[Obsolete( "Use CloseGump( Type ) instead." )]
|
||||||
{
|
public bool CloseGump( Type type, int buttonID, bool throwOnOffline ) {
|
||||||
|
return CloseGump( type );
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool CloseAllGumps() {
|
||||||
NetState ns = m_NetState;
|
NetState ns = m_NetState;
|
||||||
|
|
||||||
if( ns != null )
|
if ( ns != null ) {
|
||||||
{
|
List<Gump> gumps = new List<Gump>( ns.Gumps );
|
||||||
List<Gump> gumps = ns.Gumps;
|
|
||||||
|
|
||||||
for( int i = 0; i < gumps.Count; ++i )
|
ns.ClearGumps();
|
||||||
ns.Send( new CloseGump( gumps[i].TypeID, 0 ) );
|
|
||||||
|
|
||||||
for( int i = gumps.Count - 1; i >= 0; --i )
|
|
||||||
{
|
|
||||||
Gump gump = gumps[i];
|
|
||||||
|
|
||||||
|
foreach ( Gump gump in gumps ) {
|
||||||
ns.Send( new CloseGump( gump.TypeID, 0 ) );
|
ns.Send( new CloseGump( gump.TypeID, 0 ) );
|
||||||
|
|
||||||
if( ns.Version > m_SendsReponseOnForcedGumpClose )
|
gump.OnServerClose( ns );
|
||||||
{
|
|
||||||
ns.Gumps.Remove( gump );
|
|
||||||
gump.OnResponse( ns, new RelayInfo( 0, new int[0], new TextRelay[0] ) );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
} else {
|
||||||
else if( throwOnOffline )
|
|
||||||
{
|
|
||||||
throw new MobileNotConnectedException( this, "Gump close packets could not be sent." );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool HasGump( Type type )
|
[Obsolete( "Use CloseAllGumps() instead.", false )]
|
||||||
{
|
public bool CloseAllGumps( bool throwOnOffline ) {
|
||||||
return HasGump( type, false );
|
return CloseAllGumps();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool HasGump( Type type, bool throwOnOffline )
|
public bool HasGump( Type type ) {
|
||||||
{
|
return ( FindGump( type ) != null );
|
||||||
NetState ns = m_NetState;
|
|
||||||
|
|
||||||
if( ns != null )
|
|
||||||
{
|
|
||||||
bool contains = false;
|
|
||||||
List<Gump> gumps = ns.Gumps;
|
|
||||||
|
|
||||||
for( int i = 0; !contains && i < gumps.Count; ++i )
|
|
||||||
contains = (gumps[i].GetType() == type);
|
|
||||||
|
|
||||||
return contains;
|
|
||||||
}
|
|
||||||
else if( throwOnOffline )
|
|
||||||
{
|
|
||||||
throw new MobileNotConnectedException( this, "Mobile is not connected." );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool SendGump( Gump g )
|
[Obsolete( "Use HasGump( Type ) instead.", false )]
|
||||||
{
|
public bool HasGump( Type type, bool throwOnOffline ) {
|
||||||
|
return HasGump( type );
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool SendGump( Gump g ) {
|
||||||
return SendGump( g, false );
|
return SendGump( g, false );
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool SendGump( Gump g, bool throwOnOffline )
|
public bool SendGump( Gump g, bool throwOnOffline ) {
|
||||||
{
|
if ( m_NetState != null ) {
|
||||||
if( m_NetState != null )
|
|
||||||
{
|
|
||||||
g.SendTo( m_NetState );
|
g.SendTo( m_NetState );
|
||||||
return true;
|
return true;
|
||||||
}
|
} else if ( throwOnOffline ) {
|
||||||
else if( throwOnOffline )
|
|
||||||
{
|
|
||||||
throw new MobileNotConnectedException( this, "Gump could not be sent." );
|
throw new MobileNotConnectedException( this, "Gump could not be sent." );
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool SendMenu( IMenu m )
|
public bool SendMenu( IMenu m ) {
|
||||||
{
|
|
||||||
return SendMenu( m, false );
|
return SendMenu( m, false );
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool SendMenu( IMenu m, bool throwOnOffline )
|
public bool SendMenu( IMenu m, bool throwOnOffline ) {
|
||||||
{
|
if ( m_NetState != null ) {
|
||||||
if( m_NetState != null )
|
|
||||||
{
|
|
||||||
m.SendTo( m_NetState );
|
m.SendTo( m_NetState );
|
||||||
return true;
|
return true;
|
||||||
}
|
} else if ( throwOnOffline ) {
|
||||||
else if( throwOnOffline )
|
|
||||||
{
|
|
||||||
throw new MobileNotConnectedException( this, "Menu could not be sent." );
|
throw new MobileNotConnectedException( this, "Menu could not be sent." );
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
|
|
@ -527,25 +527,18 @@ namespace Server.Network
|
||||||
from.Attack( m );
|
from.Attack( m );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void HuePickerResponse( NetState state, PacketReader pvSrc )
|
public static void HuePickerResponse( NetState state, PacketReader pvSrc ) {
|
||||||
{
|
|
||||||
int serial = pvSrc.ReadInt32();
|
int serial = pvSrc.ReadInt32();
|
||||||
int value = pvSrc.ReadInt16();
|
int value = pvSrc.ReadInt16();
|
||||||
int hue = pvSrc.ReadInt16() & 0x3FFF;
|
int hue = pvSrc.ReadInt16() & 0x3FFF;
|
||||||
|
|
||||||
hue = Utility.ClipDyedHue( hue );
|
hue = Utility.ClipDyedHue( hue );
|
||||||
|
|
||||||
List<HuePicker> pickers = state.HuePickers;
|
foreach ( HuePicker huePicker in state.HuePickers ) {
|
||||||
|
if ( huePicker.Serial == serial ) {
|
||||||
|
state.RemoveHuePicker( huePicker );
|
||||||
|
|
||||||
for ( int i = 0; i < pickers.Count; ++i )
|
huePicker.OnResponse( hue );
|
||||||
{
|
|
||||||
HuePicker p = pickers[i];
|
|
||||||
|
|
||||||
if ( p.Serial == serial )
|
|
||||||
{
|
|
||||||
state.RemoveHuePicker( i );
|
|
||||||
|
|
||||||
p.OnResponse( hue );
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -858,30 +851,26 @@ namespace Server.Network
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void MenuResponse( NetState state, PacketReader pvSrc )
|
public static void MenuResponse( NetState state, PacketReader pvSrc ) {
|
||||||
{
|
|
||||||
int serial = pvSrc.ReadInt32();
|
int serial = pvSrc.ReadInt32();
|
||||||
int menuID = pvSrc.ReadInt16(); // unused in our implementation
|
int menuID = pvSrc.ReadInt16(); // unused in our implementation
|
||||||
int index = pvSrc.ReadInt16();
|
int index = pvSrc.ReadInt16();
|
||||||
int itemID = pvSrc.ReadInt16();
|
int itemID = pvSrc.ReadInt16();
|
||||||
int hue = pvSrc.ReadInt16();
|
int hue = pvSrc.ReadInt16();
|
||||||
|
|
||||||
List<IMenu> menus = state.Menus;
|
index -= 1; // convert from 1-based to 0-based
|
||||||
|
|
||||||
for ( int i = 0; i < menus.Count; ++i )
|
foreach ( IMenu menu in state.Menus ) {
|
||||||
{
|
if ( menu.Serial == serial ) {
|
||||||
IMenu menu = menus[i];
|
state.RemoveMenu( menu );
|
||||||
|
|
||||||
if ( menu.Serial == serial )
|
if ( index >= 0 && index < menu.EntryLength ) {
|
||||||
{
|
menu.OnResponse( state, index );
|
||||||
if ( index > 0 && index <= menu.EntryLength )
|
} else {
|
||||||
menu.OnResponse( state, index - 1 );
|
|
||||||
else
|
|
||||||
menu.OnCancel( state );
|
menu.OnCancel( state );
|
||||||
|
}
|
||||||
|
|
||||||
state.RemoveMenu( i );
|
break;
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1088,25 +1077,17 @@ namespace Server.Network
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void DisplayGumpResponse( NetState state, PacketReader pvSrc )
|
public static void DisplayGumpResponse( NetState state, PacketReader pvSrc ) {
|
||||||
{
|
|
||||||
int serial = pvSrc.ReadInt32();
|
int serial = pvSrc.ReadInt32();
|
||||||
int typeID = pvSrc.ReadInt32();
|
int typeID = pvSrc.ReadInt32();
|
||||||
int buttonID = pvSrc.ReadInt32();
|
int buttonID = pvSrc.ReadInt32();
|
||||||
|
|
||||||
List<Gump> gumps = state.Gumps;
|
foreach ( Gump gump in state.Gumps ) {
|
||||||
|
if ( gump.Serial == serial && gump.TypeID == typeID ) {
|
||||||
for ( int i = 0; i < gumps.Count; ++i )
|
|
||||||
{
|
|
||||||
Gump gump = gumps[i];
|
|
||||||
|
|
||||||
if ( gump.Serial == serial && gump.TypeID == typeID )
|
|
||||||
{
|
|
||||||
int switchCount = pvSrc.ReadInt32();
|
int switchCount = pvSrc.ReadInt32();
|
||||||
|
|
||||||
if ( switchCount < 0 || switchCount > gump.m_Switches )
|
if ( switchCount < 0 || switchCount > gump.m_Switches ) {
|
||||||
{
|
state.WriteConsole( "Invalid gump response, disconnecting..." );
|
||||||
Console.WriteLine( "Client: {0}: Invalid gump response, disconnecting...", state );
|
|
||||||
state.Dispose();
|
state.Dispose();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -1118,51 +1099,51 @@ namespace Server.Network
|
||||||
|
|
||||||
int textCount = pvSrc.ReadInt32();
|
int textCount = pvSrc.ReadInt32();
|
||||||
|
|
||||||
if ( textCount < 0 || textCount > gump.m_TextEntries )
|
if ( textCount < 0 || textCount > gump.m_TextEntries ) {
|
||||||
{
|
state.WriteConsole( "Invalid gump response, disconnecting..." );
|
||||||
Console.WriteLine( "Client: {0}: Invalid gump response, disconnecting...", state );
|
|
||||||
state.Dispose();
|
state.Dispose();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
TextRelay[] textEntries = new TextRelay[textCount];
|
TextRelay[] textEntries = new TextRelay[textCount];
|
||||||
|
|
||||||
for ( int j = 0; j < textEntries.Length; ++j )
|
for ( int j = 0; j < textEntries.Length; ++j ) {
|
||||||
{
|
|
||||||
int entryID = pvSrc.ReadUInt16();
|
int entryID = pvSrc.ReadUInt16();
|
||||||
int textLength = pvSrc.ReadUInt16();
|
int textLength = pvSrc.ReadUInt16();
|
||||||
|
|
||||||
if ( textLength > 239 )
|
if ( textLength > 239 ) {
|
||||||
|
state.WriteConsole( "Invalid gump response, disconnecting..." );
|
||||||
|
state.Dispose();
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
string text = pvSrc.ReadUnicodeStringSafe( textLength );
|
string text = pvSrc.ReadUnicodeStringSafe( textLength );
|
||||||
textEntries[j] = new TextRelay( entryID, text );
|
textEntries[j] = new TextRelay( entryID, text );
|
||||||
}
|
}
|
||||||
|
|
||||||
state.RemoveGump( i );
|
state.RemoveGump( gump );
|
||||||
|
|
||||||
gump.OnResponse( state, new RelayInfo( buttonID, switches, textEntries ) );
|
gump.OnResponse( state, new RelayInfo( buttonID, switches, textEntries ) );
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( typeID == 461 ) // Virtue gump
|
if ( typeID == 461 ) { // Virtue gump
|
||||||
{
|
|
||||||
int switchCount = pvSrc.ReadInt32();
|
int switchCount = pvSrc.ReadInt32();
|
||||||
|
|
||||||
if ( buttonID == 1 && switchCount > 0 )
|
if ( buttonID == 1 && switchCount > 0 ) {
|
||||||
{
|
|
||||||
Mobile beheld = World.FindMobile( pvSrc.ReadInt32() );
|
Mobile beheld = World.FindMobile( pvSrc.ReadInt32() );
|
||||||
|
|
||||||
if ( beheld != null )
|
if ( beheld != null ) {
|
||||||
EventSink.InvokeVirtueGumpRequest( new VirtueGumpRequestEventArgs( state.Mobile, beheld ) );
|
EventSink.InvokeVirtueGumpRequest( new VirtueGumpRequestEventArgs( state.Mobile, beheld ) );
|
||||||
}
|
}
|
||||||
else
|
} else {
|
||||||
{
|
|
||||||
Mobile beheld = World.FindMobile( serial );
|
Mobile beheld = World.FindMobile( serial );
|
||||||
|
|
||||||
if ( beheld != null )
|
if ( beheld != null ) {
|
||||||
EventSink.InvokeVirtueItemRequest( new VirtueItemRequestEventArgs( state.Mobile, beheld, buttonID ) );
|
EventSink.InvokeVirtueItemRequest( new VirtueItemRequestEventArgs( state.Mobile, beheld, buttonID ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,52 +23,79 @@ using System.IO;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Server.Network
|
namespace Server.Network {
|
||||||
{
|
public class SendQueue {
|
||||||
public enum SendEnqueueResult
|
public class Gram {
|
||||||
{
|
private static Stack<Gram> _pool = new Stack<Gram>();
|
||||||
Begin,
|
|
||||||
Delay,
|
|
||||||
Overflow
|
|
||||||
}
|
|
||||||
|
|
||||||
public class SendQueue
|
public static Gram Acquire() {
|
||||||
{
|
lock ( _pool ) {
|
||||||
private class Entry
|
Gram gram;
|
||||||
{
|
|
||||||
public byte[] m_Buffer;
|
|
||||||
public int m_Length;
|
|
||||||
|
|
||||||
private Entry( byte[] buffer, int length )
|
if ( _pool.Count > 0 ) {
|
||||||
{
|
gram = _pool.Pop();
|
||||||
m_Buffer = buffer;
|
} else {
|
||||||
m_Length = length;
|
gram = new Gram();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Stack<Entry> m_Pool = new Stack<Entry>();
|
gram._buffer = AcquireBuffer();
|
||||||
|
gram._length = 0;
|
||||||
|
|
||||||
public static Entry Pool( byte[] buffer, int length )
|
return gram;
|
||||||
{
|
|
||||||
lock ( m_Pool )
|
|
||||||
{
|
|
||||||
if ( m_Pool.Count == 0 )
|
|
||||||
return new Entry( buffer, length );
|
|
||||||
|
|
||||||
Entry e = m_Pool.Pop();
|
|
||||||
|
|
||||||
e.m_Buffer = buffer;
|
|
||||||
e.m_Length = length;
|
|
||||||
|
|
||||||
return e;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Release( Entry e )
|
public static void Release( Gram e ) {
|
||||||
{
|
lock ( _pool ) {
|
||||||
lock ( m_Pool )
|
_pool.Push( e );
|
||||||
{
|
ReleaseBuffer( e._buffer );
|
||||||
m_Pool.Push( e );
|
}
|
||||||
ReleaseBuffer( e.m_Buffer );
|
}
|
||||||
|
|
||||||
|
private byte[] _buffer;
|
||||||
|
private int _length;
|
||||||
|
|
||||||
|
public byte[] Buffer {
|
||||||
|
get {
|
||||||
|
return _buffer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Length {
|
||||||
|
get {
|
||||||
|
return _length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Available {
|
||||||
|
get {
|
||||||
|
return ( _buffer.Length - _length );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsFull {
|
||||||
|
get {
|
||||||
|
return ( _length == _buffer.Length );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Gram() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Write( byte[] buffer, int offset, int length ) {
|
||||||
|
int write = Math.Min( length, this.Available );
|
||||||
|
|
||||||
|
System.Buffer.BlockCopy( buffer, offset, _buffer, _length, write );
|
||||||
|
|
||||||
|
_length += write;
|
||||||
|
|
||||||
|
return write;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Release() {
|
||||||
|
lock ( _pool ) {
|
||||||
|
_pool.Push( this );
|
||||||
|
ReleaseBuffer( _buffer );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -76,11 +103,11 @@ namespace Server.Network
|
||||||
private static int m_CoalesceBufferSize = 512;
|
private static int m_CoalesceBufferSize = 512;
|
||||||
private static BufferPool m_UnusedBuffers = new BufferPool( "Coalesced", 2048, m_CoalesceBufferSize );
|
private static BufferPool m_UnusedBuffers = new BufferPool( "Coalesced", 2048, m_CoalesceBufferSize );
|
||||||
|
|
||||||
public static int CoalesceBufferSize
|
public static int CoalesceBufferSize {
|
||||||
{
|
get {
|
||||||
get{ return m_CoalesceBufferSize; }
|
return m_CoalesceBufferSize;
|
||||||
set
|
}
|
||||||
{
|
set {
|
||||||
if ( m_CoalesceBufferSize == value )
|
if ( m_CoalesceBufferSize == value )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
@ -92,132 +119,126 @@ namespace Server.Network
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] GetUnusedBuffer()
|
public static byte[] AcquireBuffer() {
|
||||||
{
|
|
||||||
return m_UnusedBuffers.AcquireBuffer();
|
return m_UnusedBuffers.AcquireBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void ReleaseBuffer( byte[] buffer )
|
public static void ReleaseBuffer( byte[] buffer ) {
|
||||||
{
|
if ( buffer != null && buffer.Length == m_CoalesceBufferSize ) {
|
||||||
if ( buffer == null )
|
|
||||||
Console.WriteLine( "Warning: Attempting to release null packet buffer" );
|
|
||||||
else if ( buffer.Length == m_CoalesceBufferSize )
|
|
||||||
m_UnusedBuffers.ReleaseBuffer( buffer );
|
m_UnusedBuffers.ReleaseBuffer( buffer );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Queue<Entry> m_Queue;
|
private Queue<Gram> _pending;
|
||||||
|
|
||||||
private Entry m_Buffered;
|
private Gram _buffered;
|
||||||
|
|
||||||
public bool IsFlushReady{ get{ return ( m_Queue.Count == 0 && m_Buffered != null ); } }
|
public bool IsFlushReady {
|
||||||
public bool IsEmpty{ get{ return ( m_Queue.Count == 0 && m_Buffered == null ); } }
|
get {
|
||||||
|
return ( _pending.Count == 0 && _buffered != null );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void Clear()
|
public bool IsEmpty {
|
||||||
{
|
get {
|
||||||
if ( m_Buffered != null )
|
return ( _pending.Count == 0 && _buffered == null );
|
||||||
{
|
}
|
||||||
Entry.Release( m_Buffered );
|
}
|
||||||
m_Buffered = null;
|
|
||||||
|
public SendQueue() {
|
||||||
|
_pending = new Queue<Gram>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Gram CheckFlushReady() {
|
||||||
|
Gram gram = null;
|
||||||
|
|
||||||
|
if ( _pending.Count == 0 && _buffered != null ) {
|
||||||
|
gram = _buffered;
|
||||||
|
|
||||||
|
_pending.Enqueue( _buffered );
|
||||||
|
_buffered = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
while ( m_Queue.Count > 0 )
|
return gram;
|
||||||
Entry.Release( m_Queue.Dequeue() );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] CheckFlushReady( ref int length )
|
public Gram Dequeue() {
|
||||||
{
|
Gram gram = null;
|
||||||
Entry buffered = m_Buffered;
|
|
||||||
|
|
||||||
if ( m_Queue.Count == 0 && buffered != null )
|
if ( _pending.Count > 0 ) {
|
||||||
{
|
_pending.Dequeue().Release();
|
||||||
m_Buffered = null;
|
|
||||||
|
|
||||||
m_Queue.Enqueue( buffered );
|
if ( _pending.Count > 0 ) {
|
||||||
length = buffered.m_Length;
|
gram = _pending.Peek();
|
||||||
return buffered.m_Buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SendQueue()
|
|
||||||
{
|
|
||||||
m_Queue = new Queue<Entry>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] Peek( ref int length )
|
|
||||||
{
|
|
||||||
if ( m_Queue.Count > 0 )
|
|
||||||
{
|
|
||||||
Entry entry = m_Queue.Peek();
|
|
||||||
|
|
||||||
length = entry.m_Length;
|
|
||||||
return entry.m_Buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] Dequeue( ref int length )
|
|
||||||
{
|
|
||||||
Entry.Release( m_Queue.Dequeue() );
|
|
||||||
|
|
||||||
if ( m_Queue.Count > 0 )
|
|
||||||
{
|
|
||||||
Entry entry = m_Queue.Peek();
|
|
||||||
|
|
||||||
length = entry.m_Length;
|
|
||||||
return entry.m_Buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private const int PendingCap = 96*1024;
|
|
||||||
|
|
||||||
public SendEnqueueResult Enqueue( byte[] buffer, int length )
|
|
||||||
{
|
|
||||||
if ( buffer == null )
|
|
||||||
{
|
|
||||||
Console.WriteLine( "Warning: Attempting to send null packet buffer" );
|
|
||||||
return SendEnqueueResult.Delay;
|
|
||||||
}
|
|
||||||
|
|
||||||
int existingBytes = ( m_Queue.Count * m_CoalesceBufferSize ) + ( m_Buffered == null ? 0 : m_Buffered.m_Length );
|
|
||||||
|
|
||||||
if ( (existingBytes + length) > PendingCap )
|
|
||||||
return SendEnqueueResult.Overflow;
|
|
||||||
|
|
||||||
int offset = 0; // offset into buffer
|
|
||||||
int remaining = length; // byte count remaining
|
|
||||||
|
|
||||||
bool startNow = false; // should we start sending the first chunk?
|
|
||||||
|
|
||||||
while ( remaining > 0 )
|
|
||||||
{
|
|
||||||
if ( m_Buffered == null ) // nothing yet buffered
|
|
||||||
m_Buffered = Entry.Pool( GetUnusedBuffer(), 0 );
|
|
||||||
|
|
||||||
byte[] page = m_Buffered.m_Buffer; // buffer page
|
|
||||||
int pageSpace = page.Length - m_Buffered.m_Length; // available bytes in page
|
|
||||||
int byteCount = ( remaining > pageSpace ? pageSpace : remaining ); // how many we can copy over
|
|
||||||
|
|
||||||
Buffer.BlockCopy( buffer, offset, page, m_Buffered.m_Length, byteCount ); // copy the data
|
|
||||||
|
|
||||||
// apply offsets
|
|
||||||
m_Buffered.m_Length += byteCount;
|
|
||||||
offset += byteCount;
|
|
||||||
remaining -= byteCount;
|
|
||||||
|
|
||||||
if ( m_Buffered.m_Length == page.Length ) // page full
|
|
||||||
{
|
|
||||||
startNow = ( startNow || m_Queue.Count == 0 );
|
|
||||||
m_Queue.Enqueue( m_Buffered );
|
|
||||||
m_Buffered = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ( startNow ? SendEnqueueResult.Begin : SendEnqueueResult.Delay );
|
return gram;
|
||||||
|
}
|
||||||
|
|
||||||
|
private const int PendingCap = 96 * 1024;
|
||||||
|
|
||||||
|
public Gram Enqueue( byte[] buffer, int length ) {
|
||||||
|
return Enqueue( buffer, 0, length );
|
||||||
|
}
|
||||||
|
|
||||||
|
public Gram Enqueue( byte[] buffer, int offset, int length ) {
|
||||||
|
if ( buffer == null ) {
|
||||||
|
throw new ArgumentNullException( "buffer" );
|
||||||
|
} else if ( !(offset >= 0 && offset < buffer.Length) ) {
|
||||||
|
throw new ArgumentOutOfRangeException( "offset", offset, "Offset must be greater than or equal to zero and less than the size of the buffer." );
|
||||||
|
} else if ( length < 0 || length > buffer.Length ) {
|
||||||
|
throw new ArgumentOutOfRangeException( "length", length, "Length cannot be less than zero or greater than the size of the buffer." );
|
||||||
|
} else if ( ( buffer.Length - offset ) < length ) {
|
||||||
|
throw new ArgumentException( "Offset and length do not point to a valid segment within the buffer." );
|
||||||
|
}
|
||||||
|
|
||||||
|
int existingBytes = ( _pending.Count * m_CoalesceBufferSize ) + ( _buffered == null ? 0 : _buffered.Length );
|
||||||
|
|
||||||
|
if ( ( existingBytes + length ) > PendingCap ) {
|
||||||
|
throw new CapacityExceededException();
|
||||||
|
}
|
||||||
|
|
||||||
|
Gram gram = null;
|
||||||
|
|
||||||
|
while ( length > 0 ) {
|
||||||
|
if ( _buffered == null ) { // nothing yet buffered
|
||||||
|
_buffered = Gram.Acquire();
|
||||||
|
}
|
||||||
|
|
||||||
|
int bytesWritten = _buffered.Write( buffer, offset, length );
|
||||||
|
|
||||||
|
offset += bytesWritten;
|
||||||
|
length -= bytesWritten;
|
||||||
|
|
||||||
|
if ( _buffered.IsFull ) {
|
||||||
|
if ( _pending.Count == 0 ) {
|
||||||
|
gram = _buffered;
|
||||||
|
}
|
||||||
|
|
||||||
|
_pending.Enqueue( _buffered );
|
||||||
|
_buffered = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return gram;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear() {
|
||||||
|
if ( _buffered != null ) {
|
||||||
|
_buffered.Release();
|
||||||
|
_buffered = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
while ( _pending.Count > 0 ) {
|
||||||
|
_pending.Dequeue().Release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class CapacityExceededException : Exception {
|
||||||
|
public CapacityExceededException()
|
||||||
|
: base( "Too much data pending." ) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue