📄 abstractsessionacceptor.cs
字号:
namespace NCindy.Session
{
using NCindy;
using NCindy.Session.AIO;
using NCindy.Util;
using NCindy.Util.Logging;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading;
public abstract class AbstractSessionAcceptor : ISessionAcceptor
{
protected int acceptedCount;
protected int backLog;
protected readonly ReaderWriterLock connectedSessionLock = new ReaderWriterLock();
protected Dictionary<IPEndPoint, ISession> connectedSessions = new Dictionary<IPEndPoint, ISession>();
protected IPEndPoint listenEndPoint;
private static readonly ILogger log = LogFactory.CreateLogger(MethodBase.GetCurrentMethod().ReflectedType);
protected IPacketDecoder packetDecoder;
protected IPacketEncoder packetEncoder;
protected const int READER_WRITER_LOCK_TIMEOUT = 0x3e8;
protected bool reuseAddress;
protected readonly EventHandler<SessionEventArgs> sessionClosedEventHandler;
protected readonly EventHandler<ExceptionEventArgs> sessionExceptionCaughtEventHandler;
protected NCindy.SessionType sessionType;
protected bool started;
public event EventHandler<ExceptionEventArgs> ExceptionCaught;
public event EventHandler<SessionAcceptedEventArgs> SessionAccepted;
public AbstractSessionAcceptor()
{
this.sessionClosedEventHandler = new EventHandler<SessionEventArgs>(this, (IntPtr) this.OnSessionClosed);
this.sessionExceptionCaughtEventHandler = new EventHandler<ExceptionEventArgs>(this, (IntPtr) this.OnSessionCaughtException);
}
protected virtual void AcceptedSession(SessionAcceptedEventArgs sessionAcceptedEventArgs)
{
if (this.SessionAccepted != null)
{
try
{
this.SessionAccepted.Invoke(this, sessionAcceptedEventArgs);
}
catch (Exception exception)
{
this.CaughtException(exception);
}
}
}
protected virtual void AddConnectedSession(ISession session)
{
ValueTypeWrapper<LockCookie> lockCookie = LockHelper.SafeAcquireWriterLock(this.connectedSessionLock, 0x3e8);
try
{
this.connectedSessions.Add(((AsyncSocketSession) session).RemoteEndPoint, session);
}
finally
{
LockHelper.SafeReleaseWriterLock(this.connectedSessionLock, lockCookie);
}
}
protected virtual void CaughtException(Exception cause)
{
if (this.ExceptionCaught != null)
{
try
{
this.ExceptionCaught.Invoke(this, new ExceptionEventArgs(cause));
}
catch (Exception exception)
{
log.Error("CaughtException failed.", exception);
}
}
}
public abstract void Close();
protected int IncrementAcceptedCount()
{
return Interlocked.Increment(ref this.acceptedCount);
}
protected virtual void OnSessionCaughtException(object sender, ExceptionEventArgs e)
{
if (e.Cause is SocketException)
{
try
{
((ISession) sender).Close();
}
catch (Exception exception)
{
log.Error("Close session failed.", exception);
}
}
}
protected virtual void OnSessionClosed(object sender, SessionEventArgs e)
{
try
{
this.RemoveConnectedSession(e.Session);
e.Session.StateChanged -= this.sessionClosedEventHandler;
}
catch
{
}
}
protected virtual void RemoveConnectedSession(ISession session)
{
ValueTypeWrapper<LockCookie> lockCookie = LockHelper.SafeAcquireWriterLock(this.connectedSessionLock, 0x3e8);
try
{
this.connectedSessions.Remove(((AsyncSocketSession) session).RemoteEndPoint);
}
finally
{
LockHelper.SafeReleaseWriterLock(this.connectedSessionLock, lockCookie);
}
}
public abstract void Start();
public int AcceptedCount
{
get
{
return this.acceptedCount;
}
}
public int Backlog
{
get
{
return this.backLog;
}
set
{
if (this.Started)
{
throw new InvalidOperationException("Can't set listen backlog after acceptor started");
}
this.backLog = value;
}
}
public IPEndPoint ListenEndPoint
{
get
{
return this.listenEndPoint;
}
set
{
if (this.listenEndPoint == null)
{
if (this.Started)
{
throw new InvalidOperationException("Can't set listen address after acceptor started");
}
this.listenEndPoint = value;
}
}
}
public IPacketDecoder PacketDecoder
{
get
{
return this.packetDecoder;
}
set
{
this.packetDecoder = value;
}
}
public IPacketEncoder PacketEncoder
{
get
{
return this.packetEncoder;
}
set
{
this.packetEncoder = value;
}
}
public bool ReuseAddress
{
get
{
return this.reuseAddress;
}
set
{
this.reuseAddress = value;
}
}
public NCindy.SessionType SessionType
{
get
{
return this.sessionType;
}
}
public bool Started
{
get
{
return this.started;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -