Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Matthews Lab
Search
Search
Appearance
Log in
Personal tools
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
P2pd
(section)
Page
Discussion
British English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
= Peer-to-peer networking with P2PD = Having a library that works well is great and I’ve already used it to build many programs. But the reason I started this project was to make peer-to-peer connections easier. Some of the coolest software today seems to use peer-to-peer networking. Bitcoin, Bittorrent, Skype, and any number of games all use peer-to-peer features. These services are powerful because they let their users be part of running them rather than relying on a trusted third-party. The downside is they’re more complex. Routers, NATs, firewalls, and dynamic IPs all contribute to making the process difficult. '''To do P2P networking right involves a mishmash of esoteric ideas.''' <details> <summary> Show P2P connectivity methods </summary> # Direct connect – If a peer’s node port is open then a regular TCP connection can be used. When the node server is started the library handles IPv4 port forwarding and IPv6 pin hole rules. # Reverse connect – If a host’s node port is reachable then you can simply tell a peer to connect to you. Reverse connect means that connectivity is possible if either side to a connection is able to open a port. # TCP hole punching – A little known feature of TCP allows for a new connection to be created if two peers connect at the same time. P2PD extensively enumerates NAT behaviour to optimise success. # TURN servers – A protocol for proxy servers called ‘TURN’ can be used as a last resort. By default this method is not enabled as it uses UDP for the transport so packets may arrive out of order or get lost. </details> <details> <summary> P2P direct connect example </summary> <syntaxhighlight lang="python">from p2pd import * # Put your custom protocol code here. async def msg_cb(msg, client_tup, pipe): # E.G. add a ping feature to your protocol. if b"PING" in msg: await pipe.send(b"PONG") async def make_p2p_con(): # Initalize p2pd. netifaces = await init_p2pd() # # Start our main node server. # The node implements your protocol. node = await start_p2p_node(netifaces=netifaces) node.add_msg_cb(msg_cb) # # Spawn a new pipe from a P2P con. # Connect to our own node server. pipe = await node.connect(node.addr_bytes) pipe.subscribe(SUB_ALL) # # Test send / receive. msg = b"test send" await pipe.send(b"ECHO " + msg) out = await pipe.recv() # # Cleanup. assert(msg in out) await pipe.close() await node.close() # Run the coroutine. # Or await make_p2p_con() if in async REPL. async_test(make_p2p_con)</syntaxhighlight> </details> In P2PD there are nodes who run their own TCP servers that implement one or more protocol handlers. These are the msg_cb functions listed earlier. Nodes have their own address that can be given out to connect to them. The address includes a lot of information like what interfaces the node has, it’s NAT configurations, information on signalling servers, and so on. The Node object has a connect function to handle making P2P connections. More details on peer-to-peer networking here: https://p2pd.readthedocs.io/en/latest/python/index.html <span id="how-p2pd-compares-to-libp2p"></span>
Summary:
Please note that all contributions to Matthews Lab may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Matthews Lab:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)