Delphi XE DataSnap.DSServer[2] TDSServer[1] 介绍

Delphi XE DataSnap.DSServer[2] TDSServer[1] 介绍

  • 管理传输和服务器类的创建和生存期。
  • 此DataSnap服务器组件管理传输和服务器类的创建和生存期。
 //管理传输和服务器的创建和生存期
  TDSServer = class(TDSCustomServer)
  private
    FAutoStart:       Boolean;
    FConnectEvent:    TDSConnectEvent;
    FPrepareEvent:    TDSPrepareEvent;
    FDisconnectEvent: TDSConnectEvent;
    FErrorEvent:      TDSErrorEvent;
    FCallbacks:       TObjectList<TDSCallbackTunnel>;

    FSetupConverterEvent: TDSSetupConverterEvent;
    FSetupReverterEvent:  TDSSetupReverterEvent;
    FRegConverters: TObjectDictionary<string, TConverterEvent>;
    FRegReverters:  TObjectDictionary<string, TReverterEvent>;

    FChannelQueueSize: Integer;

    FChannelResponseTimeout: Integer;

    class var
      FServerList:    TDBXArrayList;
    class procedure AddServer(Server: TDSCustomServer); static;
    class procedure RemoveServer(Server: TDSCustomServer); static;

    procedure ReadHideDSAdmin(Reader: TReader);
    procedure ShutdownCallbacks;
  protected
    /// <summary> Shutdown all the callbacks in the server before transport layer stops.</summary>
    procedure TransportStopping(const Sender: TDSServerTransport); override;
    procedure Loaded; override;
    procedure Connecting(const ConnectEventObject: TDSConnectEventObject); override;
    procedure Preparing(const PrepareEventObject: TDSPrepareEventObject); override;
    procedure Disconnecting(const ConnectEventObject: TDSConnectEventObject); override;
    procedure Error(const ErrorEventObject: TDSErrorEventObject); override;
    function  GetTraceInfoEvent: TDBXTraceEvent; virtual;
    procedure SetTraceInfoEvent(const TraceInfoEvent: TDBXTraceEvent); virtual;
    procedure DefineProperties(Filer: TFiler); override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;

    ///<summary>
    ///  Find the instance of the server by its component name.
    ///</summary>
    class function FindServer(Name: string): TDSCustomServer; static;
    ///<summary>
    ///  Create an local, inprocess connection to this server with the connection
    ///  properties provided.
    ///</summary>
    function GetServerConnection(const Properties: TDBXProperties): TDBXConnection; overload; override;

    ///<summary>
    ///  Start the server.  This will call the Start method on all <c>TDSServerTransport</c>
    ///  component implementations and all <c>TDSServerClass</c> components that have set
    ///  their <c>Server</c> property to this instance.
    ///  This method is called automatically if/when the component is loaded if
    ///  the <c>AutoStart</c> property is set to true.
    ///
    ///</summary>
    procedure Start; override;
    ///<summary>
    ///  Stop the server.  This will call the Stop method on all <c>TDSServerTransport</c>
    ///  implementations and all <c>TDSServerClass</c> components that have set
    ///  their <c>Server</c> property to this instance.
    ///
    ///</summary>
    procedure Stop; override;

    ///  <summary>
    ///    Registers a channel callback with current instance and returns the communication tunnel
    ///  </summary>
    ///  <remarks>
    ///    It is expected that the callback is created per-need bases. When the first callback
    ///    registers with the client channel then and only then the callback tunnel is created. If
    ///    no client callback registers, no resources are allocated. Hence the need for CallbackId.
    ///
    ///    The function returns nil if there is already a channel created for the client id
    ///  </remarks>
    function CreateChannelTunnel(const ChannelName: string; const ClientId: string;
                                     const CallbackId: string; ChannelNames: TStringList;
                                     const SecurityToken: string): TDSCallbackTunnel;
    function RegisterChannelCallback(const ClientId: string; const CallbackId: string;
                                     ChannelNames: TStringList; const SecurityToken: string): Boolean;
    function GetCallbackTunnel(const ClientManagerId: string): TDSCallbackTunnel;
    function CallbackTunnelOf(const ClientManagerId: string): TDSCallbackTunnel;


    ///  <summary>
    ///    Returns true if there is a tunnel for a given client id. False otherwise.
    ///  </summary>
    function HasChannelCallback(const ClientId: string): Boolean;

    ///  <summary>
    ///    Unregisters the client channel based on channel name and client id
    ///  </summary>
    procedure UnregisterChannelCallback(const ClientId: string;
                                        const SecurityToken: string;
                                        Owner: Boolean = false); overload;
    ///  <summary>
    ///    Unregisters a client callback. No notification or broadcasts will be possible after that
    ///  </summary>
    procedure UnregisterChannelCallback(const ClientId: string;
                                        const CallbackId: string; const SecurityToken: string); overload;

    ///  <summary>
    ///    Unregisters a client callback. No notification or broadcasts will be possible after that
    ///  </summary>
    procedure UnregisterChannelCallback(Tunnel: TDSCallbackTunnel; DoBroadcast: Boolean = True); overload;

    ///  <summary> sends a message to all client callbacks </summary>
    function BroadcastMessage(const ChannelName: string; const Msg: TJSONValue;
                              const ArgType: Integer = TDBXCallback.ArgJson): Boolean; overload;
    function BroadcastObject(const ChannelName: string; const Msg: TObject): Boolean; overload;
    ///  <summary> sends a message to all client callbacks with a given registered callback id </summary>
    function BroadcastMessage(const ChannelName: string; const CallbackId: string;
                              const Msg: TJSONValue; const ArgType: Integer = TDBXCallback.ArgJson): Boolean; overload;
    function BroadcastObject(const ChannelName: string; const CallbackId: string;
                              const Msg: TObject): Boolean; overload;

