11from sys.ffi import c_uint
22from sys.info import CompilationTarget
33
4+ from lightbug_http.c.aliases import c_void
5+
46from lightbug_http.address import (
57 Addr,
68 NetworkType,
@@ -17,6 +19,7 @@ from lightbug_http.c.socket import (
1719 ShutdownOption,
1820 SocketOption,
1921 SocketType,
22+ _setsockopt,
2023 accept,
2124 bind,
2225 close,
@@ -808,13 +811,25 @@ struct Socket[
808811 """ Return the timeout value for the socket."""
809812 return self .get_socket_option(SocketOption.SO_RCVTIMEO )
810813
811- fn set_timeout (self , var duration : Int) raises SetsockoptError :
812- """ Set the timeout value for the socket.
814+ fn set_timeout (self , seconds : Int) raises SetsockoptError :
815+ """ Set the receive timeout for the socket.
813816
814817 Args:
815- duration: Seconds - The timeout duration in seconds.
818+ seconds: The timeout duration in seconds.
819+
820+ Raises:
821+ SetsockoptError: If setting the socket option fails.
816822 """
817- self .set_socket_option(SocketOption.SO_RCVTIMEO , duration)
823+ # SO_RCVTIMEO requires a timeval struct: {tv_sec: Int64, tv_usec: Int64}
824+ # (16 bytes on both macOS and Linux 64-bit).
825+ var timeval = InlineArray[Int64, 2 ](seconds, 0 )
826+ _ = _setsockopt(
827+ self .fd.value,
828+ SOL_SOCKET ,
829+ SocketOption.SO_RCVTIMEO .value,
830+ UnsafePointer(to = timeval).bitcast[c_void](),
831+ 16 ,
832+ )
818833
819834
820835comptime UDPSocket [address : Addr] = Socket[
0 commit comments