-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathServer.pas
More file actions
192 lines (172 loc) · 4.65 KB
/
Server.pas
File metadata and controls
192 lines (172 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
unit Server;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, Forms,
Dialogs, IdAntiFreezeBase, IdContext, strutils,
IdAntiFreeze, IdTCPServer,idsockethandle,
about, StdCtrls, IdCustomTCPServer, IdBaseComponent, IdComponent;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
Edit2: TEdit;
Button3: TButton;
TCP: TIdTCPServer;
IdAntiFreeze1: TIdAntiFreeze;
Label1: TLabel;
Label5: TLabel;
Label3: TLabel;
Button5: TButton;
Memo2: TMemo;
Label2: TLabel;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure UpdateBindings;
procedure TCPConnect(AThread: TIdContext);
procedure TCPDisconnect(AThread: TIdContext);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure TCPExecute(AThread: TIdContext);
procedure Button3Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TClientThread = class(TObject)
IP : String;
Index : Integer;
Thread : Pointer;
end;
var
Form1: TForm1;
Clients : TList;
ClientList : array[0..49]of string;
implementation
{$R *.dfm}
procedure TForm1.UpdateBindings;
var Binding : TIdSocketHandle;
begin
tcp.DefaultPort := StrToInt(edit1.text);
tcp.Bindings.Clear;
Binding := tcp.Bindings.Add;
Binding.Port := tcp.DefaultPort;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
tcp.Active:=true;
Button2.Enabled:=true;
Button3.Enabled:=true;
Button5.Enabled:=true;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
listbox1.Clear;
Clients := TList.Create;
UpdateBindings;
end;
procedure TForm1.TCPConnect(AThread: TIdContext);
var Client : TClientThread;
begin
Client := TClientThread.Create;
Client.IP := athread.Connection.Socket.Binding.PeerIP;
Client.Index := ListBox1.Count;
Client.Thread := AThread;
AThread.Data := Client;
Clients.Add(Client);
listbox1.Items.Add(Client.IP+' '+inttostr(ListBox1.Count));
end;
procedure TForm1.TCPDisconnect(AThread: TIdContext);
var Client : TClientThread;
begin
Client := Pointer(AThread.Data);
if ListBox1.Items.IndexOf(ClientList[Client.index])>-1 then
listbox1.Items.Delete(ListBox1.Items.IndexOf(ClientList[Client.index]))
else
listbox1.Items.Delete(Client.index);
Clients.Delete(Client.index);
Client.Free;
AThread.Data := nil;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Clients.Free; tcp.Active:=false;
end;
procedure TForm1.TCPExecute(AThread: TIdContext);
var s,t:string; Client : TClientThread;
begin
s:= AThread.Connection.IOHandler.ReadLn;
t:=MidStr(s,1,5);
if t='nick:' then
begin
if ListBox1.Items.IndexOf(MidStr(s,6,255))<0 then
begin
listbox1.Items.Strings[listbox1.Count-1]:=MidStr(s,6,255);
ClientList[listbox1.Count-1]:=MidStr(s,6,255);
end
else
begin
Client := Clients.Items[ListBox1.Count-1];
TIdContext(Client.Thread).Connection.IOHandler.WriteLn('Nickname allready in list! select another!');
TIdContext(Client.Thread).Connection.Disconnect;
end
end
else if t='list:' then
begin
listbox1.Items.Delimiter:=':';
AThread.Connection.IOHandler.WriteLn('list:'+listbox1.Items.DelimitedText);
end
else
begin
t:=MidStr(s,1,pos('>',s)-1);
s:=MidStr(s,pos('>',s)+1,255);
ListBox1.Selected[listbox1.Items.IndexOf(t)]:=true;
Client := Clients.Items[ListBox1.ItemIndex];
TIdContext(Client.Thread).Connection.IOHandler.WriteLn(s);
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
var Client : TClientThread;
begin
if listbox1.ItemIndex < 0 then exit;
Client := Clients.Items[listbox1.ItemIndex];
TIdContext(Client.Thread).Connection.IOHandler.WriteLn(Edit2.Text);
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
form2.Show;
end;
procedure TForm1.Button5Click(Sender: TObject);
var Client : TClientThread;
begin
if listbox1.ItemIndex < 0 then exit;
Client := Clients.Items[listbox1.itemIndex];
TIdContext(Client.Thread).Connection.Disconnect;
//in onDisconnect item deleted from listbox & Clients
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if listbox1.Count > 0 then
begin
if MessageDlg('Killing all threads',mtWarning,[mbYes,mbNo],0)= mrYes then
while(listbox1.Count >0) do
begin
listbox1.Selected[0]:=true;
Button5.Click;
end
end
Else
begin
tcp.Active:=False;
Button2.Enabled:=false;
Button3.Enabled:=false;
Button5.Enabled:=false;
end;
end;
end.