DataSnap XE2 and TStream method parameters

后端 未结 2 1814
死守一世寂寞
死守一世寂寞 2021-01-14 23:35

I am working on DataSnap project in Delphi XE2 using TCP/IP protocol that needs to pass a stream of binary data to the server as a method parameter. The problem I am running

相关标签:
2条回答
  • 2021-01-14 23:44

    DataSnap transfers the data in 32k chunks. The receiving end has no way of knowing how many bytes will be received until after all chunks have been reassembled. Once all the data has been received, DataSnap doesn't set the size of the TStream that received the data, so you can't use it until you move it to another stream that knows how many bytes are in the stream.

    I know that pulling 32k+ from a DataSnap server is not the same as pushing 32k+ to a DataSnap server, but this may work for you as well. Try running the TStream through this code after the DataSnap server finishes receiving the data:

    procedure CopyStreamToMemoryStream(const ASource: TStream; var ADest: TMemoryStream; const Rewind: Boolean = True);
    const
      LBufSize = $F000;
    var
      LBuffer: PByte;
      LReadCount: Integer;
    begin
      GetMem(LBuffer, LBufSize);
      ADest.Clear;
      try
        repeat
          LReadCount := ASource.Read(LBuffer^, LBufSize);
          if LReadCount > 0 then
            ADest.WriteBuffer(LBuffer^, LReadCount);
        until LReadCount < LBufSize;
      finally
        FreeMem(LBuffer, LBufSize);
      end;
      if Rewind then
        ADest.Seek(0, TSeekOrigin.soBeginning);
    end;
    

    I can't remember where I found this code (years ago), so I can't give credit where credit is due, but it has been working for me reliably for years now.

    0 讨论(0)
  • 2021-01-14 23:53

    I got thinking about it and it occurred to me that transferring the data to another memory stream just wastes memory, especially if the file is very large. All we need to do is count the bytes and set the stream size, right?!

    procedure FixStream(const AStream: TStream);
    const
      LBufSize = $F000;
    var
      LBuffer: PByte;
      LReadCount, StreamSize: Integer;
    begin
      GetMem(LBuffer, LBufSize);
      try
        StreamSize := 0;
        repeat
          LReadCount := AStream.Read(LBuffer^, LBufSize);
          Inc(StreamSize, LReadCount);
        until LReadCount < LBufSize;
        AStream.Size := StreamSize;
      finally
        FreeMem(LBuffer, LBufSize);
      end;
    end;
    

    Do you want to give that a try? I'm not able to test the code right now or I would...

    0 讨论(0)
提交回复
热议问题