TSimpleMsgPack的样例代码
unit uMain;
interface
uses
SimpleMsgPack, Windows, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms, Dialogs, IdBaseComponent, IdComponent, IdTCPServer,
IdThreadMgr, IdThreadMgrPool;
const
cmd_querysql = 1;
type
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
IdThreadMgrPool1: TIdThreadMgrPool;
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
uDM;
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
msgpack: TSimpleMsgPack;
stream, stream2: TStream;
dm: TfrmDM;
begin
stream := nil;
msgpack := TSimpleMsgPack.Create;
stream2 := TMemoryStream.Create;
dm := TfrmDM.Create(nil);
try
try
AThread.Connection.ReadStream(stream);
stream.Position := 0;
msgpack.DecodeFromStream(stream);
case msgpack.ForcePathObject('cmd').AsInteger of
cmd_querysql:
msgpack.ForcePathObject('result').AsVariant := frmDM.QuerySQL(msgpack.ForcePathObject('sql').AsString);
end;
except
on E: Exception do
begin
msgpack.clear;
Exit;
end;
end;
msgpack.EncodeToStream(stream2);
stream2.Position := 0;
AThread.Connection.WriteStream(stream2);
finally
msgpack.Free;
stream2.Free;
dm.Free;
end;
end;
end.