13
13
from core .util .TimerHub import TimerHub
14
14
from core .util .UtilApi import Singleton
15
15
16
- from core . common . sanic_jwt_extended import JWT , refresh_jwt_required , jwt_required , jwt_optional
17
- from core . common . sanic_jwt_extended .tokens import Token
16
+ from sanic_jwt_extended import JWT
17
+ from sanic_jwt_extended .tokens import Token
18
18
19
19
if typing .TYPE_CHECKING :
20
- from RpcHandler import RpcHandler , RPC_TYPE_REQUEST
20
+ from RpcHandler import RpcHandler
21
21
import TcpConn
22
22
23
23
24
- CONN_TYPE_TCP = 0
25
- CONN_TYPE_RUDP = 1
24
+ PROTO_TYPE_TCP = 0
25
+ PROTO_TYPE_RUDP = 1
26
26
27
27
ROLE_TYPE_ACTIVE = 0
28
28
ROLE_TYPE_PASSIVE = 1
@@ -43,13 +43,13 @@ def connection_made(self, transport: transports.BaseTransport) -> None:
43
43
addr = transport .get_extra_info ('peername' ) # type: typing.Tuple[str, int]
44
44
if self ._role_type == ROLE_TYPE_PASSIVE :
45
45
self ._conn = ConnMgr .instance ().add_incoming_conn (
46
- CONN_TYPE_TCP , transport , addr
46
+ PROTO_TYPE_TCP , transport , addr
47
47
# self._rpc_handler
48
48
)
49
49
else :
50
- self ._conn = ConnMgr .instance ().get_conn (addr , CONN_TYPE_TCP )
50
+ self ._conn = ConnMgr .instance ().get_conn (addr , PROTO_TYPE_TCP )
51
51
assert self ._conn
52
- LogManager .get_logger ().info (f"{ addr !r} is connected !!!!" )
52
+ LogManager .get_logger ().info (f"TCP { addr !r} is connected !!!!" )
53
53
54
54
def data_received (self , data : bytes ) -> None :
55
55
self ._conn .handle_read (data )
@@ -70,6 +70,9 @@ class RudpProtocol(asyncio.DatagramProtocol):
70
70
# def __init__(self, create_kcp_conn_cb):
71
71
def __init__ (self ):
72
72
# self._create_kcp_conn_cb = create_kcp_conn_cb
73
+ with JWT .initialize () as manager :
74
+ manager .config .secret_key = "new_byte"
75
+
73
76
self .transport = None
74
77
75
78
def connection_made (self , transport ):
@@ -82,6 +85,7 @@ def datagram_received(self, data: bytes, addr):
82
85
global RUDP_HANDSHAKE_ACK_PREFIX
83
86
# assert callable(self._create_kcp_conn_cb)
84
87
# self._create_kcp_conn_cb(addr)
88
+ # print(f'datagram_received: {data=}, {addr=}')
85
89
if data == RUDP_HANDSHAKE_SYN :
86
90
RUDP_CONV += 1
87
91
access_token_jwt : bytes = JWT .create_access_token (
@@ -96,14 +100,14 @@ def datagram_received(self, data: bytes, addr):
96
100
token_obj = Token (raw_jwt )
97
101
if token_obj .type != "access" :
98
102
raise Exception ("Only access tokens are allowed" )
99
- conv = token_obj .identity
103
+ conv = int ( token_obj .identity )
100
104
101
105
ConnMgr .instance ().add_incoming_conn (
102
- CONN_TYPE_RUDP , self .transport , addr , rudp_conv = conv
106
+ PROTO_TYPE_RUDP , self .transport , addr , rudp_conv = conv
103
107
# self._rpc_handler
104
108
)
105
109
# addr = transport.get_extra_info('peername') # type: typing.Tuple[str, int]
106
- LogManager .get_logger ().info (f"{ addr !r} is connected !!!!" )
110
+ LogManager .get_logger ().info (f"RUDP { addr !r} is connected !!!!" )
107
111
elif data .startswith (RUDP_HANDSHAKE_SYN_ACK_PREFIX ):
108
112
parts = data .split (RUDP_HANDSHAKE_SYN_ACK_PREFIX , 2 )
109
113
if len (parts ) != 2 :
@@ -114,22 +118,18 @@ def datagram_received(self, data: bytes, addr):
114
118
if token_obj .type != "access" :
115
119
raise Exception ("Only access tokens are allowed" )
116
120
self .transport .sendto (RUDP_HANDSHAKE_ACK_PREFIX + raw_jwt .encode (), addr )
117
- conv = token_obj .identity
118
- try :
119
- ConnMgr .instance ().set_fut_result (addr , conv )
120
- except asyncio .InvalidStateError :
121
- # 并发情况即使`fut`已经done了也无所谓, 不处理即可
122
- pass
121
+ conv = int (token_obj .identity )
122
+ ConnMgr .instance ().set_fut_result (addr , conv )
123
123
124
124
# ConnMgr.instance().add_incoming_conn(
125
125
# ROLE_TYPE_ACTIVE, CONN_TYPE_RUDP, self.transport,
126
126
# rudp_conv=conv, rudp_peer_addr=addr
127
127
# # self._rpc_handler
128
128
# )
129
129
# addr = transport.get_extra_info('peername') # type: typing.Tuple[str, int]
130
- LogManager .get_logger ().info (f"{ addr !r} is connected !!!!" )
130
+ LogManager .get_logger ().info (f"RUDP { addr !r} is connected !!!!" )
131
131
else :
132
- _cur_conn = ConnMgr .instance ().get_conn (addr , CONN_TYPE_RUDP )
132
+ _cur_conn = ConnMgr .instance ().get_conn (addr , PROTO_TYPE_RUDP )
133
133
assert _cur_conn
134
134
_cur_conn .handle_read (data )
135
135
@@ -141,7 +141,7 @@ def __init__(self):
141
141
self ._timer_hub = TimerHub ()
142
142
self ._logger = LogManager .get_logger ()
143
143
self ._is_proxy = False
144
- self ._conn_type_2_addr_2_conn = {CONN_TYPE_TCP : {}, CONN_TYPE_RUDP : {}}
144
+ self ._proto_type_2_addr_2_conn = {PROTO_TYPE_TCP : {}, PROTO_TYPE_RUDP : {}}
145
145
self ._addr_2_try_connect_times = defaultdict (int )
146
146
self ._addr_2_rudp_conned_fut = {}
147
147
@@ -156,58 +156,68 @@ def final_fut_cb(fut, _addr=addr):
156
156
_fut .add_done_callback (final_fut_cb )
157
157
return _fut
158
158
159
+ def set_fut_result (self , addr , conv ):
160
+ _fut = self ._addr_2_rudp_conned_fut .get (addr , None )
161
+ if _fut is None :
162
+ return
163
+ try :
164
+ _fut .set_result (conv )
165
+ except asyncio .InvalidStateError :
166
+ # 并发情况即使`fut`已经done了也无所谓, 不处理即可
167
+ pass
168
+
159
169
def set_is_proxy (self , is_proxy ):
160
170
self ._is_proxy = is_proxy
161
171
162
- def get_conn (self , addr , conn_type = None ) -> ConnBase :
163
- if conn_type is not None :
164
- return self ._conn_type_2_addr_2_conn [ conn_type ].get (addr , None )
165
- _conn = self ._conn_type_2_addr_2_conn [ CONN_TYPE_RUDP ].get (addr , None )
172
+ def get_conn (self , addr , proto_type = None ) -> ConnBase :
173
+ if proto_type is not None :
174
+ return self ._proto_type_2_addr_2_conn [ proto_type ].get (addr , None )
175
+ _conn = self ._proto_type_2_addr_2_conn [ PROTO_TYPE_RUDP ].get (addr , None )
166
176
if _conn is None :
167
- _conn = self ._conn_type_2_addr_2_conn [ CONN_TYPE_TCP ].get (addr , None )
177
+ _conn = self ._proto_type_2_addr_2_conn [ PROTO_TYPE_TCP ].get (addr , None )
168
178
return _conn
169
179
170
180
def add_incoming_conn (
171
- self , conn_type , transport : transports .BaseTransport ,
181
+ self , proto_type , transport : transports .BaseTransport ,
172
182
peer_addr : Optional [Tuple [str , int ]] = None ,
173
183
rudp_conv : int = None
174
184
# rpc_handler: Optional[RpcHandler] = None
175
185
) -> ConnBase :
176
- if conn_type == CONN_TYPE_TCP :
186
+ if proto_type == PROTO_TYPE_TCP :
177
187
from TcpConn import TcpConn
178
188
_conn = TcpConn (
179
189
ROLE_TYPE_PASSIVE , peer_addr ,
180
- close_cb = lambda ct = conn_type , a = peer_addr : self ._remove_conn (ct , a ),
190
+ close_cb = lambda ct = proto_type , a = peer_addr : self ._remove_conn (ct , a ),
181
191
is_proxy = self ._is_proxy ,
182
192
transport = transport )
183
193
else :
184
194
from RudpConn import RudpConn
185
195
assert rudp_conv > 0
186
196
_conn = RudpConn (
187
197
ROLE_TYPE_PASSIVE , peer_addr ,
188
- close_cb = lambda ct = conn_type , a = peer_addr : self ._remove_conn (ct , a ),
198
+ close_cb = lambda ct = proto_type , a = peer_addr : self ._remove_conn (ct , a ),
189
199
is_proxy = self ._is_proxy ,
190
200
transport = transport ,
191
201
conv = rudp_conv )
192
202
193
- self ._conn_type_2_addr_2_conn [ conn_type ][peer_addr ] = _conn
203
+ self ._proto_type_2_addr_2_conn [ proto_type ][peer_addr ] = _conn
194
204
return _conn
195
205
196
206
async def open_conn_by_addr (
197
- self , conn_type , addr : typing .Tuple [str , int ],
207
+ self , proto_type , addr : typing .Tuple [str , int ],
198
208
rpc_handler : RpcHandler = None
199
209
) -> (ConnBase , bool ):
200
210
201
- _conn = self ._conn_type_2_addr_2_conn [ conn_type ].get (addr , None )
211
+ _conn = self ._proto_type_2_addr_2_conn [ proto_type ].get (addr , None )
202
212
is_conned = True
203
213
if _conn is None :
204
- if conn_type == CONN_TYPE_TCP :
214
+ if proto_type == PROTO_TYPE_TCP :
205
215
from TcpConn import TcpConn
206
216
conn_cls = TcpConn
207
217
# addr = transport.get_extra_info('peername') # type: typing.Tuple[str, int]
208
218
# _conn = TcpConn.TcpConn(
209
219
# ROLE_TYPE_ACTIVE, addr,
210
- # close_cb=lambda ct=conn_type , a=addr: self._remove_conn(ct, a),
220
+ # close_cb=lambda ct=proto_type , a=addr: self._remove_conn(ct, a),
211
221
# is_proxy=self._is_proxy,
212
222
# # transport=transport
213
223
# )
@@ -221,19 +231,19 @@ async def open_conn_by_addr(
221
231
_conn = conn_cls (
222
232
ROLE_TYPE_ACTIVE , addr ,
223
233
rpc_handler = rpc_handler ,
224
- close_cb = lambda ct = conn_type , a = addr : self ._remove_conn (ct , a ),
234
+ close_cb = lambda ct = proto_type , a = addr : self ._remove_conn (ct , a ),
225
235
is_proxy = self ._is_proxy ,
226
236
# transport=transport
227
237
)
228
238
229
- self ._conn_type_2_addr_2_conn [ conn_type ][addr ] = _conn
239
+ self ._proto_type_2_addr_2_conn [ proto_type ][addr ] = _conn
230
240
# if rpc_handler is not None:
231
241
# _conn.add_rpc_handler(rpc_handler)
232
242
is_conned = await _conn .try_connect ()
233
243
return _conn , is_conned
234
244
235
- def _remove_conn (self , conn_type , addr : typing .Tuple [str , int ]):
236
- self ._conn_type_2_addr_2_conn [ conn_type ].pop (addr , None )
245
+ def _remove_conn (self , proto_type , addr : typing .Tuple [str , int ]):
246
+ self ._proto_type_2_addr_2_conn [ proto_type ].pop (addr , None )
237
247
238
248
# def add_incoming_conn(
239
249
# self,
0 commit comments