码迷,mamicode.com
首页 > 其他好文 > 详细

IOCP实现的任务队列

时间:2016-09-12 14:28:24      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

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实现的任务队列

标签:

原文地址:http://www.cnblogs.com/hnxxcxg/p/5864524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!