Thema: Delphi Udp - Newbe

Einzelnen Beitrag anzeigen

NeoLuxembourg

Registriert seit: 16. Jul 2004
12 Beiträge
 
#6

Re: Udp - Newbe

  Alt 29. Jul 2004, 21:14
Ich hab schon vor ein paar monate versuch das selbe fur Half-Life zu machen. Sollte nicht viel anders sein:


Delphi-Quellcode:
unit UMain;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, StdCtrls,
  IdUDPBase, IdUDPClient, IdAntiFreezeBase, IdAntiFreeze, IdRawBase,
  IdRawClient, IdIcmpClient, ScktComp, math, ExtCtrls;

  type
  TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase);

  type
  TForm1 = class(TForm)
    lstLog: TListBox;
    memo: TMemo;
    Button1: TButton;
    UDPPlayers: TIdUDPClient;
    edtIP: TEdit;
    edtPort: TEdit;
    TimerPlayer: TTimer;
    IdUDPClient1: TIdUDPClient;
    lstplayers: TListBox;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    function StrEqualText(Text: PChar; SearchText: PChar; MaxLen: Integer;IgnoreCase: Boolean): Boolean;
    function FastStringReplace(const Text, SearchText, ReplaceText: string;ReplaceAll, IgnoreCase: Boolean): string;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var get,newstr, test: string;
        posi : integer;

begin
        lstlog.Clear;
        IdUDPClient1.Host := edtIP.text;
        IdUDPClient1.Port := strtoint(edtPort.text);
        IdUDPClient1.Active := true;
        IdUDPClient1.Send('˙˙˙˙details');

        newstr := '[ļ]';

        get := IdUDPClient1.ReceiveString;
        get := FastStringReplace(get, chr(00),newstr,true, true);

        memo.Lines.add(get); //debug

        // Server IP ---
        posi := Pos(newstr,get);
        lstLog.Items.Add('IP: '+copy(get,6,posi-6));
        get := copy(get,posi+strlen(PChar(newstr)),strlen(Pchar(get)));

        // Server name ---
        posi := Pos(newstr,get);
        lstLog.Items.Add('Name: '+copy(get,0,posi-1));
        get := copy(get,posi+strlen(PChar(newstr)),strlen(Pchar(get)));

        // Map name
        posi := Pos(newstr,get);
        lstLog.Items.Add('Map: '+copy(get,0,posi-1));
        get := copy(get,posi+strlen(PChar(newstr)),strlen(Pchar(get)));

        // MOD name
        posi := Pos(newstr,get);
        lstLog.Items.Add('MOD: '+copy(get,0,posi-1));
        get := copy(get,posi+strlen(PChar(newstr)),strlen(Pchar(get)));

        // Game name
        posi := Pos(newstr,get);
        lstLog.Items.Add('Game: '+copy(get,0,posi-1));
        get := copy(get,posi+strlen(PChar(newstr)),strlen(Pchar(get)));

        // Info
        posi := Pos(newstr,get);
        // lstLog.Items.Add('Info: '+copy(get,0,posi-1));
        get := copy(get,posi+strlen(PChar(newstr)),strlen(Pchar(get)));

        // Type
        posi := Pos(newstr,get);
        test := copy(get,2,1);
        lstLog.Items.Add('Version: '+inttostr(ord(test[1])));


        if copy(get,3,1) = 'lthen
                lstLog.Items.Add('Type: Listen server')
        else
                lstLog.Items.Add('Type: Dedicated server');

        if copy(get,4,1) = 'wthen
                lstLog.Items.Add('OS: Windows')
        else
                lstLog.Items.Add('OS: Linux');

        get := copy(get,posi+strlen(PChar(newstr)),strlen(Pchar(get)));

        // -------------

       IdUDPClient1.Active := false;

end;

function TForm1.StrEqualText(Text: PChar; SearchText: PChar; MaxLen: Integer;
  IgnoreCase: Boolean): Boolean;
