Panda3D

connectionListener.cxx

00001 // Filename: connectionListener.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 "connectionListener.h"
00016 #include "connection.h"
00017 #include "connectionManager.h"
00018 #include "netAddress.h"
00019 #include "config_net.h"
00020 #include "socket_tcp_listen.h"
00021 
00022 static string 
00023 listener_thread_name(const string &thread_name) {
00024   if (!thread_name.empty()) {
00025     return thread_name;
00026   }
00027   return "ListenerThread";
00028 }
00029 
00030 ////////////////////////////////////////////////////////////////////
00031 //     Function: ConnectionListener::Constructor
00032 //       Access: Public
00033 //  Description:
00034 ////////////////////////////////////////////////////////////////////
00035 ConnectionListener::
00036 ConnectionListener(ConnectionManager *manager, int num_threads,
00037                    const string &thread_name) :
00038   ConnectionReader(manager, num_threads, listener_thread_name(thread_name))
00039 {
00040 }
00041 
00042 ////////////////////////////////////////////////////////////////////
00043 //     Function: ConnectionListener::receive_datagram
00044 //       Access: Protected, Virtual
00045 //  Description: This function must be declared because it is pure
00046 //               virtual in the base class, but it isn't used in this
00047 //               class and doesn't do anything.
00048 ////////////////////////////////////////////////////////////////////
00049 void ConnectionListener::
00050 receive_datagram(const NetDatagram &) {
00051   net_cat.error()
00052     << "ConnectionListener::receive_datagram called.\n";
00053 }
00054 
00055 ////////////////////////////////////////////////////////////////////
00056 //     Function: ConnectionListener::process_incoming_data
00057 //       Access: Protected, Virtual
00058 //  Description: This is the function that is called when activity is
00059 //               detected on a rendezvous port.  In this case, it
00060 //               performs the accept().
00061 ////////////////////////////////////////////////////////////////////
00062 bool ConnectionListener::
00063 process_incoming_data(SocketInfo *sinfo) {
00064   Socket_TCP_Listen *socket;
00065   DCAST_INTO_R(socket, sinfo->get_socket(), false);
00066 
00067   Socket_Address addr;
00068   Socket_TCP *session = new Socket_TCP;
00069 
00070   bool got_connection = socket->GetIncomingConnection(*session, addr);
00071 #if defined(HAVE_THREADS) && defined(SIMPLE_THREADS)
00072   while (!got_connection && socket->GetLastError() == LOCAL_BLOCKING_ERROR) {
00073     Thread::force_yield();
00074     got_connection = socket->GetIncomingConnection(*session, addr);
00075   }
00076 #endif  // SIMPLE_THREADS
00077 
00078   if (!got_connection) {
00079     net_cat.error()
00080       << "Error when accepting new connection.\n";
00081     delete session;
00082     finish_socket(sinfo);
00083     return false;
00084   }
00085 
00086   NetAddress net_addr(addr);
00087   net_cat.info()
00088     << "Received TCP connection from client " << net_addr.get_ip_string()
00089     << " on port " << sinfo->_connection->get_address().get_port()
00090     << "\n";
00091   
00092   PT(Connection) new_connection = new Connection(_manager, session);
00093   if (_manager != (ConnectionManager *)NULL) {
00094     _manager->new_connection(new_connection);
00095   }
00096   connection_opened(sinfo->_connection, net_addr, new_connection);
00097 
00098   finish_socket(sinfo);
00099   return true;
00100 }
 All Classes Functions Variables Enumerations