Framework_4_0 preprocessor is necessary for legacy compatibility.

This commit is contained in:
mark 2012-01-03 00:36:52 +00:00
parent 12fe97babd
commit 558fcf495a
15 changed files with 755 additions and 46 deletions

View file

@ -51,7 +51,11 @@ namespace Server.Network {
private bool m_Seeded;
private bool m_Running;
#if Framework_4_0
private SocketAsyncEventArgs m_ReceiveEventArgs, m_SendEventArgs;
#else
private AsyncCallback m_OnReceive, m_OnSend;
#endif
private MessagePump m_MessagePump;
private ServerInfo[] m_ServerInfo;
@ -611,8 +615,17 @@ namespace Server.Network {
}
if ( gram != null ) {
#if Framework_4_0
m_SendEventArgs.SetBuffer( gram.Buffer, 0, gram.Length );
Send_Start();
#else
try {
m_Socket.BeginSend( gram.Buffer, 0, gram.Length, SocketFlags.None, m_OnSend, m_Socket );
} catch ( Exception ex ) {
TraceException( ex );
Dispose( false );
}
#endif
}
} catch ( CapacityExceededException ) {
Console.WriteLine( "Client: {0}: Too much data pending, disconnecting...", this );
@ -635,6 +648,7 @@ namespace Server.Network {
}
}
#if Framework_4_0
public void Start() {
m_ReceiveEventArgs = new SocketAsyncEventArgs();
m_ReceiveEventArgs.Completed += new EventHandler<SocketAsyncEventArgs>( Receive_Completion );
@ -810,6 +824,172 @@ namespace Server.Network {
return false;
}
#else
public void Start() {
m_OnReceive = new AsyncCallback( OnReceive );
m_OnSend = new AsyncCallback( OnSend );
m_Running = true;
if ( m_Socket == null || m_Paused ) {
return;
}
try {
lock ( m_AsyncLock ) {
if ( ( m_AsyncState & ( AsyncState.Pending | AsyncState.Paused ) ) == 0 ) {
InternalBeginReceive();
}
}
} catch ( Exception ex ) {
TraceException( ex );
Dispose( false );
}
}
private void InternalBeginReceive() {
m_AsyncState |= AsyncState.Pending;
m_Socket.BeginReceive( m_RecvBuffer, 0, m_RecvBuffer.Length, SocketFlags.None, m_OnReceive, m_Socket );
}
private void OnReceive( IAsyncResult asyncResult ) {
Socket s = (Socket)asyncResult.AsyncState;
try {
int byteCount = s.EndReceive( asyncResult );
if ( byteCount > 0 ) {
m_NextCheckActivity = DateTime.Now + TimeSpan.FromMinutes( 1.2 );
byte[] buffer = m_RecvBuffer;
if ( m_Encoder != null )
m_Encoder.DecodeIncomingPacket( this, ref buffer, ref byteCount );
lock ( m_Buffer )
m_Buffer.Enqueue( buffer, 0, byteCount );
m_MessagePump.OnReceive( this );
lock ( m_AsyncLock ) {
m_AsyncState &= ~AsyncState.Pending;
if ( ( m_AsyncState & AsyncState.Paused ) == 0 ) {
try {
InternalBeginReceive();
} catch ( Exception ex ) {
TraceException( ex );
Dispose( false );
}
}
}
} else {
Dispose( false );
}
} catch {
Dispose( false );
}
}
private void OnSend( IAsyncResult asyncResult ) {
Socket s = (Socket)asyncResult.AsyncState;
try {
int bytes = s.EndSend( asyncResult );
if ( bytes <= 0 ) {
Dispose( false );
return;
}
m_NextCheckActivity = DateTime.Now + TimeSpan.FromMinutes( 1.2 );
if ( m_CoalesceSleep >= 0 ) {
Thread.Sleep( m_CoalesceSleep );
}
SendQueue.Gram gram;
lock ( m_SendQueue ) {
gram = m_SendQueue.Dequeue();
}
if ( gram != null ) {
try {
s.BeginSend( gram.Buffer, 0, gram.Length, SocketFlags.None, m_OnSend, s );
} catch ( Exception ex ) {
TraceException( ex );
Dispose( false );
}
}
} catch ( Exception ){
Dispose( false );
}
}
public static void Pause() {
m_Paused = true;
for ( int i = 0; i < m_Instances.Count; ++i ) {
NetState ns = m_Instances[i];
lock ( ns.m_AsyncLock ) {
ns.m_AsyncState |= AsyncState.Paused;
}
}
}
public static void Resume() {
m_Paused = false;
for ( int i = 0; i < m_Instances.Count; ++i ) {
NetState ns = m_Instances[i];
if ( ns.m_Socket == null ) {
continue;
}
lock ( ns.m_AsyncLock ) {
ns.m_AsyncState &= ~AsyncState.Paused;
try {
if ( ( ns.m_AsyncState & AsyncState.Pending ) == 0 )
ns.InternalBeginReceive();
} catch ( Exception ex ) {
TraceException( ex );
ns.Dispose( false );
}
}
}
}
public bool Flush() {
if ( m_Socket == null || !m_SendQueue.IsFlushReady ) {
return false;
}
SendQueue.Gram gram;
lock ( m_SendQueue ) {
gram = m_SendQueue.CheckFlushReady();
}
if ( gram != null ) {
try {
m_Socket.BeginSend( gram.Buffer, 0, gram.Length, SocketFlags.None, m_OnSend, m_Socket );
return true;
} catch ( Exception ex ) {
TraceException( ex );
Dispose( false );
}
}
return false;
}
#endif
public PacketHandler GetHandler( int packetID )
{
if ( ContainerGridLines )
@ -908,8 +1088,13 @@ namespace Server.Network {
m_Buffer = null;
m_RecvBuffer = null;
#if Framework_4_0
m_ReceiveEventArgs = null;
m_SendEventArgs = null;
#else
m_OnReceive = null;
m_OnSend = null;
#endif
m_Running = false;