unit UMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, IdThreadMgr, IdThreadMgrDefault, IdBaseComponent, IdComponent,
IdTCPServer, StdCtrls;
// ---- INTERN CLIENT RECORDS ------
type
TSimpleClient =
class(TObject)
IP,
Name :
String;
ID : Integer;
Thread : Pointer;
end;
// ---------------------------------
// ------- NETWORK PROTOKOL --------
// v001
type
TNetConfig =
record
data : boolean;
CMD :
string[20];
SIP :
string[16];
DIP :
string[16];
end;
// ---------------------------------
type
TMain =
class(TForm)
tcpServer: TIdTCPServer;
IdThreadMgrDefault1: TIdThreadMgrDefault;
btnStart: TButton;
Memo1: TMemo;
lbClients: TListBox;
lblClientName: TLabel;
lblClientDNS: TLabel;
lblClientID: TLabel;
edtSend: TEdit;
Button1: TButton;
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
procedure tcpServerConnect(AThread: TIdPeerThread);
procedure btnStartClick(Sender: TObject);
procedure tcpServerExecute(AThread: TIdPeerThread);
procedure tcpServerDisconnect(AThread: TIdPeerThread);
procedure lbClientsClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
Clients : TList;
end;
var
Main: TMain;
implementation
{$R *.DFM}
procedure TMain.FormCreate(Sender: TObject);
begin
Clients := TList.Create;
end;
procedure TMain.tcpServerConnect(AThread: TIdPeerThread);
var
Client : TSimpleClient;
Data : TNetConfig;
begin
Memo1.Lines.Add('
User connected');
// Welcome MSG
Data.data := false;
Data.CMD := '
Welcome to Server';
Data.SIP := AThread.Connection.Socket.Binding.IP;
Data.DIP := AThread.Connection.Socket.Binding.PeerIP;
AThread.Connection.WriteBuffer(Data,sizeof(Data));
// Create a client object
Client := TSimpleClient.Create;
// Assign its default values
Client.IP := Athread.Connection.Socket.Binding.PeerIP;
Client.
Name := '
Logging In';
Client.ID := lbClients.Items.Count;
Client.Thread := AThread;
lbClients.Items.Add(Client.IP);
AThread.Data := Client;
Clients.Add(Client);
end;
procedure TMain.btnStartClick(Sender: TObject);
begin
tcpServer.DefaultPort := 10024;
tcpServer.Active := true;
memo1.Lines.add('
Server start');
end;
procedure TMain.tcpServerExecute(AThread: TIdPeerThread);
var
Data : TNetConfig;
begin
AThread.Connection.ReadBuffer(Data,sizeof(Data));
if Data.data = false
then
Memo1.Lines.add('
From:'+Data.SIP+'
To:'+Data.DIP+'
STR:'+Data.CMD)
else
Memo1.Lines.add('
From:'+Data.SIP+'
To:'+Data.DIP+'
CMD:'+Data.CMD)
end;
procedure TMain.tcpServerDisconnect(AThread: TIdPeerThread);
var
Client : TSimpleClient;
begin
{
Client := Pointer(AThread.Data);
memo1.Lines.Add('User ['+ Client.ip +'] disconnected');
Clients.Delete(Client.ID);
lbClients.Items.Delete(lbClients.Items.IndexOf(Client.IP));
Client.Free;
AThread.Data := nil;
}
end;
procedure TMain.lbClientsClick(Sender: TObject);
var
Client : TSimpleClient;
begin
if lbClients.ItemIndex = -1
then
exit;
Client := Clients.Items[lbClients.ItemIndex];
lblClientName.Caption := Client.
Name;
lblClientDNS.Caption := Client.IP;
lblClientID.Caption := inttostr(Client.ID);
end;
procedure TMain.Button1Click(Sender: TObject);
var
Client : TSimpleClient;
Data : TNetConfig;
begin
if (lbClients.ItemIndex <> -1)
then
begin
Client := Clients.Items[lbClients.ItemIndex];
if pos('
/',edtSend.Text) = 1
then
Data.data := true
else
Data.data := false;
Data.CMD := edtSend.Text;
Data.SIP := TIdPeerThread(Client.Thread).Connection.Socket.Binding.IP;
Data.DIP := TIdPeerThread(Client.Thread).Connection.Socket.Binding.PeerIP;
TIdPeerThread(Client.Thread).Connection.WriteBuffer(Data,sizeof(Data));
end;
end;
end.