偶根据<<Window网络与通信>>一书上的,改写Delphi版本的IOCP组件,可以给一份给你测试一下性能。(用多线程池和缓冲区池来控制连接与网络通信)
主要属性与过程申请如下:
type
{ TIOCPServer }
TIOCPServer = class(TComponent)
private
FOnConnectionEstablished: TOnConnectionEstablished;
FOnConnectionClosing: TOnConnectionClosing;
FOnConnectionError: TOnConnectionError;
FOnReadCompleted: TOnReadCompleted;
FOnWriteCompleted: TOnWriteCompleted;
FActive: Boolean;
procedure SetActive(const Value: Boolean);
procedure SetPort(const Value: Integer);
procedure SetMaxConnections(const Value: Integer);
procedure SetMaxFreeBuffers(const Value: Integer);
procedure SetMaxFreeContexts(const Value: Integer);
procedure SetIP(const Value: string);
protected
// 记录空闲结构信息
m_pFreeBufferList: PIOCPBuffer;
m_pFreeContextList: PIOCPContext;
m_nFreeBufferCount: int;
m_nFreeContextCount: int;
m_FreeBufferListLock: RTL_CRITICAL_SECTION;
m_FreeContextListLock: RTL_CRITICAL_SECTION;
// 记录抛出的Accept请求
m_pPendingAccepts: PIOCPBuffer; // 抛出请求列表。
m_nPendingAcceptCount: long;
m_PendingAcceptsLock: RTL_CRITICAL_SECTION;
// 记录连接列表
m_pConnectionList: PIOCPContext;
m_nCurrentConnection: int;
m_ConnectionListLock: RTL_CRITICAL_SECTION;
// 用于投递Accept请求
m_hAcceptEvent: THANDLE;
m_hRepostEvent: THANDLE;
m_nRepostCount: LONG;
m_nPort: int; // 服务器监听的端口
m_IP: string; //服务器绑定的IP
m_nInitialAccepts: int;
m_nMaxAccepts: int;
m_nMaxSends: int;
m_nMaxFreeBuffers: int;
m_nMaxFreeContexts: int;
m_nMaxConnections: int;
m_hListenThread: THANDLE; // 监听线程
m_hCompletion: THANDLE; // 完成端口句柄
m_sListen: TSOCKET; // 监听套节字句柄
m_lpfnAcceptEx: LPFN_ACCEPTEX; // AcceptEx函数地址
m_lpfnGetAcceptExSockaddrs: LPFN_GETACCEPTEXSOCKADDRS; // GetAcceptExSockaddrs函数地址
m_bShutDown: BOOL; // 用于通知监听线程退出
m_bServerStarted: BOOL; // 记录服务是否启动
m_HasLoad: Boolean;
// 申请和释放缓冲区对象
function AllocateBuffer(nLen: int): PIOCPBuffer;
procedure ReleaseBuffer(pBuffer: PIOCPBuffer);
// 申请和释放套节字上下文
function AllocateContext(s: TSOCKET): PIOCPContext;
procedure ReleaseContext(pContext: PIOCPContext);
// 释放空闲缓冲区对象列表和空闲上下文对象列表
procedure FreeBuffers();
procedure FreeContexts();
// 向连接列表中添加一个连接
function AddAConnection(pContext: PIOCPContext): BOOL;
// 插入和移除未决的接受请求
function InsertPendingAccept(pBuffer: PIOCPBuffer): BOOL;
function RemovePendingAccept(pBuffer: PIOCPBuffer): BOOL;
// 取得下一个要读取的
function GetNextReadBuffer(pContext: PIOCPContext; pBuffer: PIOCPBuffer): PIOCPBuffer;
// 投递接受I/O、发送I/O、接收I/O
function PostAccept(pBuffer: PIOCPBuffer): BOOL;
function PostSend(pContext: PIOCPContext; pBuffer: PIOCPBuffer): BOOL;
function PostRecv(pContext: PIOCPContext; pBuffer: PIOCPBuffer): BOOL;
procedure HandleIO(dwKey: DWORD; pBuffer: PIOCPBuffer; dwTrans: DWORD; nError: int);
// 开始/停止服务
function Start(): BOOL;
procedure Shutdown();
// 线程等待
function WaitFor(pHandle: THANDLE): LongWord;
procedure Loaded; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
// 关闭一个连接和关闭所有连接
procedure CloseAConnection(pContext: PIOCPContext);
procedure CloseAllConnections();
// 向指定客户发送文本
function SendText(pContext: PIOCPContext; pszText: Pchar; nLen: int): BOOL;
// 取得当前的连接数量
function GetCurrentConnection: int;
property ConnectionList: PIOCPContext read m_pConnectionList;
property ConnectionCount: int read m_nCurrentConnection;
published
property Active: Boolean read FActive write SetActive;
property Port: Integer read m_nPort write SetPort;
property IP: string read m_IP write SetIP;
property MaxConnections: Integer read m_nMaxConnections write SetMaxConnections;
property MaxFreeBuffers: Integer read m_nMaxFreeBuffers write SetMaxFreeBuffers;
property MaxFreeContexts: Integer read m_nMaxFreeContexts write SetMaxFreeContexts;
property OnConnectionEstablished: TOnConnectionEstablished read FOnConnectionEstablished write FOnConnectionEstablished;
property OnConnectionClosing: TOnConnectionClosing read FOnConnectionClosing write FOnConnectionClosing;
property OnConnectionError: TOnConnectionError read FOnConnectionError write FOnConnectionError;
property OnReadCompleted: TOnReadCompleted read FOnReadCompleted write FOnReadCompleted;
property OnWriteCompleted: TOnWriteCompleted read FOnWriteCompleted write FOnWriteCompleted;
end; //