标签:
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;
标签:
原文地址:http://www.cnblogs.com/hnxxcxg/p/5864524.html