00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
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
00032
00033
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
00044
00045
00046
00047
00048
00049 void ConnectionListener::
00050 receive_datagram(const NetDatagram &) {
00051 net_cat.error()
00052 << "ConnectionListener::receive_datagram called.\n";
00053 }
00054
00055
00056
00057
00058
00059
00060
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 }