Opening a UDP connection

Hi! I would like to open a UDP connection for passing data from a client to a server (anyway my objective will be to get bidirectional communication).

If I use TCP everything works. I’ve defined this server script:

from panda3d.core import *
loadPrcFileData('', 'window-type none')
from direct.distributed.PyDatagram import *
from direct.distributed.PyDatagramIterator import *
import direct.directbase.DirectStart


class Server:

    def __init__(self):
        conn_mgr = QueuedConnectionManager()
        self.conn_reader = QueuedConnectionReader(conn_mgr, 0)
        self.conn_listener = QueuedConnectionListener(conn_mgr, 0)
        tcp_socket = conn_mgr.open_TCP_server_rendezvous(9099, 1000)
        self.conn_listener.add_connection(tcp_socket)
        taskMgr.add(self.on_frame, 'on frame')
        taskMgr.add(self.task_listener, 'task listener')

    def task_listener(self, task):
        if not self.conn_listener.new_connection_available(): return task.cont
        new_conn = PointerToConnection()
        has_conn = self.conn_listener.get_new_connection(
            PointerToConnection(), NetAddress(), new_conn)
        if not has_conn: return task.cont
        self.conn_reader.add_connection(new_conn.p())
        return task.cont

    def on_frame(self, task):
        if not self.conn_reader.data_available(): return task.again
        datagram = NetDatagram()
        self.conn_reader.get_data(datagram)
        if not datagram: return task.again
        print 'received', PyDatagramIterator(datagram).get_int32()
        return task.again


Server()
base.run()

I’ve defined this client script:

from panda3d.core import *
loadPrcFileData('', 'window-type none')
from direct.distributed.PyDatagram import *
from direct.distributed.PyDatagramIterator import *
import direct.directbase.DirectStart


class Client:

    def __init__(self):
        self.conn_mgr = QueuedConnectionManager()
        self.conn_writer = ConnectionWriter(self.conn_mgr, 0)
        self.conn = self.conn_mgr.open_TCP_client_connection('127.0.0.1', 9099, 3000)
        if not self.conn: raise ClientError
        taskMgr.add(self.on_frame, 'on frame')

    def on_frame(self, task):
        datagram = PyDatagram()
        datagram.add_int32(int(globalClock.getFrameTime()))
        print 'sent', int(globalClock.getFrameTime())
        self.conn_writer.send(datagram, self.conn)
        return task.again


Client()
base.run()

The output in the console says that the messages are correctly sent and received. Now i try to replace the Connection with a UDP connection in the client script:

from panda3d.core import *
loadPrcFileData('', 'window-type none')
from direct.distributed.PyDatagram import *
from direct.distributed.PyDatagramIterator import *
import direct.directbase.DirectStart


class Client:

    def __init__(self):
        self.conn_mgr = QueuedConnectionManager()
        self.conn_writer = ConnectionWriter(self.conn_mgr, 0)
        self.conn = self.conn_mgr.open_UDP_connection(9099)
        if not self.conn: raise ClientError
        taskMgr.add(self.on_frame, 'on frame')

    def on_frame(self, task):
        datagram = PyDatagram()
        datagram.add_int32(int(globalClock.getFrameTime()))
        print 'sent', int(globalClock.getFrameTime())
        self.conn_writer.send(datagram, self.conn)
        return task.again


Client()
base.run()

But now it doesn’t work: I get this:

Traceback (most recent call last):
  File "client.py", line 22, in on_frame
    self.conn_writer.send(datagram, self.conn)
AssertionError: connection->get_socket()->is_exact_type(Socket_TCP::get_class_type()) at line 146 of panda/src/net/connectionWriter.cxx

What am I missing? Thank you very much!

There are two versions of the send() function. One is meant for TCP use, the other for UDP.

The one for TCP has this signature:

conn_writer.send(datagram, connection, block=False)

With UDP, however, you have to set specify a destination host address, so the signature looks as follows:

conn_writer.send(datagram, connection, address, block=False)