⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtcconn.pas

📁 Delphi快速开发Web Server
💻 PAS
📖 第 1 页 / 共 5 页
字号:
{
  @html(<b>)
  Connection
  @html(</b>)
  - Copyright (c) Danijel Tkalcec
  @html(<br><br>)

  This unit defines the basic wrappers for all RTC Connection Components
  and is used in all your units which work with any RTC connection component.
  The most-used and referenced components throughout the RTC Component Suite
  will be @Link(TRtcConnection), @Link(TRtcServer) and @Link(TRtcClient).
}

unit rtcConn;

{$INCLUDE rtcDefs.inc}

interface

uses
{$IFDEF MEMCONTROL}
  rtcMemory,
{$ENDIF}

  Classes,
  SysUtils,

  rtcTrashcan,

  rtcLog,
  rtcInfo,
  rtcTimer,
  rtcSyncObjs,
  rtcThrPool,

  rtcConnProv,

{$IFDEF IDE_1}
  FileCtrl,
{$ENDIF}

{$IFDEF CLR}
  System.Threading;
{$ELSE}
  Windows;
{$ENDIF}

const
  // Version of the RTC SDK components
  RTCSDK_VERSION:string='2.49';

var
  // Write all Timet-out disconnects to a Log file?
  LOG_TIMEOUT_DISCONNECTS:boolean=False;

  { @name defines the Memory limit in Bytes.
    No new connections will be accepted by any server connection
    component in this application after this limit is reached.
    @html(<br><br>)

    Each Windows installation has its memory limitations.
    By setting the memory limit for the app here, you can
    throw away new connections in case your memory usage goes
    over the normal value.
    @html(<br>)
    By setting the limit too low, new connections will be abandoned for no reason.
    @html(<br>)
    By setting the limit too high, if you don't check your free memory anywhere
    else in the application and the application needs a lot of memory, you
    could end up with Out-of-memory or Access-Violation exceptions.
    @html(<br><br>)

    One 32-bit application can not address more then 4GB of RAM,
    so the highest value you should use here is 4.000.000.000 (under 4 GB),
    in case you have more RAM then you could ever need.
    @html(<br>)
    Standard value here is 500.000.000 (under 500 MB). }
  RTC_MEMORY_LIMIT:int64=500000000;

  { @name sets the Total connection count limit,
    limiting the number of total active connections (server+client).
    @html(<br><br>)

    Windows has a total connection limit of arround 32K active connections.
    @html(<br>)
    There is no actual need to limit the connections for the application,
    but if you plan on running several servers on one PC, this limit can
    be used for each application, so that applications can co-exist.
    Limiting applications connections will allow other applications to
    use TCP/IP for themselves, even if your server has a very high load.
    @html(<br><br>)

    By using the standard value of 32.000 for all connection limits,
    your app will use any resources it can get from Windows.
    @html(<br><br>)

    In conjunction with @Link(RTC_CLIENT_CONNECT_LIMIT) and @Link(RTC_SERVER_ACCEPT_LIMIT),
    you can precisely define how much Windows connection resources your application may use
    for which purpose. }
  {$IFNDEF Evaluate}
    RTC_CONNECTION_LIMIT:integer=32000;
  {$ELSE}
  const
    RTC_CONNECTION_LIMIT=3; // Limit evaluation version to 3 connections.
  var
  {$ENDIF}

  { @name sets the connection count limit for the client,
    limiting the number of connections that can be made using any TRtcClient
    connection component descendant. If more client connections are attempted,
    an exception will be raised from the 'Connect' method. }
  RTC_CLIENT_CONNECT_LIMIT:integer=32000;

  { @name sets the limit for accepting connections on the server
    by using any TRtcServer connection component descendant. If more connections try
    to come, they will be abandoned (disconnected without asking or notifying). }
  RTC_SERVER_ACCEPT_LIMIT:integer=32000;

  { RTC_WAIT_BEFORE_RECONNECT sets the default Wait period (here in miliseconds) for Reconnect.
    @html(<br>)
    This parameter will be only used if ReconnectOn.Wait<=0. }
  RTC_WAIT_BEFORE_RECONNECT:integer=100;

  { RTC_WAIT_BEFORE_RESTART sets the default Wait period (here in miliseconds) for Restart.
    @html(<br>)
    This parameter will only be used if RestartOn.Wait<=0. }
  RTC_WAIT_BEFORE_RESTART:integer=100;

