Sorry, as i've said i've not played with sockets directly much.

On Monday, May 25, 2015 at 7:43:25 PM UTC-4, PirlKnighte wrote:
>
> 1) How did you yield the mail loop to allow for packet passing?  I've 
> tried adding forced pauses, but that didn't seem to do anything.
>
 
Yield is a python generator <https://wiki.python.org/moin/Generators> 
function, I had adapted an example I had found to work with Twisted 
specifically. I can't quite find the link
right now but I'll just attach an example script that will hopefully 
provide the answers you need (you'll need twisted to run it).

2) What does it mean to 'fold' the network I/O and update?  I have not run 
> across that term before.
>

Generally it means to include, or combine, more or less, its not 
necessarily a programmer specific term. In this case to include a
reference to your network code in your update loop so it can check for 
incoming data. 

-- 
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pyglet-users.
For more options, visit https://groups.google.com/d/optout.
import pyglet
from pyglet.window import mouse
from pyglet.window import key
from pyglet import clock

from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.internet.protocol import Protocol
from twisted.internet.protocol import Factory
from twisted.internet.protocol import ClientFactory
from twisted.internet import reactor
from twisted.internet import task

import pickle
import zlib

pyglet.options['debug_gl'] = False

#primary window/game class
#/---------------------------------------------------------------------------/
class Prototype(pyglet.window.Window):
    def __init__(self):
        super(Prototype, self).__init__(640, 480, resizable=False, fullscreen=False, caption="Test")
        self.clear()

    #connected user variable, when someone connects the Echo Class will add itself into it.
    #then just call self.user.sendData to transmit to the connected user or self.user.dataRecieved to get data.
        self.user = []

        self.server = None

        self.old_dt = clock.tick()
        self.fps_display = pyglet.clock.ClockDisplay()

#main game loop
#/---------------------------------------------------------------------------/
    def update(self):
        while not self.has_exit:
        #Make sure events are timed properly while coiterating with network layer.
            dt = clock.tick()
            self.old_dt = self.old_dt + dt

        #if enough time has passed, update game state
            if self.old_dt >= 0.025:
                self.old_dt = 0

        #draw screen
            self.draw()

        #manually dispatch window events (since twisted reactor is in control)
            self.dispatch_events()

        #return control to twisted Reactor
            yield 1


#draw screen
#/---------------------------------------------------------------------------/
    def draw(self):
        self.clear()
        self.fps_display.draw()

    #manually flip frame buffers
        self.flip()


#mouse click
#/---------------------------------------------------------------------------/
    def on_mouse_press(self,x,y,button,modifiers):
        if button == pyglet.window.mouse.LEFT:
            if len(self.user) != 0:
                data = ['left_press',x,y]
                for a in self.user:
                    a.sendData(data)

        if button == pyglet.window.mouse.RIGHT:
            if len(self.user) != 0:
                data = ['right_press',x,y]
                for a in self.user:
                    a.sendData(data)

#/---------------------------------------------------------------------------/
    def on_mouse_release(self,x,y,button,modifiers):
        if button == pyglet.window.mouse.LEFT:
        #if someone has logged in, transmit if you click the mouse
            if len(self.user) != 0:
                data = ['left_release',x,y]
                for a in self.user:
                    a.sendData(data)

        if button == pyglet.window.mouse.RIGHT:
        #if someone has logged in, transmit if you click the mouse
            if len(self.user) != 0:
                data = ['right_release',x,y]
                for a in self.user:
                    a.sendData(data)

#/---------------------------------------------------------------------------/
    def on_key_release(self,symbol, modifiers):
    #start the server and listen on port 8007for connections
        if symbol == key.S:
            self.server = TCP4ServerEndpoint(reactor, 8007)
            self.server.listen(QOTDFactory())
            print "server initializing"
    #establish a client connection to a server
        if symbol == key.C:
            reactor.connectTCP('localhost', 8007, EchoClientFactory())
            print "connecting"

#shutdown program gracefully
#/---------------------------------------------------------------------------/
    def shutdown(self, result):
        reactor.stop()

#system error shutdown
#/---------------------------------------------------------------------------/
    def bailout(self, reason):
        reason.printTraceback()
        reactor.stop()


#main network/server client
#/---------------------------------------------------------------------------/
class Echo(Protocol):
#connection established
    def connectionMade(self):
        data = "An apple a day keeps the doctor away\r\n"
        data = pickle.dumps(data)
        compressed = zlib.compress(data)
        self.transport.write(compressed)
        window.user.append(self)

#recieve data
#/---------------------------------------------------------------------------/
    def dataReceived(self, data):
        data = zlib.decompress(data)
        data = pickle.loads(data)

        if data[0] == 'left_press':
            print '1'
        if data[0] == 'left_release':
            print '2'
        if data[0] == 'right_press':
            print '3'
        if data[0] == 'right_release':
            print '4'


#        print data

#send data
#/---------------------------------------------------------------------------/
    def sendData(self,data):
        data = pickle.dumps(data)
        data = zlib.compress(data)
        self.transport.write(data)

#this monitors the port and automatically calls Echo() when someone connects, passing their address to it.
#/---------------------------------------------------------------------------/
class QOTDFactory(Factory):
    def buildProtocol(self, addr):
        print addr, "connected."
        return Echo()

#This class establishes a connection to a server.
#/---------------------------------------------------------------------------/
class EchoClientFactory(ClientFactory):
    def startedConnecting(self, connector):
        print 'Started to connect.'

    def buildProtocol(self, addr):
        print 'Connected.'
        return Echo()

    def clientConnectionLost(self, connector, reason):
        print 'Lost connection. Reason:', reason

    def clientConnectionFailed(self, connector, reason):
        print 'Connection failed. Reason:', reason



if __name__ == '__main__':
#initialize main window
    window = Prototype()
#Co-iterate between the twisted reactor and pyglet
    task.coiterate(window.update()).addCallback(window.shutdown).addErrback(window.bailout)
#start reactor loop
    reactor.run()

Reply via email to