Panda3D
 All Classes Functions Variables Enumerations
connectionManager.h
1 // Filename: connectionManager.h
2 // Created by: jns (07Feb00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef CONNECTIONMANAGER_H
16 #define CONNECTIONMANAGER_H
17 
18 #include "pandabase.h"
19 
20 #include "netDatagram.h"
21 #include "connection.h"
22 #include "pointerTo.h"
23 #include "pset.h"
24 #include "pvector.h"
25 #include "lightMutex.h"
26 
27 class NetAddress;
28 class ConnectionReader;
29 class ConnectionWriter;
30 
31 ////////////////////////////////////////////////////////////////////
32 // Class : ConnectionManager
33 // Description : The primary interface to the low-level networking
34 // layer in this package. A ConnectionManager is used
35 // to establish and destroy TCP and UDP connections.
36 // Communication on these connections, once established,
37 // is handled via ConnectionReader, ConnectionWriter,
38 // and ConnectionListener.
39 //
40 // You may use this class directly if you don't care
41 // about tracking which connections have been
42 // unexpectedly closed; otherwise, you should use
43 // QueuedConnectionManager to get reports about these
44 // events (or derive your own class to handle these
45 // events properly).
46 ////////////////////////////////////////////////////////////////////
47 class EXPCL_PANDA_NET ConnectionManager {
48 PUBLISHED:
50  virtual ~ConnectionManager();
51 
52  PT(Connection) open_UDP_connection(int port = 0);
53  PT(Connection) open_UDP_connection(const string &hostname, int port, bool for_broadcast = false);
54 
55  BLOCKING PT(Connection) open_TCP_server_rendezvous(int port, int backlog);
56  BLOCKING PT(Connection) open_TCP_server_rendezvous(const string &hostname,
57  int port, int backlog);
58  BLOCKING PT(Connection) open_TCP_server_rendezvous(const NetAddress &address,
59  int backlog);
60  BLOCKING PT(Connection) open_TCP_client_connection(const NetAddress &address,
61  int timeout_ms);
62  BLOCKING PT(Connection) open_TCP_client_connection(const string &hostname, int port,
63  int timeout_ms);
64 
65  bool close_connection(const PT(Connection) &connection);
66  BLOCKING bool wait_for_readers(double timeout);
67 
68  static string get_host_name();
69 
70  class EXPCL_PANDA_NET Interface {
71  PUBLISHED:
72  const string &get_name() const { return _name; }
73  const string &get_mac_address() const { return _mac_address; }
74  bool has_ip() const { return (_flags & F_has_ip) != 0; }
75  const NetAddress &get_ip() const { return _ip; }
76  bool has_netmask() const { return (_flags & F_has_netmask) != 0; }
77  const NetAddress &get_netmask() const { return _netmask; }
78  bool has_broadcast() const { return (_flags & F_has_broadcast) != 0; }
79  const NetAddress &get_broadcast() const { return _broadcast; }
80  bool has_p2p() const { return (_flags & F_has_p2p) != 0; }
81  const NetAddress &get_p2p() const { return _p2p; }
82 
83  void output(ostream &out) const;
84 
85  public:
86  Interface() { _flags = 0; }
87  void set_name(const string &name) { _name = name; }
88  void set_mac_address(const string &mac_address) { _mac_address = mac_address; }
89  void set_ip(const NetAddress &ip) { _ip = ip; _flags |= F_has_ip; }
90  void set_netmask(const NetAddress &ip) { _netmask = ip; _flags |= F_has_netmask; }
91  void set_broadcast(const NetAddress &ip) { _broadcast = ip; _flags |= F_has_broadcast; }
92  void set_p2p(const NetAddress &ip) { _p2p = ip; _flags |= F_has_p2p; }
93 
94  private:
95  string _name;
96  string _mac_address;
97 
98  NetAddress _ip;
99  NetAddress _netmask;
100  NetAddress _broadcast;
101  NetAddress _p2p;
102  int _flags;
103 
104  enum Flags {
105  F_has_ip = 0x001,
106  F_has_netmask = 0x002,
107  F_has_broadcast = 0x004,
108  F_has_p2p = 0x008,
109  };
110  };
111 
112  void scan_interfaces();
113  int get_num_interfaces();
114  const Interface &get_interface(int n);
115  MAKE_SEQ(get_interfaces, get_num_interfaces, get_interface);
116 
117 protected:
118  void new_connection(const PT(Connection) &connection);
119  virtual void flush_read_connection(Connection *connection);
120  virtual void connection_reset(const PT(Connection) &connection,
121  bool okflag);
122 
123  void add_reader(ConnectionReader *reader);
124  void remove_reader(ConnectionReader *reader);
125  void add_writer(ConnectionWriter *writer);
126  void remove_writer(ConnectionWriter *writer);
127 
128  string format_mac_address(const unsigned char *data, int data_size);
129 
130  typedef phash_set< PT(Connection) > Connections;
131  typedef phash_set<ConnectionReader *, pointer_hash> Readers;
132  typedef phash_set<ConnectionWriter *, pointer_hash> Writers;
133  Connections _connections;
134  Readers _readers;
135  Writers _writers;
136  LightMutex _set_mutex;
137 
139  Interfaces _interfaces;
140  bool _interfaces_scanned;
141 
142 private:
143  friend class ConnectionReader;
144  friend class ConnectionWriter;
145  friend class ConnectionListener;
146  friend class Connection;
147 };
148 
149 INLINE ostream &operator << (ostream &out, const ConnectionManager::Interface &iface) {
150  iface.output(out);
151  return out;
152 }
153 
154 #endif
The primary interface to the low-level networking layer in this package.
This is an abstract base class for a family of classes that listen for activity on a socket and respo...
This class handles threaded delivery of datagrams to various TCP or UDP sockets.
This is a standard, non-reentrant mutex, similar to the Mutex class.
Definition: lightMutex.h:45
Represents a single TCP or UDP socket for input or output.
Definition: connection.h:32
This is a special kind of ConnectionReader that waits for activity on a rendezvous port and accepts a...
Represents a network address to which UDP packets may be sent or to which a TCP socket may be bound...
Definition: netAddress.h:27