In connection with the sometimes pop-up questions regarding the network capabilities of panda, I post an example: of using a low level to create a network and send data in the form of coordinates to the server side when connecting.
server.py
from panda3d.core import QueuedConnectionManager
from panda3d.core import QueuedConnectionListener
from panda3d.core import QueuedConnectionReader
from panda3d.core import ConnectionWriter
from panda3d.core import PointerToConnection
from panda3d.core import NetAddress
from panda3d.core import NetDatagram
from panda3d.core import Datagram
from direct.distributed.PyDatagramIterator import PyDatagramIterator
from direct.task import Task
cManager = QueuedConnectionManager()
cListener = QueuedConnectionListener(cManager, 0)
cReader = QueuedConnectionReader(cManager, 0)
cWriter = ConnectionWriter(cManager, 0)
activeConnections = []
tcpSocket = cManager.openTCPServerRendezvous(9099, 1000)
cListener.addConnection(tcpSocket)
# The command code for the recipient.
MOVE = 1
def tskListenerPolling(taskdata):
if cListener.newConnectionAvailable():
rendezvous = PointerToConnection()
netAddress = NetAddress()
newConnection = PointerToConnection()
if cListener.getNewConnection(rendezvous,netAddress,newConnection):
newConnection = newConnection.p()
activeConnections.append(newConnection)
cReader.addConnection(newConnection)
return taskdata.cont
def tskReaderPolling(taskdata):
if cReader.dataAvailable():
datagram = NetDatagram()
if cReader.getData(datagram):
myIterator = PyDatagramIterator(datagram)
msgID = myIterator.getUint8()
if msgID == MOVE:
print('x =', myIterator.getFloat64())
print('y =', myIterator.getFloat64())
print('z =', myIterator.getFloat64())
return taskdata.cont
taskMgr = Task.TaskManager()
taskMgr.add(tskListenerPolling, "Poll the connection listener", -39)
taskMgr.add(tskReaderPolling, "Poll the connection reader", -40)
taskMgr.run()
client.py
from panda3d.core import QueuedConnectionManager
from panda3d.core import QueuedConnectionReader
from panda3d.core import ConnectionWriter
from direct.distributed.PyDatagram import PyDatagram
cManager = QueuedConnectionManager()
cReader = QueuedConnectionReader(cManager, 0)
cWriter = ConnectionWriter(cManager, 0)
# The command code for the recipient.
MOVE = 1
myPyDatagram = PyDatagram()
myPyDatagram.addUint8(MOVE)
myPyDatagram.addFloat64(2.4) # x
myPyDatagram.addFloat64(4.93) # y
myPyDatagram.addFloat64(0.13) # z
myConnection = cManager.openTCPClientConnection("127.0.0.1", 9099, 3000)
if myConnection:
cReader.addConnection(myConnection)
cWriter.send(myPyDatagram, myConnection)