var
  i: Integer;
begin
  if IgnoreCase then
    Result := StrLIComp(Text, SearchText, MaxLen) = 0
  else
  begin
    Result := False;
    for i := 0 to MaxLen - 1 do
      if (Text[i] = #0) or {(SearchText[i] = #0) or} 
         (Text[i] <> SearchText[i]) then Exit;
    Result := True;
  end;
end;



function TForm1.FastStringReplace(const Text, SearchText, ReplaceText: string;
  ReplaceAll, IgnoreCase: Boolean): string;
var
  LenSearchText, LenReplaceText, LenText: Integer;
  Index, Len, StartIndex: Integer;
begin
  LenSearchText := Length(SearchText);
  LenReplaceText := Length(ReplaceText);
  LenText := Length(Text);
  if LenSearchText = 0 then
  begin
    Result := Text;
    Exit;
  end;

 // Speicher für den schlimmsten Fall reservieren.
  if ReplaceAll then
  begin
    if LenReplaceText - LenSearchText > 0 then
      SetLength(Result, LenText +
        (LenReplaceText - LenSearchText) * (LenText div LenSearchText))
    else
      SetLength(Result, LenText);
  end
  else
    SetLength(Result, LenText + (LenReplaceText - LenSearchText));


  Len := 0;
  StartIndex := 1;
  for Index := 1 to LenText do
  begin
    if StrEqualText(PChar(Pointer(Text)) + Index - 1, Pointer(SearchText),
                   LenSearchText, IgnoreCase) then
    begin
     // davor liegenden Text kopieren
      if Index > StartIndex then
      begin
        Move(Text[StartIndex], Result[Len + 1], Index - StartIndex);
        Inc(Len, Index - StartIndex);
      end;
      StartIndex := Index + LenSearchText;

     // Ersetzungstext einfügen
      if LenReplaceText > 0 then
      begin
        Move(ReplaceText[1], Result[Len + 1], LenReplaceText);
        Inc(Len, LenReplaceText);
      end;

      if not ReplaceAll then Break;
    end;
  end;

 // Rest kopieren
  Index := LenText + 1;
  if Index > StartIndex then
  begin
    Move(Text[StartIndex], Result[Len + 1], Index - StartIndex);
    Inc(Len, Index - StartIndex);
  end;

  SetLength(Result, Len); // String auf die tatsächliche Länge kürzen
end;

//players list, not done yet!
procedure TForm1.Button2Click(Sender: TObject);
var get,newstr, test: string;
        posi,I : integer;
begin
        UDPPlayers.Host := edtIP.text;
        UDPPlayers.Port := strtoint(edtPort.text);
        UDPPlayers.Active := true;
        UDPPlayers.Send('˙˙˙˙players');

        newstr := '[ļ]';
        get := UDPPlayers.ReceiveString;
        get := FastStringReplace(get, chr(00),newstr,true, true);
        get := copy(get,6,strlen(PChar(get)));
        //lstplayers.Items.Add();
        test := copy(get,0,1);
        get := copy(get,2,strlen(PChar(get)));
        memo.Lines.add(get); //debug

       { for I := 1 to ord(test[1]) do
                begin}

                posi := Pos(newstr,get);
                lstplayers.Items.Add(copy(get,2,posi-2));
                get := copy(get,posi+strlen(PChar(newstr)),strlen(Pchar(get)));
                memo.Clear;
                memo.lines.add(get);
                posi := Pos(newstr,get);
                lstplayers.Items.Add(copy(get,2,posi-2));
                get := copy(get,posi+strlen(PChar(newstr)),strlen(Pchar(get)));

                //end;

        UDPPlayers.Active := false;
end;


end.
Have fun!
Angehängte Dateien
Dateityp: zip hlview.zip (189,2 KB, 22x aufgerufen)
  Mit Zitat antworten Zitat