FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
netcode.io-typescript/src/NetcodeConnection.ts at master · bennychen/netcode.io-typescript · GitHub
bennychen
/
netcode.io-typescript
Public
Notifications
You must be signed in to change notification settings
Fork
5
Star
31
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
netcode.io-typescript
/
src
/
NetcodeConnection.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
134 lines (117 loc) · 3.16 KB
Breadcrumbs
netcode.io-typescript
/
src
/
NetcodeConnection.ts
Copy path
File metadata and controls
134 lines (117 loc) · 3.16 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
namespace
Netcode
{
export
const
SOCKET_RCVBUF_SIZE
=
2048
*
1024
;
export
const
SOCKET_SNDBUF_SIZE
=
2048
*
1024
;
export
type
NetcodeRecvHandler
=
(
data
:
INetcodeData
)
=>
void
;
export
type
UDPConnCreator
=
(
)
=>
IUDPConn
;
export
interface
INetcodeData
{
data
:
Uint8Array
;
from
?:
IUDPAddr
;
}
export
class
NetcodeConn
{
public
constructor
(
)
{
this
.
_isClosed
=
true
;
this
.
_maxBytes
=
MAX_PACKET_BYTES
;
this
.
_recvSize
=
SOCKET_RCVBUF_SIZE
;
this
.
_sendSize
=
SOCKET_SNDBUF_SIZE
;
}
public
setRecvHandler
(
recvHandlerFn
:
NetcodeRecvHandler
)
{
this
.
_recvHandlerFn
=
recvHandlerFn
;
}
public
write
(
b
:
Uint8Array
)
:
boolean
{
if
(
this
.
_isClosed
)
{
return
false
;
}
this
.
_conn
.
send
(
b
)
;
return
true
;
}
public
writeTo
(
b
:
Uint8Array
,
addr
:
IUDPAddr
)
:
boolean
{
if
(
this
.
_isClosed
)
{
return
false
;
}
this
.
_conn
.
sendTo
(
b
,
addr
)
;
return
true
;
}
public
close
(
)
{
if
(
!
this
.
_isClosed
)
{
this
.
_isClosed
=
true
;
if
(
this
.
_conn
)
{
this
.
_conn
.
close
(
)
;
}
}
}
public
setReadBuffer
(
bytes
:
number
)
{
if
(
bytes
)
{
this
.
_recvSize
=
bytes
;
if
(
this
.
_conn
)
{
this
.
_conn
.
setReadBuffer
(
this
.
_recvSize
)
;
}
}
}
public
setWriteBuffer
(
bytes
:
number
)
{
if
(
bytes
)
{
this
.
_sendSize
=
bytes
;
if
(
this
.
_conn
)
{
this
.
_conn
.
setWriteBuffer
(
this
.
_recvSize
)
;
}
}
}
public
dial
(
createUdpConn
:
UDPConnCreator
,
addr
:
IUDPAddr
)
:
boolean
{
if
(
!
this
.
_recvHandlerFn
)
{
return
false
;
}
this
.
_conn
=
createUdpConn
(
)
;
if
(
this
.
_conn
!==
undefined
)
{
this
.
init
(
)
;
this
.
_conn
.
connect
(
addr
)
;
return
true
;
}
else
{
return
false
;
}
}
public
listen
(
createUdpConn
:
UDPConnCreator
,
addr
:
IUDPAddr
)
:
boolean
{
if
(
!
this
.
_recvHandlerFn
)
{
return
false
;
}
this
.
_conn
=
createUdpConn
(
)
;
if
(
this
.
_conn
!==
undefined
)
{
this
.
_conn
.
bind
(
addr
)
;
this
.
init
(
)
;
return
true
;
}
else
{
return
false
;
}
}
private
init
(
)
{
this
.
_isClosed
=
false
;
if
(
this
.
_recvSize
)
{
this
.
_conn
.
setReadBuffer
(
this
.
_recvSize
)
;
}
if
(
this
.
_sendSize
)
{
this
.
_conn
.
setWriteBuffer
(
this
.
_sendSize
)
;
}
this
.
_conn
.
onMessage
(
this
.
onMessage
.
bind
(
this
)
)
;
}
private
onMessage
(
message
:
Uint8Array
,
remote
:
IUDPAddr
)
{
if
(
!
this
.
_recvHandlerFn
)
{
console
.
warn
(
'no recv handler is set for connection'
)
;
return
;
}
if
(
message
&&
message
.
length
<=
this
.
_maxBytes
)
{
if
(
PacketFactory
.
peekPacketType
(
message
)
>=
PacketType
.
numPackets
)
{
console
.
warn
(
'invalid netcode packet is received'
)
;
}
else
{
this
.
_recvHandlerFn
(
{
data
:
message
,
from
:
remote
,
}
)
;
}
}
}
private
_conn
:
IUDPConn
;
private
_isClosed
:
boolean
;
private
_maxBytes
:
number
;
private
_recvSize
:
number
;
private
_sendSize
:
number
;
private
_recvHandlerFn
:
NetcodeRecvHandler
;
}
}
Back
|
FazBrowse Home
|
New Git URL