-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSimpleServerThread.pas
200 lines (175 loc) · 4.39 KB
/
SimpleServerThread.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
unit SimpleServerThread;
interface
uses
Classes, Windows, WinSock;
var
WSAData: TWSAData;
type
TSimpleServerThread = class(TThread)
private
FWSAError: Boolean;
FServerSocket: TSocket;
FOnExecute: TNotifyEvent;
FOnDestroy: TNotifyEvent;
procedure SetOnExecute(const Value: TNotifyEvent);
procedure SetOnDestroy(const Value: TNotifyEvent);
protected
procedure Execute; override;
procedure NewClientThread(ASocket: TSocket; AAddr: TSockAddr); dynamic;
public
constructor Create(Port: Integer);
destructor Destroy; override;
property OnExecute: TNotifyEvent read FOnExecute write SetOnExecute;
property OnDestroy: TNotifyEvent read FOnDestroy write SetOnDestroy;
property WSAError: Boolean read FWSAError;
end;
implementation
uses
SimpleClientThread, LogFileUnit, SysUtils, uClientControl, uLogWSA,
uFileVersion, uMultiThreadVars;
procedure InitWSA;
var
WSARes: Integer;
begin
//WSARes := WSAStartup(MAKEWORD(2,2), WSAData);
WSARes := WSAStartup($0101, WSAData);
if WSARes <> NO_ERROR then
LogError('WSA', 'WSA Startup Error!');
end;
procedure EndWSA;
begin
WSACleanup;
end;
{ TSimpleServerThread }
constructor TSimpleServerThread.Create(Port: Integer);
var
intTrue: Integer;
intFalse: Integer;
//OptVal: DWORD;
addr: TSockAddr;
begin
FWSAError := False;
ClientCounter := 0;
inherited Create(True);
//FreeOnTerminate := True;
intTrue := 1;
intFalse := 0;
LogInfo('ServerThread', '//******************************');
{
Create server socket}
FServerSocket := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if FServerSocket = INVALID_SOCKET then
begin
FWSAError := True;
LogWSAError;
Terminate;
exit;
end;
// Íåëüçÿ èñïîëüçîâàòü àäðåñ, êîòîðûé óæå çàíÿò
{
Disable reuse address}
if setsockopt(FServerSocket, SOL_SOCKET, SO_REUSEADDR, PChar(@intFalse),
SizeOf(intFalse)) <> NO_ERROR then
(*OptVal := 1;
if setsockopt(FServerSocket, SOL_SOCKET, SO_REUSEADDR, PAnsiChar(@OptVal),
SizeOf(OptVal)) <> NO_ERROR then*)
begin
FWSAError := True;
LogWSAError;//
Terminate;
exit;
end;
{
Set in nonblocking mode}
if ioctlsocket(FServerSocket, FIONBIO, intTrue) <> NO_ERROR then
begin
FWSAError := True;
LogWSAError;//WriteLog('Server socket nonblocking error!');
Terminate;
exit;
end;
{
Bind socket to interface}
addr.sin_family := AF_INET;
addr.sin_addr.s_addr := htonl(INADDR_ANY);
addr.sin_port := htons(Port);
if bind(FServerSocket, addr, sizeof(addr)) = SOCKET_ERROR then
begin
FWSAError := True;
LogWSAError;//WriteLog('Server socket binding error!');
Terminate;
exit;
end;
{
Start listener}
//if listen(FServerSocket, 1) = SOCKET_ERROR then
if listen(FServerSocket, SOMAXCONN) = SOCKET_ERROR then
begin
FWSAError := True;
LogWSAError;//WriteLog('Server socket listen error!');
Terminate;
exit;
end;
end;
destructor TSimpleServerThread.Destroy;
begin
evStopAllTCPThrds.SetEvent;
while ClientCounter > 0 do ;
closesocket(FServerSocket);
if Assigned(FOnDestroy) then FOnDestroy(Self);
inherited;
end;
procedure TSimpleServerThread.Execute;
var
ClientSocket: TSocket;
addr: TSockAddr;
len: Integer;
WSAErr: Integer;
begin
if not FWSAError then
begin
if Assigned(FOnExecute) then FOnExecute(Self);
len := sizeof(addr);
Fillchar(addr, sizeof(addr), 0);
while not Terminated do
begin
ClientSocket := accept(FServerSocket, @addr, @len);
if ClientSocket = INVALID_SOCKET then
begin
WSAErr := WSAGetLastError;
if WSAErr <> WSAEWOULDBLOCK then
begin
if WSAErr = WSAEINTR then
LogError('WSA', 'Interrupted function call')
else
LogError('WSA', 'Client socket accept WSA error: ' + IntToStr(WSAErr));
end;
Sleep(1);
end
else
begin
LogInfo('NEW', inet_ntoa(addr.sin_addr) + ':' + IntToStr(ntohs(addr.sin_port)));
NewClientThread(ClientSocket, addr);
end;
end;
end;
LogInfo('Server finished', '******************************//');
end;
procedure TSimpleServerThread.SetOnExecute(const Value: TNotifyEvent);
begin
FOnExecute := Value;
end;
procedure TSimpleServerThread.SetOnDestroy(const Value: TNotifyEvent);
begin
FOnDestroy := Value;
end;
procedure TSimpleServerThread.NewClientThread(ASocket: TSocket;
AAddr: TSockAddr);
begin
TSimpleClientThread.Create(ASocket, AAddr);
end;
initialization
InitWSA;
finalization
EndWSA;
end.