📄 abstractsession.cs
字号:
namespace NCindy.Session
{
using NCindy;
using NCindy.Filter;
using NCindy.Util;
using NCindy.Util.Logging;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
public abstract class AbstractSession : AbstractDisposable, ISession, ISessionEvents, IExceptionEvents
{
protected Hashtable attributes;
private static readonly DispatcherFilter DISPATCH_FILTER = new DispatcherFilter(DispatcherFactory.GetDispatcher());
protected readonly List<ISessionFilter> filters;
protected readonly ISessionFilter handlerFilter = new SessionHandlerFilter();
private static readonly ILogger log = LogFactory.CreateLogger(MethodBase.GetCurrentMethod().ReflectedType);
protected IPacketDecoder packetDecoder;
protected readonly ISessionFilter packetDecoderFilter;
protected IPacketEncoder packetEncoder;
protected SessionState sessionState;
protected NCindy.SessionType sessionType;
protected int timeout;
public event EventHandler<SessionEventArgs> Closed;
public event EventHandler<ExceptionEventArgs> ExceptionCaught;
public event EventHandler<SessionEventArgs> ObjectReceived;
public event EventHandler<SessionEventArgs> ObjectSent;
public event EventHandler<SessionEventArgs> Opened;
public event EventHandler<SessionEventArgs> StateChanged;
public event EventHandler<SessionEventArgs> TimedOut;
internal AbstractSession(NCindy.SessionType sessionType)
{
this.sessionType = sessionType;
this.packetDecoderFilter = PacketDecoderFilter.NewInstance(this);
this.filters = new List<ISessionFilter>();
this.attributes = Hashtable.Synchronized(new Hashtable());
this.sessionState = SessionState.Initial;
}
public void AddSessionFilter(ISessionFilter filter)
{
if (filter == null)
{
throw new ArgumentNullException("filter");
}
this.filters.Add(filter);
}
public abstract IFuture Close();
public bool ContainsAttribute(object key)
{
return this.attributes.Contains(key);
}
private void FireEvent(EventHandler<SessionEventArgs> target, SessionEventArgs arg)
{
if (target != null)
{
try
{
target.Invoke(this, arg);
}
catch (Exception exception)
{
log.Error(target.ToString() + " failed.", exception);
}
}
}
public IFuture Flush(IPacket packet)
{
return this.Flush(packet, 0);
}
public IFuture Flush(IPacket packet, int priority)
{
return this.Send(null, packet, priority);
}
public object GetAttribute(object key)
{
return this.attributes[key];
}
public ISessionFilter GetSessionFilter(int index)
{
return this.filters.get_Item(index);
}
public ISessionFilterChain GetSessionFilterChain(FilterChainMode filterChainMode)
{
return new DefaultSessionFilterChain(this, filterChainMode);
}
protected ISessionFilterChain GetSessionFilterChain(ISessionFilter operateFilter, FilterChainMode filterChainMode)
{
return new DefaultSessionFilterChain(this, filterChainMode, operateFilter);
}
public void InsertSessionFilter(int index, ISessionFilter filter)
{
if (filter == null)
{
throw new ArgumentNullException("filter");
}
this.filters.Insert(index, filter);
}
public abstract IFuture Open();
protected override void ReleaseUnManagedResources()
{
try
{
this.Close().Complete();
}
catch (Exception exception)
{
log.Error("ReleaseUnManagedResources failed.", exception);
}
}
public void RemoveAttribute(object key)
{
if (this.attributes.ContainsKey(key))
{
this.attributes.Remove(key);
}
}
public void RemoveSessionFilter(ISessionFilter filter)
{
this.filters.Remove(filter);
}
public IFuture Send(object obj)
{
return this.Send(obj, 0);
}
public IFuture Send(object obj, int priority)
{
IPacket packet;
try
{
packet = this.PacketEncoder.Encode(this, obj);
}
catch (Exception exception)
{
this.SessionCaughtException(exception);
return new DefaultFuture(this, false);
}
return this.Send(obj, packet, priority);
}
protected abstract IFuture Send(object obj, IPacket packet, int priority);
protected void SessionCaughtException(Exception cause)
{
try
{
EventHandler<ExceptionEventArgs> exceptionCaught = this.ExceptionCaught;
if (!Configuration.DisableInnerException && (exceptionCaught != null))
{
exceptionCaught.Invoke(this, new ExceptionEventArgs(cause));
}
}
catch (Exception exception)
{
log.Error("SessionCaughtException failed.", exception);
}
}
protected void SessionClosed()
{
this.FireEvent(this.Closed, new SessionEventArgs(this, null));
}
protected void SessionReceivedObject(object obj)
{
this.FireEvent(this.ObjectReceived, new SessionEventArgs(this, obj));
}
protected void SessionSentObject(object obj)
{
this.FireEvent(this.ObjectSent, new SessionEventArgs(this, obj));
}
protected void SessionStateChanged()
{
this.FireEvent(this.StateChanged, new SessionEventArgs(this, null));
}
protected void SessionTimeout()
{
this.FireEvent(this.TimedOut, new SessionEventArgs(this, null));
}
public void SetAttribute(object key, object attribute)
{
this.attributes[key] = attribute;
}
public IDictionary Attributes
{
get
{
return this.attributes;
}
}
public bool IsOpened
{
get
{
return (this.sessionState == SessionState.Opened);
}
}
public IPacketDecoder PacketDecoder
{
get
{
return this.packetDecoder;
}
set
{
this.packetDecoder = value;
}
}
public IPacketEncoder PacketEncoder
{
get
{
return this.packetEncoder;
}
set
{
this.packetEncoder = value;
}
}
public IList<ISessionFilter> SessionFilters
{
get
{
return this.filters;
}
}
public NCindy.SessionType SessionType
{
get
{
return this.sessionType;
}
}
public SessionState State
{
get
{
return this.sessionState;
}
protected set
{
if (this.sessionState != value)
{
this.sessionState = value;
this.GetSessionFilterChain(FilterChainMode.Send).SessionStateChanged();
}
}
}
public int Timeout
{
get
{
return this.timeout;
}
set
{
this.timeout = value;
}
}
public class DefaultSessionFilterChain : AbstractSessionFilterChain
{
private readonly List<ISessionFilter> appFilters;
private int cursor;
private ISessionFilter decodeFilter;
private ISessionFilter dispatchFilter;
private readonly FilterChainMode filterChainMode;
private ISessionFilter handlerFilter;
private const int INIT_CURSOR = -1;
private ISessionFilter operateFilter;
private readonly AbstractSession parentSession;
public DefaultSessionFilterChain(AbstractSession session, FilterChainMode filterChainMode) : this(session, filterChainMode, null)
{
}
public DefaultSessionFilterChain(AbstractSession session, FilterChainMode filterChainMode, ISessionFilter operateFilter)
{
this.cursor = -1;
this.dispatchFilter = AbstractSession.DISPATCH_FILTER;
this.parentSession = session;
this.appFilters = new List<ISessionFilter>(this.parentSession.filters);
this.decodeFilter = this.parentSession.packetDecoderFilter;
this.operateFilter = operateFilter;
this.filterChainMode = filterChainMode;
this.handlerFilter = session.handlerFilter;
this.cursor = (this.filterChainMode == FilterChainMode.Send) ? this.appFilters.get_Count() : -1;
}
private bool HasNextFilter()
{
if ((this.filterChainMode == FilterChainMode.Send) && (--this.cursor >= 0))
{
return true;
}
if (this.filterChainMode == FilterChainMode.Receive)
{
return (++this.cursor < this.appFilters.get_Count());
}
return false;
}
protected override ISessionFilter NextFilter
{
get
{
ISessionFilter dispatchFilter;
if (this.dispatchFilter != null)
{
dispatchFilter = this.dispatchFilter;
this.dispatchFilter = null;
return dispatchFilter;
}
if (this.HasNextFilter())
{
return this.appFilters.get_Item(this.cursor);
}
if (this.operateFilter != null)
{
dispatchFilter = this.operateFilter;
this.operateFilter = null;
return dispatchFilter;
}
if (this.decodeFilter != null)
{
dispatchFilter = this.decodeFilter;
this.decodeFilter = null;
return dispatchFilter;
}
dispatchFilter = this.handlerFilter;
this.handlerFilter = null;
return dispatchFilter;
}
}
public override ISession Session
{
get
{
return this.parentSession;
}
}
}
private class SessionHandlerFilter : SessionFilterAdapter
{
public override void ExceptionCaught(ISessionFilterChain filterChain, Exception cause)
{
((AbstractSession) filterChain.Session).SessionCaughtException(cause);
base.ExceptionCaught(filterChain, cause);
}
public override void ObjectReceived(ISessionFilterChain filterChain, object obj)
{
((AbstractSession) filterChain.Session).SessionReceivedObject(obj);
base.ObjectReceived(filterChain, obj);
}
public override void ObjectSent(ISessionFilterChain filterChain, object obj)
{
((AbstractSession) filterChain.Session).SessionSentObject(obj);
base.ObjectSent(filterChain, obj);
}
public override void SessionStateChanged(ISessionFilterChain filterChain)
{
((AbstractSession) filterChain.Session).SessionStateChanged();
base.SessionStateChanged(filterChain);
}
public override void SessionTimeout(ISessionFilterChain filterChain)
{
((AbstractSession) filterChain.Session).SessionTimeout();
base.SessionTimeout(filterChain);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -