Delphi 7: Handling events in console application (TidIRC)
Asked Answered
V

1

2

I'm trying to make a console application based on Indy's IRC Component (TIdIRC) but I'm having trouble with events. Here's my code:

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Classes,
  Math,
  IdBaseComponent,
  IdComponent,
  IdTCPConnection,
  IdTCPClient,
  IdIRC;

type
  TEvents = class
  public
    procedure Raw(Sender: TObject; AUser: TIdIRCUser; ACommand, AContent: String; var Suppress: Boolean);
  end;

const
  IrcServ = 'gr.irc.gr';
  IrcPort = 6667;
  IrcChan = '#lalala';

var
  Irc: TidIRC;
  Event: TEvents;
  uName, rName: string;

function Log(s: string): string;
var now: TDateTime;
begin
  now := Time;
  result := FormatDateTime('[hh:nn:ss] ', now) + s;
end;

procedure TEvents.Raw(Sender: TObject; AUser: TIdIRCUser; ACommand, AContent: String; var Suppress: Boolean);
begin
  Log(AUser.Nick+' '+ACommand+' '+AContent);
end;

begin
  Event := TEvents.Create;
  Irc := TidIRC.Create(nil);
  Irc.OnRaw := Event.Raw;
  Randomize;
  Write('Nickname: ');
  ReadLn(uName);
  rName := 'IDM' + IntToStr(RandomRange(1000, 9999)) + uName;
  with Irc do begin
    AltNick := 'IDM' + IntToStr(RandomRange(1000, 9999)) + uName;
    Nick := rName;
    Username := rName;
    RealName := 'I.D.M.';
    Host := IrcHost;
    Port := IrcPort;
    //MaxLineAction := maException;  <-- [ERROR] Undeclared identifier: 'maException'
    ReadTimeout := 0;
    UserMode := [];
    Connect();
    Join(IrcChan);
  end;
  ReadLn;

end.

I've tried so far everything i could think of, but, although the app is connected successfully, it won't return any raw message... What am i missing?

Visional answered 6/7, 2012 at 14:43 Comment(0)
C
4

TdIRC uses an internal worker thread to receive data. The OnRaw event is triggered when that thread is parsing data. The thread uses TThread.Synchronize() to do that parsing. Since your main thread does not have an active VCL message loop, you can pump the Synchronize() queue manually. After you connect, call the CheckSynchronize() function from the Classes unit in a loop while you are connected to IRC, eg:

begin 
  ...
  Connect; 
  try
    Join(IrcChan); 
    do
      CheckSynchronize;
      Sleep(10);
    until SomeCondition;
  finally
    Disconnect;
  end;
  ...  
end. 

For good measure, you can assign a handler to the WakeMainThread event in the Classes unit to help control when CheckSynchronize() should be called, so the main thread can go to sleep while the IRC connection is idle, eg:

program Project1;     

{$APPTYPE CONSOLE}     

uses     
  SysUtils,     
  Classes,     
  Math,     
  IdBaseComponent,     
  IdComponent,     
  IdTCPConnection,     
  IdTCPClient,     
  IdIRC;     

type     
  TEvents = class     
  private
    FSyncEvent: TEvent;
  public     
    constructor Create;
    destructor Destroy; override;
    procedure Raw(Sender: TObject; AUser: TIdIRCUser; ACommand, AContent: String; var Suppress: Boolean);     
    procedure Wake(Sender: TObject);
    procedure CheckSync;
  end;     

function Log(s: string): string;      
begin      
  result := FormatDateTime('[hh:nn:ss] ', Time) + s;      
end;      

constructor TEvents.Create;
begin
  inherited;
  FSyncEvent := TEvent.Create(nil, False, False, '');
end;

destructor TEvents.Destroy;
begin
  FSyncEvent.Free;
  inherited;
end;

procedure TEvents.Raw(Sender: TObject; AUser: TIdIRCUser; ACommand, AContent: String; var Suppress: Boolean);      
begin      
  Log(AUser.Nick+' '+ACommand+' '+AContent);      
end;      

procedure TEvents.Wake(Sender: TObject);
begin
  FSyncEvent.SetEvent;
end;

procedure TEvents.CheckSync;
begin
  FSyncEvent.WaitFor(Infinite);
  CheckSynchronize;
end;

const     
  IrcServ = 'gr.irc.gr';     
  IrcPort = 6667;     
  IrcChan = '#lalala';     

var      
  Irc: TidIRC;      
  Event: TEvents;      
  uName, rName: string;      

begin
  Event := TEvents.Create;       
  try
    WakeMainThread := Event.Wake;
    Irc := TIdIRC.Create(nil);       
    try
      Irc.OnRaw := Event.Raw;       
      Randomize;       
      Write('Nickname: ');       
      ReadLn(uName);       
      rName := 'IDM' + IntToStr(RandomRange(1000, 9999)) + uName;       
      with Irc do begin       
        AltNick := 'IDM' + IntToStr(RandomRange(1000, 9999)) + uName;
        Nick := rName;       
        Username := rName;       
        RealName := 'I.D.M.';       
        Host := IrcHost;       
        Port := IrcPort;       
        //MaxLineAction := maException;  <-- [ERROR] Undeclared identifier: 'maException'       
        ReadTimeout := 0;       
        UserMode := [];       
        Connect;       
        try
          Join(IrcChan);       
          do
            Event.CheckSync;
          until SomeCondition;
        finally
          Disconnect;
        end;
      end;       
    finally
      Irc.Free;
    end;
  finally
    Event.Free;
  end;
end.
Crown answered 6/7, 2012 at 16:30 Comment(6)
Thanks for your reply Remy. Would it be possible for your to explain with some code how to "pump the message queue"? The only thread i found in idirc.pas is TIdIrcReadThread. I tried to catch it with a custom thread in project but no luck.Visional
Does a console app actually have an Application that has a ProcessMessages() method?Rub
@rudy It does if the project uses Forms.pas. That's monstrous overkill though. Just use while GetMessage(...) do begin TranslateMessage(...); DispatchMessage(...); end; to pump the queue.Mimosa
@David: Overkill indeed. It would never occur to me to use Forms in a console app.Rub
Actually, now that I think of it, there is a simpler solution. In D7, TThread.Synchronize() puts sync requests into an in-memory queue which can be pumped manually using the CheckSynchronize() function in the Classes unit, without involving any message loop at all. I have updated my answer to reflect that.Crown
@Remy awesome! I had no idea about SyncObjs. Thanks for your effort and your time!!Visional

© 2022 - 2024 — McMap. All rights reserved.