cConnectionRepository.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #ifndef CCONNECTIONREPOSITORY_H
  14. #define CCONNECTIONREPOSITORY_H
  15. #include "directbase.h"
  16. #include "pointerTo.h"
  17. #include "dcbase.h"
  18. #include "dcFile.h"
  19. #include "dcField.h" // to pick up Python.h
  20. #include "pStatCollector.h"
  21. #include "datagramIterator.h"
  22. #include "clockObject.h"
  23. #include "reMutex.h"
  24. #include "reMutexHolder.h"
  25. #ifdef HAVE_NET
  26. #include "queuedConnectionManager.h"
  27. #include "connectionWriter.h"
  28. #include "queuedConnectionReader.h"
  29. #include "connection.h"
  30. #endif
  31. #ifdef WANT_NATIVE_NET
  32. #include "buffered_datagramconnection.h"
  33. #include "socket_address.h"
  34. #endif
  35. class URLSpec;
  36. class HTTPChannel;
  37. class SocketStream;
  38. /**
  39. * This class implements the C++ side of the ConnectionRepository object. In
  40. * particular, it manages the connection to the server once it has been opened
  41. * (but does not open it directly). It manages reading and writing datagrams
  42. * on the connection and monitoring for unexpected disconnects as well as
  43. * handling intentional disconnects.
  44. *
  45. * Certain server messages, like field updates, are handled entirely within
  46. * the C++ layer, while server messages that are not understood by the C++
  47. * layer are returned up to the Python layer for processing.
  48. */
  49. class CConnectionRepository {
  50. PUBLISHED:
  51. explicit CConnectionRepository(bool has_owner_view = false,
  52. bool threaded_net = false);
  53. ~CConnectionRepository();
  54. /*
  55. * Any methods of this class that acquire _lock (which is most of them) *must*
  56. * be tagged BLOCKING, to avoid risk of a race condition in Python when
  57. * running in true threaded mode. The BLOCKING tag releases the Python GIL
  58. * during the function call, and we re-acquire it when needed within these
  59. * functions to call out to Python. If any functions acquire _lock while
  60. * already holding the Python GIL, there could be a deadlock between these
  61. * functions and the ones that are acquiring the GIL while already holding
  62. * _lock.
  63. */
  64. INLINE DCFile &get_dc_file();
  65. INLINE bool has_owner_view() const;
  66. INLINE void set_handle_c_updates(bool handle_c_updates);
  67. INLINE bool get_handle_c_updates() const;
  68. INLINE void set_client_datagram(bool client_datagram);
  69. INLINE bool get_client_datagram() const;
  70. INLINE void set_handle_datagrams_internally(bool handle_datagrams_internally);
  71. INLINE bool get_handle_datagrams_internally() const;
  72. void set_tcp_header_size(int tcp_header_size);
  73. INLINE int get_tcp_header_size() const;
  74. #ifdef HAVE_PYTHON
  75. INLINE void set_python_repository(PyObject *python_repository);
  76. #endif
  77. #ifdef HAVE_OPENSSL
  78. BLOCKING void set_connection_http(HTTPChannel *channel);
  79. BLOCKING SocketStream *get_stream();
  80. #endif
  81. #ifdef HAVE_NET
  82. BLOCKING bool try_connect_net(const URLSpec &url);
  83. INLINE QueuedConnectionManager &get_qcm();
  84. INLINE ConnectionWriter &get_cw();
  85. INLINE QueuedConnectionReader &get_qcr();
  86. #endif
  87. #ifdef WANT_NATIVE_NET
  88. BLOCKING bool connect_native(const URLSpec &url);
  89. INLINE Buffered_DatagramConnection &get_bdc();
  90. #endif
  91. #ifdef SIMULATE_NETWORK_DELAY
  92. BLOCKING void start_delay(double min_delay, double max_delay);
  93. BLOCKING void stop_delay();
  94. #endif
  95. BLOCKING bool check_datagram();
  96. BLOCKING INLINE void get_datagram(Datagram &dg);
  97. BLOCKING INLINE void get_datagram_iterator(DatagramIterator &di);
  98. BLOCKING INLINE CHANNEL_TYPE get_msg_channel(int offset = 0) const;
  99. BLOCKING INLINE int get_msg_channel_count() const;
  100. BLOCKING INLINE CHANNEL_TYPE get_msg_sender() const;
  101. // INLINE unsigned char get_sec_code() const;
  102. BLOCKING INLINE unsigned int get_msg_type() const;
  103. INLINE static const std::string &get_overflow_event_name();
  104. BLOCKING bool is_connected();
  105. BLOCKING bool send_datagram(const Datagram &dg);
  106. BLOCKING INLINE void set_want_message_bundling(bool flag);
  107. BLOCKING INLINE bool get_want_message_bundling() const;
  108. BLOCKING INLINE void set_in_quiet_zone(bool flag);
  109. BLOCKING INLINE bool get_in_quiet_zone() const;
  110. BLOCKING void start_message_bundle();
  111. BLOCKING INLINE bool is_bundling_messages() const;
  112. BLOCKING void send_message_bundle(unsigned int channel, unsigned int sender_channel);
  113. BLOCKING void abandon_message_bundles();
  114. BLOCKING void bundle_msg(const Datagram &dg);
  115. BLOCKING bool consider_flush();
  116. BLOCKING bool flush();
  117. BLOCKING void disconnect();
  118. BLOCKING void shutdown();
  119. INLINE void set_simulated_disconnect(bool simulated_disconnect);
  120. INLINE bool get_simulated_disconnect() const;
  121. INLINE void toggle_verbose();
  122. INLINE void set_verbose(bool verbose);
  123. INLINE bool get_verbose() const;
  124. INLINE void set_time_warning(float time_warning);
  125. INLINE float get_time_warning() const;
  126. private:
  127. bool do_check_datagram();
  128. bool handle_update_field();
  129. bool handle_update_field_owner();
  130. void describe_message(std::ostream &out, const std::string &prefix,
  131. const Datagram &dg) const;
  132. private:
  133. ReMutex _lock;
  134. #ifdef HAVE_PYTHON
  135. PyObject *_python_repository;
  136. #endif
  137. #ifdef HAVE_OPENSSL
  138. SocketStream *_http_conn;
  139. #endif
  140. #ifdef HAVE_NET
  141. QueuedConnectionManager _qcm;
  142. ConnectionWriter _cw;
  143. QueuedConnectionReader _qcr;
  144. PT(Connection) _net_conn;
  145. #endif
  146. #ifdef WANT_NATIVE_NET
  147. Buffered_DatagramConnection _bdc;
  148. bool _native;
  149. #endif
  150. DCFile _dc_file;
  151. bool _has_owner_view;
  152. bool _handle_c_updates;
  153. bool _client_datagram;
  154. bool _handle_datagrams_internally;
  155. int _tcp_header_size;
  156. bool _simulated_disconnect;
  157. bool _verbose;
  158. bool _in_quiet_zone;
  159. float _time_warning;
  160. Datagram _dg;
  161. DatagramIterator _di;
  162. std::vector<CHANNEL_TYPE> _msg_channels;
  163. CHANNEL_TYPE _msg_sender;
  164. unsigned int _msg_type;
  165. static const std::string _overflow_event_name;
  166. bool _want_message_bundling;
  167. unsigned int _bundling_msgs;
  168. typedef std::vector< std::string > BundledMsgVector;
  169. BundledMsgVector _bundle_msgs;
  170. static PStatCollector _update_pcollector;
  171. };
  172. #include "cConnectionRepository.I"
  173. #endif // CCONNECTIONREPOSITORY_H