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