Panda3D

connectionManager.h

00001 // Filename: connectionManager.h
00002 // Created by:  jns (07Feb00)
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 #ifndef CONNECTIONMANAGER_H
00016 #define CONNECTIONMANAGER_H
00017 
00018 #include "pandabase.h"
00019 
00020 #include "netDatagram.h"
00021 #include "connection.h"
00022 #include "pointerTo.h"
00023 #include "pset.h"
00024 #include "lightMutex.h"
00025 
00026 class NetAddress;
00027 class ConnectionReader;
00028 class ConnectionWriter;
00029 
00030 ////////////////////////////////////////////////////////////////////
00031 //       Class : ConnectionManager
00032 // Description : The primary interface to the low-level networking
00033 //               layer in this package.  A ConnectionManager is used
00034 //               to establish and destroy TCP and UDP connections.
00035 //               Communication on these connections, once established,
00036 //               is handled via ConnectionReader, ConnectionWriter,
00037 //               and ConnectionListener.
00038 //
00039 //               You may use this class directly if you don't care
00040 //               about tracking which connections have been
00041 //               unexpectedly closed; otherwise, you should use
00042 //               QueuedConnectionManager to get reports about these
00043 //               events (or derive your own class to handle these
00044 //               events properly).
00045 ////////////////////////////////////////////////////////////////////
00046 class EXPCL_PANDA_NET ConnectionManager {
00047 PUBLISHED:
00048   ConnectionManager();
00049   virtual ~ConnectionManager();
00050 
00051   PT(Connection) open_UDP_connection(int port = 0);
00052 
00053   BLOCKING PT(Connection) open_TCP_server_rendezvous(int port, int backlog);
00054   BLOCKING PT(Connection) open_TCP_server_rendezvous(const string &hostname, 
00055                                                      int port, int backlog);
00056   BLOCKING PT(Connection) open_TCP_server_rendezvous(const NetAddress &address, 
00057                                                      int backlog);
00058   BLOCKING PT(Connection) open_TCP_client_connection(const NetAddress &address,
00059                                                      int timeout_ms);
00060   BLOCKING PT(Connection) open_TCP_client_connection(const string &hostname, int port,
00061                                                      int timeout_ms);
00062 
00063   bool close_connection(const PT(Connection) &connection);
00064   BLOCKING bool wait_for_readers(double timeout);
00065 
00066   static string get_host_name();
00067 
00068 protected:
00069   void new_connection(const PT(Connection) &connection);
00070   virtual void flush_read_connection(Connection *connection);
00071   virtual void connection_reset(const PT(Connection) &connection, 
00072                                 bool okflag);
00073 
00074   void add_reader(ConnectionReader *reader);
00075   void remove_reader(ConnectionReader *reader);
00076   void add_writer(ConnectionWriter *writer);
00077   void remove_writer(ConnectionWriter *writer);
00078 
00079   typedef phash_set< PT(Connection) > Connections;
00080   typedef phash_set<ConnectionReader *, pointer_hash> Readers;
00081   typedef phash_set<ConnectionWriter *, pointer_hash> Writers;
00082   Connections _connections;
00083   Readers _readers;
00084   Writers _writers;
00085   LightMutex _set_mutex;
00086 
00087 private:
00088   friend class ConnectionReader;
00089   friend class ConnectionWriter;
00090   friend class ConnectionListener;
00091   friend class Connection;
00092 };
00093 
00094 #endif
 All Classes Functions Variables Enumerations