forked from terrylao/Artemis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerThread.pas
306 lines (294 loc) · 7.64 KB
/
ServerThread.pas
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
unit ServerThread;
{$mode objfpc}{$H+}
interface
uses
Types,
{$ifdef unix}cthreads, {$endif}
{$IFDEF WINDOWS}
winsock2,windows,
{$ENDIF}
SysUtils,Classes,dateutils,syncobjs,socketfunc,atermisworker;
const
BUFSIZE=4096*2;
{$IFDEF unix}
{$DEFINE TSOCKET := Integer}
{$DEFINE closesocket:=close}
INVALID_SOCKET = -1;
SOCKET_ERROR = -1;
{$ENDIF}
type
TCPServerThread = class(TThread)
public
mEvent:TEventObject;
perhapsbeclosed:boolean;
serverhost:string;
serverport:integer;
workers:array of Tatermisworker;
function bindto(servIP:string;PORT:integer):integer;
procedure sendOut(data:pbyte;size:integer);
procedure close();
procedure log(s:string);
constructor Create(b:boolean;maxworkers:integer);
procedure doTerminate;
destructor Destroy; override;
private
timeouts:integer;
logger:textfile;
protected
svrSock:Integer;//Socket物件
procedure Execute; override;
end;
implementation
function GetIPByName(const Name:String):String;
var
r:PHostEnt;
a:TInAddr;
begin
Result:='';
r:= gethostbyname(PChar(Name));
if Assigned(r) then
begin
a:=PInAddr(r^.h_Addr_List^)^;
Result:=inet_ntoa(a);
end;
end;
destructor TCPServerThread.Destroy;
var
i:integer;
begin
for i:=0 to length(workers)-1 do
if workers[i].isWaiting then
begin
workers[i].free;
end;
inherited; // Also called parent class destroyer
end;
constructor TCPServerThread.Create(b:boolean;maxworkers:integer);
var
i:integer;
begin
inherited Create(b);
Freeonterminate:=false;
mEvent := TEventObject.Create(nil,true,false,'');
perhapsbeclosed:=true;
svrSock:=-1;
setlength(workers,maxworkers);
assignfile(logger,'server.log');
rewrite(logger);
for i:=0 to maxworkers-1 do
begin
workers[i]:=Tatermisworker.create(false,'worker'+Inttostr(i)+'.log');
workers[i].atermisidx:=i;
end;
log('worker created...');
end;
function TCPServerThread.bindto(servIP:string;PORT:integer):integer;
var
wsd:WSADATA;
timeout:Ttimeval;
addr : TSockAddrIn;
begin
if (WSAStartup(MAKEWORD(2,0),wsd)<0) then
exit(-11);
svrSock:=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
//if (svrSock=INVALID_SOCKET) then
// exit(-2);
ZeroMemory(@addr,sizeof(addr));
addr.sin_family:=AF_INET;
addr.sin_port:=htons(port);
if servIP='0.0.0.0' then
begin
addr.sin_addr.S_addr:=htonl(INADDR_ANY);
end
else
begin
addr.sin_addr.S_addr:=inet_addr(pchar(servIP));
end;
if bind(svrSock,addr,sizeof(addr))=SOCKET_ERROR{<>0} then
exit(-3);
if listen(svrSock,5)<>0 then
begin
exit(-4);
end;
perhapsbeclosed:=false;
result:=0;
serverhost:=servIP;
serverport:=port;
end;
procedure TCPServerThread.sendOut(data:pbyte;size:integer);
begin
send(svrSock,data,size,0);
end;
procedure TCPServerThread.close();
begin
closesocket(svrSock);
svrSock:=-1;
perhapsbeclosed:=true;
end;
procedure TCPServerThread.log(s:string);
begin
writeln(logger,s);
end;
procedure TCPServerThread.Execute;
var
res,i:integer;
timeval:TTimeVal;
client:PSockAddr;
namelen:PInteger;
clientskt:integer;
begin
timeval.tv_sec:=timeouts*1000;
timeval.tv_usec:=50;
if client=nil then
begin
new(client);
new(namelen);
namelen^:=sizeof(client^);
end;
log('TCPServerThread working...');
while Terminated=false do
begin
if svrSock=-1 then
begin
log('TCPServerThread suspended...');
mEvent.WaitFor(INFINITE);
mEvent.ResetEvent;
if Terminated then
continue;
if svrSock=-1 then
begin
res:=bindTo(serverhost,serverport);
if res<0 then
begin
doTerminate();
break;
end;
log('TCPServerThread binded...');
end;
end;
clientskt:=accept(svrSock,client,namelen);
log('new socket imcoming:'+inttostr(clientskt));
res:=0;
for i:=0 to length(workers)-1 do
begin
if workers[i].isWaiting then
begin
log('TCPServerThread workers['+inttostr(i)+'] taken');
workers[i].doWork(clientskt);
workers[i].ShowClientInfo;
res:=1;
break;
end;
end;
if res=0 then
closesocket(clientskt);
end;//end while true
dispose(client);
dispose(namelen);
closefile(logger);
end;
procedure TCPServerThread.doTerminate;
var
i:integer;
begin
// Signal event to wake up the thread
Terminate;
mEvent.SetEvent;
CLOSE();
for i:=0 to length(workers)-1 do
begin
workers[i].doterminate;
end;
// Base Terminate method (to set Terminated=true)
end;
//You can use the getsockname function for the local port and address and the getpeername for the remote port like so
{
例子代码:(关键是使用winsock2单元)
var
stLclAddr, stDstAddr: sockaddr_in;
stMreq: ip_mreq;
hSocket: TSOCKET;
stWSAData: TWSADATA;
procedure TForm1.FormCreate(Sender: TObject);
var
nRet:Integer;
fFlag:Boolean;
begin
// Init WinSock
nRet := WSAStartup($0202, stWSAData);
if nRet<>0 then
begin
StatusBar.SimpleText := Format('WSAStartup failed: %d', [nRet]);
Exit;
end;
// Multicast Group Address and Port setting
StatusBar.SimpleText := Format('Multicast Address:%s, Port:%d, IP TTL:%d, Interval:%d.',[achMCAddr, nPort, lTTL, nInterval]);
// Get a datagram socket
hSocket := socket(AF_INET,SOCK_DGRAM,0);
if (hSocket = INVALID_SOCKET) then
begin
StatusBar.SimpleText := Format('socket() failed, Err: %d', [WSAGetLastError]);
Exit;
end;
// Bind the socket
stLclAddr.sin_family := AF_INET;
stLclAddr.sin_addr.s_addr := htonl(INADDR_ANY); // any interface
stLclAddr.sin_port := 0; //any port
nRet := bind(hSocket,stLclAddr,sizeof(stLclAddr));
if (nRet = SOCKET_ERROR) then
begin
StatusBar.SimpleText := Format('bind() port: %d failed, Err: %d', [nPort,WSAGetLastError]);
Exit;
end;
// Join the multicast group
stMreq.imr_multiaddr.s_addr := inet_addr(achMCAddr);
stMreq.imr_interface.s_addr := INADDR_ANY;
nRet := setsockopt(hSocket,IPPROTO_IP,IP_ADD_MEMBERSHIP,@stMreq,sizeof(stMreq));
if (nRet = SOCKET_ERROR) then
begin
StatusBar.SimpleText := Format('setsockopt() IP_ADD_MEMBERSHIP address %s failed, Err: %d',[achMCAddr, WSAGetLastError]);
Exit;
end;
// Set IP TTL to traverse up to multiple routers
nRet := setsockopt(hSocket,IPPROTO_IP,IP_MULTICAST_TTL,@lTTL,sizeof(lTTL));
if (nRet = SOCKET_ERROR) then
begin
StatusBar.SimpleText := Format('setsockopt() IP_MULTICAST_TTL failed, Err: %d',[WSAGetLastError]);
Exit;
end;
// Disable loopback
fFlag := False;
nRet := setsockopt(hSocket,IPPROTO_IP,IP_MULTICAST_LOOP,@fFlag,sizeof(fFlag));
if (nRet = SOCKET_ERROR) then
begin
StatusBar.SimpleText := Format('setsockopt() IP_MULTICAST_LOOP failed, Err: %d',[WSAGetLastError]);
end;
SndTimer.Enabled := True;
end;
procedure TForm1.SndTimerTimer(Sender: TObject);
var
nRet:Integer;
SndStr:String;
begin
//Get System (UTC) Time
GetSystemTime(stSysTime);
//Assign our destination address
stDstAddr.sin_family := AF_INET;
stDstAddr.sin_addr.s_addr := inet_addr(achMCAddr);
stDstAddr.sin_port := htons(nPort);
// Send the time to our multicast group!
nRet := sendto(hSocket,stSysTime,sizeof(stSysTime),0,stDstAddr,sizeof(stDstAddr));
if (nRet < 0) then
begin
StatusBar.SimpleText := Format('sendto() failed, Error: %d', [WSAGetLastError]);
Exit;
end
else
begin
SndStr:=Format('Sent UTC Time %02d:%02d:%02d:%03d ',[stSysTime.wHour,stSysTime.wMinute,stSysTime.wSecond,stSysTime.wMilliseconds]);
SndStr:=SndStr+Format('Date: %02d-%02d-%02d to: %s:%d',[stSysTime.wMonth,stSysTime.wDay,stSysTime.wYear,inet_ntoa(stDstAddr.sin_addr),ntohs(stDstAddr.sin_port)]);
TimeLog.Lines.Add(SndStr);
end;
end;
}
end.