View Single Post
Old 03.03.2006, 16:21   #21
kazhkaz
Архивный участник
 
Join Date: Oct 2005
Posts: 55
Default

Ooh sory i didn`t include the server status check code in this version of launcher. Here ya go:
Code:
unit l2bw_srvchk;

interface

uses
  Classes, IdIOHandler, IdIOHandlerSocket, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, SysUtils;

type
  TServChk = class(TThread)
  private
    { Private declarations }
  protected
    { Private declarations }    
    TCP: TIdTCPClient;
    IOHS: TIdIOHandlerSocket;
    ip: string;
    ls, gs: boolean;
  protected
    procedure Execute; override;
    procedure UpdateLSGS; virtual;
    procedure GetIP; virtual;
  end;

implementation   

uses l2bw_main;

{ TServChk }

procedure TServChk.GetIP;
begin
  ip:= Main.ini.ReadString('Launcher', 'ip', 'localhost');
end;

procedure TServChk.Execute;
var ac: array[1..255] of byte;
    b: byte;
begin
  // init connection
  TCP:= TIdTCPClient.Create(nil);
  IOHS:= TIdIOHandlerSocket.Create(nil);
  TCP.IOHandler:= IOHS;

  while not Terminated do begin

    Synchronize(GetIP);

    // attempt to connect to port #2106 (login/auth server) of the specified IP
    TCP.Port:= 2106;
    TCP.Host:= ip;
    try
      TCP.Connect();

      // check for login init packet from server
      TCP.ReadBuffer(b, 1);
      TCP.ReadBuffer(ac, b-1);

      ls:= true;

      TCP.Disconnect;
    except
      on E: Exception do begin
        ls:= False;
      end;
    end;

    if Terminated then Exit;

    // attempt to connect to port #7777 (game server) of the specified IP
    TCP.Port:= 7777;
    TCP.Host:= ip;
    try
      TCP.Connect();

      gs:= True;

      TCP.Disconnect;
    except
      on E: Exception do begin
        gs:= False;
      end;
    end;
    Synchronize(UpdateLSGS);
    Sleep(30000);
  end;

  TCP.Free;
  IOHS.Free;
end;


procedure TServChk.UpdateLSGS;
begin
  if ls then begin
    Main.ls_on.Visible:= True;
    Main.ls_off.Visible:= False;
  end else begin
    Main.ls_on.Visible:= False;
    Main.ls_off.Visible:= True;
  end;

  if gs then begin
    Main.gs_on.Visible:= True;
    Main.gs_off.Visible:= False;
  end else begin
    Main.gs_on.Visible:= False;
    Main.gs_off.Visible:= True;
  end;
end;

end.
In fact it is one complete unit from the server i`m working for launcher`s code, also the same code (except for the UpdateLSGS method) is used in IRC bot which in real-time keeps server`s channel topic up-to-date with server status. This code is even older than the launcher project itself (i first used it in stand alone server check app :P)
kazhkaz is offline