Using Asyncore/chat for game server question.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jos

    #1

    Using Asyncore/chat for game server question.

    Hello all.

    I have a working server, using the asyncore/chat module, which enables
    logging in, rooms and private messaging.

    I've used this framework to make some simple games that only required
    message broadcasting via the server.

    I'd now like to move the game logic into the server.

    My recent reading includes the article "Multithrea ded Game Scripting
    with Stackless Python"



    I'm not so interested in moving to Stackless, although i've also been
    looking at Nanothreads offered in the "Lightweigh t Game Toolkit" as a
    way to implement the above ideas in plain python.



    However, that's a bit off from my Question. So here is is:

    How do i enable the/a game object, running on the server, to send
    messages "on its own". I understand that the asyncore module is based
    on the "Reactor" design, which is, duh, reactive rather then active...
    So how do i make my server "active" ;).

    A simple example would be a server based on asyncore, with multiple
    connections, which broadcasts a "pulse" to the connected clients every
    second.

    Thanks for any help, pointers, words of advice, etc etc!

    Jos

  • Jos

    #2
    Re: Using Asyncore/chat for game server question.

    Thanks to Matt Hammond and Jonathan LaCour for getting back to me!

    Jonathan sent me some code which took me several days to figure out,
    but does _exactly_ what i wanted.

    You can find the code he sent (dug out of the medusa distro) here:


    And how i warped it to my needs here:

    (please don't laugh at the rough code! :)

    Jonathan's original reply, via email, is quoted here with his
    permission.

    Thanks!
    jos

    <quote>
    I saw your post to comp.lang.pytho n about asyncore and making a bit
    more of an "active" process. The problem with asyncore, as you stated,
    is that it "reacts" to events on file descriptors (through
    select/poll). If you have something that occurs in a second thread
    that could cause data to be available (ie, sending a "pulse" every
    second), you can use some old code from the medusa web server to do
    this, which I will attached to this email.

    You will need to do something like this (I haven't tested this code):

    from select_trigger import trigger
    from threading import Thread
    import time

    class pulse_thread(Th read):
    def __init__(self, interval, thunk):
    Thread.__init__ (self)
    self.trigger = trigger()
    self.thunk = thunk
    self.interval = interval

    def run(self):
    while True:
    self.trigger.pu ll_trigger(self .thunk)
    time.sleep(self .interval)

    The pulse thread will pull the trigger how ever often you like, thus
    waking up the asyncore loop for events being fired. I hope this helps.

    -- Jonathan
    </quote>

    Comment

    Working...