datasnap 2010 心跳包,連接斷開處理

 

procedure TMainForm.DSServer1Connect
(DSConnectEventObject: TDSConnectEventObject);
var
// RowIndx: Integer;
InVal: TCP_KeepAlive;
Ret: DWORD;
ClientConnection: TIdTCPConnection;
begin
EnterCriticalSection(CSConnect);
try
    // 最大連接數量,驗證來訪者密碼
    if (ConnectCount >= 500) or (DSConnectEventObject.ChannelInfo = nil) or
      (DSConnectEventObject.ConnectProperties[TDBXPropertyNames.UserName]
        <> 'sunstone') or (DSConnectEventObject.ConnectProperties
        [TDBXPropertyNames.Password] <> 'mypassword') then
    begin
      // ClientConnection.Disconnect;
      DSConnectEventObject.DbxConnection.Destroy;
      Exit;
    end
    else
    begin
      // 獲取socket連接
      ClientConnection := TIdTCPConnection(DSConnectEventObject.ChannelInfo.Id);

      // 同IP連接頻率控制
      // for RowIndx := ShowConnections.RowCount - 1 downto 1 do
      // begin
      // if AnsiPos(ClientConnection.Socket.Binding.PeerIP + ':',
      // ShowConnections.Cells[1, RowIndx]) > 0 then
      // begin
      // if SecondsBetween(Now,
      // StrToDateTimeDef(ShowConnections.Cells[4, RowIndx], Now)) >= 1 then
      // begin
      // Break;
      // end
      // else
      // begin
      // DSConnectEventObject.DbxConnection.Destroy;
      // Exit;
      // end;
      // end;
      // end;

      // 設置心跳包
      Ret := 0;
      InVal.OnOff := 1;
      InVal.KeepAliveTime := 4000;
      InVal.KeepAliveInterval := 1;
      if WSAIoctl(ClientConnection.Socket.Binding.Handle,
        IOC_IN or IOC_VENDOR or 4, @InVal, sizeof(TCP_KeepAlive),
        nil, 0, @Ret, nil, nil) = SOCKET_ERROR then
      begin
        DSConnectEventObject.DbxConnection.Destroy;
        Exit;
      end;

      // 建立網絡異常斷開事件
      ClientConnection.OnDisconnected := ClientDisconnectEvent;

      // 顯示來訪者信息
      inc(ConnectCount);
      ConnectInfo.Panels.Items[1].Text := IntToStr(ConnectCount);

      if Trim(ShowConnections.Cells[0, 1]) <> '' then
        ShowConnections.RowCount := ShowConnections.RowCount + 1;

      ShowConnections.Cells[0, ShowConnections.RowCount - 1] := IntToStr
        (DSConnectEventObject.ChannelInfo.Id);
      ShowConnections.Cells[1, ShowConnections.RowCount - 1] :=
        ClientConnection.Socket.Binding.PeerIP + ':' + IntToStr
        (ClientConnection.Socket.Binding.PeerPort);
      ShowConnections.Cells[2, ShowConnections.RowCount - 1] :=
        DSConnectEventObject.ConnectProperties[TDBXPropertyNames.UserName];
      ShowConnections.Cells[3, ShowConnections.RowCount - 1] :=
        DSConnectEventObject.ConnectProperties[TDBXPropertyNames.Password];
      ShowConnections.Cells[4, ShowConnections.RowCount - 1] := FormatDateTime
        ('yyyy-mm-dd hh:nn:ss', Now);
    end;
finally
    LeaveCriticalSection(CSConnect);
end;
end;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章