-
Notifications
You must be signed in to change notification settings - Fork 2
/
socketstuff.pas
421 lines (369 loc) · 8.69 KB
/
socketstuff.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
unit socketstuff;
interface
uses sysutils,winsock2,windows;
type
TDynByteArray = array of byte;
TSocketFrame=class
public
data:TDynByteArray; // csínján.
cursor:integer;
// átveszi a char* kezelését, felszabadítja destruáláskor.
constructor CreateFromData(adata:TDynByteArray);
constructor Create;
procedure Reset;
function ReadChar:byte;
function ReadInt:LongInt;
function ReadString:string;
procedure WriteChar(mit:byte);
procedure WriteInt(mit:integer);
procedure WriteString(const mit:string);
end;
TBufferedSocket=class
private
sock:TSocket;
recvbuf:TDynByteArray;
sendbuf:TDynByteArray;
public
addr:Tsockaddrin;
error:integer;
closeaftersend:boolean;
constructor Create(asock:Tsocket);
destructor Destroy;override;
procedure Update;
function RecvFrame(var hova:TSocketFrame):boolean;
procedure SendFrame(var mit:TSocketFrame);
end;
TUDPSocket=class
private
sock:TSocket;
public
error:integer;
constructor Create(port:integer);
destructor Destroy;override;
function Recv(var hova:TDynByteArray;var ip:DWORD;var port:WORD):boolean;
procedure Send(mit:TDynByteArray;ip:DWORD;port:WORD);
end;
function SelectForRead(sock:TSocket):integer;
function SelectForWrite(sock:TSocket):integer;
function SelectForError(sock:TSocket):integer;
function CreateClientSocket(srvc:sockaddr_in):TSocket;
function gethostbynamewrap(nam:string):Tinaddr;
implementation
procedure InitWinsock;
var
dat:TWSAData;
begin
WSAStartup(MAKEWORD(2,2),dat);
end;
function gethostbynamewrap(nam:string):Tinaddr;
var
hste:Phostent;
begin
hste:=gethostbyname(Pchar(nam));
if hste=nil then begin result.s_addr:=0; exit; end;
result := Pinaddr(hste.h_addr^ )^;
end;
function CreateClientSocket(srvc:sockaddr_in):TSocket;
begin
result:=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
connect(result,@srvc,sizeof(srvc));
end;
constructor TSocketFrame.CreateFromData(adata:TDynByteArray);
var
i:integer;
begin
setlength(data,length(adata));
for i:=0 to high(data) do
data[i]:=adata[i];
cursor:=0;
end;
constructor TSocketFrame.Create;
begin
inherited Create;
cursor:=0;
end;
procedure TSocketFrame.Reset;
begin
cursor:=0;
end;
function TSocketFrame.ReadChar:byte;
begin
cursor:=cursor+1;
if cursor<=length(data) then
result:=data[cursor-1]
else
result:=0;
end;
function TSocketFrame.ReadInt:LongInt;
begin
cursor:=cursor+4;
if cursor<=length(data) then
result:=data[cursor-4] or
(data[cursor-3] shl 8) or
(data[cursor-2] shl 16) or
(data[cursor-1] shl 24)
else
result:=0;
end;
function TSocketFrame.ReadString:string;
var
lngt:integer;
i:integer;
begin
lngt:=ReadChar;
if cursor+lngt<=length(data) then
begin
SetLength(result,lngt);
for i:=1 to lngt do
result[i]:=chr(data[cursor+i-1]);
cursor:=cursor+lngt;
end
else
result:='';
end;
procedure TSocketFrame.WriteChar(mit:byte);
begin
setlength(data,length(data)+1);
data[high(data)]:=mit;
end;
procedure TSocketFrame.WriteInt(mit:integer);
begin
setlength(data,length(data)+4);
data[high(data)-3]:=mit;
data[high(data)-2]:=mit shr 8;
data[high(data)-1]:=mit shr 16;
data[high(data) ]:=mit shr 24;
end;
procedure TSocketFrame.WriteString(const mit:string);
var
lngt:byte;
tmp:integer;
i:integer;
begin
lngt:=length(mit);
tmp:=length(data);
setlength(data,length(data)+1+lngt);
data[tmp]:=lngt;
for i:=1 to lngt do
data[tmp+i]:=ord(mit[i]);
end;
constructor TBufferedSocket.Create(asock:Tsocket);
begin
inherited Create;
sock:=asock;
closeaftersend:=false;
end;
destructor TBufferedSocket.Destroy;
begin
closesocket(sock);
inherited Destroy;
end;
procedure erasefront(var arr:TDynByteArray; mennyit:integer);
var
i:integer;
begin
if length(arr)<=mennyit then
setlength(arr,0)
else
begin
for i:=0 to high(arr)-mennyit do
arr[i]:=arr[i+mennyit];
setlength(arr,length(arr)-mennyit);
end;
end;
procedure TBufferedSocket.Update;
var
mit:PByteArray;
mennyit,mennyilett:integer;
hova: array[0..16*1024] of byte;
i,tmp:integer;
begin
while (length(sendbuf)>0) and (SelectForWrite(sock)<>0) do
begin
mit:=@sendbuf[0];
mennyit:=length(sendbuf);
if mennyit>16*1024 then
mennyit:=16*1024;
mennyilett:=send(sock,mit^,mennyit,0);
if (mennyilett=SOCKET_ERROR) then
error:=WSAGetLastError()
else
erasefront(sendbuf,mennyilett);
if closeaftersend and (length(sendbuf)=0) then
error:=1;
if (mennyit<>mennyilett) then
break;
end;
//recv
while SelectForRead(sock)<>0 do
begin
mennyit:=1024*16;
mennyilett:=recv(sock,hova,mennyit,0);
if (mennyilett=SOCKET_ERROR) then
error:=WSAGetLastError()
else
if (mennyilett=0) then //connection closed
error:=1
else
begin
tmp:=length(recvbuf);
setlength(recvbuf,length(recvbuf)+mennyilett);
for i:=0 to mennyilett-1 do
recvbuf[tmp+i]:=hova[i];
end;
if (mennyit<>mennyilett) then
break;
end;
if(SelectForError(sock))<>0 then
error:=2;//ezt észre fogod venni.
if (length(sendbuf)=0) and closeaftersend then
error:=3;//ezt is. Ez után töröld és csá
end;
function TBufferedSocket.RecvFrame(var hova:TSocketFrame):boolean;
var
siz:integer;
i:integer;
begin
result:=false;
if (length(recvbuf)<2) then
exit;
siz:=recvbuf[0] or (recvbuf[1] shl 8);
if (siz<1) then //faszom.
exit;
if (length(recvbuf)<2+siz) then
exit;
setlength(hova.data,siz);
for i:=0 to siz-1 do
hova.data[i]:=recvbuf[2+i];
hova.cursor:=0;
erasefront(recvbuf,siz+2);
result:=true;
end;
procedure TBufferedSocket.SendFrame(var mit:TSocketFrame);
var
tmp:integer;
i:integer;
meret:integer;
begin
if (length(mit.data)<=0) then // me no like
exit;
tmp:=length(sendbuf);
setlength(sendbuf,length(sendbuf)+2+length(mit.data));
meret:=length(mit.data);
sendbuf[tmp]:=meret;
sendbuf[tmp+1]:=meret shr 8;
tmp:=tmp+2;
for i:=0 to high(mit.data) do
sendbuf[tmp+i]:=mit.data[i];
end;
function SelectForRead(sock:TSocket):integer;
var
csillamvaltozo:TFDSet;
csodavaltozo:Ttimeval;
begin
csodavaltozo.tv_sec :=0;
csodavaltozo.tv_usec:=0;
FD_ZERO(csillamvaltozo);
FD_SET(sock,csillamvaltozo);
result:=select(1,@csillamvaltozo,nil,nil,@csodavaltozo);
end;
function SelectForWrite(sock:TSocket):integer;
var
csillamvaltozo:TFDSet;
csodavaltozo:Ttimeval;
begin
csodavaltozo.tv_sec :=0;
csodavaltozo.tv_usec:=0;
FD_ZERO(csillamvaltozo);
FD_SET(sock,csillamvaltozo);
result:=select(1,nil,@csillamvaltozo,nil,@csodavaltozo);
end;
function SelectForError(sock:TSocket):integer;
var
csillamvaltozo:TFDSet;
csodavaltozo:Ttimeval;
begin
csodavaltozo.tv_sec :=0;
csodavaltozo.tv_usec:=0;
FD_ZERO(csillamvaltozo);
FD_SET(sock,csillamvaltozo);
result:=select(1,nil,nil,@csillamvaltozo,@csodavaltozo);
end;
constructor TUDPSocket.Create(port:integer);
var
srvc:sockaddr_in;
hiba:integer;
amode:DWORD;
begin
inherited Create;
error:=0;
sock:=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
if sock=0 then
begin
error:=1;
exit;
end;
zeromemory(@srvc,sizeof(srvc));
Srvc.sin_family := AF_INET;
Srvc.sin_addr.S_addr := htonl(INADDR_ANY);
Srvc.sin_port := htons(port);
hiba:=bind(sock,@srvc,sizeof(srvc));
if hiba=SOCKET_ERROR then
begin
error:=2;
exit;
end;
amode:=1;
ioctlsocket(sock, FIONBIO, amode);
end;
destructor TUDPSocket.Destroy;
begin
closesocket(sock);
inherited Destroy;
end;
function TUDPSocket.Recv(var hova:TDynByteArray;var ip:DWORD;var port:WORD):boolean;
var
recvbuf:array[0..1500] of byte;
jott:integer;
fraddr:TSockaddr;
frlen:integer;
err:integer;
i:integer;
begin
result:=false;
zeromemory(@fraddr,sizeof(fraddr));
frlen:=sizeof(fraddr);
jott:=recvfrom(sock,recvbuf,sizeof(recvbuf),0,@fraddr,@frlen);
if jott>0 then
begin
result:=true;
setlength(hova,jott);
for i:=0 to jott-1 do
hova[i]:=recvbuf[i];
ip:=fraddr.sin_addr.S_addr;
port:=ntohs(fraddr.sin_port);
end
else
if jott=SOCKET_ERROR then
begin
err:=WSAGetLastError();
if err<>WSAEWOULDBLOCK then
error:=err;
end;
end;
procedure TUDPSocket.Send(mit:TDynByteArray;ip:DWORD;port:WORD);
var
toaddr:TSockaddr;
tolen:integer;
begin
zeromemory(@toaddr,sizeof(toaddr));
tolen:=sizeof(toaddr);
toaddr.sin_family := AF_INET;
toaddr.sin_addr.S_addr := ip;
toaddr.sin_port := htons(port);
if SOCKET_ERROR=sendto(sock,mit[0],length(mit),0,@toaddr,tolen) then
error:=WSAGetLastError;
end;
initialization
InitWinsock;
end.