//## Project : ETMASS——Extensible Multi-Thread Asynchronous Socket Server Framework
//## Author : Hulihui(ehulh@163.com)
//## Creation Date : 2008-10-13
//## Modified Date : 2008-11-09
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.OleDb;
namespace CSUST.Net
{
#region 定义相关枚举变量
public enum TDisconnectType
{
Normal, // disconnect normally
Timeout, // disconnect because of timeout
Exception // disconnect because of exception
}
public enum TSessionState
{
Active, // state is active
Inactive, // session is inactive and will be closed
Shutdown, // session is shutdownling
Closed // session is closed
}
#endregion
#region 会话类核心成员对象实体
/// <summary>
/// 会话类核心成员对象实体
/// </summary>
public class TSessionCoreInfo
{
}
#endregion
#region 异常类接口
public class TExceptionEventArgs : EventArgs
{
}
public class TSessionEventArgs : EventArgs
{
}
public class TSessionExceptionEventArgs : TSessionEventArgs
{
}
/// <summary>
/// 异常委托接口
/// </summary>
public interface ISessionEvent
{
event EventHandler<TSessionExceptionEventArgs> SessionReceiveException;
event EventHandler<TSessionExceptionEventArgs> SessionSendException;
event EventHandler<TSessionEventArgs> DatagramDelimiterError;
event EventHandler<TSessionEventArgs> DatagramOversizeError;
event EventHandler<TSessionEventArgs> DatagramAccepted;
event EventHandler<TSessionEventArgs> DatagramError;
event EventHandler<TSessionEventArgs> DatagramHandled;
}
#endregion
#region 会话基类,对外访问的类,继承于TSessionCoreInfo,ISessionEvent
/// <summary>
/// 会话基类(抽象类, 必须实现其 AnalyzeDatagram 方法)
/// </summary>
public abstract class TSessionBase: TSessionCoreInfo, ISessionEvent
{
}
#endregion
#region 数据库操作相关类
/// <summary>
/// 定义数据库异常委托
/// </summary>
public interface IDatabaseEvent
{
event EventHandler<TExceptionEventArgs> DatabaseOpenException;
event EventHandler<TExceptionEventArgs> DatabaseException;
event EventHandler<TExceptionEventArgs> DatabaseCloseException;
}
/// <summary>
/// 数据库抽象基类, 只给出了几个抽象方法, 派生后需要增加实现
/// 1) Open方法, 给出具体的SqlConnection/OleDbConnection
/// 2) 其它抽象方法, 这些实现要在 TSocketServerBase 中调用
/// 3) 已经给出了两个派生类:TSqlServerBase/TOleDatabaseBase
/// </summary>
public abstract class TDatabaseBase: IDatabaseEvent
{
..........
}
/// <summary>
/// SqlServer数据库类, 可以再派生并增加属性与字段
/// </summary>
public class TSqlServerBase : TDatabaseBase
{
...............
}
/// <summary>
/// OldDb数据库类, 可以再派生并增加属性与字段
/// </summary>
public class TOleDatabaseBase : TDatabaseBase
{
..........
}
#endregion
//主要说明这里调用比较底层的类型如抽象类,接口,而不调用子类,因为调用抽象类或接口使得这个类比较稳定一致性不易变形,
//因为不会随着子类的变化而变化,通过抽象或接口来到达统一
public class TSocketServerBase<TSession, TDatabase> : IDisposable, IDatabaseEvent, ISessionEvent
where TSession : TSessionBase, new()
where TDatabase : TDatabaseBase, new()
{
..........
}
}