litenetlib-0952 1.0.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.
- litenetlib/__init__.py +66 -0
- litenetlib/channels/__init__.py +16 -0
- litenetlib/channels/base_channel.py +115 -0
- litenetlib/channels/reliable_channel.py +451 -0
- litenetlib/channels/sequenced_channel.py +239 -0
- litenetlib/core/__init__.py +53 -0
- litenetlib/core/connection_request.py +240 -0
- litenetlib/core/constants.py +186 -0
- litenetlib/core/events.py +336 -0
- litenetlib/core/internal_packets.py +358 -0
- litenetlib/core/manager.py +355 -0
- litenetlib/core/packet.py +457 -0
- litenetlib/core/peer.py +334 -0
- litenetlib/utils/__init__.py +21 -0
- litenetlib/utils/data_reader.py +447 -0
- litenetlib/utils/data_writer.py +402 -0
- litenetlib/utils/fast_bit_converter.py +199 -0
- litenetlib/utils/net_utils.py +165 -0
- litenetlib_0952-1.0.0.dist-info/METADATA +448 -0
- litenetlib_0952-1.0.0.dist-info/RECORD +23 -0
- litenetlib_0952-1.0.0.dist-info/WHEEL +5 -0
- litenetlib_0952-1.0.0.dist-info/licenses/LICENSE +21 -0
- litenetlib_0952-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"""
|
|
2
|
+
LiteNetLib 网络常量和枚举 / Network constants and enumerations for LiteNetLib v0.9.5.2
|
|
3
|
+
|
|
4
|
+
本模块定义了所有网络通信中使用的常量和枚举类型。
|
|
5
|
+
This module defines all constants and enumeration types used in network communication.
|
|
6
|
+
|
|
7
|
+
与 C# LiteNetLib v0.9.5.2 完全兼容 / Fully compatible with C# LiteNetLib v0.9.5.2.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from enum import IntEnum
|
|
11
|
+
from typing import List
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class PacketProperty(IntEnum):
|
|
15
|
+
"""
|
|
16
|
+
数据包属性类型 / Packet property types stored in the first byte of packet header
|
|
17
|
+
|
|
18
|
+
v0.9.5.2 版本枚举值(无 ReliableMerged)
|
|
19
|
+
v0.9.5.2 enum values (no ReliableMerged)
|
|
20
|
+
"""
|
|
21
|
+
UNRELIABLE = 0
|
|
22
|
+
CHANNELED = 1
|
|
23
|
+
ACK = 2
|
|
24
|
+
PING = 3
|
|
25
|
+
PONG = 4
|
|
26
|
+
CONNECT_REQUEST = 5
|
|
27
|
+
CONNECT_ACCEPT = 6
|
|
28
|
+
DISCONNECT = 7
|
|
29
|
+
UNCONNECTED_MESSAGE = 8
|
|
30
|
+
MTU_CHECK = 9
|
|
31
|
+
MTU_OK = 10
|
|
32
|
+
BROADCAST = 11
|
|
33
|
+
MERGED = 12
|
|
34
|
+
SHUTDOWN_OK = 13
|
|
35
|
+
PEER_NOT_FOUND = 14
|
|
36
|
+
INVALID_PROTOCOL = 15
|
|
37
|
+
NAT_MESSAGE = 16
|
|
38
|
+
EMPTY = 17
|
|
39
|
+
# Total is implicit (18)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class DeliveryMethod(IntEnum):
|
|
43
|
+
"""
|
|
44
|
+
数据传输方法 / Sending method type
|
|
45
|
+
|
|
46
|
+
定义数据包如何被传输到对等端。
|
|
47
|
+
Defines how packets are delivered to the peer.
|
|
48
|
+
|
|
49
|
+
传输方法说明 / Delivery method details:
|
|
50
|
+
- UNRELIABLE (不可靠): 可丢包、可重复、无序 / Can be dropped, duplicated, arrive out of order
|
|
51
|
+
- RELIABLE_UNORDERED (可靠无序): 不丢包、不重复、无序 / Won't be dropped/duplicated, can arrive out of order
|
|
52
|
+
- SEQUENCED (有序): 可丢包、不重复、有序 / Can be dropped, won't be duplicated, arrives in order
|
|
53
|
+
- RELIABLE_ORDERED (可靠有序): 不丢包、不重复、有序 / Won't be dropped/duplicated, arrives in order
|
|
54
|
+
- RELIABLE_SEQUENCED (可靠序列): 仅最后一个包可靠 / Only last packet reliable, cannot be fragmented
|
|
55
|
+
|
|
56
|
+
⚠️ 重要 / IMPORTANT:
|
|
57
|
+
枚举值必须与 C# LiteNetLib v0.9.5.2 的 DeliveryMethod 枚举完全匹配以实现互操作性。
|
|
58
|
+
Values MUST match C# LiteNetLib v0.9.5.2 DeliveryMethod enum for interoperability.
|
|
59
|
+
|
|
60
|
+
C# 参考代码 / C# Reference (NetConstants.cs):
|
|
61
|
+
Unreliable = 4
|
|
62
|
+
ReliableUnordered = 0
|
|
63
|
+
Sequenced = 1
|
|
64
|
+
ReliableOrdered = 2
|
|
65
|
+
ReliableSequenced = 3
|
|
66
|
+
"""
|
|
67
|
+
UNRELIABLE = 4
|
|
68
|
+
RELIABLE_UNORDERED = 0
|
|
69
|
+
SEQUENCED = 1
|
|
70
|
+
RELIABLE_ORDERED = 2
|
|
71
|
+
RELIABLE_SEQUENCED = 3
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class DisconnectReason(IntEnum):
|
|
75
|
+
"""
|
|
76
|
+
断开连接的原因 / Reason for peer disconnection
|
|
77
|
+
|
|
78
|
+
定义对等端断开连接时的原因代码。
|
|
79
|
+
Defines the reason code when a peer disconnects.
|
|
80
|
+
"""
|
|
81
|
+
CONNECTION_FAILED = 0
|
|
82
|
+
TIMEOUT = 1
|
|
83
|
+
HOST_UNREACHABLE = 2
|
|
84
|
+
NETWORK_UNREACHABLE = 3
|
|
85
|
+
REMOTE_CONNECTION_CLOSE = 4
|
|
86
|
+
DISCONNECT_PEER_CALLED = 5
|
|
87
|
+
CONNECTION_REJECTED = 6
|
|
88
|
+
INVALID_PROTOCOL = 7
|
|
89
|
+
UNKNOWN_HOST = 8
|
|
90
|
+
RECONNECT = 9
|
|
91
|
+
PEER_TO_PEER_CONNECTION = 10
|
|
92
|
+
PEER_NOT_FOUND = 11
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class UnconnectedMessageType(IntEnum):
|
|
96
|
+
"""
|
|
97
|
+
无连接消息类型 / Type of unconnected message
|
|
98
|
+
|
|
99
|
+
定义未连接状态下发送的消息类型。
|
|
100
|
+
Defines the type of message sent in unconnected state.
|
|
101
|
+
"""
|
|
102
|
+
BASIC_MESSAGE = 0
|
|
103
|
+
BROADCAST = 1
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class NetConstants:
|
|
107
|
+
"""
|
|
108
|
+
网络常量和配置值 / Network constants and configuration values
|
|
109
|
+
|
|
110
|
+
包含所有网络通信中使用的常量配置。
|
|
111
|
+
Contains all constant configurations used in network communication.
|
|
112
|
+
|
|
113
|
+
v0.9.5.2 特定值 / v0.9.5.2 specific values:
|
|
114
|
+
- ProtocolId = 11
|
|
115
|
+
- PossibleMtu has 7 options
|
|
116
|
+
"""
|
|
117
|
+
|
|
118
|
+
# 可调优参数 / Can be tuned
|
|
119
|
+
DEFAULT_WINDOW_SIZE: int = 64
|
|
120
|
+
SOCKET_BUFFER_SIZE: int = 1024 * 1024 # 1 MB
|
|
121
|
+
SOCKET_TTL: int = 255
|
|
122
|
+
|
|
123
|
+
# Packet headers
|
|
124
|
+
HEADER_SIZE: int = 1
|
|
125
|
+
CHANNELED_HEADER_SIZE: int = 4
|
|
126
|
+
FRAGMENT_HEADER_SIZE: int = 6
|
|
127
|
+
FRAGMENTED_HEADER_TOTAL_SIZE: int = CHANNELED_HEADER_SIZE + FRAGMENT_HEADER_SIZE
|
|
128
|
+
|
|
129
|
+
# Sequence numbers
|
|
130
|
+
MAX_SEQUENCE: int = 32768
|
|
131
|
+
HALF_MAX_SEQUENCE: int = MAX_SEQUENCE // 2
|
|
132
|
+
|
|
133
|
+
# Protocol
|
|
134
|
+
PROTOCOL_ID: int = 11 # v0.9.5.2 uses ProtocolId = 11
|
|
135
|
+
MAX_UDP_HEADER_SIZE: int = 68
|
|
136
|
+
|
|
137
|
+
# Possible MTU values (v0.9.5.2 has 7 options)
|
|
138
|
+
POSSIBLE_MTU: List[int] = [
|
|
139
|
+
576 - MAX_UDP_HEADER_SIZE, # Minimal (RFC 1191)
|
|
140
|
+
1024, # Most games standard
|
|
141
|
+
1232 - MAX_UDP_HEADER_SIZE, #
|
|
142
|
+
1460 - MAX_UDP_HEADER_SIZE, # Google Cloud
|
|
143
|
+
1472 - MAX_UDP_HEADER_SIZE, # VPN
|
|
144
|
+
1492 - MAX_UDP_HEADER_SIZE, # Ethernet with LLC and SNAP, PPPoE (RFC 1042)
|
|
145
|
+
1500 - MAX_UDP_HEADER_SIZE, # Ethernet II (RFC 1191)
|
|
146
|
+
]
|
|
147
|
+
|
|
148
|
+
# Max possible single packet size
|
|
149
|
+
INITIAL_MTU: int = POSSIBLE_MTU[0]
|
|
150
|
+
MAX_PACKET_SIZE: int = POSSIBLE_MTU[-1]
|
|
151
|
+
MAX_UNRELIABLE_DATA_SIZE: int = MAX_PACKET_SIZE - HEADER_SIZE
|
|
152
|
+
|
|
153
|
+
# Peer specific
|
|
154
|
+
MAX_CONNECTION_NUMBER: int = 4
|
|
155
|
+
|
|
156
|
+
# Packet pool
|
|
157
|
+
PACKET_POOL_SIZE: int = 1000
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
# Header sizes for each packet property
|
|
161
|
+
# Must match C# NetPacket.cs HeaderSizes exactly for v0.9.5.2
|
|
162
|
+
_HEADER_SIZES = {
|
|
163
|
+
PacketProperty.UNRELIABLE: NetConstants.HEADER_SIZE,
|
|
164
|
+
PacketProperty.CHANNELED: NetConstants.CHANNELED_HEADER_SIZE,
|
|
165
|
+
PacketProperty.ACK: NetConstants.CHANNELED_HEADER_SIZE,
|
|
166
|
+
PacketProperty.PING: NetConstants.HEADER_SIZE + 2,
|
|
167
|
+
PacketProperty.PONG: NetConstants.HEADER_SIZE + 10,
|
|
168
|
+
PacketProperty.CONNECT_REQUEST: 18, # NetConnectRequestPacket.HeaderSize
|
|
169
|
+
PacketProperty.CONNECT_ACCEPT: 15, # NetConnectAcceptPacket.Size
|
|
170
|
+
PacketProperty.DISCONNECT: NetConstants.HEADER_SIZE + 8,
|
|
171
|
+
PacketProperty.UNCONNECTED_MESSAGE: NetConstants.HEADER_SIZE,
|
|
172
|
+
PacketProperty.MTU_CHECK: NetConstants.HEADER_SIZE + 2,
|
|
173
|
+
PacketProperty.MTU_OK: NetConstants.HEADER_SIZE,
|
|
174
|
+
PacketProperty.BROADCAST: NetConstants.HEADER_SIZE,
|
|
175
|
+
PacketProperty.MERGED: NetConstants.HEADER_SIZE,
|
|
176
|
+
PacketProperty.SHUTDOWN_OK: NetConstants.HEADER_SIZE,
|
|
177
|
+
PacketProperty.PEER_NOT_FOUND: NetConstants.HEADER_SIZE,
|
|
178
|
+
PacketProperty.INVALID_PROTOCOL: NetConstants.HEADER_SIZE,
|
|
179
|
+
PacketProperty.NAT_MESSAGE: NetConstants.HEADER_SIZE,
|
|
180
|
+
PacketProperty.EMPTY: NetConstants.HEADER_SIZE,
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def get_header_size(prop: PacketProperty) -> int:
|
|
185
|
+
"""Get header size for a specific packet property."""
|
|
186
|
+
return _HEADER_SIZES.get(prop, NetConstants.HEADER_SIZE)
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Event system for LiteNetLib.
|
|
3
|
+
|
|
4
|
+
Defines event listeners and related types for handling network events.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import Optional, Callable, Any
|
|
8
|
+
from abc import ABC, abstractmethod
|
|
9
|
+
from dataclasses import dataclass
|
|
10
|
+
from litenetlib.core.constants import DisconnectReason, UnconnectedMessageType, DeliveryMethod
|
|
11
|
+
from litenetlib.utils.data_reader import NetDataReader
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass
|
|
15
|
+
class DisconnectInfo:
|
|
16
|
+
"""
|
|
17
|
+
Additional information about disconnection.
|
|
18
|
+
|
|
19
|
+
Attributes:
|
|
20
|
+
reason: The reason for disconnection
|
|
21
|
+
socket_error: Socket error code (if applicable)
|
|
22
|
+
additional_data: Additional data received with disconnect (only for RemoteConnectionClose)
|
|
23
|
+
"""
|
|
24
|
+
reason: DisconnectReason
|
|
25
|
+
socket_error: Optional[int] = None
|
|
26
|
+
additional_data: Optional[NetDataReader] = None
|
|
27
|
+
|
|
28
|
+
def __repr__(self) -> str:
|
|
29
|
+
return f"DisconnectInfo(reason={self.reason.name}, socket_error={self.socket_error})"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class INetEventListener(ABC):
|
|
33
|
+
"""
|
|
34
|
+
Interface for implementing network event listener.
|
|
35
|
+
|
|
36
|
+
Implement this interface to handle network events.
|
|
37
|
+
This is more efficient than using EventBasedNetListener.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
@abstractmethod
|
|
41
|
+
def on_peer_connected(self, peer) -> None:
|
|
42
|
+
"""
|
|
43
|
+
Called when a new peer connects.
|
|
44
|
+
|
|
45
|
+
Args:
|
|
46
|
+
peer: The connected peer object
|
|
47
|
+
"""
|
|
48
|
+
pass
|
|
49
|
+
|
|
50
|
+
@abstractmethod
|
|
51
|
+
def on_peer_disconnected(self, peer, disconnect_info: DisconnectInfo) -> None:
|
|
52
|
+
"""
|
|
53
|
+
Called when a peer disconnects.
|
|
54
|
+
|
|
55
|
+
Args:
|
|
56
|
+
peer: The disconnected peer
|
|
57
|
+
disconnect_info: Additional information about the disconnection
|
|
58
|
+
"""
|
|
59
|
+
pass
|
|
60
|
+
|
|
61
|
+
def on_network_error(self, address: Optional[tuple], socket_error: int) -> None:
|
|
62
|
+
"""
|
|
63
|
+
Called when a network error occurs.
|
|
64
|
+
|
|
65
|
+
Args:
|
|
66
|
+
address: Remote address (can be None)
|
|
67
|
+
socket_error: Socket error code
|
|
68
|
+
"""
|
|
69
|
+
pass
|
|
70
|
+
|
|
71
|
+
@abstractmethod
|
|
72
|
+
def on_network_receive(
|
|
73
|
+
self,
|
|
74
|
+
peer,
|
|
75
|
+
reader: NetDataReader,
|
|
76
|
+
channel_number: int,
|
|
77
|
+
delivery_method: DeliveryMethod
|
|
78
|
+
) -> None:
|
|
79
|
+
"""
|
|
80
|
+
Called when data is received from a peer.
|
|
81
|
+
|
|
82
|
+
Args:
|
|
83
|
+
peer: The peer that sent the data
|
|
84
|
+
reader: Data reader containing received data
|
|
85
|
+
channel_number: Channel number the data was received on
|
|
86
|
+
delivery_method: Delivery method of the received packet
|
|
87
|
+
"""
|
|
88
|
+
pass
|
|
89
|
+
|
|
90
|
+
def on_network_receive_unconnected(
|
|
91
|
+
self,
|
|
92
|
+
remote_address: tuple,
|
|
93
|
+
reader: NetDataReader,
|
|
94
|
+
message_type: UnconnectedMessageType
|
|
95
|
+
) -> None:
|
|
96
|
+
"""
|
|
97
|
+
Called when an unconnected message is received.
|
|
98
|
+
|
|
99
|
+
Args:
|
|
100
|
+
remote_address: Remote endpoint (host, port)
|
|
101
|
+
reader: Data reader containing message data
|
|
102
|
+
message_type: Type of unconnected message
|
|
103
|
+
"""
|
|
104
|
+
pass
|
|
105
|
+
|
|
106
|
+
def on_network_latency_update(self, peer, latency: int) -> None:
|
|
107
|
+
"""
|
|
108
|
+
Called when latency information is updated.
|
|
109
|
+
|
|
110
|
+
Args:
|
|
111
|
+
peer: The peer with updated latency
|
|
112
|
+
latency: Latency value in milliseconds
|
|
113
|
+
"""
|
|
114
|
+
pass
|
|
115
|
+
|
|
116
|
+
@abstractmethod
|
|
117
|
+
def on_connection_request(self, request) -> None:
|
|
118
|
+
"""
|
|
119
|
+
Called when a connection request is received.
|
|
120
|
+
|
|
121
|
+
Args:
|
|
122
|
+
request: Connection request information
|
|
123
|
+
"""
|
|
124
|
+
pass
|
|
125
|
+
|
|
126
|
+
def on_message_delivered(self, peer, user_data: Any) -> None:
|
|
127
|
+
"""
|
|
128
|
+
Called when a reliable message is delivered.
|
|
129
|
+
|
|
130
|
+
Args:
|
|
131
|
+
peer: The peer the message was delivered to
|
|
132
|
+
user_data: User data attached to the message
|
|
133
|
+
"""
|
|
134
|
+
pass
|
|
135
|
+
|
|
136
|
+
def on_peer_address_changed(self, peer, previous_address: tuple) -> None:
|
|
137
|
+
"""
|
|
138
|
+
Called when a peer's address changes.
|
|
139
|
+
|
|
140
|
+
Args:
|
|
141
|
+
peer: The peer with new address
|
|
142
|
+
previous_address: The previous address
|
|
143
|
+
"""
|
|
144
|
+
pass
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
class EventBasedNetListener(INetEventListener):
|
|
148
|
+
"""
|
|
149
|
+
Event-based listener using Python callbacks.
|
|
150
|
+
|
|
151
|
+
Simpler to use than implementing INetEventListener directly,
|
|
152
|
+
but slightly less efficient due to callback overhead.
|
|
153
|
+
"""
|
|
154
|
+
|
|
155
|
+
def __init__(self):
|
|
156
|
+
self._peer_connected_callback: Optional[Callable] = None
|
|
157
|
+
self._peer_disconnected_callback: Optional[Callable] = None
|
|
158
|
+
self._network_error_callback: Optional[Callable] = None
|
|
159
|
+
self._network_receive_callback: Optional[Callable] = None
|
|
160
|
+
self._network_receive_unconnected_callback: Optional[Callable] = None
|
|
161
|
+
self._network_latency_update_callback: Optional[Callable] = None
|
|
162
|
+
self._connection_request_callback: Optional[Callable] = None
|
|
163
|
+
self._message_delivered_callback: Optional[Callable] = None
|
|
164
|
+
self._peer_address_changed_callback: Optional[Callable] = None
|
|
165
|
+
|
|
166
|
+
# Callback setters
|
|
167
|
+
|
|
168
|
+
def set_peer_connected_callback(self, callback: Callable) -> 'EventBasedNetListener':
|
|
169
|
+
"""Set callback for peer connected event."""
|
|
170
|
+
self._peer_connected_callback = callback
|
|
171
|
+
return self
|
|
172
|
+
|
|
173
|
+
def set_peer_disconnected_callback(self, callback: Callable) -> 'EventBasedNetListener':
|
|
174
|
+
"""Set callback for peer disconnected event."""
|
|
175
|
+
self._peer_disconnected_callback = callback
|
|
176
|
+
return self
|
|
177
|
+
|
|
178
|
+
def set_network_error_callback(self, callback: Callable) -> 'EventBasedNetListener':
|
|
179
|
+
"""Set callback for network error event."""
|
|
180
|
+
self._network_error_callback = callback
|
|
181
|
+
return self
|
|
182
|
+
|
|
183
|
+
def set_network_receive_callback(self, callback: Callable) -> 'EventBasedNetListener':
|
|
184
|
+
"""Set callback for data received event."""
|
|
185
|
+
self._network_receive_callback = callback
|
|
186
|
+
return self
|
|
187
|
+
|
|
188
|
+
def set_network_receive_unconnected_callback(self, callback: Callable) -> 'EventBasedNetListener':
|
|
189
|
+
"""Set callback for unconnected message received event."""
|
|
190
|
+
self._network_receive_unconnected_callback = callback
|
|
191
|
+
return self
|
|
192
|
+
|
|
193
|
+
def set_network_latency_update_callback(self, callback: Callable) -> 'EventBasedNetListener':
|
|
194
|
+
"""Set callback for latency update event."""
|
|
195
|
+
self._network_latency_update_callback = callback
|
|
196
|
+
return self
|
|
197
|
+
|
|
198
|
+
def set_connection_request_callback(self, callback: Callable) -> 'EventBasedNetListener':
|
|
199
|
+
"""Set callback for connection request event."""
|
|
200
|
+
self._connection_request_callback = callback
|
|
201
|
+
return self
|
|
202
|
+
|
|
203
|
+
def set_message_delivered_callback(self, callback: Callable) -> 'EventBasedNetListener':
|
|
204
|
+
"""Set callback for message delivered event."""
|
|
205
|
+
self._message_delivered_callback = callback
|
|
206
|
+
return self
|
|
207
|
+
|
|
208
|
+
def set_peer_address_changed_callback(self, callback: Callable) -> 'EventBasedNetListener':
|
|
209
|
+
"""Set callback for peer address changed event."""
|
|
210
|
+
self._peer_address_changed_callback = callback
|
|
211
|
+
return self
|
|
212
|
+
|
|
213
|
+
# Clear callbacks
|
|
214
|
+
|
|
215
|
+
def clear_all_callbacks(self) -> None:
|
|
216
|
+
"""Clear all registered callbacks."""
|
|
217
|
+
self._peer_connected_callback = None
|
|
218
|
+
self._peer_disconnected_callback = None
|
|
219
|
+
self._network_error_callback = None
|
|
220
|
+
self._network_receive_callback = None
|
|
221
|
+
self._network_receive_unconnected_callback = None
|
|
222
|
+
self._network_latency_update_callback = None
|
|
223
|
+
self._connection_request_callback = None
|
|
224
|
+
self._message_delivered_callback = None
|
|
225
|
+
self._peer_address_changed_callback = None
|
|
226
|
+
|
|
227
|
+
# INetEventListener implementation
|
|
228
|
+
|
|
229
|
+
def on_peer_connected(self, peer) -> None:
|
|
230
|
+
if self._peer_connected_callback:
|
|
231
|
+
self._peer_connected_callback(peer)
|
|
232
|
+
|
|
233
|
+
def on_peer_disconnected(self, peer, disconnect_info: DisconnectInfo) -> None:
|
|
234
|
+
if self._peer_disconnected_callback:
|
|
235
|
+
self._peer_disconnected_callback(peer, disconnect_info)
|
|
236
|
+
|
|
237
|
+
def on_network_error(self, address: Optional[tuple], socket_error: int) -> None:
|
|
238
|
+
if self._network_error_callback:
|
|
239
|
+
self._network_error_callback(address, socket_error)
|
|
240
|
+
|
|
241
|
+
def on_network_receive(
|
|
242
|
+
self,
|
|
243
|
+
peer,
|
|
244
|
+
reader: NetDataReader,
|
|
245
|
+
channel_number: int,
|
|
246
|
+
delivery_method: DeliveryMethod
|
|
247
|
+
) -> None:
|
|
248
|
+
if self._network_receive_callback:
|
|
249
|
+
self._network_receive_callback(peer, reader, channel_number, delivery_method)
|
|
250
|
+
|
|
251
|
+
def on_network_receive_unconnected(
|
|
252
|
+
self,
|
|
253
|
+
remote_address: tuple,
|
|
254
|
+
reader: NetDataReader,
|
|
255
|
+
message_type: UnconnectedMessageType
|
|
256
|
+
) -> None:
|
|
257
|
+
if self._network_receive_unconnected_callback:
|
|
258
|
+
self._network_receive_unconnected_callback(remote_address, reader, message_type)
|
|
259
|
+
|
|
260
|
+
def on_network_latency_update(self, peer, latency: int) -> None:
|
|
261
|
+
if self._network_latency_update_callback:
|
|
262
|
+
self._network_latency_update_callback(peer, latency)
|
|
263
|
+
|
|
264
|
+
def on_connection_request(self, request) -> None:
|
|
265
|
+
if self._connection_request_callback:
|
|
266
|
+
self._connection_request_callback(request)
|
|
267
|
+
|
|
268
|
+
def on_message_delivered(self, peer, user_data: Any) -> None:
|
|
269
|
+
if self._message_delivered_callback:
|
|
270
|
+
self._message_delivered_callback(peer, user_data)
|
|
271
|
+
|
|
272
|
+
def on_peer_address_changed(self, peer, previous_address: tuple) -> None:
|
|
273
|
+
if self._peer_address_changed_callback:
|
|
274
|
+
self._peer_address_changed_callback(peer, previous_address)
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
class ConnectionRequest:
|
|
278
|
+
"""
|
|
279
|
+
Represents an incoming connection request.
|
|
280
|
+
|
|
281
|
+
Provides methods to accept or reject the connection.
|
|
282
|
+
"""
|
|
283
|
+
|
|
284
|
+
def __init__(self, remote_address: tuple, data: Optional[bytes] = None):
|
|
285
|
+
"""
|
|
286
|
+
Create a new connection request.
|
|
287
|
+
|
|
288
|
+
Args:
|
|
289
|
+
remote_address: Remote endpoint (host, port)
|
|
290
|
+
data: Additional data sent with request
|
|
291
|
+
"""
|
|
292
|
+
self._remote_address = remote_address
|
|
293
|
+
self._data = data
|
|
294
|
+
self._accepted = False
|
|
295
|
+
self._rejected = False
|
|
296
|
+
|
|
297
|
+
@property
|
|
298
|
+
def remote_address(self) -> tuple:
|
|
299
|
+
"""Get remote endpoint address."""
|
|
300
|
+
return self._remote_address
|
|
301
|
+
|
|
302
|
+
@property
|
|
303
|
+
def data(self) -> Optional[bytes]:
|
|
304
|
+
"""Get additional request data."""
|
|
305
|
+
return self._data
|
|
306
|
+
|
|
307
|
+
def accept(self) -> None:
|
|
308
|
+
"""Accept the connection request."""
|
|
309
|
+
if self._rejected:
|
|
310
|
+
raise RuntimeError("Cannot accept: request already rejected")
|
|
311
|
+
self._accepted = True
|
|
312
|
+
|
|
313
|
+
def reject(self, reject_data: Optional[bytes] = None) -> None:
|
|
314
|
+
"""
|
|
315
|
+
Reject the connection request.
|
|
316
|
+
|
|
317
|
+
Args:
|
|
318
|
+
reject_data: Optional data to send with rejection
|
|
319
|
+
"""
|
|
320
|
+
if self._accepted:
|
|
321
|
+
raise RuntimeError("Cannot reject: request already accepted")
|
|
322
|
+
self._rejected = True
|
|
323
|
+
self._reject_data = reject_data
|
|
324
|
+
|
|
325
|
+
@property
|
|
326
|
+
def is_accepted(self) -> bool:
|
|
327
|
+
"""Check if request was accepted."""
|
|
328
|
+
return self._accepted
|
|
329
|
+
|
|
330
|
+
@property
|
|
331
|
+
def is_rejected(self) -> bool:
|
|
332
|
+
"""Check if request was rejected."""
|
|
333
|
+
return self._rejected
|
|
334
|
+
|
|
335
|
+
def __repr__(self) -> str:
|
|
336
|
+
return f"ConnectionRequest(address={self._remote_address})"
|