Re: Creating IDTCPServer Runtime

Giganews Newsgroups
Subject: Re: Creating IDTCPServer Runtime
Posted by:  Remy Lebeau \(Indy Team\) (no.spam@no.spam.com)
Date: Fri, 1 Dec 2006

"FeliXus" <bilcan [at] gmail.com> wrote in message
news:67D2927F9011E340bilcan [at] gmail.com...

> I receive this error "Incompatible types : method pointer and regular
procedure"

You must use a procedure that is a member of a class, not a standalone
procedure.  You will two choices:

1) wrap your TIdTCPServer in another class:

    type
        TMyServer = class
        private
            SS: TIdTCPServer;
            procedure SSExecute(AContext: TIdContext);
        public
            constructor Create(Port: Integer);
            destructor Destroy; override;
        end;

    constructor TMyServer.Create(Port: Integer);
    begin
        SS := TIdTCPServer.Create(nil);
        With SS do
        begin
            DefaultPort := Port;
            OnExecute := SSExecute; // Errror line
            Active := True;
        end;
    end;

    destructor TMyServer.Destroy;
    begin
        SS.Free;
        inherited Destroy;
    end;

    procedure TMyServer.SSExecute(AContext: TIdContext);
    begin
        //...
    end;

    procedure SSOpen;
    begin
        SS := TMyServer.Create(10100);
    end;

    procedure SSClose;
    begin
        FreeAndNil(SS);
    end;

2) you can take that a step further by deriving a new class from
TCustomIdTCPServer and then override its virtual methods.  Then you can
forget the event handlers altogether:

    type
        TMyServer = class(TIdCustomTCPServer)
        protected
            function DoExecute(AContext: TIdContext): Boolean; override;
        end;

    function TMyServer.DoExecute(AContext: TIdContext): Boolean;
    begin
        //...
        Result := True; // keep the connection alive
        Result := False; // terminate the connection
    end;

    procedure SSOpen;
    begin
        SS := TMyServer.Create(nil);
        With SS do
        begin
            DefaultPort := 10100;
            Active := True;
        end;
    end;

    procedure SSClose;
    begin
        FreeAndNil(SS);
    end;

Gambit

Replies

None

In response to

Creating IDTCPServer Runtime posted by FeliXus on Fri, 1 Dec 2006