Panda3D
cConnectionRepository.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 cConnectionRepository.h
10  * @author drose
11  * @date 2004-05-17
12  */
13 
14 #ifndef CCONNECTIONREPOSITORY_H
15 #define CCONNECTIONREPOSITORY_H
16 
17 #include "directbase.h"
18 #include "pointerTo.h"
19 
20 #include "dcbase.h"
21 #include "dcFile.h"
22 #include "dcField.h" // to pick up Python.h
23 #include "pStatCollector.h"
24 #include "datagramIterator.h"
25 #include "clockObject.h"
26 #include "reMutex.h"
27 #include "reMutexHolder.h"
28 
29 #ifdef HAVE_NET
31 #include "connectionWriter.h"
32 #include "queuedConnectionReader.h"
33 #include "connection.h"
34 #endif
35 
36 #ifdef WANT_NATIVE_NET
38 #include "socket_address.h"
39 #endif
40 
41 class URLSpec;
42 class HTTPChannel;
43 class SocketStream;
44 
45 /**
46  * This class implements the C++ side of the ConnectionRepository object. In
47  * particular, it manages the connection to the server once it has been opened
48  * (but does not open it directly). It manages reading and writing datagrams
49  * on the connection and monitoring for unexpected disconnects as well as
50  * handling intentional disconnects.
51  *
52  * Certain server messages, like field updates, are handled entirely within
53  * the C++ layer, while server messages that are not understood by the C++
54  * layer are returned up to the Python layer for processing.
55  */
56 class EXPCL_DIRECT_DISTRIBUTED CConnectionRepository {
57 PUBLISHED:
58  explicit CConnectionRepository(bool has_owner_view = false,
59  bool threaded_net = false);
61 
62 /*
63  * Any methods of this class that acquire _lock (which is most of them) *must*
64  * be tagged BLOCKING, to avoid risk of a race condition in Python when
65  * running in true threaded mode. The BLOCKING tag releases the Python GIL
66  * during the function call, and we re-acquire it when needed within these
67  * functions to call out to Python. If any functions acquire _lock while
68  * already holding the Python GIL, there could be a deadlock between these
69  * functions and the ones that are acquiring the GIL while already holding
70  * _lock.
71  */
72 
73  INLINE DCFile &get_dc_file();
74 
75  INLINE bool has_owner_view() const;
76 
77  INLINE void set_handle_c_updates(bool handle_c_updates);
78  INLINE bool get_handle_c_updates() const;
79 
80  INLINE void set_client_datagram(bool client_datagram);
81  INLINE bool get_client_datagram() const;
82 
83  INLINE void set_handle_datagrams_internally(bool handle_datagrams_internally);
84  INLINE bool get_handle_datagrams_internally() const;
85 
86  void set_tcp_header_size(int tcp_header_size);
87  INLINE int get_tcp_header_size() const;
88 
89 #ifdef HAVE_PYTHON
90  INLINE void set_python_repository(PyObject *python_repository);
91 #endif
92 
93 #ifdef HAVE_OPENSSL
94  BLOCKING void set_connection_http(HTTPChannel *channel);
95  BLOCKING SocketStream *get_stream();
96 #endif
97 #ifdef HAVE_NET
98  BLOCKING bool try_connect_net(const URLSpec &url);
99 
100  INLINE QueuedConnectionManager &get_qcm();
101  INLINE ConnectionWriter &get_cw();
102  INLINE QueuedConnectionReader &get_qcr();
103 #endif
104 
105 #ifdef WANT_NATIVE_NET
106  BLOCKING bool connect_native(const URLSpec &url);
107  INLINE Buffered_DatagramConnection &get_bdc();
108 #endif
109 
110 #ifdef SIMULATE_NETWORK_DELAY
111  BLOCKING void start_delay(double min_delay, double max_delay);
112  BLOCKING void stop_delay();
113 #endif
114 
115  BLOCKING bool check_datagram();
116 
117  BLOCKING INLINE void get_datagram(Datagram &dg);
118  BLOCKING INLINE void get_datagram_iterator(DatagramIterator &di);
119  BLOCKING INLINE CHANNEL_TYPE get_msg_channel(int offset = 0) const;
120  BLOCKING INLINE int get_msg_channel_count() const;
121  BLOCKING INLINE CHANNEL_TYPE get_msg_sender() const;
122 // INLINE unsigned char get_sec_code() const;
123  BLOCKING INLINE unsigned int get_msg_type() const;
124 
125  INLINE static const std::string &get_overflow_event_name();
126 
127  BLOCKING bool is_connected();
128 
129  BLOCKING bool send_datagram(const Datagram &dg);
130 
131  BLOCKING INLINE void set_want_message_bundling(bool flag);
132  BLOCKING INLINE bool get_want_message_bundling() const;
133 
134  BLOCKING INLINE void set_in_quiet_zone(bool flag);
135  BLOCKING INLINE bool get_in_quiet_zone() const;
136 
137  BLOCKING void start_message_bundle();
138  BLOCKING INLINE bool is_bundling_messages() const;
139  BLOCKING void send_message_bundle(unsigned int channel, unsigned int sender_channel);
140  BLOCKING void abandon_message_bundles();
141  BLOCKING void bundle_msg(const Datagram &dg);
142 
143  BLOCKING bool consider_flush();
144  BLOCKING bool flush();
145 
146  BLOCKING void disconnect();
147  BLOCKING void shutdown();
148 
149  INLINE void set_simulated_disconnect(bool simulated_disconnect);
150  INLINE bool get_simulated_disconnect() const;
151 
152  INLINE void toggle_verbose();
153  INLINE void set_verbose(bool verbose);
154  INLINE bool get_verbose() const;
155 
156  INLINE void set_time_warning(float time_warning);
157  INLINE float get_time_warning() const;
158 
159 private:
160  bool do_check_datagram();
161  bool handle_update_field();
162  bool handle_update_field_owner();
163 
164  void describe_message(std::ostream &out, const std::string &prefix,
165  const Datagram &dg) const;
166 
167 private:
168  ReMutex _lock;
169 
170 #ifdef HAVE_PYTHON
171  PyObject *_python_repository;
172 #endif
173 
174 #ifdef HAVE_OPENSSL
175  SocketStream *_http_conn;
176 #endif
177 
178 #ifdef HAVE_NET
180  ConnectionWriter _cw;
182  PT(Connection) _net_conn;
183 #endif
184 
185 #ifdef WANT_NATIVE_NET
187  bool _native;
188 #endif
189 
190  DCFile _dc_file;
191  bool _has_owner_view;
192  bool _handle_c_updates;
193  bool _client_datagram;
194  bool _handle_datagrams_internally;
195  int _tcp_header_size;
196  bool _simulated_disconnect;
197  bool _verbose;
198  bool _in_quiet_zone;
199  float _time_warning;
200 
201  Datagram _dg;
202  DatagramIterator _di;
203 
204  std::vector<CHANNEL_TYPE> _msg_channels;
205  CHANNEL_TYPE _msg_sender;
206  unsigned int _msg_type;
207 
208  static const std::string _overflow_event_name;
209 
210  bool _want_message_bundling;
211  unsigned int _bundling_msgs;
212  typedef std::vector< std::string > BundledMsgVector;
213  BundledMsgVector _bundle_msgs;
214 
215  static PStatCollector _update_pcollector;
216 };
217 
218 #include "cConnectionRepository.I"
219 
220 #endif // CCONNECTIONREPOSITORY_H
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A container for a URL, e.g.
Definition: urlSpec.h:28
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This flavor of ConnectionManager will queue up all of the reset-connection messages from the Connecti...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Represents the complete list of Distributed Class descriptions as read from a .dc file.
Definition: dcFile.h:32
A lightweight class that represents a single element that may be timed and/or counted via stats.
bool is_connected(MObject &node, const string &attribute_name)
Returns true if the named connection exists on the node and is connected to anything,...
Definition: maya_funcs.cxx:72
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This flavor of ConnectionReader will read from its sockets and queue up all of the datagrams read for...
A class to retrieve the individual data elements previously stored in a Datagram.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Represents a single TCP or UDP socket for input or output.
Definition: connection.h:29
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
A reentrant mutex.
Definition: reMutex.h:32
This class implements the C++ side of the ConnectionRepository object.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.