Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Is the idea strange?

Posted on 2003-03-13
7
Medium Priority
?
198 Views
Last Modified: 2010-04-03
I declare a class of TObject and add a member variable to it, the member variable has some event handlers. I want to reuse the event handler in the  class, but I don't know what to do. For instance, I need something like:

type
    TMyClass = class(TObject)
    private
        FSock:TCustomWinSocket;
    protected
    public
        procedure OnSockAccept(...);
        procedure OnSockRecv(...);
    end;

I need that the OnSockAccept and OnSockRecv is just FSock's relative event handlers that can be triggered by FSock;
0
Comment
Question by:liyuncheng
7 Comments
 
LVL 3

Expert Comment

by:Stefaan
ID: 8134725
Hi,

Could you be a little bit more detailed about what you actually want to do ? If I got it right you just want the event handlers for the FSock available.  To be able to help you we will need more detailed information on what you actually want to do.

Regards,


Stefaan
0
 
LVL 2

Expert Comment

by:j42
ID: 8134973
Hi,
just some thoughts:



type
  TFnWhatever = procedure(...) of object;

type
  TMyClass = class(TObject)
  private
    FSock:TCustomWinSocket;

    procedure FOnSockAccept(...);
    procedure FOnSockRecv(...);
  protected
  public
    OnSockAccept: TFnWhatever;
    OnSockRecv: TFnWhatever;
  end;

...

  // Maybe in constructor  
  FSock.OnSockAccept := FOnSockAccept;
  FSock.OnSockRecv:= FOnSockRecv;

...

procedure TMyClass.FOnSockAccept(...);
begin
  if Assigned(OnSockAccept) then OnSockAccept(...);
end;



Regards
J
0
 
LVL 9

Expert Comment

by:mocarts
ID: 8135202
you must declare those events as properties and write getter and setter methods:

type
   TMyClass = class(TObject)
   private
     FSock: TCustomWinSocket;
     function ReadSockAccept: TSocketNotifyEvent;
     function ReadSockRecv: TSocketNotifyEvent;
     procedure WriteSockAccept(Value: TSocketNotifyEvent);
     procedure WriteSockRecv(Value: TSocketNotifyEvent);
   public
     property OnSockAccept: TSocketNotifyEvent read ReadSockAccept write WriteSockAccept;
     property OnSockRecv: TSocketNotifyEvent read ReadSockRecv write WriteSockRecv;
   end;

implementation


function TMyClass.ReadSockAccept: TSocketNotifyEvent;
begin
  Result := FSock.OnAccept;
end;

function TMyClass.ReadSockRecv: TSocketNotifyEvent;
begin
  Result := FSock.OnRecv;
end;

procedure TMyClass.WriteSockAccept(Value: TSocketNotifyEvent);
begin
  FSock.OnAccept := Value;
end;

procedure TMyClass.WriteSockRecv(Value: TSocketNotifyEvent);
begin
  FSock.OnRecv := Value;
end;

wbr, mo.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Accepted Solution

by:
steve_hsk earned 375 total points
ID: 8135330
Hi liv ...

The idea is not strange at all, and creating VCL objects dynamically and binding them to events at run-time is something you'll do more and more as you become comfortable with the idea.

However, TCustomWinSocket has no Accept or Recv events, and so you would have to write these yourself, in which case let us know, and I'll post another example.

Another choice you have is to develop the WinSock code yourself, without using Borland's API, and interface with the Winsock API directly with calls to WSAStartup, WSAListen, WSASend, etc, etc ... let me know if you want a simple example.

To demonstrate creating a simple socket at run-time, I'll use TServerSocket, as this seems to match TCustomWinSocket and the Server Accept funcitonality u have :

TYPE TMyClass = CLASS(TObject)
PRIVATE
   FSock : TServerSocket;
PUBLIC      
   CONSTRUCTOR Create;
   DESTRUCTOR  Destroy;
   PROCEDURE   OnAccept(...); // arg list must match TServerSocket.'OnEvent' args
   PROCEDURE   OnListen(...);
END;

CONSTRUCTOR TMyClass.Create;
BEGIN
   FSock := TServerSocket.Create(...);
   FSock.OnListen := OnListen;
   FSock.OnAccept := OnAccept;
   // etc, etc
END;

DESTRUCTOR TMyClass.Destroy;
BEGIN
   FSock.Free;
END;

PROCEDURE TMyClass.OnSockAccept(...);
BEGIN

PROCEDURE TMyClass.OnSockRecv(...);
BEGIN

etc etc

Hope at least some of this helps,
STeve
0
 
LVL 2

Expert Comment

by:j42
ID: 8142985
Hi Steve,

you made it. Congratulations!

:-)
J
0
 
LVL 2

Expert Comment

by:steve_hsk
ID: 8143363
ha ha ... hey J ... how's tricks :-) ?
0
 
LVL 2

Expert Comment

by:j42
ID: 8144772
> how's tricks
Huh... I've got to look up this phrase in my dictonary!
Well, I'm fine. Thanks for asking.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question