[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] SOC Proposal: Networking for Pygame



Hello,

I'm a student interested in working on Pygame for Google's Summer of Code. I'm 
working on a proposal to add an event based networking framework to Pygame, 
inspired by this Wiki entry:

http://wiki.python.org/moin/SummerOfCode/PythonLibraries/SimpleNetworkingForPygame

The latest version of my proposal lives here:
http://www.mumstudents.org/~bda/soc/NetworkingForPygame.html

I'm including my first draft below. Let me know what you think!

Thanks,
Bryce Allen


* Proposal

Create a networking framework for Pygame which integrates into the event 
queue, using the Python socket API. The framework will include both 
peer-to-peer networking and game discovery via a game server or local 
broadcast.

* Acknowledgements

http://wiki.python.org/moin/SummerOfCode/PythonLibraries/SimpleNetworkingForPygame

This proposal builds on ideas from the Summer of Code wiki. Thanks to all the 
contributers!


* Details
 
Games have very different requirements in terms of networking - typically each 
game defines it's own high level protocol. However, there are common needs 
which may be shared among multiple games. Creating an inheritance hiearchy of 
protocols would promote reuse of protocol layers, and provide a convenient 
way to parsel off networking code. The events posted to the queue would 
depend on which protocol is initialized.

I think the most useful mid-level protocol which would satisfy the needs of 
most games is a TCP based remote events protocol like dkeeney suggested (see 
the wiki link above). TCP is the easiest way to get reliable delivery. 
However, a UDP version could also be developed with a more lightweight 
connection management system, and it could be swapped in seamlessly if it 
emits the same events.

TCP and UDP protocols would mainly be used by people implementing their own 
special purpose protocol. Creating a new protocol object inheriting from TCP 
or UDP and posting custom events would be encouraged, rather then using the 
TCP or UDP events directly. However, for simple games using the TCP/UDP 
events directly could be convenient.

Game discovery could also be implemented through pluggable classes. Specific 
games could extend the basic classes with support for searching for games 
with specific options set, for example.

Higher level protocols, like the remote events protocol, would have it's own 
(possibly pluggable) serialization. TCP and UDP would accept strings only, as 
suggested in the wiki. I think it is important to provide a simple safe 
serializer to discourage people from using pickle.


* Potential API

# find a game to join
discovery = pygame.network.discovery.GameServer(gameGUID, server)
discovery = pygame.network.discovery.LocalBroadcast(gameGUID, server)

# blocking version
gameDescriptors = discovery.list_games()

# event queue version
discover.start_search()

# initialize the network with a given protocol object
connection = gameDescriptor.get_connection() # from the game descriptor
connection = pygame.network.TCPRemoteEvents(CLIENT, serverAddr)
pygame.network.init(connection)

connection.start()

# after we've gotten a connect event, we can send data
connection.send(event) # in the case of TCPRemoteEvents, we send events

# using it
pygame.network.pump_events()
pygame.network.discovery.pump_events()

# some example events
for event in pygame.events.get()
    if e.type == NET:
        if e.what == NET_CONNECT:
            startGame()
    if e.type == NET_DISCOVERY:
        if e.what == NET_DISCOVERY_GAME_FOUND:
            # update display with available games
            print e.protocol
            print e.connection_arguments
            print e.options
    if e.type == KEYDOWN:
        if e.key == player1.keys.up:
            connection.send(Event(MyGlobals.PLAYER_MOVE_EVENT, {"player" : 
1, "direction": Direction.UP} )



* Protocol Inheritance

# When inheriting from TCP or UDP, you can choose to pass or consume the TCP 
events and add new events

# this would consume the standard TCP DATA_IN event and parse the data into an 
event object, then post the remote event to the local queue
pygame.network.init(TCPRemoteEvents(CLIENT, serverAddr))