type
  { All RTC components use at least one class declared in this unit.

    For Delphi to add this unit to the uses clause,
    all RTC components inherit from this class.
   @exclude }
  TRtcComponent = class(TRtc_Component);

  { @abstract(Used for exceptions raised by TRtcConnection)

    To stay on the safe side, you should catch ALL exceptions
    of type "Exception" in order to handle exceptions that may be
    raised from the RTC communication components. Some underlying components
    (for example: connection providers) may use their own exception types. }
  ERtcConnection = class(Exception);

{ --- TRtcConnection --- }

  TRtcConnection = class;

  // @exclude
  TRtcSimpleEvent = procedure(Sender:TObject) of object;

  { TRtcUserEvent is the event handler type for user-defined events. }
  TRtcUserEvent = procedure(Sender:TRtcConnection; Obj:TObject) of object;

  { TRtcNotifyEvent is the standard event handler type.
    Using one basic event type makes writing event handles much easier,
    since most methods are exchangeable. }
  TRtcNotifyEvent = procedure(Sender:TRtcConnection) of object;

  { TRtcErrorEvent is the error event handler type.
    All events that react on exceptions will use handlers of this type. }
  TRtcErrorEvent = procedure(Sender:TRtcConnection; E:Exception) of object;

  { TRtcFunctionCallEvent is the function call event handler type. }
  TRtcFunctionCallEvent=procedure(Sender:TRtcConnection;
                                  Param:TRtcFunctionInfo;
                                  Result:TRtcValue) of object;

  { TRtcResultEvent is the event handler to receive the result of a remote function call. }
  TRtcResultEvent=procedure(Sender:TRtcConnection;
                                  Data:TRtcValue;
                                  Result:TRtcValue) of object;

  { @abstract(Provides Timeout capability to all TRtcConnection components)

    TRtcTimeout is tightly coupled with TRtcConnection component.
    It encapsulates all Timeout parametes for active client
    and server connections, defining how long a connection can
    stay Idle after a specific task, before a Timeout period is
    triggered which closes idle connections (timed-out disconnect). }
  TRtcTimeout = class(TPersistent)
  private
    FTimer: TRtcTimer;

    FInterval:integer;

    FAfterConnecting:integer;
    FAfterConnect:integer;
    FAfterDataReceived:integer;
    FAfterDataLost:integer;
    FAfterDataSend:integer;
    FAfterDataOut:integer;
    FAfterDataIn:integer;
    FAfterDataSent:integer;

    FConn:TRtcConnection;
    FThr:TRtcThread;
    FJob:TRtcJob;

    procedure TriggerTimeout;

    procedure TimerSet;
    procedure TimerReset;

    property Conn:TRtcConnection read FConn write FConn;

  public
    { @exclude }
    constructor Create(Con:TRtcConnection);
    { @exclude }
    destructor Destroy; override;

    { Start the Timeout counter.
      @exclude }
    procedure Start(Multi_Threaded:boolean);
    { Stop the Timeout counter
      @exclude }
    procedure Stop;

    { Temporary Disable the Timeout counter.
      You can use this to disable the timeout while you are processing data. }
    procedure Disable;
    { Enable the Timeout counter and set its intervals to 'timeout' miliseconds.
      You can use this to set the next timeout period,
      overwriting the last value set by the connection component.
      This new interval will only have effect until the connection component changes it,
      which could also be immeriatelly after your call to Enable(). }
    procedure Enable(timeout:integer);

    { Connceting started, set timeout to AfterConnecting.
      @exclude }
    procedure Connecting;
    { Connect accomplished, set timeout to AfterConnect.
      @exclude }
    procedure Connect;

    { Data Sending started, set timeout to AfterDataSend.
      @exclude }
    procedure DataSending;
    { Data sent Out, set timeout to AfterDataOut.
      @exclude }
    procedure DataOut;
    { Data read In, set timeout to AfterDataIn.
      @exclude }
    procedure DataIn;
    { Data Sent, set timeout to AfterDataSent.
      @exclude }
    procedure DataSent;

    { New Data received, set timeout to AfterDataReceived.
      @exclude }
    procedure DataReceived;
    { Data Packet Lost (UDP only), set timeout to AfterDataLost.
      @exclude }
    procedure DataLost;

  published
    { Time (seconds) to wait for a Connect (Connect means that you can send and/or
      receive data through the connection) after Connecting was initiated (Connect message received,
      but the connection is not yet ready to be used).
      Set to zero (0) to reset the timeout counter (will start counting again, using
      the last set timeout interval), or to a negative value (-1) to
      disable the timeout on this event. }
    property AfterConnecting:integer read FAfterConnecting write FAfterConnecting default 0;
    { Time (seconds) to wait for something to come through the connection or for user
      to start sending data through, after a Connect (Connect means that you can send and/or
      receive data through the connection).
      Set to zero (0) to reset the timeout counter (will start counting again, using
      the last set timeout interval), or to a negative value (-1) to
      disable the timeout on this event. }
    property AfterConnect:integer read FAfterConnect write FAfterConnect default 0;
    { Time (seconds) to wait for the next event to trigger for this connection, after a
      data package was received (OnDataReceived event was triggered) .
      Set to zero (0) to reset the timeout counter (will start counting again, using
      the last set timeout interval), or to a negative value (-1) to
      disable the timeout on this event. }
    property AfterDataReceived:integer read FAfterDataReceived write FAfterDataReceived default 0;
    { Time (seconds) to wait for the next event to trigger for this connection,
      after a sent package was lost (UDP Only timeout; OnDataLost event was triggered).
      Set to zero (0) to reset the timeout counter (will start counting again, using
      the last set timeout interval), or to a negative value (-1) to
      disable the timeout on this event. }
    property AfterDataLost:integer read FAfterDataLost write FAfterDataLost default 0;
    { Time (seconds) to wait for the next event to trigger for this connection,
      after Data was prepared for sending (Write() or some other method to fill
      data into sending buffer was called).
      Set to zero (0) to reset the timeout counter (will start counting again, using
      the last set timeout interval), or to a negative value (-1) to
      disable the timeout on this event. }
    property AfterDataSend:integer read FAfterDataSend write FAfterDataSend default 0;
    { Time (seconds) to wait for the next event to trigger for this connection,
      after a Data chunk was sent out (OnDataOut event was triggered).
      Set to zero (0) to reset the timeout counter (will start counting again, using
      the last set timeout interval), or to a negative value (-1) to
      disable the timeout on this event. }
    property AfterDataOut:integer read FAfterDataOut write FAfterDataOut default 0;
    { Time (seconds) to wait for the next event to trigger for this connection,
      after a Data chunk was received in (OnDataIn event was triggered).
      Set to zero (0) to reset the timeout counter (will start counting again, using
      the last set timeout interval), or to a negative value (-1) to
      disable the timeout on this event. }
    property AfterDataIn:integer read FAfterDataIn write FAfterDataIn default 0;
    { Time (seconds) to wait for the next event to trigger for this connection,
      after the sending buffer was completey emptied and all Data was Sent out
      (OnDataSent event was triggered).
      Set to zero (0) to reset the timeout counter (will start counting again, using

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -