MORMOT的数据序列

MORMOT的数据序列

mormot服务器回复客户端通过Ctxt.OutContent属性。

此属性的类型是:SockString。   // property OutContent: SockString read fOutContent write fOutContent ;

继续跟代码,发现SockString是RawByteString类型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
type
{$ifdef UNICODE}
  /// define the fastest Unicode string type of the compiler
  SynUnicode = UnicodeString;
  /// define a raw 8-bit storage string type, used for data buffer management
  SockString = type RawByteString;
{$else}
  /// define the fastest 16-bit Unicode string type of the compiler
  SynUnicode = WideString;
  {$ifdef HASCODEPAGE} // FPC may expect a CP, e.g. to compare two string constants
  SockString = type RawByteString;
  {$else}
  /// define a 8-bit raw storage string type, used for data buffer management
  SockString = type AnsiString;
  {$endif}
{$endif}

 继续跟代码,发现RawByteString是AnsiString类型。从下面的代码可以看出,

RawByteString是跨平台的,针对不同的平台有不同的编译开关。这也是为什么要封装一个RawByteString类型。
复制代码
{$IFDEF NEXTGEN}
  UTF8String = type _AnsiString(65001);
  RawByteString = type _AnsiString($ffff);
  {$NODEFINE UTF8String}
  {$NODEFINE RawByteString}
{$ELSEIF Defined(LINUX64) or Defined(OSX64)}
  UTF8String = type AnsiString(65001);
  RawByteString = type AnsiString($ffff);
  {$NODEFINE UTF8String}
  {$NODEFINE RawByteString}
{$ELSE}
  UTF8String = type AnsiString(65001);
  RawByteString = type AnsiString($ffff);
{$ENDIF}
复制代码

mormot在SynCommons.pas单元定义了一些RawByteString和其它数据类型相互转换的公共函数,一起来看看。

stream和RawByteString相互转换函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function StreamToRawByteString(aStream: TStream): RawByteString;
var current, size: Int64;
begin
  result := '';
  if aStream=nil then
    exit;
  current := aStream.Position;
  if (current=0) and aStream.InheritsFrom(TRawByteStringStream) then begin
    result := TRawByteStringStream(aStream).DataString; // fast COW
    exit;
  end;
  size := aStream.Size-current;
  if (size=0) or (size>maxInt) then
    exit;
  SetLength(result,size);
  aStream.Read(pointer(result)^,size);
  aStream.Position := current;
end;

  

1
2
3
4
function RawByteStringToStream(const aString: RawByteString): TStream;
begin
  result := TRawByteStringStream.Create(aString);
end;

  bytes和RawByteString相互转换函数:

1
2
3
4
procedure BytesToRawByteString(const bytes: TBytes; out buf: RawByteString);
begin
  SetString(buf,PAnsiChar(pointer(bytes)),Length(bytes));
end;

  

1
2
3
4
5
6
7
8
9
procedure RawByteStringToBytes(const buf: RawByteString; out bytes: TBytes);
var L: Integer;
begin
  L := Length(buf);
  if L<>0 then begin
    SetLength(bytes,L);
    MoveFast(pointer(buf)^,pointer(bytes)^,L);
  end;
end;

  RawByteString和Variant相互转换函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
procedure RawByteStringToVariant(const Data: RawByteString; var Value: variant);
begin
  with TVarData(Value) do begin
    {$ifndef FPC}if VType and VTYPE_STATIC<>0 then{$endif}
      VarClear(Value);
    if Data='' then
      VType := varNull else begin
      VType := varString;
      VAny := nil; // avoid GPF below when assigning a string variable to VAny
      RawByteString(VAny) := Data;
    end;
  end;
end;

  

1
2
3
4
5
6
7
8
9
10
11
procedure VariantToRawByteString(const Value: variant; var Dest: RawByteString);
begin
  case TVarData(Value).VType of
  varEmpty, varNull:
    Dest := '';
  varString:
    Dest := RawByteString(TVarData(Value).VAny);
  else // not from RawByteStringToVariant() -> conversion to string
    Dest := {$ifdef UNICODE}RawByteString{$else}string{$endif}(Value);
  end;
end;

  stream,ansistring,variant基本上代表了常用的数据格式。

posted @   delphi中间件  阅读(1063)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
历史上的今天:
2017-12-18 TSynAuthentication SESSION验证
2017-12-18 生成SESSIONID
2017-12-18 http session
2017-12-18 THttpClientSocket token验证
2017-12-18 TWinHttp之二
2017-12-18 日志池
2015-12-18 MSSQL的表备份成INSERT脚本的存储过程
点击右上角即可分享
微信分享提示