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:
krrios 2006-08-27 20:35:58 +00:00
parent ab373c4084
commit 6095f152cf
5 changed files with 665 additions and 762 deletions

View file

@ -418,5 +418,8 @@ namespace Server.Gumps
public virtual void OnResponse( NetState sender, RelayInfo info )
{
}
public virtual void OnServerClose( NetState owner ) {
}
}
}

View file

@ -6435,222 +6435,138 @@ namespace Server
}
}
public bool Send( Packet p )
{
public bool Send( Packet p ) {
return Send( p, false );
}
public bool Send( Packet p, bool throwOnOffline )
{
if( m_NetState != null )
{
public bool Send( Packet p, bool throwOnOffline ) {
if ( m_NetState != null ) {
m_NetState.Send( p );
return true;
}
else if( throwOnOffline )
{
} else if ( throwOnOffline ) {
throw new MobileNotConnectedException( this, "Packet could not be sent." );
}
else
{
} else {
return false;
}
}
public bool SendHuePicker( HuePicker p )
{
public bool SendHuePicker( HuePicker p ) {
return SendHuePicker( p, false );
}
public bool SendHuePicker( HuePicker p, bool throwOnOffline )
{
if( m_NetState != null )
{
public bool SendHuePicker( HuePicker p, bool throwOnOffline ) {
if ( m_NetState != null ) {
p.SendTo( m_NetState );
return true;
}
else if( throwOnOffline )
{
} else if ( throwOnOffline ) {
throw new MobileNotConnectedException( this, "Hue picker could not be sent." );
}
else
{
} else {
return false;
}
}
public Gump FindGump( Type type )
{
public Gump FindGump( Type type ) {
NetState ns = m_NetState;
if( ns != null )
{
List<Gump> gumps = ns.Gumps;
for( int i = 0; i < gumps.Count; ++i )
{
if( gumps[i].GetType() == type )
return gumps[i];
if ( ns != null ) {
foreach ( Gump gump in ns.Gumps ) {
if ( type.IsAssignableFrom( gump.GetType() ) ) {
return gump;
}
}
}
return null;
}
private static readonly ClientVersion m_SendsReponseOnForcedGumpClose = new ClientVersion( "4.0.9b" );
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 ) );
public bool CloseGump( Type type ) {
if ( m_NetState != null ) {
Gump gump = FindGump( type );
if( gump != null )
{
m_NetState.Send( new CloseGump( Gump.GetTypeID( type ), buttonID ) );
if ( gump != null ) {
m_NetState.Send( new CloseGump( gump.TypeID, 0 ) );
if( m_NetState.Version > m_SendsReponseOnForcedGumpClose )
{
m_NetState.Gumps.Remove( gump );
gump.OnResponse( m_NetState, new RelayInfo( buttonID, new int[0], new TextRelay[0] ) );
}
m_NetState.RemoveGump( gump );
gump.OnServerClose( m_NetState );
}
return true;
}
else if( throwOnOffline )
{
throw new MobileNotConnectedException( this, "Gump close packet could not be sent." );
}
else
{
} else {
return false;
}
}
public bool CloseAllGumps()
{
return CloseAllGumps( false );
[Obsolete( "Use CloseGump( Type ) instead." )]
public bool CloseGump( Type type, int buttonID ) {
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;
if( ns != null )
{
List<Gump> gumps = ns.Gumps;
if ( ns != null ) {
List<Gump> gumps = new List<Gump>( ns.Gumps );
for( int i = 0; i < gumps.Count; ++i )
ns.Send( new CloseGump( gumps[i].TypeID, 0 ) );
for( int i = gumps.Count - 1; i >= 0; --i )
{
Gump gump = gumps[i];
ns.ClearGumps();
foreach ( Gump gump in gumps ) {
ns.Send( new CloseGump( gump.TypeID, 0 ) );
if( ns.Version > m_SendsReponseOnForcedGumpClose )
{
ns.Gumps.Remove( gump );
gump.OnResponse( ns, new RelayInfo( 0, new int[0], new TextRelay[0] ) );
}
gump.OnServerClose( ns );
}
return true;
}
else if( throwOnOffline )
{
throw new MobileNotConnectedException( this, "Gump close packets could not be sent." );
}
else
{
} else {
return false;
}
}
public bool HasGump( Type type )
{
return HasGump( type, false );
[Obsolete( "Use CloseAllGumps() instead.", false )]
public bool CloseAllGumps( bool throwOnOffline ) {
return CloseAllGumps();
}
public bool HasGump( Type type, bool throwOnOffline )
{
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 HasGump( Type type ) {
return ( FindGump( type ) != null );
}
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 );
}
public bool SendGump( Gump g, bool throwOnOffline )
{
if( m_NetState != null )
{
public bool SendGump( Gump g, bool throwOnOffline ) {
if ( m_NetState != null ) {
g.SendTo( m_NetState );
return true;
}
else if( throwOnOffline )
{
} else if ( throwOnOffline ) {
throw new MobileNotConnectedException( this, "Gump could not be sent." );
}
else
{
} else {
return false;
}
}
public bool SendMenu( IMenu m )
{
public bool SendMenu( IMenu m ) {
return SendMenu( m, false );
}
public bool SendMenu( IMenu m, bool throwOnOffline )
{
if( m_NetState != null )
{
public bool SendMenu( IMenu m, bool throwOnOffline ) {
if ( m_NetState != null ) {
m.SendTo( m_NetState );
return true;
}
else if( throwOnOffline )
{
} else if ( throwOnOffline ) {
throw new MobileNotConnectedException( this, "Menu could not be sent." );
}
else
{
} else {
return false;
}
}

File diff suppressed because it is too large Load diff

View file

@ -527,25 +527,18 @@ namespace Server.Network
from.Attack( m );
}
public static void HuePickerResponse( NetState state, PacketReader pvSrc )
{
public static void HuePickerResponse( NetState state, PacketReader pvSrc ) {
int serial = pvSrc.ReadInt32();
int value = pvSrc.ReadInt16();
int hue = pvSrc.ReadInt16() & 0x3FFF;
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 p = pickers[i];
if ( p.Serial == serial )
{
state.RemoveHuePicker( i );
p.OnResponse( hue );
huePicker.OnResponse( hue );
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 menuID = pvSrc.ReadInt16(); // unused in our implementation
int index = pvSrc.ReadInt16();
int index = 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 )
{
IMenu menu = menus[i];
foreach ( IMenu menu in state.Menus ) {
if ( menu.Serial == serial ) {
state.RemoveMenu( menu );
if ( menu.Serial == serial )
{
if ( index > 0 && index <= menu.EntryLength )
menu.OnResponse( state, index - 1 );
else
if ( index >= 0 && index < menu.EntryLength ) {
menu.OnResponse( state, index );
} else {
menu.OnCancel( state );
}
state.RemoveMenu( i );
return;
break;
}
}
}
@ -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 typeID = pvSrc.ReadInt32();
int buttonID = pvSrc.ReadInt32();
List<Gump> gumps = state.Gumps;
for ( int i = 0; i < gumps.Count; ++i )
{
Gump gump = gumps[i];
if ( gump.Serial == serial && gump.TypeID == typeID )
{
foreach ( Gump gump in state.Gumps ) {
if ( gump.Serial == serial && gump.TypeID == typeID ) {
int switchCount = pvSrc.ReadInt32();
if ( switchCount < 0 || switchCount > gump.m_Switches )
{
Console.WriteLine( "Client: {0}: Invalid gump response, disconnecting...", state );
if ( switchCount < 0 || switchCount > gump.m_Switches ) {
state.WriteConsole( "Invalid gump response, disconnecting..." );
state.Dispose();
return;
}
@ -1118,51 +1099,51 @@ namespace Server.Network
int textCount = pvSrc.ReadInt32();
if ( textCount < 0 || textCount > gump.m_TextEntries )
{
Console.WriteLine( "Client: {0}: Invalid gump response, disconnecting...", state );
if ( textCount < 0 || textCount > gump.m_TextEntries ) {
state.WriteConsole( "Invalid gump response, disconnecting..." );
state.Dispose();
return;
}
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 textLength = pvSrc.ReadUInt16();
if ( textLength > 239 )
if ( textLength > 239 ) {
state.WriteConsole( "Invalid gump response, disconnecting..." );
state.Dispose();
return;
}
string text = pvSrc.ReadUnicodeStringSafe( textLength );
textEntries[j] = new TextRelay( entryID, text );
}
state.RemoveGump( i );
state.RemoveGump( gump );
gump.OnResponse( state, new RelayInfo( buttonID, switches, textEntries ) );
return;
}
}
if ( typeID == 461 ) // Virtue gump
{
if ( typeID == 461 ) { // Virtue gump
int switchCount = pvSrc.ReadInt32();
if ( buttonID == 1 && switchCount > 0 )
{
if ( buttonID == 1 && switchCount > 0 ) {
Mobile beheld = World.FindMobile( pvSrc.ReadInt32() );
if ( beheld != null )
if ( beheld != null ) {
EventSink.InvokeVirtueGumpRequest( new VirtueGumpRequestEventArgs( state.Mobile, beheld ) );
}
else
{
}
} else {
Mobile beheld = World.FindMobile( serial );
if ( beheld != null )
if ( beheld != null ) {
EventSink.InvokeVirtueItemRequest( new VirtueItemRequestEventArgs( state.Mobile, beheld, buttonID ) );
}
}
}
}

View file

@ -23,52 +23,79 @@ using System.IO;
using System.Collections;
using System.Collections.Generic;
namespace Server.Network
{
public enum SendEnqueueResult
{
Begin,
Delay,
Overflow
}
namespace Server.Network {
public class SendQueue {
public class Gram {
private static Stack<Gram> _pool = new Stack<Gram>();
public class SendQueue
{
private class Entry
{
public byte[] m_Buffer;
public int m_Length;
public static Gram Acquire() {
lock ( _pool ) {
Gram gram;
private Entry( byte[] buffer, int length )
{
m_Buffer = buffer;
m_Length = length;
}
if ( _pool.Count > 0 ) {
gram = _pool.Pop();
} else {
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 )
{
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;
return gram;
}
}
public static void Release( Entry e )
{
lock ( m_Pool )
{
m_Pool.Push( e );
ReleaseBuffer( e.m_Buffer );
public static void Release( Gram e ) {
lock ( _pool ) {
_pool.Push( e );
ReleaseBuffer( e._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 BufferPool m_UnusedBuffers = new BufferPool( "Coalesced", 2048, m_CoalesceBufferSize );
public static int CoalesceBufferSize
{
get{ return m_CoalesceBufferSize; }
set
{
public static int CoalesceBufferSize {
get {
return m_CoalesceBufferSize;
}
set {
if ( m_CoalesceBufferSize == value )
return;
@ -92,132 +119,126 @@ namespace Server.Network
}
}
public static byte[] GetUnusedBuffer()
{
public static byte[] AcquireBuffer() {
return m_UnusedBuffers.AcquireBuffer();
}
public static void ReleaseBuffer( byte[] buffer )
{
if ( buffer == null )
Console.WriteLine( "Warning: Attempting to release null packet buffer" );
else if ( buffer.Length == m_CoalesceBufferSize )
public static void ReleaseBuffer( byte[] buffer ) {
if ( buffer != null && buffer.Length == m_CoalesceBufferSize ) {
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 IsEmpty{ get{ return ( m_Queue.Count == 0 && m_Buffered == null ); } }
public bool IsFlushReady {
get {
return ( _pending.Count == 0 && _buffered != null );
}
}
public void Clear()
{
if ( m_Buffered != null )
{
Entry.Release( m_Buffered );
m_Buffered = null;
public bool IsEmpty {
get {
return ( _pending.Count == 0 && _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 )
Entry.Release( m_Queue.Dequeue() );
return gram;
}
public byte[] CheckFlushReady( ref int length )
{
Entry buffered = m_Buffered;
public Gram Dequeue() {
Gram gram = null;
if ( m_Queue.Count == 0 && buffered != null )
{
m_Buffered = null;
if ( _pending.Count > 0 ) {
_pending.Dequeue().Release();
m_Queue.Enqueue( buffered );
length = buffered.m_Length;
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;
if ( _pending.Count > 0 ) {
gram = _pending.Peek();
}
}
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." ) {
}
}
}