async def afd_poll(self, sock, flags, *, exclusive=0): print(f"Starting a poll for {flags!r}") lpOverlapped = ffi.new("LPOVERLAPPED") poll_info = ffi.new("AFD_POLL_INFO *") poll_info.Timeout = 2**63 - 1 # INT64_MAX poll_info.NumberOfHandles = 1 poll_info.Exclusive = exclusive poll_info.Handles[0].Handle = _get_base_socket(sock) poll_info.Handles[0].Status = 0 poll_info.Handles[0].Events = flags try: _check( kernel32.DeviceIoControl( self._afd, IoControlCodes.IOCTL_AFD_POLL, poll_info, ffi.sizeof("AFD_POLL_INFO"), poll_info, ffi.sizeof("AFD_POLL_INFO"), ffi.NULL, lpOverlapped, )) except OSError as exc: if exc.winerror != ErrorCodes.ERROR_IO_PENDING: # pragma: no cover raise try: await trio.lowlevel.wait_overlapped(self._afd, lpOverlapped) except: print(f"Poll for {flags!r}: {sys.exc_info()[1]!r}") raise out_flags = AFDPollFlags(poll_info.Handles[0].Events) print(f"Poll for {flags!r}: got {out_flags!r}") return out_flags
def set_leap_seconds_enabled(enabled): plsi = ffi.new("PROCESS_LEAP_SECOND_INFO*") if enabled: plsi.Flags = PROCESS_LEAP_SECOND_INFO_FLAG_ENABLE_SIXTY_SECOND else: plsi.Flags = 0 plsi.Reserved = 0 if not kernel32.SetProcessInformation( ffi.cast("HANDLE", -1), # current process ProcessLeapSecondInfo, plsi, ffi.sizeof("PROCESS_LEAP_SECOND_INFO"), ): raise_winerror()
async def main(): h = kernel32.CreateWaitableTimerW(ffi.NULL, True, ffi.NULL) if not h: raise_winerror() print(h) SECONDS = 2 wakeup = datetime.now(timezone.utc) + timedelta(seconds=SECONDS) wakeup_filetime = py_datetime_to_win_filetime(wakeup) wakeup_cffi = ffi.new("LARGE_INTEGER *") wakeup_cffi[0] = wakeup_filetime print(wakeup_filetime, wakeup_cffi) print(f"Sleeping for {SECONDS} seconds (until {wakeup})") if not kernel32.SetWaitableTimer( h, wakeup_cffi, 0, ffi.NULL, ffi.NULL, False, ): raise_winerror() await trio.hazmat.WaitForSingleObject(h) print(f"Current FILETIME: {now_as_filetime()}") set_leap_seconds_enabled(False) print(f"Current FILETIME: {now_as_filetime()}") set_leap_seconds_enabled(True) print(f"Current FILETIME: {now_as_filetime()}") set_leap_seconds_enabled(False) print(f"Current FILETIME: {now_as_filetime()}")
def now_as_filetime(): ft = ffi.new("LARGE_INTEGER*") kernel32.GetSystemTimeAsFileTime(ft) return ft[0]