private void _set_timeout(int timeout_ms, int optname) { zts_timeval tv = new zts_timeval(); // Convert milliseconds to timeval struct tv.tv_sec = timeout_ms / 1000; tv.tv_usec = (timeout_ms % 1000) * 1000; IntPtr tv_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(zts_timeval))); Marshal.StructureToPtr(tv, tv_ptr, false); ushort option_size = (ushort)Marshal.SizeOf(typeof(zts_sockaddr_in)); int err = 0; if ((err = zts_setsockopt(_fd, ZeroTier.Constants.SOL_SOCKET, ZeroTier.Constants.SO_RCVTIMEO, tv_ptr, option_size)) < 0) { throw new ZeroTier.Sockets.SocketException(err, ZeroTier.Core.Node.ErrNo); } Marshal.FreeHGlobal(tv_ptr); }
private int _get_timeout(int optname) { zts_timeval tv = new zts_timeval(); IntPtr tv_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(zts_timeval))); Marshal.StructureToPtr(tv, tv_ptr, false); ushort optlen = (ushort)Marshal.SizeOf(typeof(zts_timeval)); GCHandle optlen_gc_handle = GCHandle.Alloc(optlen, GCHandleType.Pinned); IntPtr optlen_ptr = optlen_gc_handle.AddrOfPinnedObject(); int err = 0; if ((err = zts_getsockopt(_fd, ZeroTier.Constants.SOL_SOCKET, ZeroTier.Constants.SO_RCVTIMEO, tv_ptr, optlen_ptr)) < 0) { throw new ZeroTier.Sockets.SocketException(err, ZeroTier.Core.Node.ErrNo); } tv = (zts_timeval)Marshal.PtrToStructure(tv_ptr, typeof(zts_timeval)); optlen_gc_handle.Free(); Marshal.FreeHGlobal(tv_ptr); // Convert timeval struct to milliseconds return (int)((tv.tv_sec * 1000) + (tv.tv_usec / 1000)); }