Panda3D Manual: Client-Server ConnectionThe first step in network communication is to establish the client-server connection. This entails two sets of operations: one for the server side (which listens for incoming connections), and one for the client side (which establishes a connection to the server). Both of these processes are described below. Preparing the server for connectionAn average Panda program acting as a server will need to create four classes:
The first step is to instantiate these four classes. cManager = QueuedConnectionManager() This method of instantiation prepares the classes in single-thread mode, which that realtime communication requires them to be polled periodically. To accept client connections, the server opens a special "rendezvous" socket at a specific port address. This port address must be known by both the client and the server. Additionally, a backlog is specified; this is the number of incoming connection requests that the connection will track before it starts rejecting connection attempts. The responsibility for managing the rendezvous socket is passed to the QueuedConnectionListener, and a task is spawned to periodically poll the listener. port_address=9099 #No-other TCP/IP services are using this port
taskMgr.add(tskListenerPolling,"Poll the connection listener",-39) When a connection comes in, the tskListenerPolling function below handles the incoming connection and hands it to the QueuedConnectionReader. The connection is now established. def tskListenerPolling(taskdata): Once a connection has been opened, the QueuedConnectionReader may begin processing incoming packets. This is similar to the flow of the listener's task, but it is up to the server code to handle the incoming data. def tskReaderPolling(taskdata): Note that the QueuedConnectionReader retrieves data from all clients connected to the server. The NetDatagram can be queried using NetDatagram.getConnection to determine which client sent the message. If the server wishes to send data to the client, it can use the ConnectionWriter to transmit back along the connection. # broadcast a message to all clients Finally, the server may terminate a connection by removing it from the QueuedConnectionReader's responsibility. It may also deactivate its listener so that no more connections are received # terminate connection to all clients Connecting with a clientThe process the client undertakes to connect to a server is extremely similar to the process the server undertakes to receive connections. Like the server, a client instantiates a QueuedConnectionManager, QueuedConnectionReader, and ConnectionWriter. However, there are some differences in the process. In general, a client has no need to open a rendezvous socket or create a QueuedConnectionListener, since it will be doing the connecting itself. Instead, the client connects to a specific server by specifying the server's IP address and the correct socket ID. port_address=9099 # same for client and server When the client has finished communicating with the server, it can close the connection. cManager.closeConnection(myConnection) © Carnegie Mellon University 2010 |