File tree Expand file tree Collapse file tree
SimpleNetworkManager.NET/Network Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -90,7 +90,7 @@ public void OnDisconnected()
9090 /// </summary>
9191 internal abstract UniTask DisconnectAsync ( ) ;
9292
93- internal async UniTask < TResponse > SendRequestAsync < TResponse > ( BaseRequestMessage request )
93+ internal async UniTask < TResponse > SendRequestAsync < TResponse > ( BaseRequestMessage request , int timeoutMs = 10_000 )
9494 where TResponse : BaseResponseMessage
9595 {
9696 uint requestId = GetNewRequestId ( ) ;
@@ -99,24 +99,22 @@ internal async UniTask<TResponse> SendRequestAsync<TResponse>(BaseRequestMessage
9999
100100 // Waiting for the response
101101 BaseResponseMessage ? response ;
102- // 10 seconds timeout
103- int timeoutCountDown = 10_000 ;
104102 do
105103 {
106- if ( timeoutCountDown <= 0 )
104+ if ( timeoutMs <= 0 )
107105 {
108106 response = null ;
109107 break ;
110108 }
111109 await Task . Delay ( 100 ) ;
112- timeoutCountDown -= 100 ;
110+ timeoutMs -= 100 ;
113111 }
114112 while ( ! _pendingResponses . TryRemove ( requestId , out response ) ) ;
115113 s_unassignedRequestIds . Enqueue ( requestId ) ;
116114
117115 if ( response == null )
118116 {
119- throw new TimeoutException ( $ "Request timed out after 10 seconds (RequestId: { requestId } ).") ;
117+ throw new TimeoutException ( $ "Request timed out after { timeoutMs } milliseconds (RequestId: { requestId } ).") ;
120118 }
121119
122120 if ( response is not TResponse castedResponse )
Original file line number Diff line number Diff line change @@ -39,13 +39,13 @@ public async UniTask DisconnectAsync()
3939 ClientConnection . Dispose ( ) ;
4040 }
4141
42- public async UniTask < TResponse > SendRequestAsync < TResponse > ( BaseRequestMessage request )
42+ public async UniTask < TResponse > SendRequestAsync < TResponse > ( BaseRequestMessage request , int timeoutMs = 10_000 )
4343 where TResponse : BaseResponseMessage
4444 {
4545 if ( ClientConnection == null || ! ClientConnection . IsConnected )
4646 throw new InvalidOperationException ( "Cannot send request: client is not connected." ) ;
4747 _messageRouterService . RegisterHandler ( new ResponseMessageHandler < TResponse > ( ) , true ) ;
48- return await ClientConnection . SendRequestAsync < TResponse > ( request ) ;
48+ return await ClientConnection . SendRequestAsync < TResponse > ( request , timeoutMs ) ;
4949 }
5050
5151 public void RegisterHandler < T > ( BaseMessageHandler < T > handler )
Original file line number Diff line number Diff line change @@ -50,15 +50,15 @@ public async UniTask DisconnectAsync(uint connectionId)
5050 await clientConnection . DisconnectAsync ( ) ;
5151 }
5252
53- public async UniTask < TResponse > SendRequestAsync < TResponse > ( uint connectionId , BaseRequestMessage request )
53+ public async UniTask < TResponse > SendRequestAsync < TResponse > ( uint connectionId , BaseRequestMessage request , int timeoutMs = 10_000 )
5454 where TResponse : BaseResponseMessage
5555 {
5656 if ( ! _connectionManager . TryGetConnection ( connectionId , out var clientConnection ) )
5757 throw new KeyNotFoundException ( $ "No connection found with ID { connectionId } .") ;
5858 if ( clientConnection == null || ! clientConnection . IsConnected )
5959 throw new InvalidOperationException ( $ "Cannot send request: client { connectionId } is not connected.") ;
6060 _messageRouterService . RegisterHandler ( new ResponseMessageHandler < TResponse > ( ) , true ) ;
61- return await clientConnection . SendRequestAsync < TResponse > ( request ) ;
61+ return await clientConnection . SendRequestAsync < TResponse > ( request , timeoutMs ) ;
6262 }
6363
6464 public void RegisterHandler < T > ( BaseMessageHandler < T > handler )
You can’t perform that action at this time.
0 commit comments