RTC Forums
March 29, 2024, 01:54:16 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: RtcHttpClient: Computer exiting sleep mode then application not response  (Read 4315 times)
nusret
RTC Expired
*
Posts: 4


« on: May 14, 2018, 11:14:35 AM »

Wait few minutes  in sleep mode, exiting sleep mode then application is not response.
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: May 14, 2018, 11:23:23 AM »

Please, provide more information and some example code.
Logged
nusret
RTC Expired
*
Posts: 4


« Reply #2 on: May 14, 2018, 12:49:57 PM »

unit Unit1;

interface

uses
  Winapi.Windows,
  Winapi.Messages,

  System.SysUtils,
  System.Variants,
  System.Classes,

  Vcl.Graphics,
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Dialogs,
  Vcl.StdCtrls,

  rtcConn,
  rtcDataCli,
  rtcHttpCli,
  rtcSystem,
  rtcTimer
  ;

type
  TfrmClient = class(TForm)
    ListBox1: TListBox;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);

  private
    { Private declarations }
    FClient: TRtcHttpClient;
    FDataRq: TRtcDataRequest;
    FConnLost,
    FConnBusy: boolean;
    FtmLastPing: Cardinal;
    FPingTimer: TRtcTimer;


    procedure DataRq_OnBeginRequest(Sender: TRtcConnection);
    procedure DataRq_OnDataReceived(Sender: TRtcConnection);
    procedure DataRq_OnResponseAbort(Sender: TRtcConnection);

    procedure DoConnect;
    procedure DoConnLost(Sender: TRtcConnection);
    procedure DoPing;

  public
    { Public declarations }

  end;

var
  frmClient: TfrmClient;

implementation

{$R *.dfm}

procedure TfrmClient.FormCreate(Sender: TObject);
  begin
    FClient := TRtcHttpClient.Create(nil);
    FDataRq := TRtcDataRequest.Create(nil);
    with FDataRq do
      begin
        AutoSyncEvents := True;
        Client := FClient;
        HyperThreading := True;
        OnBeginRequest := DataRq_OnBeginRequest;
        OnDataReceived := DataRq_OnDataReceived;
        OnResponseAbort := DataRq_OnResponseAbort;
      end;

    FClient.ServerAddr := '127.0.0.1';
    FClient.ServerPort := '8088';
    //FClient.MultiThreaded := True;
    FClient.AutoConnect := True;

    DoConnect;
  end;

procedure TfrmClient.FormDestroy(Sender: TObject);
  begin
    if Assigned(FPingTimer) then
      TRtcTimer.Stop(FPingTimer);
    FreeAndNil(FClient);
  end;

procedure TfrmClient.DataRq_OnBeginRequest(Sender: TRtcConnection);
  var
    Cli: TRtcDataClient absolute Sender;
  begin
    if FConnLost then
      exit;

    if Cli.Request.FileName = '/ping' then
      Cli.Write;
  end;

procedure TfrmClient.DataRq_OnDataReceived(Sender: TRtcConnection);
  var
    Cli: TRtcDataClient absolute Sender;
    Dt: RtcByteArray;
  begin
    if FConnLost then
      exit;

    if Cli.Response.StatusCode <> 200 then
      DoConnLost(Sender)
    else
    if Cli.Response.Done then
      begin
        Dt := Cli.ReadEx;
        FtmLastPing := GetTickCount;
        FConnBusy := False;
      end;
  end;

procedure TfrmClient.DataRq_OnResponseAbort(Sender: TRtcConnection);
  var
    Cli: TRtcDataClient absolute Sender;
  begin
    Cli.Request.Skip;
    DoConnLost(Sender);
  end;

procedure TfrmClient.DoConnect;
  begin
    if not Assigned(FPingTimer) then
      begin
        FPingTimer := TRtcTimer.Create(True);
        TRtcTimer.Enable(FPingTimer, 1000, DoPing);
      end;

    FConnLost := False;
    FConnBusy := False;

    DoPing;
  end;

procedure TfrmClient.DoConnLost(Sender: TRtcConnection);
  begin
    if FConnLost then
      exit;

    FConnLost := True;
    FClient.AutoConnect := False;
    //FClient.Disconnect;
    FClient.DisconnectNow(True);
    FClient.SkipRequests;

    FConnBusy := False;
    FtmLastPing := 0;
  end;

procedure TfrmClient.DoPing;
  begin
    if (FtmLastPing + (2*1000) > GetTickCount) or FConnBusy then
      Exit;

    FConnBusy := True;
    FtmLastPing := GetTickCount;
    FDataRq.Request.Method := 'GET';
    FDataRq.Request.FileName := '/ping';
    FDataRq.Post;
  end;

end.
Logged
nusret
RTC Expired
*
Posts: 4


« Reply #3 on: May 14, 2018, 12:51:56 PM »

problem in DisconnectNow procedure.
(Sorry my english is very poor)
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #4 on: May 14, 2018, 01:03:03 PM »

You are calling "FClient.DisconnectNow" from inside several events triggered by the "FClient:TRtcHttpClient" component and/or linked components (like FDataReq). That is a definitive no-go. NEVER call "DisconnectNow" from inside ANY events triggered by RTC components, because this creates an endless loop inside the "DisconnectNow" event. If you need to signal the component to close a connection from inside a RTC event, you can use the "Disconnect" method, which returns immediately and handles the disconnect asynchronously after exiting the event.

The "DisconnectNow" method may ONLY be called from inside the Main Thread and ONLY from code triggered as a result of user interaction and/or messages received by the OS.
Logged
nusret
RTC Expired
*
Posts: 4


« Reply #5 on: May 14, 2018, 01:07:09 PM »

Thank you.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.024 seconds with 16 queries.