From d09782e53d3f42132532b6e39011cd27e7f41d25 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Fri, 24 Jan 2020 18:23:59 +0200 Subject: [PATCH] Obsolete bitmessagemain.connectToStream(), use BMConnectionPool method --- src/bitmessagemain.py | 30 +++--------------------------- src/helper_startup.py | 19 ++++++++++++++----- src/network/connectionpool.py | 1 + 3 files changed, 18 insertions(+), 32 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index d9a7c7f5..2a279aca 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -38,9 +38,7 @@ import state from bmconfigparser import BMConfigParser from debug import logger # this should go before any threads from helper_startup import ( - isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections, - start_proxyconfig -) + adjustHalfOpenConnectionsLimit, start_proxyconfig) from inventory import Inventory from knownnodes import readKnownNodes # Network objects and threads @@ -55,27 +53,6 @@ from threads import ( addressGenerator, objectProcessor, singleCleaner, singleWorker, sqlThread) -def connectToStream(streamNumber): - """Connect to a stream""" - state.streamsInWhichIAmParticipating.append(streamNumber) - - if isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections(): - # Some XP and Vista systems can only have 10 outgoing connections - # at a time. - state.maximumNumberOfHalfOpenConnections = 9 - else: - state.maximumNumberOfHalfOpenConnections = 64 - try: - # don't overload Tor - if BMConfigParser().get( - 'bitmessagesettings', 'socksproxytype') != 'none': - state.maximumNumberOfHalfOpenConnections = 4 - except: - pass - - BMConnectionPool().connectToStream(streamNumber) - - def _fixSocket(): if sys.platform.startswith('linux'): socket.SO_BINDTODEVICE = 25 @@ -174,6 +151,7 @@ class Main(object): """Start main application""" # pylint: disable=too-many-statements,too-many-branches,too-many-locals _fixSocket() + adjustHalfOpenConnectionsLimit() config = BMConfigParser() daemon = config.safeGetBoolean('bitmessagesettings', 'daemon') @@ -332,7 +310,7 @@ class Main(object): # start network components if networking is enabled if state.enableNetwork: start_proxyconfig() - BMConnectionPool() + BMConnectionPool().connectToStream(1) asyncoreThread = BMNetworkThread() asyncoreThread.daemon = True asyncoreThread.start() @@ -356,8 +334,6 @@ class Main(object): state.uploadThread.daemon = True state.uploadThread.start() - connectToStream(1) - if config.safeGetBoolean('bitmessagesettings', 'upnp'): import upnp upnpThread = upnp.uPnPThread() diff --git a/src/helper_startup.py b/src/helper_startup.py index 9711c339..fcd12aa4 100644 --- a/src/helper_startup.py +++ b/src/helper_startup.py @@ -276,19 +276,28 @@ def updateConfig(): config.save() -def isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections(): - """Check for (mainly XP and Vista) limitations""" +def adjustHalfOpenConnectionsLimit(): + """Check and satisfy half-open connections limit (mainly XP and Vista)""" + if BMConfigParser().safeGet( + 'bitmessagesettings', 'socksproxytype', 'none') != 'none': + state.maximumNumberOfHalfOpenConnections = 4 + return + + is_limited = False try: if sys.platform[0:3] == "win": + # Some XP and Vista systems can only have 10 outgoing + # connections at a time. VER_THIS = StrictVersion(platform.version()) - return ( + is_limited = ( StrictVersion("5.1.2600") <= VER_THIS and StrictVersion("6.0.6000") >= VER_THIS ) - return False - except Exception: + except ValueError: pass + state.maximumNumberOfHalfOpenConnections = 9 if is_limited else 64 + def start_proxyconfig(): """Check socksproxytype and start any proxy configuration plugin""" diff --git a/src/network/connectionpool.py b/src/network/connectionpool.py index 6264191d..77eb58e4 100644 --- a/src/network/connectionpool.py +++ b/src/network/connectionpool.py @@ -87,6 +87,7 @@ class BMConnectionPool(object): def connectToStream(self, streamNumber): """Connect to a bitmessage stream""" self.streams.append(streamNumber) + state.streamsInWhichIAmParticipating.append(streamNumber) def getConnectionByAddr(self, addr): """