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