11from direct .distributed .PyDatagram import PyDatagram
2+ from direct .distributed .PyDatagramIterator import PyDatagramIterator
3+ from direct .task import Task
24from panda3d .core import QueuedConnectionManager , QueuedConnectionListener , QueuedConnectionReader , ConnectionWriter
35from panda3d .core import PointerToConnection , NetAddress , NetDatagram
46
57from Blobtory .Scripts .Pipeline .WindowCreator import WindowCreator
8+ from Blobtory .Scripts .game .SceneBuilder import SceneBuilder
9+
610
711class Client :
8- def __init__ (self , winCreator : WindowCreator ):
12+ def __init__ (self , winCreator : WindowCreator , scene : SceneBuilder ):
13+ self .winCreator = winCreator
14+ self .scene = scene
915 self .cManager = QueuedConnectionManager ()
1016 self .cReader = QueuedConnectionReader (self .cManager , 0 )
1117 self .cWriter = ConnectionWriter (self .cManager , 0 )
@@ -14,12 +20,35 @@ def __init__(self, winCreator: WindowCreator):
1420 ip_address = "localhost"
1521 timeout = 3000 # 3 seconds
1622
17- myConnection = self .cManager .openTCPClientConnection (ip_address , port_address , timeout )
18- if myConnection :
19- self .cReader .addConnection (myConnection ) # receive messages from server
23+ self .winCreator .base .taskMgr .add (self .tskReaderPolling , "Poll the connection reader" , - 41 )
24+
25+ self .myConnection = self .cManager .openTCPClientConnection (ip_address , port_address , timeout )
26+ if self .myConnection :
27+ self .cReader .addConnection (self .myConnection ) # receive messages from server
28+
29+ def tskReaderPolling (self , taskdata ):
30+ if self .cReader .dataAvailable ():
31+ datagram = NetDatagram () # catch the incoming data in this instance
32+ # Check the return value; if we were threaded, someone else could have
33+ # snagged this data before we did
34+ if self .cReader .getData (datagram ):
35+ self .myProcessDataFunction (datagram )
36+ return Task .cont
37+
38+ def myProcessDataFunction (self , datagram ):
39+ myIterator = PyDatagramIterator (datagram )
40+ msgID = myIterator .getUint8 ()
41+ if msgID == 1 :
42+ messageToPrint = myIterator .getString ()
43+ if "Go" in messageToPrint :
44+ if "Next" in messageToPrint :
45+ print ("yay it worked" )
46+ self .scene .planetGen .NextPoint ()
2047
21- myPyDatagram = PyDatagram ()
22- myPyDatagram .addUint8 (1 )
23- myPyDatagram .addString ("Hello, world!" )
48+ print (messageToPrint )
2449
25- self .cWriter .send (myPyDatagram , myConnection )
50+ def GoNextMsg (self ):
51+ myPyDatagram = PyDatagram ()
52+ myPyDatagram .addUint8 (1 )
53+ myPyDatagram .addString ("Go Next" )
54+ self .cWriter .send (myPyDatagram , self .myConnection )
0 commit comments