Improving the network communication #4

Merged
PeterSurda merged 12 commits from lee.miller/MiNode:network into v0.3 2024-07-15 07:58:57 +02:00
2 changed files with 24 additions and 8 deletions
Showing only changes of commit 7719de5338 - Show all commits

View File

@ -77,7 +77,7 @@ class Manager(threading.Thread):
with shared.connections_lock: with shared.connections_lock:
shared.connections.remove(c) shared.connections.remove(c)
else: else:
hosts.add(c.host) hosts.add(structure.NetAddrNoPrefix.network_group(c.host))
if not c.server: if not c.server:
outgoing_connections += 1 outgoing_connections += 1
@ -121,15 +121,16 @@ class Manager(threading.Thread):
else: else:
to_connect.update(shared.i2p_node_pool) to_connect.update(shared.i2p_node_pool)
for addr in to_connect: for host, port in to_connect:
if addr[0] in hosts: group = structure.NetAddrNoPrefix.network_group(host)
if group in hosts:
continue continue
if addr[1] == 'i2p' and shared.i2p_enabled: if port == 'i2p' and shared.i2p_enabled:
if shared.i2p_session_nick and addr[0] != shared.i2p_dest_pub: if shared.i2p_session_nick and host != shared.i2p_dest_pub:
try: try:
d = I2PDialer( d = I2PDialer(
shared, shared,
addr[0], shared.i2p_session_nick, host, shared.i2p_session_nick,
shared.i2p_sam_host, shared.i2p_sam_port) shared.i2p_sam_host, shared.i2p_sam_port)
d.start() d.start()
hosts.add(d.destination) hosts.add(d.destination)
@ -141,9 +142,9 @@ class Manager(threading.Thread):
else: else:
continue continue
else: else:
c = Connection(addr[0], addr[1]) c = Connection(host, port)
c.start() c.start()
hosts.add(c.host) hosts.add(group)
with shared.connections_lock: with shared.connections_lock:
shared.connections.add(c) shared.connections.add(c)
shared.hosts = hosts shared.hosts = hosts

View File

@ -173,6 +173,21 @@ class NetAddrNoPrefix():
b += struct.pack('>H', int(self.port)) b += struct.pack('>H', int(self.port))
return b return b
@staticmethod
def network_group(host):
"""A simplified network group identifier from pybitmessage protocol"""
try:
host = socket.inet_pton(socket.AF_INET, host)
return host[:2]
except socket.error:
try:
host = socket.inet_pton(socket.AF_INET6, host)
return host[:12]
except OSError:
return host
except TypeError:
return host
@classmethod @classmethod
def from_bytes(cls, b): def from_bytes(cls, b):
services, host, port = struct.unpack('>Q16sH', b) services, host, port = struct.unpack('>Q16sH', b)