pyrak 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyrak/__init__.py +38 -0
- pyrak/const.py +70 -0
- pyrak/enums.py +200 -0
- pyrak/errors.py +42 -0
- pyrak/peer.py +829 -0
- pyrak/reliability_layer.py +502 -0
- pyrak/session.py +350 -0
- pyrak/types/__init__.py +59 -0
- pyrak/types/datagram.py +158 -0
- pyrak/types/frame.py +287 -0
- pyrak/types/message.py +272 -0
- pyrak/types/primitives.py +156 -0
- pyrak/utils.py +10 -0
- pyrak-0.1.0.dist-info/METADATA +165 -0
- pyrak-0.1.0.dist-info/RECORD +17 -0
- pyrak-0.1.0.dist-info/WHEEL +4 -0
- pyrak-0.1.0.dist-info/licenses/LICENSE +207 -0
pyrak/__init__.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""pyrak: a Python implementation of the RakNet protocol."""
|
|
2
|
+
|
|
3
|
+
from .enums import RELIABLE as RELIABLE
|
|
4
|
+
from .enums import RELIABLE_ORDERED as RELIABLE_ORDERED
|
|
5
|
+
from .enums import RELIABLE_SEQUENCED as RELIABLE_SEQUENCED
|
|
6
|
+
from .enums import UNRELIABLE as UNRELIABLE
|
|
7
|
+
from .enums import UNRELIABLE_SEQUENCED as UNRELIABLE_SEQUENCED
|
|
8
|
+
from .enums import ConnectMode as ConnectMode
|
|
9
|
+
from .enums import MessageID as MessageID
|
|
10
|
+
from .enums import Priority as Priority
|
|
11
|
+
from .enums import Reliability as Reliability
|
|
12
|
+
from .errors import AlreadyConnectedError as AlreadyConnectedError
|
|
13
|
+
from .errors import ConnectionAttemptFailedError as ConnectionAttemptFailedError
|
|
14
|
+
from .errors import ConnectionBannedError as ConnectionBannedError
|
|
15
|
+
from .errors import (
|
|
16
|
+
IncompatibleProtocolVersionError as IncompatibleProtocolVersionError,
|
|
17
|
+
)
|
|
18
|
+
from .errors import (
|
|
19
|
+
NoFreeIncomingConnectionsError as NoFreeIncomingConnectionsError,
|
|
20
|
+
)
|
|
21
|
+
from .errors import PeerClosedError as PeerClosedError
|
|
22
|
+
from .errors import PeerNotStartedError as PeerNotStartedError
|
|
23
|
+
from .errors import RakError as RakError
|
|
24
|
+
from .errors import SessionClosedError as SessionClosedError
|
|
25
|
+
from .errors import SessionNotFound as SessionNotFound
|
|
26
|
+
from .peer import RakPeer as RakPeer
|
|
27
|
+
from .reliability_layer import ReliabilityLayer as ReliabilityLayer
|
|
28
|
+
from .session import Session as Session
|
|
29
|
+
from .types import (
|
|
30
|
+
ConnectionAttemptFailed as ConnectionAttemptFailed,
|
|
31
|
+
)
|
|
32
|
+
from .types import ConnectionLost as ConnectionLost
|
|
33
|
+
from .types import ConnectionRequestAccepted as ConnectionRequestAccepted
|
|
34
|
+
from .types import DisconnectionNotification as DisconnectionNotification
|
|
35
|
+
from .types import NewIncomingConnection as NewIncomingConnection
|
|
36
|
+
from .types import Packet as Packet
|
|
37
|
+
from .types import UnconnectedPong as UnconnectedPong
|
|
38
|
+
from .types import UserMessage as UserMessage
|
pyrak/const.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import binascii
|
|
2
|
+
import os
|
|
3
|
+
import typing as t
|
|
4
|
+
|
|
5
|
+
IP_HEADER_SIZE: t.Final[int] = 20
|
|
6
|
+
UDP_HEADER_SIZE: t.Final[int] = 8
|
|
7
|
+
PACKET_HEADER_SIZE: t.Final[int] = IP_HEADER_SIZE + UDP_HEADER_SIZE
|
|
8
|
+
FRAME_SET_HEADER_SIZE: t.Final[int] = 4
|
|
9
|
+
"packet type (1 byte) + sequence_num (3 bytes)"
|
|
10
|
+
RAKNET_HEADER_SIZE: t.Final[int] = 8
|
|
11
|
+
ALL_HEADER_SIZE: t.Final[int] = (
|
|
12
|
+
PACKET_HEADER_SIZE + RAKNET_HEADER_SIZE + FRAME_SET_HEADER_SIZE
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
# 1500. The largest Ethernet packet size recommended. This is the
|
|
17
|
+
# typical setting for non-PPPoE, non-VPN connections. The default
|
|
18
|
+
# value for NETGEAR routers, adapters and switches.
|
|
19
|
+
# 1492. The size PPPoE prefers.
|
|
20
|
+
# 1472. Maximum size to use for pinging. (Bigger packets are fragmented.)
|
|
21
|
+
# 1468. The size DHCP prefers.
|
|
22
|
+
# 1460. Usable by AOL if you don't have large email attachments, etc.
|
|
23
|
+
# 1430. The size VPN and PPTP prefer.
|
|
24
|
+
# 1400. Maximum size for AOL DSL.
|
|
25
|
+
# 576. Typical value to connect to dial-up ISPs.
|
|
26
|
+
#
|
|
27
|
+
# Source: https://github.com/facebookarchive/RakNet/blob/1a169895a900c9fc4841c556e16514182b75faf8/Source/MTUSize.h
|
|
28
|
+
MAX_MTU_SIZE: t.Final[int] = 1492
|
|
29
|
+
MIN_MTU_SIZE: t.Final[int] = 400
|
|
30
|
+
SESSION_TIMEOUT: t.Final[int] = 10000 if not __debug__ else 30000
|
|
31
|
+
RAKNET_PROTOCOL_VERSION: t.Final[int] = 6
|
|
32
|
+
"""Default vanilla RakNet protocol version that this library
|
|
33
|
+
implements.
|
|
34
|
+
|
|
35
|
+
Source: https://github.com/facebookarchive/RakNet/blob/1a169895a900c9fc4841c556e16514182b75faf8/Source/RakNetVersion.h#L19
|
|
36
|
+
"""
|
|
37
|
+
MAX_LOCAL_ADDRESSES: t.Final[int] = int(
|
|
38
|
+
os.environ.get("PYRAK_MAX_LOCAL_ADDRESSES", "20")
|
|
39
|
+
)
|
|
40
|
+
"""Maximum number of local IP addresses supported.
|
|
41
|
+
Regular RakNet uses 10 by default, MCPE and its successors use 20.
|
|
42
|
+
|
|
43
|
+
Source: https://github.com/facebookarchive/RakNet/blob/1a169895a900c9fc4841c556e16514182b75faf8/Source/RakNetDefines.h#L76
|
|
44
|
+
"""
|
|
45
|
+
MAX_OFFLINE_DATA_LENGTH: t.Final[int] = 400
|
|
46
|
+
|
|
47
|
+
MAGIC: t.Final[bytes] = binascii.unhexlify(b"00ffff00fefefefefdfdfdfd12345678")
|
|
48
|
+
"""
|
|
49
|
+
Unconnected message signature, those bytes used to identify the type of a
|
|
50
|
+
UDP-message (unconnected or reliabile).
|
|
51
|
+
Source: https://github.com/facebookarchive/RakNet/blob/1a169895a900c9fc4841c556e16514182b75faf8/Source/RakPeer.cpp#L135
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
RESEND_TIME: t.Final[int] = 500
|
|
55
|
+
"ms, a fixed retransmission timeout of a reliable frame"
|
|
56
|
+
PING_INTERVAL: t.Final[int] = 5000
|
|
57
|
+
"ms, how often a connected ping is sent"
|
|
58
|
+
UPDATE_INTERVAL: t.Final[int] = 10
|
|
59
|
+
"ms, how often RakPeer updates its sessions"
|
|
60
|
+
MAX_SPLIT_COUNT: t.Final[int] = 16384
|
|
61
|
+
"The maximum number of parts a split message may have."
|
|
62
|
+
MAX_SPLIT_ENTRIES: t.Final[int] = 256
|
|
63
|
+
"The maximum number of incomplete split messages per session."
|
|
64
|
+
MAX_ORDERING_QUEUE: t.Final[int] = 2048
|
|
65
|
+
"The maximum number of out-of-order frames cached per channel."
|
|
66
|
+
ORDERING_CHANNELS: t.Final[int] = 32
|
|
67
|
+
MAX_DATAGRAMS_PER_UPDATE: t.Final[int] = 128
|
|
68
|
+
"Pacing: the maximum number of frame sets sent per session per update."
|
|
69
|
+
MTU_SIZES: t.Final[tuple[int, ...]] = (MAX_MTU_SIZE, 1200, 576)
|
|
70
|
+
"MTU sizes a client probes during the offline handshake."
|
pyrak/enums.py
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import enum
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class ConnectMode(enum.Enum):
|
|
5
|
+
NO_ACTION = enum.auto()
|
|
6
|
+
DISCONNECT_ASAP = enum.auto()
|
|
7
|
+
DISCONNECT_ASAP_SILENTLY = enum.auto()
|
|
8
|
+
DISCONNECT_ON_NO_ACK = enum.auto()
|
|
9
|
+
REQUESTED_CONNECTION = enum.auto()
|
|
10
|
+
HANDLING_CONNECTION_REQUEST = enum.auto()
|
|
11
|
+
UNVERIFIED_SENDER = enum.auto()
|
|
12
|
+
CONNECTED = enum.auto()
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class Priority(enum.IntEnum):
|
|
16
|
+
"Strict send priority: a lower value is sent first."
|
|
17
|
+
|
|
18
|
+
IMMEDIATE = 0
|
|
19
|
+
HIGH = 1
|
|
20
|
+
MEDIUM = 2
|
|
21
|
+
LOW = 3
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@enum.unique
|
|
25
|
+
class Reliability(enum.IntEnum):
|
|
26
|
+
"""The value is the base flags byte of a frame (`reliability << 5`).
|
|
27
|
+
The split bit (0x10) is added by the frame classes.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
UNRELIABLE = 0x00 # 0
|
|
31
|
+
UNRELIABLE_SPLIT = 0x10 # 0 + SPLIT_FLAG
|
|
32
|
+
UNRELIABLE_SEQUENCED = 0x20 # 1
|
|
33
|
+
UNRELIABLE_SEQUENCED_SPLIT = 0x30 # 1 + SPLIT_FLAG
|
|
34
|
+
RELIABLE = 0x40 # 2
|
|
35
|
+
RELIABLE_SPLIT = 0x50 # 2 + SPLIT_FLAG
|
|
36
|
+
RELIABLE_ORDERED = 0x60 # 3
|
|
37
|
+
RELIABLE_ORDERED_SPLIT = 0x70 # 3 + SPLIT_FLAG
|
|
38
|
+
RELIABLE_SEQUENCED = 0x80 # 4
|
|
39
|
+
RELIABLE_SEQUENCED_SPLIT = 0x90 # 4 + SPLIT_FLAG
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
UNRELIABLE = Reliability.UNRELIABLE
|
|
43
|
+
UNRELIABLE_SEQUENCED = Reliability.UNRELIABLE_SEQUENCED
|
|
44
|
+
RELIABLE = Reliability.RELIABLE
|
|
45
|
+
RELIABLE_ORDERED = Reliability.RELIABLE_ORDERED
|
|
46
|
+
RELIABLE_SEQUENCED = Reliability.RELIABLE_SEQUENCED
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@enum.unique
|
|
50
|
+
class MessageID(enum.IntEnum):
|
|
51
|
+
"""
|
|
52
|
+
Source: https://github.com/facebookarchive/RakNet/blob/master/Source/MessageIdentifiers.h
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
# Commented message ids are not implemented.
|
|
56
|
+
# Messages 0x00 - 0x0f only for internal use, they are never
|
|
57
|
+
# returned from Peer.receive()
|
|
58
|
+
|
|
59
|
+
CONNECTED_PING = 0x00
|
|
60
|
+
"""Ping from a connected system. Update timestamps.
|
|
61
|
+
(internal use only)
|
|
62
|
+
"""
|
|
63
|
+
UNCONNECTED_PING = 0x01
|
|
64
|
+
"""Ping from an unconnected system. Reply but do not update timestamps
|
|
65
|
+
(internal use only)
|
|
66
|
+
"""
|
|
67
|
+
UNCONNECTED_PING_OPEN_CONNECTIONS = 0x02
|
|
68
|
+
"""Ping from an unconnected system. Only reply if we have open connections.
|
|
69
|
+
Do not update timestamps.
|
|
70
|
+
(internal use only)
|
|
71
|
+
"""
|
|
72
|
+
CONNECTED_PONG = 0x03
|
|
73
|
+
"""Pong from a connected system. Update timestamps.
|
|
74
|
+
(internal use only)
|
|
75
|
+
"""
|
|
76
|
+
# DETECT_LOST_CONNECTIONS = 0x04
|
|
77
|
+
"""A reliable packet to detect lost connections
|
|
78
|
+
(internal use only)
|
|
79
|
+
"""
|
|
80
|
+
OPEN_CONNECTION_REQUEST_1 = 0x05
|
|
81
|
+
"""C2S: Used to determine the optimal MTU size for a connection and to
|
|
82
|
+
check whether the client's RakNet protocol version is compatible
|
|
83
|
+
with the server's protocol version.
|
|
84
|
+
Set with no fragment set. If protocol fails on server,
|
|
85
|
+
returns ID_INCOMPATIBLE_PROTOCOL_VERSION to client
|
|
86
|
+
"""
|
|
87
|
+
OPEN_CONNECTION_REPLY_1 = 0x06
|
|
88
|
+
"""
|
|
89
|
+
S2C: Header(1), OfflineMesageID(16), server GUID(8), HasSecurity(1),
|
|
90
|
+
Cookie(4, if HasSecurity), public key (if do security is true), MTU(2).
|
|
91
|
+
If public key fails on client, returns ID_PUBLIC_KEY_MISMATCH
|
|
92
|
+
"""
|
|
93
|
+
OPEN_CONNECTION_REQUEST_2 = 0x07
|
|
94
|
+
"""
|
|
95
|
+
C2S: eader(1), OfflineMesageID(16), Cookie(4, if HasSecurity is true on
|
|
96
|
+
the server), clientSupportsSecurity(1 bit), handshakeChallenge (if has
|
|
97
|
+
security on both server and client), remoteBindingAddress(6), MTU(2),
|
|
98
|
+
client GUID(8)
|
|
99
|
+
Connection slot allocated if cookie is valid, server is not full, GUID and
|
|
100
|
+
IP not already in use.
|
|
101
|
+
"""
|
|
102
|
+
OPEN_CONNECTION_REPLY_2 = 0x08
|
|
103
|
+
"""
|
|
104
|
+
S2C: Header(1), OfflineMesageID(16), server GUID(8), mtu(2),
|
|
105
|
+
doSecurity(1 bit), handshakeAnswer (if do security is true)
|
|
106
|
+
"""
|
|
107
|
+
CONNECTION_REQUEST = 0x09
|
|
108
|
+
"C2S: Header(1), GUID(8), Timestamp, HasSecurity(1), Proof(32)"
|
|
109
|
+
# REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY = 0x0a
|
|
110
|
+
"""RakPeer - Remote system requires secure connections, pass a public key
|
|
111
|
+
to RakPeerInterface::Connect()"""
|
|
112
|
+
# OUR_SYSTEM_REQUIRES_SECURITY = 0x0b
|
|
113
|
+
"""RakPeer - We passed a public key to RakPeerInterface::Connect(), but
|
|
114
|
+
the other system did not have security turned on"""
|
|
115
|
+
# PUBLIC_KEY_MISMATCH = 0x0c
|
|
116
|
+
"RakPeer - Wrong public key passed to RakPeerInterface::Connect()"
|
|
117
|
+
# OUT_OF_BAND_INTERNAL = 0x0d
|
|
118
|
+
"""RakPeer - Same as ID_ADVERTISE_SYSTEM, but intended for internal use
|
|
119
|
+
rather than being passed to the user. Second byte indicates type. Used
|
|
120
|
+
currently for NAT punchthrough for receiver port advertisement.
|
|
121
|
+
See ID_NAT_ADVERTISE_RECIPIENT_PORT
|
|
122
|
+
"""
|
|
123
|
+
# SND_RECEIPT_ACKED = 0x0e
|
|
124
|
+
"""
|
|
125
|
+
If RakPeerInterface::Send() is called where PacketReliability contains
|
|
126
|
+
_WITH_ACK_RECEIPT, then on a later call to RakPeerInterface::Receive() you
|
|
127
|
+
will get ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS. The message will be
|
|
128
|
+
5 bytes long, and bytes 1-4 inclusive will contain a number in native order
|
|
129
|
+
containing a number that identifies this message. This number will be
|
|
130
|
+
returned by RakPeerInterface::Send() or RakPeerInterface::SendList().
|
|
131
|
+
ID_SND_RECEIPT_ACKED means that the message arrived
|
|
132
|
+
"""
|
|
133
|
+
# SND_RECEIPT_LOSS = 0x0f
|
|
134
|
+
"""
|
|
135
|
+
If RakPeerInterface::Send() is called where PacketReliability contains
|
|
136
|
+
UNRELIABLE_WITH_ACK_RECEIPT, then on a later call to
|
|
137
|
+
RakPeerInterface::Receive() you will get ID_SND_RECEIPT_ACKED or
|
|
138
|
+
ID_SND_RECEIPT_LOSS. The message will be 5 bytes long, and bytes 1-4
|
|
139
|
+
inclusive will contain a number in native order containing a number that
|
|
140
|
+
identifies this message. This number will be returned by
|
|
141
|
+
RakPeerInterface::Send() or RakPeerInterface::SendList().
|
|
142
|
+
ID_SND_RECEIPT_LOSS means that an ack for the message did not arrive (it
|
|
143
|
+
may or may not have been delivered, probably not). On disconnect or
|
|
144
|
+
shutdown, you will not get ID_SND_RECEIPT_LOSS for unsent messages, you
|
|
145
|
+
should consider those messages as all lost.
|
|
146
|
+
"""
|
|
147
|
+
|
|
148
|
+
# User types - do not change this.
|
|
149
|
+
|
|
150
|
+
CONNECTION_REQUEST_ACCEPTED = 0x10
|
|
151
|
+
CONNECTION_ATTEMPT_FAILED = 0x11
|
|
152
|
+
"""Local event: our connection attempt failed (never sent over the wire)"""
|
|
153
|
+
ALREADY_CONNECTED = 0x12
|
|
154
|
+
NEW_INCOMING_CONNECTION = 0x13
|
|
155
|
+
NO_FREE_INCOMING_CONNECTIONS = 0x14
|
|
156
|
+
DISCONNECTION_NOTIFICATION = 0x15
|
|
157
|
+
CONNECTION_LOST = 0x16
|
|
158
|
+
"""Local event: the connection timed out (never sent over the wire)"""
|
|
159
|
+
CONNECTION_BANNED = 0x17
|
|
160
|
+
# INVALID_PASSWORD = 0x18
|
|
161
|
+
INCOMPATIBLE_PROTOCOL_VERSION = 0x19
|
|
162
|
+
"""This packet is sent when the server's protocol version doesn't
|
|
163
|
+
match the client's protocol version.
|
|
164
|
+
"""
|
|
165
|
+
# IP_RECENTLY_CONNECTED = 0x1a
|
|
166
|
+
# TIMESTAMP = 0x1b
|
|
167
|
+
UNCONNECTED_PONG = 0x1C
|
|
168
|
+
"""Pong from an unconnected system.
|
|
169
|
+
"""
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
@enum.unique
|
|
173
|
+
class DatagramID(enum.IntEnum):
|
|
174
|
+
"""These datagrams are sent at the UDP layer and they are used to
|
|
175
|
+
provide reliability and ordering.
|
|
176
|
+
"""
|
|
177
|
+
|
|
178
|
+
# The low nibble of 0x80-0x8f holds service flags (e.g. "has B and AS",
|
|
179
|
+
# "is packet pair", "is continuous send"). Any of them is accepted
|
|
180
|
+
# on receive, we always send FRAME_SET_4 (0x84).
|
|
181
|
+
|
|
182
|
+
FRAME_SET_0 = 0x80
|
|
183
|
+
FRAME_SET_1 = 0x81
|
|
184
|
+
FRAME_SET_2 = 0x82
|
|
185
|
+
FRAME_SET_3 = 0x83
|
|
186
|
+
FRAME_SET_4 = 0x84
|
|
187
|
+
FRAME_SET_5 = 0x85
|
|
188
|
+
FRAME_SET_6 = 0x86
|
|
189
|
+
FRAME_SET_7 = 0x87
|
|
190
|
+
FRAME_SET_8 = 0x88
|
|
191
|
+
FRAME_SET_9 = 0x89
|
|
192
|
+
FRAME_SET_A = 0x8A
|
|
193
|
+
FRAME_SET_B = 0x8B
|
|
194
|
+
FRAME_SET_C = 0x8C
|
|
195
|
+
FRAME_SET_D = 0x8D
|
|
196
|
+
FRAME_SET_E = 0x8E
|
|
197
|
+
FRAME_SET_F = 0x8F
|
|
198
|
+
|
|
199
|
+
NAK = 0xA0
|
|
200
|
+
ACK = 0xC0
|
pyrak/errors.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
class RakError(Exception):
|
|
2
|
+
"Base class of all pyrak errors."
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class PeerNotStartedError(RakError):
|
|
6
|
+
"RakPeer must be used as `async with RakPeer() as peer`."
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class PeerClosedError(RakError):
|
|
10
|
+
"The peer has been shut down."
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class SessionNotFound(RakError):
|
|
14
|
+
"There is no session for the given address or GUID."
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class ConnectionAttemptFailedError(RakError):
|
|
18
|
+
"The remote system did not answer or the handshake did not complete."
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class IncompatibleProtocolVersionError(ConnectionAttemptFailedError):
|
|
22
|
+
def __init__(self, protocol_version: int) -> None:
|
|
23
|
+
super().__init__(
|
|
24
|
+
f"Remote system uses RakNet protocol version {protocol_version}"
|
|
25
|
+
)
|
|
26
|
+
self.protocol_version = protocol_version
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class ConnectionBannedError(ConnectionAttemptFailedError):
|
|
30
|
+
"The remote system has banned us."
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class NoFreeIncomingConnectionsError(ConnectionAttemptFailedError):
|
|
34
|
+
"The remote system has no free incoming slots."
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class AlreadyConnectedError(ConnectionAttemptFailedError):
|
|
38
|
+
"We (or our GUID) are already connected to the remote system."
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class SessionClosedError(RakError):
|
|
42
|
+
"The session is closed or not connected, nothing can be sent."
|