00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
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
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
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
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 shutdown = true;
00117 }
00118 }
00119 }
00120
00121
00122 PR_Sleep(PR_MillisecondsToInterval(100));
00123 }
00124
00125 return (0);
00126 }
00127
00128
00129
00130
00131