Simple Networking For Pygame
Write a simple network library which integrates with the pygame event queue.
- Seamless reconnection
- Support TCP and UDP
- use select based non blocking IO
- network library is iterated manually (so we use our own event loops)
- connect(uri, onConnection, onLostConnection)
- send(conn, data)
- recv(conn, num_bytes)
- listen(uri, onConnection, onLostConnection)
Things not to worry about:
- Object serialization
NOTES, api part 2?
I think I like the url idea. We could not worry about connections at all by using urls. Optional carring about connections could be implmented.
Two or three example games should be implemented to test the API. Something turnbased. Something with lots of action. Something with lots of connections (eg > 100 players).
UDP and ENet are interesting things to consider for the api. UDP is connectionless.
All bits of data could be strings. Serialisation should be done outside of it... however perhaps we can add a safe basic serialisation so that people don't use pickle.
The event queue api could event be used for some of it.
#pygame.network.send(data, channel, lossy) pygame.network.send("hello network.", CHANNEL_CHAT, 0) for e in pygame.event.get(): if e.type == NETWORK: if e.what == NETWORK_DATA_IN: print e.channel print e.data print e.sequenceid print e.peer if e.channel == CHANNEL_CHAT: # we write the incomming text to our chat window. chatwindow.write_text(e.peer, e.data)
The pygame queue integrated networking library is a great idea.
I would suggest supporting client-to-client (peer-to-peer) networking, and adding functionality to find other networked machines running the same application.
Using 'connection-less' http and urls introduced some limitations: How does the server initiate contact with the client (assuming it keeps track of clients)? It could use http, but firewalls on many workstations/play-stations/LANs block incoming http requests.
Creating and maintaining connections would make the library more generally useful, I think, than using independent one-transaction connections like http. It would allow asynchronous notifications both ways, and would allow peer-to-peer networking.
I'm interested in taking on this project, and I've started to write up my ideas on my website. Please take a look and let me know what you think.