cross socket和msgpack的数据序列和还原

cross socket和msgpack的数据序列和还原

procedure TForm1.Button1Click(Sender: TObject);
begin
  var pack: TSimpleMsgPack := TSimpleMsgPack.Create;
  pack.ForcePathObject('sql').AsString := '测试';
  IdTCPClient1.IOHandler.Write(tidbytes(pack.EncodeToBytes));  //发送TBytes
end;

procedure TcrossTcp.OnReceived(Sender: TObject; AConnection: ICrossConnection; ABuf: Pointer; ALen: Integer);
begin
  var b: tbytes;
  SetLength(b, ALen);
  Move(ABuf^, b[0], ALen); // 还原TBytes
  var pack: TSimpleMsgPack := TSimpleMsgPack.Create;
  pack.DecodeFromBytes(b);
  Writeln(pack.ForcePathObject('sql').AsString);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  var pack: TSimpleMsgPack := TSimpleMsgPack.Create;
  pack.ForcePathObject('sql').AsString := '测试';
  var ms: TMemoryStream := TMemoryStream.Create;
  pack.EncodeToStream(ms);
  ms.Position := 0;
  IdTCPClient1.IOHandler.Write(ms, ms.Size); //发送流
end;

procedure TcrossTcp.OnReceived(Sender: TObject; AConnection: ICrossConnection; ABuf: Pointer; ALen: Integer);
begin
  var ms: TMemoryStream := TMemoryStream.Create;
  ms.Size := ALen;
  ms.Write(ABuf^, ALen);//还原流
  var pack: TSimpleMsgPack := TSimpleMsgPack.Create;
  ms.Position := 0;
  pack.DecodeFromStream(ms);
  Writeln(pack.ForcePathObject('sql').AsString);
end;

  

posted @ 2019-07-20 16:50  delphi中间件  阅读(585)  评论(0编辑  收藏  举报