📄 udihasmapclientchannel.pas
字号:
unit uDIHasMapClientChannel;
interface
uses
Windows, SysUtils, uCriticalSection, uDIClientChannel;
type
PClientChannelNode = ^TClientChannelNode;
TClientChannelNode = record
Next: PClientChannelNode;
pDIChannel: Pointer;
end;
TDIHasMapClientChannel = class
private
m_nClientChannelCount: DWORD; //数量
FHashMapLock: TCriticalSection; //临界
FHasMapNodes: array[0..65535] of PClientChannelNode; //结点
public
//得到上下文数量
function GetClientChannelCount: DWORD;
function AddClientChannel(const dKey: Word; FClientChannel: TDIClientChannel): Boolean;
function RemoveClientChannel(const dKey: Word): Boolean;
function GetTopClientChannel: TDIClientChannel;
public
constructor Create;
destructor Destroy; override;
end;
implementation
uses uFileLogger;
constructor TDIHasMapClientChannel.Create;
begin
inherited Create;
m_nClientChannelCount := 0;
FHashMapLock := TCriticalSection.Create;
ZeroMemory(@FHasMapNodes, Sizeof(FHasMapNodes));
end;
destructor TDIHasMapClientChannel.Destroy;
begin
FreeAndNil(FHashMapLock);
inherited Destroy;
end;
function TDIHasMapClientChannel.GetClientChannelCount: DWORD;
begin
try
FHashMapLock.Lock;
Result := m_nClientChannelCount;
finally
FHashMapLock.UnLock;
end;
end;
function TDIHasMapClientChannel.AddClientChannel(const dKey: Word; FClientChannel: TDIClientChannel): Boolean;
var
pNode: PClientChannelNode;
begin
try
FHashMapLock.Lock;
if FHasMapNodes[dKey] = nil then begin
New(pNode);
pNode^.pDIChannel := FClientChannel;
pNode^.Next := FHasMapNodes[dKey];
FHasMapNodes[dKey] := pNode;
//数量加一
Inc(m_nClientChannelCount);
Result := TRUE;
_FileLogger.WriteLogMsg( 'AddClientChannel, Key:'+ IntToStr(dKey) +
' 上下文数量:'+IntToStr(m_nClientChannelCount));
end
else
begin
Result := FALSE;
_FileLogger.WriteLogMsg('AddClientChannel失败,FHasMapNodes[dKey] <> nil, Key is: '+IntToStr(dKey));
end;
finally
FHashMapLock.UnLock;
end;
end;
function TDIHasMapClientChannel.RemoveClientChannel(const dKey: Word): Boolean;
var
pNode: PClientChannelNode;
begin
try
FHashMapLock.Lock;
if FHasMapNodes[dKey] <> nil then begin
pNode := FHasMapNodes[dKey];
Dispose(pNode);
//数量减一
Dec(m_nClientChannelCount);
_FileLogger.WriteLogMsg('RemoveClientChannel成功, Key:'+ IntToStr(dKey) +
' 上下文数量:'+IntToStr(m_nClientChannelCount));
end
else
begin
Result := FALSE;
_FileLogger.WriteLogMsg('RemoveClientChannel失败,FHasMapNodes[dKey] = nil, Key is: '+IntToStr(dKey));
end;
finally
FHashMapLock.UnLock;
end;
end;
function TDIHasMapClientChannel.GetTopClientChannel: TDIClientChannel;
var
i: Integer;
pNode: PClientChannelNode;
begin
try
FHashMapLock.Lock;
Result := nil;
for i:=0 to 65535 do begin
if FHasMapNodes[i] <> nil then begin
pNode := FHasMapNodes[i];
if pNode^.pDIChannel <> nil then begin
Result := TDIClientChannel(pNode^.pDIChannel);
Dispose(pNode);
//数量减一
Dec(m_nClientChannelCount);
FHasMapNodes[i] := nil;
_FileLogger.WriteLogMsg('GetTopClientChannel成功, Key:'+ IntToStr(i) +
' 上下文数量:'+IntToStr(m_nClientChannelCount));
break;
end
else
_FileLogger.WriteLogMsg('pNode^.pDIChannel为空, 错误!');
end;
end;
finally
FHashMapLock.UnLock;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -