-
Notifications
You must be signed in to change notification settings - Fork 0
/
uenetclass.pas
335 lines (287 loc) · 10.4 KB
/
uenetclass.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
unit uENetClass;
{ ENet UDP Class for FreePascal
Copyright (c) 2013 Do-wan Kim
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
- Add SendMsgEventPeer
}
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils,
enet_consts, enet_peer, enet_host, enet_packet, enet_socket,
enet_protocol;
type
TENet_Event_Type = (ENetEventNone, ENetEventConnect, ENetEventDisConnect, ENetEventReceive);
TENetPacketFlag = (ENetPacketReliable, ENetPacketUnsequenced,
ENetPacketNoAllocate, ENetPacketUnReliableFragment);
TENetPacketFlags = set of TENetPacketFlag;
TENetEventProc = procedure (const Event:ENetEvent) of object;
TENetEventRecv = procedure (const Event:ENetEvent; var BroadcastMsg : Boolean; var BroadcastChannel : Byte) of object;
{ TENetClass }
TENetClass = class
private
FInit : Boolean;
FHostname : string;
FAddress : ENetAddress;
FIsServer : Boolean;
FMaxPeer : Cardinal;
FMaxChannels : Byte;
FBandwidthIncoming, FBandwidthOutgoing : Cardinal;
FHost : pENetHost;
FEvent : ENetEvent;
FPeer : pENetPeer;
FClientData : Cardinal;
FConnectTimeout : Cardinal;
FMessageTimeout : Cardinal;
FEventNone : TENetEventProc;
FEventConnect : TENetEventProc;
FEventDisConnect : TENetEventProc;
FEventReceive : TENetEventRecv;
protected
public
constructor Create(Port : word; bServer : Boolean);
destructor Destroy; override;
function InitHost:Boolean;
procedure DeInitHost;
function Connect(const Host: string; Port: Word): Boolean;
function DisConnect(bNow: Boolean): Boolean;
function SendMsg(Channel: Byte; Data: Pointer; Length: Integer; flag: TENetPacketFlags;
WaitResponse: Boolean = False): Boolean;
function SendMsgEventPeer(Data: Pointer; Length: Integer;
flag: TENetPacketFlags; WaitResponse: Boolean=False): Boolean;
procedure FlushMsg;
function BroadcastMsg(Channel: Byte; Data: Pointer; Length: Integer; flag: TENetPacketFlags; WaitResponse : Boolean = False):Boolean;
function ProcessMsg:Boolean; virtual;
procedure Ping;
property Port : Word read FAddress.port write FAddress.port;
property MaxClient : Cardinal read FMaxPeer write FMaxPeer;
property MaxChannels : Byte read FMaxChannels write FMaxChannels;
property BandwidthIncoming : Cardinal read FBandwidthIncoming write FBandwidthIncoming;
property BandwidthOutgoing : Cardinal read FBandwidthOutgoing write FBandwidthOutgoing;
property ClientData : Cardinal read FClientData;
property ConnectTimeout : Cardinal read FConnectTimeout write FConnectTimeout;
property MessageTimeout : Cardinal read FMessageTimeout write FMessageTimeout;
property OnNone : TENetEventProc read FEventNone write FEventNone;
property OnConnect : TENetEventProc read FEventConnect write FEventConnect;
property OnDisconnect : TENetEventProc read FEventDisConnect write FEventDisConnect;
property OnReceive : TENetEventRecv read FEventReceive write FEventReceive;
end;
implementation
function GetPacketFlag(flag:TENetPacketFlags):Integer;
begin
Result:=0;
if ENetPacketReliable in flag then
Inc(Result,ENET_PACKET_FLAG_RELIABLE);
if ENetPacketUnsequenced in flag then
Inc(Result,ENET_PACKET_FLAG_UNSEQUENCED);
if ENetPacketNoAllocate in flag then
Inc(Result,ENET_PACKET_FLAG_NO_ALLOCATE);
if ENetPacketUnReliableFragment in flag then
Inc(Result,ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT);
end;
{ TENetClass }
constructor TENetClass.Create(Port: word; bServer: Boolean);
begin
FAddress.port:=Port;
FMaxPeer:=100;
FMaxChannels:=255;
FBandwidthIncoming:=0;
FBandwidthOutgoing:=0;
FIsServer:=False;
FConnectTimeout:=5000;
FMessageTimeout:=100;
FHost:=nil;
FPeer:=nil;
FEventNone:=nil;
FEventConnect:=nil;
FEventDisConnect:=nil;
FEventReceive:=nil;
FIsServer:=bServer;
FInit := enet_initialize = 0;
end;
destructor TENetClass.Destroy;
begin
DisConnect(True);
if FInit then
enet_deinitialize;
inherited Destroy;
end;
function TENetClass.InitHost: Boolean;
begin
DeInitHost;
if FInit then
if FIsServer then begin
// for server
FAddress.host:=ENET_HOST_ANY;
FHost:=enet_host_create(@FAddress,
FMaxPeer,
FMaxChannels,
FBandwidthIncoming,
FBandwidthOutgoing
);
end else begin
// for client
FMaxPeer:=1;
FHost:=enet_host_create(nil,
FMaxPeer,
FMaxChannels,
FBandwidthIncoming,
FBandwidthOutgoing
);
end;
Result:= FHost<>nil;
end;
procedure TENetClass.DeInitHost;
begin
if FHost<>nil then
enet_host_destroy(FHost);
FHost:=nil;
end;
function TENetClass.Connect(const Host: string; Port: Word): Boolean;
begin
Result:=False;
if not FIsServer then begin
DisConnect(True);
InitHost;
enet_address_set_host(@FAddress,PAnsiChar(Host));
FAddress.port:=Port;
FClientData:=Random(MaxInt);
FPeer:=enet_host_connect(FHost,@FAddress,FMaxChannels,FClientData);
Result:=FPeer<>nil;
if Result then
if (enet_host_service(FHost,@FEvent,FConnectTimeout)>0) and
(FEvent.EventType=ENET_EVENT_TYPE_CONNECT) then begin
Result:=True;
if Assigned(FEventConnect) then
FEventConnect(FEvent);
end else begin
enet_peer_reset(FPeer);
FPeer:=nil;
DeInitHost;
end;
end;
end;
function TENetClass.DisConnect(bNow:Boolean): Boolean;
begin
Result:=False;
if (not FIsServer) and (FHost<>nil) then begin
if FPeer<>nil then begin
if bNow then
enet_peer_disconnect_now(FPeer,FClientData)
else
enet_peer_disconnect(FPeer,FClientData);
FPeer:=nil;
end;
Result:=True;
end;
DeInitHost;
end;
function TENetClass.SendMsg(Channel: Byte; Data: Pointer; Length: Integer;
flag: TENetPacketFlags; WaitResponse: Boolean): Boolean;
var
FPacket : pENetPacket;
PacketFlag : Cardinal;
begin
Result:=False;
if FPeer<>nil then begin
PacketFlag:=GetPacketFlag(flag);
FPacket := enet_packet_create(Data, Length, PacketFlag);
if enet_peer_send(FPeer,Channel,FPacket)=0 then
if WaitResponse then
Result:=ProcessMsg;
end;
end;
function TENetClass.SendMsgEventPeer(Data: Pointer; Length: Integer;
flag: TENetPacketFlags; WaitResponse: Boolean = False): Boolean;
var
FPacket : pENetPacket;
PacketFlag : Cardinal;
begin
Result:=False;
if FEvent.Peer<>nil then begin
PacketFlag:=GetPacketFlag(flag);
FPacket := enet_packet_create(Data, Length, PacketFlag);
if enet_peer_send(FEvent.Peer,FEvent.channelID,FPacket)=0 then
if WaitResponse then
Result:=ProcessMsg;
end;
end;
procedure TENetClass.FlushMsg;
begin
if FHost<>nil then
enet_host_flush(FHost);
end;
function TENetClass.BroadcastMsg(Channel: Byte; Data: Pointer; Length: Integer;
flag: TENetPacketFlags; WaitResponse: Boolean): Boolean;
var
FPacket : pENetPacket;
PacketFlag : Cardinal;
begin
Result:=False;
if FPeer<>nil then begin
PacketFlag:=GetPacketFlag(flag);
FPacket:= enet_packet_create(Data, Length, PacketFlag);
enet_host_broadcast(FHost,Channel,FPacket);
if WaitResponse then
Result:=ProcessMsg;
end;
end;
function TENetClass.ProcessMsg: Boolean;
var
broadcast : Boolean;
bdChannel : Byte;
packet : pENetPacket;
pflag : Integer;
begin
Result := False;
if FHost<>nil then
if enet_host_service(FHost,@FEvent,FMessageTimeout)>0 then begin
case FEvent.EventType of
ENET_EVENT_TYPE_NONE : if Assigned(FEventNone) then
FEventNone(FEvent);
ENET_EVENT_TYPE_CONNECT : if Assigned(FEventConnect) then
FEventConnect(FEvent);
ENET_EVENT_TYPE_DISCONNECT : if Assigned(FEventDisConnect) then
FEventDisConnect(FEvent);
ENET_EVENT_TYPE_RECEIVE : begin
try
if FIsServer then begin
broadcast:=True;
pflag:=FEvent.packet^.flags;
bdChannel:= FEvent.channelID;
end;
if Assigned(FEventReceive) then
FEventReceive(FEvent,broadcast,bdChannel);
if FIsServer and broadcast then begin
packet := enet_packet_create(FEvent.packet^.data,FEvent.packet^.dataLength,pflag);
enet_host_broadcast(FHost,bdChannel,packet);
end;
finally
enet_packet_destroy(FEvent.packet);
end;
end;
else ;
end;
Result := True;
end;
end;
procedure TENetClass.Ping;
begin
if (not FIsServer) and (FPeer<>nil) then
enet_peer_ping(FPeer);
end;
end.