其他分享
首页 > 其他分享> > datasnap远程方法返回stream的客户端处理

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;

 

标签:stream,datasnap,LBuffer,LBufSize,Result,var,LReadLen,AStream,客户端
来源: https://www.cnblogs.com/hnxxcxg/p/15979109.html