开发者

Buffering data in delphi 7

I am using: HawkNL library.

There is an nlRead procedure as follows:

function nlRead(socket: NLsocket; var buffer; nbytes: NLint): NLint;

In all examples and other resources a static array is used to read the bytes to. Just something li开发者_StackOverflow社区ke this:

FBufArray = array [0..1024] of Byte;

I have a few questions regarding this matter.

  1. Which model/type would be appropriate to satisfy nlRead function that I could dynamically allocate space for read data?

    I was trying to use Pointer and GetMem or dynamic table with SetLength, but It seemed not to work as it should.

  2. What is the correct approach in the situation when I have to read bytes with the unknown speed and do it as fast as possible. I mean What should be the size of the buffor for example?

    For me it is relevant because read bytes I have to re-send further at the same time.

Generally how can I read and send bytes as fast as it is possible?


If your question is about what to pass as 'buffer'. You can pass anything you like. If you pass a pointer you have to dereference it. For example when you call nl Read

Procedure read;
  Type
    TChunk = record
      data: pointer;
      datasize: NLint;
    End;
  Var
   Chunk: TChunk;
  Const
    IdealReadSize = 1024;
Begin
  GetMem( Chunk.data, IdealReadSize);
  Try     
    Chunk.datasize := nlRead( YourSocket, Chunk.data^, IdealReadSize );
    // Chunk.datasize hold the count of bytes which have been effectively read
    // (maybe less than 1024 in case of an error)
    // do something with your chunk
  Finally
    Freemem( Chunk.data, IdealReadSize );
  End;
End;

It's the same approach as the TStream.Read() procedure.


You could use an untyped pointer: var P: Pointer;, allocate with GetMem or AllocMem, pass it to your function dereferenced: nlRead(Socket, P^, Count); and deallocate with FreeMem. With regard to speed, a static buffer, sufficiently large: Buf: array[0..BufSize - 1] of Byte is probably the best.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