📄 udimapclientcontext.pas
字号:
unit uDIMapClientContext;
interface
uses
Windows, SysUtils, WinSock2, uCriticalSection, uDIClientContext;
type
TDIMapClientContext = class
private
FClientContextLock: TCriticalSection; //锁
m_pClientContextList: TDIClientContext; //指针
m_nClientContextCount: DWORD; //空闲数量
public
//增加DIClientContext到列表
function AddDIClientContext(FClientContext: TDIClientContext): Boolean;
//把DIClientContext从列表中删除
function RemoveDIClientContext(FClientContext: TDIClientContext): Boolean;
//查找DIClientContext
function FindDIClientContext(FClientContext: TDIClientContext): Boolean;
//释放所有DIClientContext
procedure FreeClientContexts;
//得到池内DIClientContext总数量
function GetClientContextCount: DWORD;
published
property FMapClientContextLock: TCriticalSection read FClientContextLock write FClientContextLock;
property FClientContextList: TDIClientContext read m_pClientContextList write m_pClientContextList;
property FClientContextCount: DWORD read m_nClientContextCount write m_nClientContextCount;
public
constructor Create;
destructor Destroy; override;
end;
implementation
constructor TDIMapClientContext.Create;
begin
inherited Create;
m_nClientContextCount := 0;
m_pClientContextList := nil;
FClientContextLock := TCriticalSection.Create;
end;
destructor TDIMapClientContext.Destroy;
begin
FreeClientContexts;
FreeAndNil(FClientContextLock);
inherited Destroy;
end;
function TDIMapClientContext.GetClientContextCount: DWORD;
begin
try
FClientContextLock.Lock;
Result := m_nClientContextCount;
finally
FClientContextLock.UnLock;
end;
end;
function TDIMapClientContext.AddDIClientContext(FClientContext: TDIClientContext): Boolean;
begin
try
FClientContextLock.Lock;
Result := FALSE;
if FClientContext<>nil then begin
FClientContext.m_pNext := m_pClientContextList;
m_pClientContextList := FClientContext;
Inc(m_nClientContextCount);
Result := TRUE;
end;
finally
FClientContextLock.UnLock;
end;
end;
function TDIMapClientContext.RemoveDIClientContext(FClientContext: TDIClientContext): Boolean;
var
m_pClientContext: TDIClientContext;
m_pNextClientContext: TDIClientContext;
begin
try
FClientContextLock.Lock;
Result := FALSE;
if FClientContext<>nil then begin
if ( m_pClientContextList = FClientContext ) then begin
m_pClientContextList := m_pClientContextList.m_pNext;
FClientContext.m_pNext := nil;
Dec(m_nClientContextCount);
Result := TRUE;
end
else
begin
m_pClientContext := m_pClientContextList;
while (m_pClientContext<> nil) and (m_pClientContext.m_pNext<>FClientContext) do
m_pClientContext := m_pClientContext.m_pNext;
if m_pClientContext<> nil then begin
m_pClientContext.m_pNext := FClientContext.m_pNext;
FClientContext.m_pNext := nil;
Dec(m_nClientContextCount);
Result := TRUE;
end;
end;
end;
finally
FClientContextLock.UnLock;
end;
end;
function TDIMapClientContext.FindDIClientContext(FClientContext: TDIClientContext): Boolean;
var
m_pClientContext: TDIClientContext;
m_pNextClientContext: TDIClientContext;
begin
try
FClientContextLock.Lock;
Result := FALSE;
if FClientContext<>nil then begin
if ( m_pClientContextList = FClientContext ) then
Result := TRUE
else
begin
m_pClientContext := m_pClientContextList;
while (m_pClientContext<> nil) and (m_pClientContext.m_pNext<>FClientContext) do
m_pClientContext := m_pClientContext.m_pNext;
if m_pClientContext<> nil then Result := TRUE;
end;
end;
finally
FClientContextLock.UnLock;
end;
end;
procedure TDIMapClientContext.FreeClientContexts;
var
m_pFreeClientContext: TDIClientContext;
m_pNextClientContext: TDIClientContext;
begin
try
FClientContextLock.Lock;
m_pFreeClientContext := m_pClientContextList;
m_pNextClientContext := nil;
while (m_pFreeClientContext<> nil) do begin
m_pNextClientContext := m_pFreeClientContext.m_pNext;
FreeAndNil(m_pFreeClientContext);
Dec(m_nClientContextCount);
m_pFreeClientContext := m_pNextClientContext;
end;
m_pClientContextList := nil;
m_nClientContextCount := 0;
finally
FClientContextLock.UnLock;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -