中间件集群协议msgpack

中间件集群协议msgpack

/// <author>cxg 2020-8-5</author>
/// 集群协议
/// broker:代理服务器

unit groupProtocol;

interface

uses
  SysUtils, MsgPack
  ,classes
  ;

const         //命令字
  ConnMiddleWare = 2;
  ConnMiddleWareResp = 52;
  MiddleHeartBeat = 5;
  MiddleHeartBeatResp = 55;

/// <summary>
/// 向broker申请连中间件
/// </summary>
/// <returns></returns>
function connMiddle: TStream;
/// <summary>
/// broket返回可用中间件的ip和port
/// </summary>
/// <param name="status"></param>
/// <param name="ip"></param>
/// <param name="port"></param>
/// <returns></returns>
function connMiddleResp(const status: Boolean; const ip: string; const port: Integer): TStream;
/// <summary>
/// 中间件发送心跳给broker
/// </summary>
/// <param name="ip"></param>
/// <param name="port"></param>
/// <returns></returns>
function middleHeart(const ip: string; const port: Integer): TStream;
/// <summary>
/// broker返回中间件心跳应答
/// </summary>
/// <param name="status"></param>
/// <returns></returns>
function middleHeartResp: TStream;

implementation

function middleHeartResp: TStream;
begin
  var p: TMsgPack := TMsgPack.Create;
  Result := TMemoryStream.Create;
  p.Force('cmd').AsInteger := MiddleHeartBeatResp;
  p.EncodeToStream(Result);
  p.Free;
end;

function middleHeart(const ip: string; const port: Integer): TStream;
begin
  var p: TMsgPack := TMsgPack.Create;
  Result := TMemoryStream.Create;
  p.Force('cmd').AsInteger := MiddleHeartBeat;
  p.Force('ip').AsString := ip;
  p.Force('port').AsInteger := port;
  p.EncodeToStream(Result);
  p.Free;
end;

function connMiddleResp(const status: Boolean; const ip: string; const port: Integer): TStream;
begin
  var p: TMsgPack := TMsgPack.Create;
  Result := TMemoryStream.Create;
  p.Force('cmd').AsInteger := ConnMiddleWareResp;
  p.Force('status').AsBoolean := status;
  p.Force('ip').AsString := ip;
  p.Force('port').AsInteger := port;
  p.EncodeToStream(Result);
  p.Free;
end;

function connMiddle: TStream;
begin
  var p: TMsgPack := TMsgPack.Create;
  Result := TMemoryStream.Create;
  p.Force('cmd').AsInteger := ConnMiddleWare;
  p.EncodeToStream(Result);
  p.Free;
end;

end.

  

posted @ 2020-08-05 15:35  delphi中间件  阅读(324)  评论(0编辑  收藏  举报