This change introduces a demux that uses the kernel's network stack for UDP datagram-to-socket assignment based on packet source address (as opposed to the forwarding strategy of the routing demux). The osnet demux is used by default on non-Windows platforms. When possible, use of the osnet demux is preferred over the routing demux, since it can be expected to perform better. The unit test suite has been extended to run all tests first with the demux selected by default for the current platform, and then with the routing demux, if the latter differs from the former. Tests were already being run twice, first with IPv4 and then with IPv6, and thus we now run each test four times on Linux, twice on Windows. All unit tests pass with both demux types.
48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
# Demux loader: imports a demux module appropriate for this platform.
|
|
# Written by Ray Brown.
|
|
"""UDP Demux
|
|
|
|
A UDP demux is a wrapper for a datagram socket. The demux must be initialized
|
|
with an unconnected datagram socket, referred to as the root socket. Once
|
|
initialized, the demux will create new connections to peer endpoints upon
|
|
arrival of datagrams from a new endpoint. Such a connection is of a
|
|
socket-derived type, and will receive datagrams only from the peer endpoint for
|
|
which it was created, and that are sent to the root socket.
|
|
|
|
Connections must be used for receiving datagrams only. Outgoing traffic should
|
|
be sent through the root socket.
|
|
|
|
Varying implementations of this functionality are provided for different
|
|
platforms.
|
|
"""
|
|
|
|
import sys
|
|
|
|
if sys.platform.startswith('win') or sys.platform.startswith('cygwin'):
|
|
from router import UDPDemux
|
|
_routing = True
|
|
else:
|
|
from osnet import UDPDemux
|
|
_routing = False
|
|
_default_demux = None
|
|
|
|
def force_routing_demux():
|
|
global _routing
|
|
if _routing:
|
|
return False # no change - already loaded
|
|
global UDPDemux, _default_demux
|
|
import router
|
|
_default_demux = UDPDemux
|
|
UDPDemux = router.UDPDemux
|
|
_routing = True
|
|
return True # new router loaded and switched
|
|
|
|
def reset_default_demux():
|
|
global UDPDemux, _routing, _default_demux
|
|
if _default_demux:
|
|
UDPDemux = _default_demux
|
|
_default_demux = None
|
|
_routing = not _routing
|
|
|
|
__all__ = ["UDPDemux", "force_routing_demux", "reset_default_demux"]
|