{$IFNDEF NEXTGEN}
    ///  <summary>deprecated. ChannelName is no longer required</summary>
    ///  <remarks> Call the version of NotifyCallback that doesn't take a ChannelName. </remarks>
    function NotifyCallback(const ChannelName: string;
                            const ClientId: string;
                            const CallbackId: string;
                            const Msg: TJSONValue; out Response: TJSONValue;
                            Timeout: Cardinal = INFINITE;
                            const ArgType: Integer = TDBXCallback.ArgJson): Boolean; overload; deprecated 'ChannelName is no longer required';
    ///  <summary>deprecated. ChannelName is no longer required</summary>
    ///  <remarks> Call the version of NotifyObject that doesn't take a ChannelName. </remarks>
    function NotifyObject(const ChannelName: string;
                          const ClientId: string;
                          const CallbackId: string;
                          const Msg: TObject; out Response: TObject;
                          Timeout: Cardinal = INFINITE): Boolean; overload; deprecated 'ChannelName is no longer required';
{$ENDIF !NEXTGEN}

    ///  <summary>Synchroneous message delivery to a callback located in a client channel</summary>
    ///  <remarks> Returns false if the delivery fails </remarks>
    function NotifyCallback(const ClientId: string;
                            const CallbackId: string;
                            const Msg: TJSONValue; out Response: TJSONValue;
                            Timeout: Cardinal = INFINITE;
                            const ArgType: Integer = TDBXCallback.ArgJson): Boolean; overload;
    function NotifyObject(const ClientId: string;
                            const CallbackId: string;
                            const Msg: TObject; out Response: TObject;
                            Timeout: Cardinal = INFINITE): Boolean; overload;
    ///  <summary> Returns a list of all channel names</summary>
    function GetAllChannelNames: TList<string>;
    ///  <summary> Returns a list of all callbacks available on that channel</summary>
    function GetAllChannelCallbackId(const ChannelName: string): TList<string>;
    ///  <summary> Returns a list of all client channels registered with a given channel</summary>
    function GetAllChannelClientId(const ChannelName: string): TList<string>;
    ///  <summary> Returns the marshaler for TObject parameters</summary>
    function GetJSONMarshaler: TJSONMarshal;
    ///  <summary> Returns the un-marshaler for TObject parameters</summary>
    function GetJSONUnMarshaler: TJSONUnMarshal;
    ///  <summary> Marshal argument using local marshaler</summary>
    function MarshalData(Data: TObject): TJSONValue;
    ///  <summary> UnMarshal argument using local unmarshaler</summary>
    function UnMarshalJSON(Data: TJSONValue): TObject;
    property HideDSAdmin;
  published
    ///<summary>
    /// Use this event to receive notification that a server method is being
    ///  prepared for all <c>TDSServerClass</c> instances registered with
    ///  this server.  See <c>TDSServerClass.OnPrepare</c> for more information.
    ///</summary>
    property OnPrepare: TDSPrepareEvent read FPrepareEvent write FPrepareEvent;
    ///<summary>
    /// Use this event to receive notifcation when a connection is opened.
    ///  If an exception is raised, in this event handler, the connection
    ///  will be denied.
    ///</summary>
    property OnConnect: TDSConnectEvent read FConnectEvent write FConnectEvent;
    ///<summary>
    /// Use this event to receive notifcation when a connection is closed.
    ///</summary>
    property OnDisconnect: TDSConnectEvent read FDisconnectEvent write FDisconnectEvent;
    ///<summary>
    ///   Use this event to receive notifcation of execeptions that are raised
    ///  during the execution of a server method.
    ///</summary>
    property OnError: TDSErrorEvent read FErrorEvent write FErrorEvent;
    ///<summary>
    ///  When set to true, this causes the server to be automatically started when
    ///  the <c>TDSServer</c> component is loaded.
    ///</summary>
    [Default(True)]
    property AutoStart: Boolean read FAutoStart write FAutoStart default True;
    ///<returns>
    ///  Event handler used for receiving trace events.
    ///</returns>
    property OnTrace: TDBXTraceEvent read GetTraceInfoEvent write SetTraceInfoEvent;
    ///<summary>
    ///  The number of messages which can be queued for sending to a specific client at once.
    ///  If the queue reaches this size, all attempts to send to that client will fail until
    ///  the size of the queue decreases (one or more messages get sent.)
    ///</summary>
    [Default(100)]
    property ChannelQueueSize: Integer read FChannelQueueSize write FChannelQueueSize default 100;
    ///<summary>
    ///  The length in milliseconds to wait for a client's response to a heavyweight callback
    ///  message sent to them, before determining the client is lost or or disabled in some way,
    ///  and terminating the tunnel. Use a value of 0 to not automatically close a tunnel, unless
    ///  the session expires.
    ///</summary>
    [Default(30000)]
    property ChannelResponseTimeout: Integer read FChannelResponseTimeout write FChannelResponseTimeout default 30000;
  end;

  

 

 

 

 

创建时间:2022.11.04  更新时间:

posted on 2022-11-04 11:34  滔Roy  阅读(246)  评论(0编辑  收藏  举报

导航