Recent

Author Topic: [lNet] LHTTPClient - upload file. Buffer problem.  (Read 5011 times)

Dibo

  • Hero Member
  • *****
  • Posts: 1048
[lNet] LHTTPClient - upload file. Buffer problem.
« on: November 17, 2010, 12:33:39 pm »
Hi,

I'm trying to upload file with LHTTPClient. There is no demo in examples, so I'm doing the same way as with LTCPClient in this topic:
http://lazarus.firmos.at/index.php?topic=5146.0
But I have problem with buffer. Above example presents how to send large text, so we can easily offset sended chunk with String[Position] index, but how do this with file buffer? I'm not familiar with streams. This is fragment of my code:
Code: Pascal  [Select][+][-]
  1. private
  2.   { private declarations }
  3.   FFile: TFileStream;
  4.   FBuff: Pointer;
  5.   FBuffReaded, FBuffSended: Integer;  
  6.  
  7. const
  8.   ChunkSize: Longint = 8192; { copy in 8K chunks }
  9.  
  10. procedure TForm1.FormCreate(Sender: TObject);
  11. begin
  12.   FBuff := GetMem(ChunkSize);
  13.   FBuffReaded := 0;
  14.   FBuffSended := 0;
  15. end;
  16.  
  17. procedure TForm1.FormDestroy(Sender: TObject);
  18. begin
  19.   Freemem(FBuff);
  20.   FreeThenNil(FFile);
  21. end;  
  22.  
  23. procedure TForm1.http_clientCanWrite(ASocket: TLHTTPClientSocket;
  24.   var OutputEof: TWriteBlockStatus);
  25. var
  26.   n: Integer;
  27. begin
  28.   AddLog('Can write: ' + IntToStr(integer(OutputEof)));
  29.  
  30.   OutputEof := wsPendingData;
  31.   repeat
  32.     // Next chunk if whole sended
  33.     if (FBuffSended>=FBuffReaded) and (FFile.Position<FFile.Size) then begin
  34.       FBuffReaded := FFile.Read(FBuff^, ChunkSize);
  35.       FBuffSended := 0;
  36.       if (FBuffReaded=0) then Break;
  37.     end;
  38.    
  39.     n := ASocket.Send(FBuff^, FBuffReaded);
  40.     Inc(FBuffSended, n);
  41.   // Loop until filled the OS send buffer or sended whole file
  42.   until (n=0) or ((FBuffSended>=FBuffReaded) and (FFile.Position>=FFile.Size));
  43.  
  44.   if (FBuffSended>=FBuffReaded) and (FFile.Position>=FFile.Size) then
  45.     OutputEof := wsDone;
  46. end;
  47.  
Line 39. How offset buffer (FBuff^) position to FBuffSended? Because in each loop cycle it send buffer from begining. Can I somehow strip first FBuffSended bytes from FBuff?

Regards and sorry for my English
« Last Edit: November 17, 2010, 12:37:15 pm by Dibo »

 

TinyPortal © 2005-2018