Panda3D
 All Classes Functions Variables Enumerations
queuedConnectionListener.cxx
00001 // Filename: queuedConnectionListener.cxx
00002 // Created by:  drose (09Feb00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "queuedConnectionListener.h"
00016 #include "config_net.h"
00017 
00018 ////////////////////////////////////////////////////////////////////
00019 //     Function: QueuedConnectionListener::Constructor
00020 //       Access: Public
00021 //  Description:
00022 ////////////////////////////////////////////////////////////////////
00023 QueuedConnectionListener::
00024 QueuedConnectionListener(ConnectionManager *manager, int num_threads) :
00025   ConnectionListener(manager, num_threads)
00026 {
00027 }
00028 
00029 ////////////////////////////////////////////////////////////////////
00030 //     Function: QueuedConnectionListener::Destructor
00031 //       Access: Public, Virtual
00032 //  Description:
00033 ////////////////////////////////////////////////////////////////////
00034 QueuedConnectionListener::
00035 ~QueuedConnectionListener() {
00036   // We call shutdown() here to guarantee that all threads are gone
00037   // before the QueuedReturn destructs.
00038   shutdown();
00039 }
00040 
00041 ////////////////////////////////////////////////////////////////////
00042 //     Function: QueuedConnectionListener::new_connection_available
00043 //       Access: Public
00044 //  Description: Returns true if a new connection was recently
00045 //               established; the connection information may then be
00046 //               retrieved via get_new_connection().
00047 ////////////////////////////////////////////////////////////////////
00048 bool QueuedConnectionListener::
00049 new_connection_available() {
00050   poll();
00051   return thing_available();
00052 }
00053 
00054 ////////////////////////////////////////////////////////////////////
00055 //     Function: QueuedConnectionListener::get_new_connection
00056 //       Access: Public
00057 //  Description: If a previous call to new_connection_available()
00058 //               returned true, this function will return information
00059 //               about the newly established connection.
00060 //
00061 //               The rendezvous parameter is the particular rendezvous
00062 //               socket this new connection originally communicated
00063 //               with; it is provided in case the ConnectionListener
00064 //               was monitorind more than one and you care which one
00065 //               it was.  The address parameter is the net address of
00066 //               the new client, and new_connection is the socket of
00067 //               the newly established connection.
00068 //
00069 //               The return value is true if a connection was
00070 //               successfully returned, or false if there was, in
00071 //               fact, no new connection.  (This may happen if there
00072 //               are multiple threads accessing the
00073 //               QueuedConnectionListener).
00074 ////////////////////////////////////////////////////////////////////
00075 bool QueuedConnectionListener::
00076 get_new_connection(PT(Connection) &rendezvous,
00077                    NetAddress &address,
00078                    PT(Connection) &new_connection) {
00079   ConnectionListenerData result;
00080   if (!get_thing(result)) {
00081     return false;
00082   }
00083 
00084   rendezvous = result._rendezvous;
00085   address = result._address;
00086   new_connection = result._new_connection;
00087   return true;
00088 }
00089 
00090 ////////////////////////////////////////////////////////////////////
00091 //     Function: QueuedConnectionListener::get_new_connection
00092 //       Access: Public
00093 //  Description: This flavor of get_new_connection() simply returns a
00094 //               new connection, assuming the user doesn't care about
00095 //               the rendezvous socket that originated it or the
00096 //               address it came from.
00097 ////////////////////////////////////////////////////////////////////
00098 bool QueuedConnectionListener::
00099 get_new_connection(PT(Connection) &new_connection) {
00100   PT(Connection) rendezvous;
00101   NetAddress address;
00102   return get_new_connection(rendezvous, address, new_connection);
00103 }
00104 
00105 
00106 ////////////////////////////////////////////////////////////////////
00107 //     Function: QueuedConnectionListener::connection_opened
00108 //       Access: Protected, Virtual
00109 //  Description: An internal function called by ConnectionListener()
00110 //               when a new TCP connection has been established.  The
00111 //               QueuedConnectionListener simply queues up this fact
00112 //               for later retrieval by get_new_connection().
00113 ////////////////////////////////////////////////////////////////////
00114 void QueuedConnectionListener::
00115 connection_opened(const PT(Connection) &rendezvous,
00116                   const NetAddress &address,
00117                   const PT(Connection) &new_connection) {
00118   ConnectionListenerData nc;
00119   nc._rendezvous = rendezvous;
00120   nc._address = address;
00121   nc._new_connection = new_connection;
00122 
00123   if (!enqueue_thing(nc)) {
00124     net_cat.error()
00125       << "QueuedConnectionListener queue full!\n";
00126   }
00127 }
 All Classes Functions Variables Enumerations