| 1 |
# |
| 2 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
# or more contributor license agreements. See the NOTICE file |
| 4 |
# distributed with this work for additional information |
| 5 |
# regarding copyright ownership. The ASF licenses this file |
| 6 |
# to you under the Apache License, Version 2.0 (the |
| 7 |
# "License"); you may not use this file except in compliance |
| 8 |
# with the License. You may obtain a copy of the License at |
| 9 |
# |
| 10 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
# |
| 12 |
# Unless required by applicable law or agreed to in writing, |
| 13 |
# software distributed under the License is distributed on an |
| 14 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
# KIND, either express or implied. See the License for the |
| 16 |
# specific language governing permissions and limitations |
| 17 |
# under the License. |
| 18 |
# |
| 19 |
|
| 20 |
from __future__ import absolute_import |
| 21 |
|
| 22 |
import errno |
| 23 |
import socket |
| 24 |
import select |
| 25 |
import time |
| 26 |
|
| 27 |
from typing import TYPE_CHECKING, Tuple, List |
| 28 |
|
| 29 |
if TYPE_CHECKING: |
| 30 |
from proton._selectable import Selectable |
| 31 |
|
| 32 |
PN_INVALID_SOCKET = -1 |
| 33 |
|
| 34 |
|
| 35 |
class IO(object): |
| 36 |
|
| 37 |
@staticmethod |
| 38 |
def _setupsocket(s: socket.socket) -> None: |
| 39 |
s.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, True) |
| 40 |
s.setblocking(False) |
| 41 |
|
| 42 |
@staticmethod |
| 43 |
def close(s: socket.socket) -> None: |
| 44 |
s.close() |
| 45 |
|
| 46 |
@staticmethod |
| 47 |
def listen(host, port) -> socket.socket: |
| 48 |
s = socket.socket() |
| 49 |
IO._setupsocket(s) |
| 50 |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) |
| 51 |
s.bind((host, port)) |
| 52 |
s.listen(10) |
| 53 |
return s |
| 54 |
|
| 55 |
@staticmethod |
| 56 |
def accept(s: socket.socket): |
| 57 |
n = s.accept() |
| 58 |
IO._setupsocket(n[0]) |
| 59 |
return n |
| 60 |
|
| 61 |
@staticmethod |
| 62 |
def connect(addr) -> socket.socket: |
| 63 |
s = socket.socket(addr[0], addr[1], addr[2]) |
| 64 |
IO._setupsocket(s) |
| 65 |
try: |
| 66 |
s.connect(addr[4]) |
| 67 |
except socket.error as e: |
| 68 |
if e.errno not in (errno.EINPROGRESS, errno.EWOULDBLOCK, errno.EAGAIN): |
| 69 |
raise |
| 70 |
return s |
| 71 |
|
| 72 |
@staticmethod |
| 73 |
def select(*args, **kwargs): |
| 74 |
return select.select(*args, **kwargs) |
| 75 |
|
| 76 |
@staticmethod |
| 77 |
def sleep(t: float) -> None: |
| 78 |
time.sleep(t) |
| 79 |
return |
| 80 |
|
| 81 |
class Selector(object): |
| 82 |
|
| 83 |
def __init__(self) -> None: |
| 84 |
self._selectables = set() |
| 85 |
self._reading = set() |
| 86 |
self._writing = set() |
| 87 |
self._deadline = None |
| 88 |
|
| 89 |
def add(self, selectable: 'Selectable') -> None: |
| 90 |
self._selectables.add(selectable) |
| 91 |
if selectable.reading: |
| 92 |
self._reading.add(selectable) |
| 93 |
if selectable.writing: |
| 94 |
self._writing.add(selectable) |
| 95 |
if selectable.deadline: |
| 96 |
if self._deadline is None: |
| 97 |
self._deadline = selectable.deadline |
| 98 |
else: |
| 99 |
self._deadline = min(selectable.deadline, self._deadline) |
| 100 |
|
| 101 |
def remove(self, selectable: 'Selectable') -> None: |
| 102 |
self._selectables.discard(selectable) |
| 103 |
self._reading.discard(selectable) |
| 104 |
self._writing.discard(selectable) |
| 105 |
self.update_deadline() |
| 106 |
|
| 107 |
@property |
| 108 |
def selectables(self) -> int: |
| 109 |
return len(self._selectables) |
| 110 |
|
| 111 |
def update_deadline(self) -> None: |
| 112 |
for sel in self._selectables: |
| 113 |
if sel.deadline: |
| 114 |
if self._deadline is None: |
| 115 |
self._deadline = sel.deadline |
| 116 |
else: |
| 117 |
self._deadline = min(sel.deadline, self._deadline) |
| 118 |
|
| 119 |
def update(self, selectable: 'Selectable') -> None: |
| 120 |
self._reading.discard(selectable) |
| 121 |
self._writing.discard(selectable) |
| 122 |
if selectable.reading: |
| 123 |
self._reading.add(selectable) |
| 124 |
if selectable.writing: |
| 125 |
self._writing.add(selectable) |
| 126 |
self.update_deadline() |
| 127 |
|
| 128 |
def select(self, timeout: float) -> Tuple[List, List, List]: |
| 129 |
|
| 130 |
def select_inner(timeout): |
| 131 |
# This inner select adds the writing fds to the exception fd set |
| 132 |
# because Windows returns connected fds in the exception set not the |
| 133 |
# writable set |
| 134 |
r = self._reading |
| 135 |
w = self._writing |
| 136 |
|
| 137 |
now = time.time() |
| 138 |
|
| 139 |
# No timeout or deadline |
| 140 |
if timeout is None and self._deadline is None: |
| 141 |
return IO.select(r, w, w) |
| 142 |
|
| 143 |
if timeout is None: |
| 144 |
t = max(0, self._deadline - now) |
| 145 |
return IO.select(r, w, w, t) |
| 146 |
|
| 147 |
if self._deadline is None: |
| 148 |
return IO.select(r, w, w, timeout) |
| 149 |
|
| 150 |
t = max(0, min(timeout, self._deadline - now)) |
| 151 |
if len(r) == 0 and len(w) == 0: |
| 152 |
if t > 0: |
| 153 |
IO.sleep(t) |
| 154 |
return ([], [], []) |
| 155 |
|
| 156 |
return IO.select(r, w, w, t) |
| 157 |
|
| 158 |
# Need to allow for signals interrupting us on Python 2 |
| 159 |
# In this case the signal handler could have messed up our internal state |
| 160 |
# so don't retry just return with no handles. |
| 161 |
try: |
| 162 |
r, w, ex = select_inner(timeout) |
| 163 |
except select.error as e: |
| 164 |
if e.errno != errno.EINTR: |
| 165 |
raise |
| 166 |
r, w, ex = ([], [], []) |
| 167 |
|
| 168 |
# For windows non blocking connect we get exception not writable so add exceptions to writable |
| 169 |
w += ex |
| 170 |
|
| 171 |
# Calculate timed out selectables |
| 172 |
now = time.time() |
| 173 |
t = [s for s in self._selectables if s.deadline and now > s.deadline] |
| 174 |
self._deadline = None |
| 175 |
self.update_deadline() |
| 176 |
return r, w, t |