@@ -22,20 +22,23 @@ def __init__(
2222 port : int ,
2323 family : socket .AddressFamily = socket .AF_INET ,
2424 mode : str = MODE_1_1 ,
25+ timeout : float | None = 30.0 ,
2526 ) -> None :
2627 """Initialize client
2728
2829 Args:
2930 address: IP address of server
3031 port: Port of server
3132 family: address family parameter (passed to socket.getaddrinfo)
33+ timeout: Default timeout in seconds for socket operations
3234 """
3335 self .address = address
3436 self .port = port
3537 self .family = family
3638 self .mode = mode
39+ self ._timeout = timeout
3740 self .socket = socket .socket (self .family , socket .SOCK_STREAM )
38- self .socket .settimeout (30 )
41+ self .socket .settimeout (timeout )
3942 self .socket .connect ((address , port ))
4043
4144 def __enter__ (self ):
@@ -56,20 +59,21 @@ def send(self, content: Union[OscMessage, OscBundle]) -> None:
5659 b = struct .pack ("!I" , len (content .dgram ))
5760 self .socket .sendall (b + content .dgram )
5861
59- def receive (self , timeout : int = 30 ) -> List [bytes ]:
60- self .socket .settimeout (timeout )
62+ def receive (self , timeout : float | None = None ) -> List [bytes ]:
63+ effective_timeout = timeout if timeout is not None else self ._timeout
64+ self .socket .settimeout (effective_timeout )
6165 if self .mode == MODE_1_1 :
6266 try :
6367 buf = self .socket .recv (4096 )
64- except TimeoutError :
68+ except ( TimeoutError , socket . timeout ) :
6569 return []
6670 if not buf :
6771 return []
6872 # If the last byte is not an END marker there could be more data coming
6973 while buf [- 1 ] != 192 :
7074 try :
7175 newbuf = self .socket .recv (4096 )
72- except TimeoutError :
76+ except ( TimeoutError , socket . timeout ) :
7377 break
7478 if not newbuf :
7579 # Maybe should raise an exception here?
@@ -80,13 +84,13 @@ def receive(self, timeout: int = 30) -> List[bytes]:
8084 buf = b""
8185 try :
8286 lengthbuf = self .socket .recv (4 )
83- except TimeoutError :
87+ except ( TimeoutError , socket . timeout ) :
8488 return []
8589 (length ,) = struct .unpack ("!I" , lengthbuf )
8690 while length > 0 :
8791 try :
8892 newbuf = self .socket .recv (length )
89- except TimeoutError :
93+ except ( TimeoutError , socket . timeout ) :
9094 return []
9195 if not newbuf :
9296 return []
@@ -116,7 +120,7 @@ def send_message(
116120 msg = build_msg (address , value )
117121 return self .send (msg )
118122
119- def get_messages (self , timeout : int = 30 ) -> Generator :
123+ def get_messages (self , timeout : float | None = None ) -> Generator :
120124 r = self .receive (timeout )
121125 while r :
122126 for m in r :
@@ -129,7 +133,7 @@ class TCPDispatchClient(SimpleTCPClient):
129133
130134 dispatcher = Dispatcher ()
131135
132- def handle_messages (self , timeout_sec : int = 30 ) -> None :
136+ def handle_messages (self , timeout_sec : float | None = None ) -> None :
133137 """Wait :int:`timeout` seconds for a message from the server and process each message with the registered
134138 handlers. Continue until a timeout occurs.
135139
@@ -152,18 +156,21 @@ def __init__(
152156 port : int ,
153157 family : socket .AddressFamily = socket .AF_INET ,
154158 mode : str = MODE_1_1 ,
159+ timeout : float | None = 30.0 ,
155160 ) -> None :
156161 """Initialize client
157162
158163 Args:
159164 address: IP address of server
160165 port: Port of server
161166 family: address family parameter (passed to socket.getaddrinfo)
167+ timeout: Default timeout in seconds for socket operations
162168 """
163169 self .address : str = address
164170 self .port : int = port
165171 self .mode : str = mode
166172 self .family : socket .AddressFamily = family
173+ self ._timeout = timeout
167174
168175 def __await__ (self ):
169176 async def closure ():
@@ -197,19 +204,22 @@ async def send(self, content: Union[OscMessage, OscBundle]) -> None:
197204 self .writer .write (b + content .dgram )
198205 await self .writer .drain ()
199206
200- async def receive (self , timeout : int = 30 ) -> List [bytes ]:
207+ async def receive (self , timeout : float | None = None ) -> List [bytes ]:
208+ effective_timeout = timeout if timeout is not None else self ._timeout
201209 if self .mode == MODE_1_1 :
202210 try :
203- buf = await asyncio .wait_for (self .reader .read (4096 ), timeout )
204- except TimeoutError :
211+ buf = await asyncio .wait_for (self .reader .read (4096 ), effective_timeout )
212+ except ( TimeoutError , asyncio . TimeoutError ) :
205213 return []
206214 if not buf :
207215 return []
208216 # If the last byte is not an END marker there could be more data coming
209217 while buf [- 1 ] != 192 :
210218 try :
211- newbuf = await asyncio .wait_for (self .reader .read (4096 ), timeout )
212- except TimeoutError :
219+ newbuf = await asyncio .wait_for (
220+ self .reader .read (4096 ), effective_timeout
221+ )
222+ except (TimeoutError , asyncio .TimeoutError ):
213223 break
214224 if not newbuf :
215225 # Maybe should raise an exception here?
@@ -219,15 +229,19 @@ async def receive(self, timeout: int = 30) -> List[bytes]:
219229 else :
220230 buf = b""
221231 try :
222- lengthbuf = await asyncio .wait_for (self .reader .read (4 ), timeout )
223- except TimeoutError :
232+ lengthbuf = await asyncio .wait_for (
233+ self .reader .read (4 ), effective_timeout
234+ )
235+ except (TimeoutError , asyncio .TimeoutError ):
224236 return []
225237
226238 (length ,) = struct .unpack ("!I" , lengthbuf )
227239 while length > 0 :
228240 try :
229- newbuf = await asyncio .wait_for (self .reader .read (length ), timeout )
230- except TimeoutError :
241+ newbuf = await asyncio .wait_for (
242+ self .reader .read (length ), effective_timeout
243+ )
244+ except (TimeoutError , asyncio .TimeoutError ):
231245 return []
232246 if not newbuf :
233247 return []
@@ -250,8 +264,9 @@ def __init__(
250264 port : int ,
251265 family : socket .AddressFamily = socket .AF_INET ,
252266 mode : str = MODE_1_1 ,
267+ timeout : float | None = 30.0 ,
253268 ):
254- super ().__init__ (address , port , family , mode )
269+ super ().__init__ (address , port , family , mode , timeout )
255270
256271 async def send_message (
257272 self , address : str , value : Union [ArgValue , Iterable [ArgValue ]] = ""
@@ -265,7 +280,7 @@ async def send_message(
265280 msg = build_msg (address , value )
266281 return await self .send (msg )
267282
268- async def get_messages (self , timeout : int = 30 ) -> AsyncGenerator :
283+ async def get_messages (self , timeout : float | None = None ) -> AsyncGenerator :
269284 r = await self .receive (timeout )
270285 while r :
271286 for m in r :
@@ -278,7 +293,7 @@ class AsyncDispatchTCPClient(AsyncTCPClient):
278293
279294 dispatcher = Dispatcher ()
280295
281- async def handle_messages (self , timeout : int = 30 ) -> None :
296+ async def handle_messages (self , timeout : float | None = None ) -> None :
282297 """Wait :int:`timeout` seconds for a message from the server and process each message with the registered
283298 handlers. Continue until a timeout occurs.
284299
0 commit comments