datasnap远程方法返回stream的客户端处理

datasnap远程方法返回stream的客户端处理

function CopyStream(const AStream: TStream): TMemoryStream;
const
  LBufSize = $F000;  //60k
var
  LBuffer: TBytes;
  LReadLen: integer;
begin
  Result := nil;
  if AStream = nil then
    Exit;
  Result := TMemoryStream.Create;
  try
    if AStream.Size = -1 then
    begin
      SetLength(LBuffer, LBufSize);
      repeat
        LReadLen := AStream.Read(LBuffer[0], LBufSize);
        if LReadLen > 0 then
          Result.WriteBuffer(LBuffer[0], LReadLen);
        if LReadLen < LBufSize then
          break;
      until LReadLen < LBufSize;
    end
    else
      Result.CopyFrom(AStream, 0);
    Result.Position := 0;
  except
    Result.Free;
  end;
end;

procedure TForm1.btn1Click(Sender: TObject);
begin
  con1.Connected:=True;
  var c:TServerMethods1Client:=TServerMethods1Client.Create(con1.DBXConnection);
  var m: TStream := c.echo('1');
  var m2: TMemoryStream := CopyStream(m);
  var s:TSerial:= TSerial.Create;
  s.decodeFromStream(m2);
  var i:Integer := (s.force('i').AsInteger);
  mmo1.Lines.Add(i.ToString);
  mmo1.Lines.Add(s.force('s').Asutf8String);
  c.Free;
end;

 

posted @ 2022-03-08 09:21  delphi中间件  阅读(431)  评论(0编辑  收藏  举报