00001 // Filename: test_tcp_server.cxx 00002 // Created by: drose (09Feb00) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 #include <pandabase.h> 00020 00021 #include "queuedConnectionManager.h" 00022 #include "queuedConnectionListener.h" 00023 #include "queuedConnectionReader.h" 00024 #include "connectionWriter.h" 00025 #include "netAddress.h" 00026 #include "connection.h" 00027 #include "netDatagram.h" 00028 00029 #include "datagram_ui.h" 00030 00031 #include "pset.h" 00032 00033 int 00034 main(int argc, char *argv[]) { 00035 if (argc != 2) { 00036 nout << "test_tcp_server port\n"; 00037 exit(1); 00038 } 00039 00040 int port = atoi(argv[1]); 00041 00042 QueuedConnectionManager cm; 00043 PT(Connection) rendezvous = cm.open_TCP_server_rendezvous(port, 5); 00044 00045 if (rendezvous.is_null()) { 00046 nout << "Cannot grab port " << port << ".\n"; 00047 exit(1); 00048 } 00049 00050 nout << "Listening for connections on port " << port << "\n"; 00051 00052 QueuedConnectionListener listener(&cm, 0); 00053 listener.add_connection(rendezvous); 00054 00055 typedef pset< PT(Connection) > Clients; 00056 Clients clients; 00057 00058 QueuedConnectionReader reader(&cm, 1); 00059 ConnectionWriter writer(&cm, 1); 00060 00061 bool shutdown = false; 00062 while (!shutdown) { 00063 // Check for new clients. 00064 while (listener.new_connection_available()) { 00065 PT(Connection) rv; 00066 NetAddress address; 00067 PT(Connection) new_connection; 00068 if (listener.get_new_connection(rv, address, new_connection)) { 00069 nout << "Got connection from " << address << "\n"; 00070 reader.add_connection(new_connection); 00071 clients.insert(new_connection); 00072 } 00073 } 00074 00075 // Check for reset clients. 00076 while (cm.reset_connection_available()) { 00077 PT(Connection) connection; 00078 if (cm.get_reset_connection(connection)) { 00079 nout << "Lost connection from " 00080 << connection->get_address() << "\n"; 00081 clients.erase(connection); 00082 cm.close_connection(connection); 00083 } 00084 } 00085 00086 // Process all available datagrams. 00087 while (reader.data_available()) { 00088 NetDatagram datagram; 00089 if (reader.get_data(datagram)) { 00090 nout << "Got datagram " << datagram << "from " 00091 << datagram.get_address() << ", sending to " 00092 << clients.size() << " clients.\n"; 00093 datagram.dump_hex(nout); 00094 00095 Clients::iterator ci; 00096 for (ci = clients.begin(); ci != clients.end(); ++ci) { 00097 writer.send(datagram, (*ci)); 00098 } 00099 00100 if (datagram.get_length() <= 1) { 00101 /* 00102 // An empty datagram means to close the connection. 00103 PT(Connection) connection = datagram.get_connection(); 00104 if (connection.is_null()) { 00105 nout << "Empty datagram from a null connection.\n"; 00106 } else { 00107 nout << "Closing connection from " 00108 << connection->get_address() << "\n"; 00109 clients.erase(connection); 00110 cm.close_connection(connection); 00111 nout << "Closed " << connection << "\n"; 00112 } 00113 */ 00114 00115 // No, an empty datagram means to shut down the server. 00116 shutdown = true; 00117 } 00118 } 00119 } 00120 00121 // Yield the timeslice before we poll again. 00122 PR_Sleep(PR_MillisecondsToInterval(100)); 00123 } 00124 00125 return (0); 00126 } 00127 00128 00129 00130 00131