使用 DataSnap 进行大流处理

发布于 2024-08-12 06:16:13 字数 1158 浏览 3 评论 0原文

我试图在 DataSnap 服务器/客户端之间传输一些大流(~1Mb),但无济于事。我试图理解 Jim Tierney 的代码 (http://blogs. embarcadero.com/jimtierney/2009/04/06/31461)没有运气,我什至无法编译代码,因为缺少库,无论如何......

我能够的流的最大大小收到的金额是 64k,因此您可以为像我这样的周末程序员提供任何提示/想法/代码示例,我们将非常欢迎。谢谢你!

我的服务器代码:

function TsrvMethods.getStream(iCount: integer): TStream;
begin
  Result := dummyStream('0123456789', iCount);
end;

function dummyStream(sCnt: string; iCount: integer): TStream;
begin
  Result := TMemoryStream.Create;
  while iCount > 1 do begin
    Result.Write(Pointer(sCnt)^, Length(sCnt));
    Dec(iCount);
  end;
  Result.Seek(0, TSeekOrigin.soBeginning);
end;

我的客户端调用代码:

procedure TfrmMain.butStreamClick(Sender: TObject);
var
  sStr : TStream;
begin
  cycleConnection; //make sure we have an active connection

  with TsrvMethodsClient.Create( SQLConn.DBXConnection, False ) do begin
    sStr := getStream( Integer(SpinCount.Value) );
    Free;
  end;
  FreeAndNil(sStr);
end;

I'm trying to transfer some big streams (~1Mb) between DataSnap server/client but to no avail. I'm trying to understand the code of Jim Tierney (http://blogs.embarcadero.com/jimtierney/2009/04/06/31461) with no luck and i can't even compile the code because of a missing library, anyway ...

The max size of a stream i`m able to receive is 64k, so any tips/ideas/code samples you can provide for a weekend programmer like me will be very welcomed. Thank you!

my server code:

function TsrvMethods.getStream(iCount: integer): TStream;
begin
  Result := dummyStream('0123456789', iCount);
end;

function dummyStream(sCnt: string; iCount: integer): TStream;
begin
  Result := TMemoryStream.Create;
  while iCount > 1 do begin
    Result.Write(Pointer(sCnt)^, Length(sCnt));
    Dec(iCount);
  end;
  Result.Seek(0, TSeekOrigin.soBeginning);
end;

my client calling code:

procedure TfrmMain.butStreamClick(Sender: TObject);
var
  sStr : TStream;
begin
  cycleConnection; //make sure we have an active connection

  with TsrvMethodsClient.Create( SQLConn.DBXConnection, False ) do begin
    sStr := getStream( Integer(SpinCount.Value) );
    Free;
  end;
  FreeAndNil(sStr);
end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞别 2024-08-19 06:16:13

事实上,我想我已经明白了。我将此作为答案发布,也许其他人也需要这个。

procedure TfrmMain.butStreamClick(Sender: TObject);
const
  iBufSize = 128;
var
  sStr : TStream;
  sMem : TMemoryStream;
  buf: PByte;
  iRead: integer;
begin
  cycleConnection;

  with TsrvMethodsClient.Create( SQLConn.DBXConnection, False ) do begin

    sStr := getStream( 500000 ); //500k stream

    GetMem(buf, iBufSize);
    sMem := TMemoryStream.Create;
    try
      repeat
        iRead := sStr.Read( Pointer(buf)^, iBufSize);

        if iRead > 0 then sMem.WriteBuffer( Pointer(buf)^, iRead);
        if iRead < iBufSize then break;
      until iRead < iBufSize;
    finally
      FreeMem(buf, iBufSize);
    end;

    Free;
  end;
  FreeAndNil(sStr);
  FreeAndNil(sMem);
end;

PS

通过搜索 DataSnap 代码示例,我发现一个(与速度相关的)改进是将 iBufSize 设置为 61440(或等效的十六进制值 $F000),这似乎是一次性可以接收的最大大小。如果接收流更大,则报告的大小将为 -1,并且需要上面的代码来读取整个流。

Actually, i think i`ve got it. I'm posting this as an answer maybe somebody else need this.

procedure TfrmMain.butStreamClick(Sender: TObject);
const
  iBufSize = 128;
var
  sStr : TStream;
  sMem : TMemoryStream;
  buf: PByte;
  iRead: integer;
begin
  cycleConnection;

  with TsrvMethodsClient.Create( SQLConn.DBXConnection, False ) do begin

    sStr := getStream( 500000 ); //500k stream

    GetMem(buf, iBufSize);
    sMem := TMemoryStream.Create;
    try
      repeat
        iRead := sStr.Read( Pointer(buf)^, iBufSize);

        if iRead > 0 then sMem.WriteBuffer( Pointer(buf)^, iRead);
        if iRead < iBufSize then break;
      until iRead < iBufSize;
    finally
      FreeMem(buf, iBufSize);
    end;

    Free;
  end;
  FreeAndNil(sStr);
  FreeAndNil(sMem);
end;

P.S.

Searching through DataSnap code samples i`ve found that one (speed related) improvement would be to have iBufSize set to 61440 (or equivalent hex value $F000) which seems to be the biggest size can be received in one go. If receiving stream is bigger then reported size will be -1 and the code above is needed to read the entire stream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文