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;
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/11218303.html