unit uglsn_engine;
{
G L S N Engine
Benötigt werden folgende Komponenten:
# UdpSockUtil - [url]http://www.delphi-forum.de/topic_55339.html[/url]
Hinweis zur benutzung die Unit WinSock einbinden !
Copyright 2006 by Björn R. Salgert (bjoern@bsnx.net)
Internet: glsgames.bsnx.net
}
interface
uses
SysUtils, Classes, IdBaseComponent, IdComponent, IdIPWatch, UdpSockUtil,
WinSock, Forms, Dialogs;
const
ALL = '
255.255.255.255';
GLSN_DEFAULTPORT = 42768;
GLSN_STR = 0;
GLSN_POS = 1;
type
TGLS_IPString =
string[18];
{ size: 18 4*3+3 }
TGLS_SString =
string[20];
{ size: 20 }
TGLS_LString =
string[255];
{ size: 255 }
TGLS_Point =
packed record { size: 24 3*8 }
X,
Y,
Z: Double;
end;
type
TGLS_NetSend =
packed record { 394 }
C: Integer;
K: Integer;
P: Integer;
Name: TGLS_SString;
case Integer
of
0:(
M1,
M2,
M3,
M4,
M5: TGLS_SString;
ML: TGLS_LString;
);
1:(
Pos: TGLS_Point;
Dir: TGLS_Point;
Speed: Double;
Power: Double;
);
end;
type
TGLSN_Exception =
Exception;
TGLS_OnReceiveEvent =
procedure(r: TGLS_NetSend;
ip:
string)
of object;
TGLSN_Engine =
class(TDataModule)
UdpSockUtil: TUdpSockUtil;
IP: TIdIPWatch;
procedure UdpSockUtilError(Sender: TObject; Error: Integer);
procedure UdpSockUtilReceive(Sender: TObject);
procedure DataModuleCreate(Sender: TObject);
protected
FOnRecieveRecord: TGLS_OnReceiveEvent;
public
{ Public-Deklarationen }
Eceptions: boolean;
procedure BroadcastRecord(r: TGLS_NetSend);
procedure SendRecord(r: TGLS_NetSend; toip:
string);
published
property OnReceiveRecord: TGLS_OnReceiveEvent
read FOnRecieveRecord
write
FOnRecieveRecord;
end;
var
GLSN_Engine: TGLSN_Engine;
implementation
{$R *.dfm}
procedure TGLSN_Engine.UdpSockUtilError(Sender: TObject; Error: Integer);
begin
if Eceptions
then
TGLSN_Exception.Create('
GLSN Socket Error: '+inttostr(Error));
end;
procedure TGLSN_Engine.UdpSockUtilReceive(Sender: TObject);
var
Len: Integer;
Msg:
String;
vonIP: in_addr;
buffer: TGLS_NetSend;
begin
// wieviel ist angekommen?
Len := UdpSockUtil.ReceiveLength;
if (Len > 0)
then begin // wenn auch was da ist...
// Nachricht einlesen; in vonIP wird die Absender-IP zurückgegeben
UdpSockUtil.ReceiveBuf(buffer, sizeof(buffer), vonIP);
if inet_ntoa(vonIP)<>
IP.LocalIP
then
begin
if Assigned(FOnRecieveRecord)
then begin
FOnRecieveRecord(buffer, inet_ntoa(vonIP));
end;
end;
end;
end;
procedure TGLSN_Engine.DataModuleCreate(Sender: TObject);
begin
UdpSockUtil.Listen:=true;
end;
procedure TGLSN_Engine.BroadcastRecord(r: TGLS_NetSend);
var
s:
string;
begin
s:=UdpSockUtil.RemoteHost;
UdpSockUtil.RemoteHost:=ALL;
UdpSockUtil.SendBuf(r, sizeof(r));
UdpSockUtil.RemoteHost:=s;
end;
procedure TGLSN_Engine.SendRecord(r: TGLS_NetSend; toip:
string);
var
s:
string;
begin
s:=UdpSockUtil.RemoteHost;
UdpSockUtil.RemoteHost:=toip;
UdpSockUtil.SendBuf(r, sizeof(r));
UdpSockUtil.RemoteHost:=s;
end;
end.