IOCP实现的任务队列

Posted 咏南中间件和开发框架

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IOCP实现的任务队列相关的知识,希望对你有一定的参考价值。

IOCP实现的任务队列

procedure TForm1.ssocketClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
client: TClient;
pack: AnsiString;
pDecode: PTDecodePack;
begin
client := g_client_manager.getClient(Socket);
if client <> nil then
begin
client.lastTime := GetTickCount;
if Socket.Connected then
client.data := client.data + Socket.ReceiveText;
while Length(client.data) > 2 do
begin // headchar+tailchar=2
if Pos(TAILCHAR, client.data) <= 0 then
Exit;
client.data := ArrestStringEx(client.data, HEADCHAR, TAILCHAR, pack);
New(pDecode);
pDecode.socket := Socket;
pDecode.data := pack;
PostQueuedCompletionStatus(g_decode_handle, 0, 0, POverlapped(pDecode));
if Pos(TAILCHAR, client.data) <= 0 then
Exit;
end;
end;
end;

procedure TDecodeThread.Execute;
var
pDecode: PTDecodePack;
pWork: PTWorkPack;
len: Integer;
param1: DWORD;
{$IFNDEF xe}
param2: DWORD;
{$ELSE}
param2: NativeUInt;
{$ENDIF}
begin
inherited;
while not Self.Terminated do
begin
if Windows.GetQueuedCompletionStatus(g_decode_handle, param1, param2, POverlapped(pDecode), 1) then
begin
if Assigned(pDecode) then
begin
New(pWork);
pWork.socket := pDecode.socket;
pWork.msg := DecodeMessage(LeftStr(pDecode.data, DEFBLOCKSIZE));
len := Length(pDecode.data);
if len > DEFBLOCKSIZE then
pWork.body := DecodeString(RightStr(pDecode.data, len - DEFBLOCKSIZE));
PostQueuedCompletionStatus(g_work_handle, 0, 0, POverlapped(pWork));
Dispose(pDecode);
end else Break;
end
else
Continue;
end;
end;

以上是关于IOCP实现的任务队列的主要内容,如果未能解决你的问题,请参考以下文章

Redis入门 - 消息通知

千万级延时任务队列如何实现,看美图开源的-LMSTFY

Flutter上线项目实战——队列任务

定时任务、Celery、消息队列

Flutter上线项目实战——队列任务

基于消息队列(RabbitMQ)实现延迟任务