Advertisement
Guest User

AI challenge test

a guest
Nov 10th, 2010
1,619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.92 KB | None | 0 0
  1. procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
  2.  
  3.   procedure CopyInputBuffer(Comment: string; Source, Dest: TIdIOHandler);
  4.   var
  5.     Stream: TMemoryStream;
  6.   begin
  7.     Stream := TMemoryStream.Create;
  8.     try
  9.       Source.InputBufferToStream(Stream);
  10.       Stream.Position := 0;
  11.       Dest.Write(Stream, Stream.Size);
  12.       Log(Comment + ' ' + IntToStr(Stream.Size) + ' bytes');
  13.     finally
  14.       FreeAndNil(Stream);
  15.     end;
  16.   end;
  17.  
  18. var
  19.   Server: TIdTCPClient;
  20.   Host, Port: string;
  21. begin
  22.   if AContext.Data = nil then  // First connect
  23.   begin
  24.     Server := TIdTCPClient.Create;
  25.     Host := AContext.Connection.IOHandler.ReadLn(':');
  26.     Port := AContext.Connection.IOHandler.ReadLn(#13#10#13#10);
  27.     Server.Host := Host;
  28.     Server.Port := StrToIntDef(Port, 80);
  29.     Memo1.Lines.Add(Server.Host + ':' + IntToStr(Server.Port));
  30.     AContext.Data := Server;
  31.     try
  32.       Server.Connect;
  33.     except
  34.       on E: Exception do
  35.       begin
  36.         Log('Error while connecting: ' + E.ClassName + ' ' + E.Message);
  37.         Server.Disconnect;
  38.         Server.Free;
  39.         AContext.Data := nil;
  40.         AContext.Connection.Disconnect;
  41.       end;
  42.     end;
  43.   end
  44.   else
  45.   begin
  46.     Server := TIdTCPClient(AContext.Data);        
  47.     Server.IOHandler.CheckForDataOnSource(50);
  48.     AContext.Connection.IOHandler.CheckForDataOnSource(50);
  49.     if Server.Connected and AContext.Connection.Connected then
  50.     begin
  51.       if not AContext.Connection.IOHandler.InputBufferIsEmpty then
  52.         CopyInputBuffer('CLIENT -> SERVER', AContext.Connection.IOHandler, Server.IOHandler);
  53.       if not Server.IOHandler.InputBufferIsEmpty then
  54.         CopyInputBuffer('SERVER -> CLIENT', Server.IOHandler, AContext.Connection.IOHandler);
  55.     end
  56.     else
  57.     begin
  58.       Log('Disconnected');
  59.       Server.Disconnect;
  60.       Server.Free;
  61.       AContext.Data := nil;
  62.       AContext.Connection.Disconnect;
  63.     end;
  64.   end;
  65. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement