关于Delphi XE DataSnap心跳包

user Windows, IdWinsock2, IdTCPConnection;

private

FSection: TRTLCriticalSection; //临界区

procedure ClientDisconnectEvent(Sender: TObject); // 客户端异常出错处理,需要引用windows单元


procedure T****.CMServerConnect(DSConnectEventObject: TDSConnectEventObject);
var
  ClientConnection: TIdTCPConnection;
  val: TCP_KeepAlive;
  Ret: Integer;

begin

  InitializeCriticalSection(FSection); //初始化临界区
  EnterCriticalSection(FSection);     //进入临界区
  if DSConnectEventObject.ChannelInfo <> nil then
  begin
    ClientConnection := TIdTCPConnection(DSConnectEventObject.ChannelInfo.Id);
    ClientConnection.OnDisconnected := ClientDisconnectEvent; // 异常退出,这句是关键
    // 设置心跳包,如果不能运行,要引用winsock2单元
    val.OnOff := 1;
    val.KeepAliveTime := 5000;
    val.KeepAliveInterval := 3000;
    WSAIoctl(ClientConnection.Socket.Binding.Handle, IOC_IN or IOC_VENDOR or 4,
      @val, SizeOf(val), nil, 0, @Ret, nil, nil);
  end;
  LeaveCriticalSection(FSection);
end;


procedure T****.ClientDisconnectEvent(Sender: TObject);
var
  ClientConnection: TIdTCPConnection;
  ClientID: Integer;
begin
  EnterCriticalSection(FSection);
  ClientConnection := Sender as TIdTCPConnection;
  ClientID := Integer(ClientConnection);
    LeaveCriticalSection(FSection);//离开临界区
  DeleteCriticalSection(FSection);//删除临界区
end;
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值