cConnectionRepository.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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.cxx
  10. * @author drose
  11. * @date 2004-05-17
  12. */
  13. #include "cConnectionRepository.h"
  14. #include "dcmsgtypes.h"
  15. #include "dcClass.h"
  16. #include "dcPacker.h"
  17. #include "config_distributed.h"
  18. #include "config_downloader.h"
  19. #include "httpChannel.h"
  20. #include "urlSpec.h"
  21. #include "datagramIterator.h"
  22. #include "throw_event.h"
  23. #include "pStatTimer.h"
  24. #ifdef HAVE_PYTHON
  25. #include "py_panda.h"
  26. #endif
  27. const string CConnectionRepository::_overflow_event_name = "CRDatagramOverflow";
  28. #ifndef CPPPARSER
  29. PStatCollector CConnectionRepository::_update_pcollector("App:Show code:readerPollTask:Update");
  30. #endif // CPPPARSER
  31. /**
  32. *
  33. */
  34. CConnectionRepository::
  35. CConnectionRepository(bool has_owner_view, bool threaded_net) :
  36. _lock("CConnectionRepository::_lock"),
  37. #ifdef HAVE_PYTHON
  38. _python_repository(NULL),
  39. #endif
  40. #ifdef HAVE_OPENSSL
  41. _http_conn(NULL),
  42. #endif
  43. #ifdef HAVE_NET
  44. _cw(&_qcm, threaded_net ? 1 : 0),
  45. _qcr(&_qcm, threaded_net ? 1 : 0),
  46. #endif
  47. #ifdef WANT_NATIVE_NET
  48. _bdc(4096000,4096000,1400),
  49. _native(false),
  50. #endif
  51. _client_datagram(true),
  52. _handle_datagrams_internally(handle_datagrams_internally),
  53. _simulated_disconnect(false),
  54. _verbose(distributed_cat.is_spam()),
  55. _time_warning(0.0),
  56. // _msg_channels(),
  57. _msg_sender(0),
  58. _msg_type(0),
  59. _has_owner_view(has_owner_view),
  60. _handle_c_updates(true),
  61. _want_message_bundling(true),
  62. _bundling_msgs(0),
  63. _in_quiet_zone(0)
  64. {
  65. #if defined(HAVE_NET) && defined(SIMULATE_NETWORK_DELAY)
  66. if (min_lag != 0.0 || max_lag != 0.0) {
  67. _qcr.start_delay(min_lag, max_lag);
  68. }
  69. #endif
  70. _tcp_header_size = tcp_header_size;
  71. }
  72. /**
  73. *
  74. */
  75. CConnectionRepository::
  76. ~CConnectionRepository() {
  77. disconnect();
  78. }
  79. /**
  80. * Sets the header size of TCP packets. At the present, legal values for this
  81. * are 0, 2, or 4; this specifies the number of bytes to use encode the
  82. * datagram length at the start of each TCP datagram. Sender and receiver
  83. * must independently agree on this.
  84. */
  85. void CConnectionRepository::
  86. set_tcp_header_size(int tcp_header_size) {
  87. _tcp_header_size = tcp_header_size;
  88. #ifdef HAVE_OPENSSL
  89. if (_http_conn != (SocketStream *)NULL) {
  90. _http_conn->set_tcp_header_size(tcp_header_size);
  91. }
  92. #endif
  93. #ifdef HAVE_NET
  94. _cw.set_tcp_header_size(tcp_header_size);
  95. _qcr.set_tcp_header_size(tcp_header_size);
  96. #endif
  97. }
  98. #ifdef HAVE_OPENSSL
  99. /**
  100. * Once a connection has been established via the HTTP interface, gets the
  101. * connection and uses it. The supplied HTTPChannel object must have a
  102. * connection available via get_connection().
  103. */
  104. void CConnectionRepository::
  105. set_connection_http(HTTPChannel *channel) {
  106. ReMutexHolder holder(_lock);
  107. disconnect();
  108. nassertv(channel->is_connection_ready());
  109. _http_conn = channel->get_connection();
  110. _http_conn->set_tcp_header_size(_tcp_header_size);
  111. #ifdef SIMULATE_NETWORK_DELAY
  112. if (min_lag != 0.0 || max_lag != 0.0) {
  113. _http_conn->start_delay(min_lag, max_lag);
  114. }
  115. #endif
  116. }
  117. #endif // HAVE_OPENSSL
  118. #ifdef HAVE_OPENSSL
  119. /**
  120. * Returns the SocketStream that internally represents the already-established
  121. * HTTP connection. Returns NULL if there is no current HTTP connection.
  122. */
  123. SocketStream *CConnectionRepository::
  124. get_stream() {
  125. ReMutexHolder holder(_lock);
  126. return _http_conn;
  127. }
  128. #endif // HAVE_OPENSSL
  129. #ifdef HAVE_NET
  130. /**
  131. * Uses Panda's "net" library to try to connect to the server and port named
  132. * in the indicated URL. Returns true if successful, false otherwise.
  133. */
  134. bool CConnectionRepository::
  135. try_connect_net(const URLSpec &url) {
  136. ReMutexHolder holder(_lock);
  137. disconnect();
  138. _net_conn =
  139. _qcm.open_TCP_client_connection(url.get_server(), url.get_port(),
  140. game_server_timeout_ms);
  141. if (_net_conn != (Connection *)NULL) {
  142. _net_conn->set_no_delay(true);
  143. _qcr.add_connection(_net_conn);
  144. return true;
  145. }
  146. return false;
  147. }
  148. #endif // HAVE_NET
  149. #ifdef WANT_NATIVE_NET
  150. /**
  151. * Connects to the server using Panda's low-level and fast "native net"
  152. * library.
  153. */
  154. bool CConnectionRepository::
  155. connect_native(const URLSpec &url) {
  156. ReMutexHolder holder(_lock);
  157. _native=true;
  158. Socket_Address addr;
  159. addr.set_host(url.get_server(),url.get_port());
  160. _bdc.ClearAddresses();
  161. _bdc.AddAddress(addr);
  162. return _bdc.DoConnect();
  163. }
  164. #endif //WANT NATIVE NET
  165. #ifdef SIMULATE_NETWORK_DELAY
  166. /**
  167. * Enables a simulated network latency. All datagrams received from this
  168. * point on will be held for a random interval of least min_delay seconds, and
  169. * no more than max_delay seconds, before being visible. It is as if
  170. * datagrams suddenly took much longer to arrive.
  171. *
  172. * This should *only* be called if the underlying socket is non-blocking. If
  173. * you call this on a blocking socket, it will force all datagrams to be held
  174. * up until the socket closes.
  175. *
  176. * This has no effect if the connection method is via the "native net"
  177. * library.
  178. */
  179. void CConnectionRepository::
  180. start_delay(double min_delay, double max_delay) {
  181. ReMutexHolder holder(_lock);
  182. if (min_delay != 0.0 || max_delay != 0.0) {
  183. #ifdef HAVE_NET
  184. _qcr.start_delay(min_delay, max_delay);
  185. #endif // HAVE_NET
  186. #ifdef HAVE_OPENSSL
  187. if (_http_conn != (SocketStream *)NULL) {
  188. _http_conn->start_delay(min_delay, max_delay);
  189. }
  190. #endif // HAVE_OPENSSL
  191. } else {
  192. stop_delay();
  193. }
  194. }
  195. #endif // SIMULATE_NETWORK_DELAY
  196. #ifdef SIMULATE_NETWORK_DELAY
  197. /**
  198. * Disables the simulated network latency started by a previous call to
  199. * start_delay(). Datagrams will once again be visible as soon as they are
  200. * received.
  201. */
  202. void CConnectionRepository::
  203. stop_delay() {
  204. ReMutexHolder holder(_lock);
  205. #ifdef HAVE_NET
  206. _qcr.stop_delay();
  207. #endif // HAVE_NET
  208. #ifdef HAVE_OPENSSL
  209. if (_http_conn != (SocketStream *)NULL) {
  210. _http_conn->stop_delay();
  211. }
  212. #endif // HAVE_OPENSSL
  213. }
  214. #endif // SIMULATE_NETWORK_DELAY
  215. /**
  216. * Returns true if a new datagram is available, false otherwise. If the
  217. * return value is true, the new datagram may be retrieved via get_datagram(),
  218. * or preferably, with get_datagram_iterator() and get_msg_type().
  219. */
  220. bool CConnectionRepository::
  221. check_datagram() {
  222. ReMutexHolder holder(_lock);
  223. if (_simulated_disconnect) {
  224. return false;
  225. }
  226. #ifdef WANT_NATIVE_NET
  227. if(_native)
  228. _bdc.Flush();
  229. #endif //WANT_NATIVE_NET
  230. while (do_check_datagram()) {
  231. if (get_verbose()) {
  232. describe_message(nout, "RECV", _dg);
  233. }
  234. // Start breaking apart the datagram.
  235. _di = DatagramIterator(_dg);
  236. if (!_client_datagram) {
  237. unsigned char wc_cnt;
  238. wc_cnt = _di.get_uint8();
  239. _msg_channels.clear();
  240. for (unsigned char lp1 = 0; lp1 < wc_cnt; lp1++) {
  241. CHANNEL_TYPE schan = _di.get_uint64();
  242. _msg_channels.push_back(schan);
  243. }
  244. _msg_sender = _di.get_uint64();
  245. #ifdef HAVE_PYTHON
  246. // For now, we need to stuff this field onto the Python structure, to
  247. // support legacy code that expects to find it there.
  248. if (_python_repository != (PyObject *)NULL) {
  249. #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
  250. PyGILState_STATE gstate;
  251. gstate = PyGILState_Ensure();
  252. #endif
  253. PyObject *value = PyLong_FromUnsignedLongLong(_msg_sender);
  254. PyObject_SetAttrString(_python_repository, "msgSender", value);
  255. Py_DECREF(value);
  256. #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
  257. PyGILState_Release(gstate);
  258. #endif
  259. }
  260. #endif // HAVE_PYTHON
  261. }
  262. _msg_type = _di.get_uint16();
  263. // Is this a message that we can process directly?
  264. if (!_handle_datagrams_internally) {
  265. return true;
  266. }
  267. switch (_msg_type) {
  268. #ifdef HAVE_PYTHON
  269. case CLIENT_OBJECT_UPDATE_FIELD:
  270. case STATESERVER_OBJECT_UPDATE_FIELD:
  271. if (_handle_c_updates) {
  272. if (_has_owner_view) {
  273. if (!handle_update_field_owner()) {
  274. return false;
  275. }
  276. } else {
  277. if (!handle_update_field()) {
  278. return false;
  279. }
  280. }
  281. } else {
  282. // Let the caller (Python) deal with this update.
  283. return true;
  284. }
  285. break;
  286. #endif // HAVE_PYTHON
  287. default:
  288. // Some unknown message; let the caller deal with it.
  289. return true;
  290. }
  291. }
  292. // No datagrams available.
  293. return false;
  294. }
  295. /**
  296. * Returns true if the connection to the gameserver is established and still
  297. * good, false if we are not connected. A false value means either (a) we
  298. * never successfully connected, (b) we explicitly called disconnect(), or (c)
  299. * we were connected, but the connection was spontaneously lost.
  300. */
  301. bool CConnectionRepository::
  302. is_connected() {
  303. ReMutexHolder holder(_lock);
  304. #ifdef WANT_NATIVE_NET
  305. if(_native)
  306. return (_bdc.IsConnected());
  307. #endif
  308. #ifdef HAVE_NET
  309. if (_net_conn) {
  310. if (_qcm.reset_connection_available()) {
  311. PT(Connection) reset_connection;
  312. if (_qcm.get_reset_connection(reset_connection)) {
  313. _qcm.close_connection(reset_connection);
  314. if (reset_connection == _net_conn) {
  315. // Whoops, lost our connection.
  316. _net_conn = NULL;
  317. return false;
  318. }
  319. }
  320. }
  321. return true;
  322. }
  323. #endif // HAVE_NET
  324. #ifdef HAVE_OPENSSL
  325. if (_http_conn) {
  326. if (!_http_conn->is_closed()) {
  327. return true;
  328. }
  329. // Connection lost.
  330. delete _http_conn;
  331. _http_conn = NULL;
  332. }
  333. #endif // HAVE_OPENSSL
  334. return false;
  335. }
  336. /**
  337. * Queues the indicated datagram for sending to the server. It may not get
  338. * sent immediately if collect_tcp is in effect; call flush() to guarantee it
  339. * is sent now.
  340. */
  341. bool CConnectionRepository::
  342. send_datagram(const Datagram &dg) {
  343. ReMutexHolder holder(_lock);
  344. if (_simulated_disconnect) {
  345. distributed_cat.warning()
  346. << "Unable to send datagram during simulated disconnect.\n";
  347. return false;
  348. }
  349. if (get_verbose()) {
  350. describe_message(nout, "SEND", dg);
  351. }
  352. if (is_bundling_messages() && get_want_message_bundling()) {
  353. bundle_msg(dg);
  354. return true;
  355. }
  356. #ifdef WANT_NATIVE_NET
  357. if (_native) {
  358. bool result = _bdc.SendMessage(dg);
  359. if (!result && _bdc.IsConnected()) {
  360. #ifdef HAVE_PYTHON
  361. ostringstream s;
  362. #if PY_VERSION_HEX >= 0x03030000
  363. PyObject *exc_type = PyExc_ConnectionError;
  364. #else
  365. PyObject *exc_type = PyExc_OSError;
  366. #endif
  367. s << endl << "Error sending message: " << endl;
  368. dg.dump_hex(s);
  369. s << "Message data: " << dg.get_data() << endl;
  370. string message = s.str();
  371. PyErr_SetString(exc_type, message.c_str());
  372. #endif
  373. }
  374. return result;
  375. }
  376. #endif
  377. #ifdef HAVE_NET
  378. if (_net_conn) {
  379. _cw.send(dg, _net_conn);
  380. return true;
  381. }
  382. #endif // HAVE_NET
  383. #ifdef HAVE_OPENSSL
  384. if (_http_conn) {
  385. if (!_http_conn->send_datagram(dg)) {
  386. distributed_cat.warning()
  387. << "Could not send datagram.\n";
  388. return false;
  389. }
  390. return true;
  391. }
  392. #endif // HAVE_OPENSSL
  393. distributed_cat.warning()
  394. << "Unable to send datagram after connection is closed.\n";
  395. return false;
  396. }
  397. /**
  398. * Send a set of messages to the state server that will be processed
  399. * atomically. For instance, you can do a combined setLocation/setPos and
  400. * prevent race conditions where clients briefly get the setLocation but not
  401. * the setPos, because the state server hasn't processed the setPos yet
  402. */
  403. void CConnectionRepository::
  404. start_message_bundle() {
  405. ReMutexHolder holder(_lock);
  406. // store up network messages until sendMessageBundle is called all updates
  407. // in between must be sent from the same doId (updates must all affect the
  408. // same DistributedObject) it is an error to call this again before calling
  409. // sendMessageBundle
  410. if (get_verbose()) {
  411. nout << "CR::SEND:BUNDLE_START(" << _bundling_msgs << ")" << endl;
  412. }
  413. if (_bundling_msgs == 0) {
  414. _bundle_msgs.clear();
  415. }
  416. ++_bundling_msgs;
  417. }
  418. /**
  419. * Send network messages queued up since startMessageBundle was called.
  420. */
  421. void CConnectionRepository::
  422. send_message_bundle(unsigned int channel, unsigned int sender_channel) {
  423. ReMutexHolder holder(_lock);
  424. nassertv(_bundling_msgs);
  425. --_bundling_msgs;
  426. if (get_verbose()) {
  427. nout << "CR::SEND:BUNDLE_FINISH(" << _bundling_msgs << ")" << endl;
  428. }
  429. // if _bundling_msgs ref count is zero, send the bundle out
  430. if (_bundling_msgs == 0 && get_want_message_bundling()) {
  431. Datagram dg;
  432. // add server header (see PyDatagram.addServerHeader)
  433. dg.add_int8(1);
  434. dg.add_uint64(channel);
  435. dg.add_uint64(sender_channel);
  436. dg.add_uint16(STATESERVER_BOUNCE_MESSAGE);
  437. // add each bundled message
  438. BundledMsgVector::const_iterator bmi;
  439. for (bmi = _bundle_msgs.begin(); bmi != _bundle_msgs.end(); bmi++) {
  440. dg.add_string(*bmi);
  441. }
  442. send_datagram(dg);
  443. }
  444. }
  445. /**
  446. * throw out any msgs that have been queued up for message bundles
  447. */
  448. void CConnectionRepository::
  449. abandon_message_bundles() {
  450. ReMutexHolder holder(_lock);
  451. nassertv(is_bundling_messages());
  452. _bundling_msgs = 0;
  453. _bundle_msgs.clear();
  454. }
  455. /**
  456. *
  457. */
  458. void CConnectionRepository::
  459. bundle_msg(const Datagram &dg) {
  460. ReMutexHolder holder(_lock);
  461. nassertv(is_bundling_messages());
  462. _bundle_msgs.push_back(dg.get_message());
  463. }
  464. /**
  465. * Sends the most recently queued data if enough time has elapsed. This only
  466. * has meaning if set_collect_tcp() has been set to true.
  467. */
  468. bool CConnectionRepository::
  469. consider_flush() {
  470. ReMutexHolder holder(_lock);
  471. if (_simulated_disconnect) {
  472. return false;
  473. }
  474. #ifdef WANT_NATIVE_NET
  475. if(_native)
  476. return true; //Maybe we should just flush here for now?
  477. #endif
  478. #ifdef HAVE_NET
  479. if (_net_conn) {
  480. return _net_conn->consider_flush();
  481. }
  482. #endif // HAVE_NET
  483. #ifdef HAVE_OPENSSL
  484. if (_http_conn) {
  485. return _http_conn->consider_flush();
  486. }
  487. #endif // HAVE_OPENSSL
  488. return false;
  489. }
  490. /**
  491. * Sends the most recently queued data now. This only has meaning if
  492. * set_collect_tcp() has been set to true.
  493. */
  494. bool CConnectionRepository::
  495. flush() {
  496. ReMutexHolder holder(_lock);
  497. if (_simulated_disconnect) {
  498. return false;
  499. }
  500. #ifdef WANT_NATIVE_NET
  501. if(_native)
  502. return _bdc.Flush();
  503. #endif
  504. #ifdef HAVE_NET
  505. if (_net_conn) {
  506. return _net_conn->flush();
  507. }
  508. #endif // HAVE_NET
  509. #ifdef HAVE_OPENSSL
  510. if (_http_conn) {
  511. return _http_conn->flush();
  512. }
  513. #endif // HAVE_OPENSSL
  514. return false;
  515. }
  516. /**
  517. * Closes the connection to the server.
  518. */
  519. void CConnectionRepository::
  520. disconnect() {
  521. ReMutexHolder holder(_lock);
  522. #ifdef WANT_NATIVE_NET
  523. if(_native) {
  524. _bdc.Reset();
  525. _bdc.ClearAddresses();
  526. }
  527. #endif
  528. #ifdef HAVE_NET
  529. if (_net_conn) {
  530. _qcm.close_connection(_net_conn);
  531. _net_conn = NULL;
  532. }
  533. #endif // HAVE_NET
  534. #ifdef HAVE_OPENSSL
  535. if (_http_conn) {
  536. _http_conn->close();
  537. delete _http_conn;
  538. _http_conn = NULL;
  539. }
  540. #endif // HAVE_OPENSSL
  541. _simulated_disconnect = false;
  542. }
  543. /**
  544. * May be called at application shutdown to ensure all threads are cleaned up.
  545. */
  546. void CConnectionRepository::
  547. shutdown() {
  548. disconnect();
  549. #ifdef HAVE_NET
  550. _cw.shutdown();
  551. _qcr.shutdown();
  552. #endif // HAVE_NET
  553. }
  554. /**
  555. * The private implementation of check_datagram(), this gets one datagram if
  556. * it is available.
  557. */
  558. bool CConnectionRepository::
  559. do_check_datagram() {
  560. #ifdef WANT_NATIVE_NET
  561. if(_native) {
  562. return _bdc.GetMessage(_dg);
  563. }
  564. #endif
  565. #ifdef HAVE_NET
  566. if (_net_conn) {
  567. _net_conn->consider_flush();
  568. if (_qcr.get_overflow_flag()) {
  569. throw_event(get_overflow_event_name());
  570. _qcr.reset_overflow_flag();
  571. }
  572. return (_qcr.data_available() && _qcr.get_data(_dg));
  573. }
  574. #endif // HAVE_NET
  575. #ifdef HAVE_OPENSSL
  576. if (_http_conn) {
  577. _http_conn->consider_flush();
  578. return _http_conn->receive_datagram(_dg);
  579. }
  580. #endif // HAVE_OPENSSL
  581. return false;
  582. }
  583. /**
  584. * Directly handles an update message on a field. Python never touches the
  585. * datagram; it just gets its distributed method called with the appropriate
  586. * parameters. Returns true if everything is ok, false if there was an error
  587. * processing the field's update method.
  588. */
  589. bool CConnectionRepository::
  590. handle_update_field() {
  591. #ifdef HAVE_PYTHON
  592. #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
  593. PyGILState_STATE gstate;
  594. gstate = PyGILState_Ensure();
  595. #endif
  596. PStatTimer timer(_update_pcollector);
  597. unsigned int do_id = _di.get_uint32();
  598. if (_python_repository != (PyObject *)NULL)
  599. {
  600. PyObject *doId2do =
  601. PyObject_GetAttrString(_python_repository, "doId2do");
  602. nassertr(doId2do != NULL, false);
  603. #ifdef USE_PYTHON_2_2_OR_EARLIER
  604. PyObject *doId = PyInt_FromLong(do_id);
  605. #else
  606. PyObject *doId = PyLong_FromUnsignedLong(do_id);
  607. #endif
  608. PyObject *distobj = PyDict_GetItem(doId2do, doId);
  609. Py_DECREF(doId);
  610. Py_DECREF(doId2do);
  611. if (distobj != NULL) {
  612. PyObject *dclass_obj = PyObject_GetAttrString(distobj, "dclass");
  613. nassertr(dclass_obj != NULL, false);
  614. PyObject *dclass_this = PyObject_GetAttrString(dclass_obj, "this");
  615. Py_DECREF(dclass_obj);
  616. nassertr(dclass_this != NULL, false);
  617. DCClass *dclass = (DCClass *)PyLong_AsLong(dclass_this);
  618. Py_DECREF(dclass_this);
  619. // If in quiet zone mode, throw update away unless distobj has
  620. // 'neverDisable' attribute set to non-zero
  621. if (_in_quiet_zone) {
  622. PyObject *neverDisable = PyObject_GetAttrString(distobj, "neverDisable");
  623. nassertr(neverDisable != NULL, false);
  624. unsigned int cNeverDisable = PyLong_AsLong(neverDisable);
  625. if (!cNeverDisable) {
  626. // in quiet zone and distobj is disable-able drop update on the
  627. // floor
  628. #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
  629. PyGILState_Release(gstate);
  630. #endif
  631. return true;
  632. }
  633. }
  634. // It's a good idea to ensure the reference count to distobj is raised
  635. // while we call the update method--otherwise, the update method might
  636. // get into trouble if it tried to delete the object from the doId2do
  637. // map.
  638. Py_INCREF(distobj);
  639. dclass->receive_update(distobj, _di);
  640. Py_DECREF(distobj);
  641. if (PyErr_Occurred()) {
  642. #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
  643. PyGILState_Release(gstate);
  644. #endif
  645. return false;
  646. }
  647. }
  648. }
  649. #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
  650. PyGILState_Release(gstate);
  651. #endif
  652. #endif // HAVE_PYTHON
  653. return true;
  654. }
  655. /**
  656. * Directly handles an update message on a field. Supports 'owner' views of
  657. * objects, separate from 'visible' view, and forwards fields to the
  658. * appropriate view(s) based on DC flags. Python never touches the datagram;
  659. * it just gets its distributed method called with the appropriate parameters.
  660. * Returns true if everything is ok, false if there was an error processing
  661. * the field's update method.
  662. */
  663. bool CConnectionRepository::
  664. handle_update_field_owner() {
  665. #ifdef HAVE_PYTHON
  666. #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
  667. PyGILState_STATE gstate;
  668. gstate = PyGILState_Ensure();
  669. #endif
  670. PStatTimer timer(_update_pcollector);
  671. unsigned int do_id = _di.get_uint32();
  672. if (_python_repository != (PyObject *)NULL) {
  673. PyObject *doId2do =
  674. PyObject_GetAttrString(_python_repository, "doId2do");
  675. nassertr(doId2do != NULL, false);
  676. PyObject *doId2ownerView =
  677. PyObject_GetAttrString(_python_repository, "doId2ownerView");
  678. nassertr(doId2ownerView != NULL, false);
  679. #ifdef USE_PYTHON_2_2_OR_EARLIER
  680. PyObject *doId = PyInt_FromLong(do_id);
  681. #else
  682. PyObject *doId = PyLong_FromUnsignedLong(do_id);
  683. #endif
  684. // pass the update to the owner view first
  685. PyObject *distobjOV = PyDict_GetItem(doId2ownerView, doId);
  686. Py_DECREF(doId2ownerView);
  687. if (distobjOV != NULL) {
  688. PyObject *dclass_obj = PyObject_GetAttrString(distobjOV, "dclass");
  689. nassertr(dclass_obj != NULL, false);
  690. PyObject *dclass_this = PyObject_GetAttrString(dclass_obj, "this");
  691. Py_DECREF(dclass_obj);
  692. nassertr(dclass_this != NULL, false);
  693. DCClass *dclass = (DCClass *)PyLong_AsLong(dclass_this);
  694. Py_DECREF(dclass_this);
  695. // check if we should forward this update to the owner view
  696. DCPacker packer;
  697. packer.set_unpack_data(_di.get_remaining_bytes());
  698. int field_id = packer.raw_unpack_uint16();
  699. DCField *field = dclass->get_field_by_index(field_id);
  700. if (field->is_ownrecv()) {
  701. // It's a good idea to ensure the reference count to distobjOV is
  702. // raised while we call the update method--otherwise, the update
  703. // method might get into trouble if it tried to delete the object from
  704. // the doId2do map.
  705. Py_INCREF(distobjOV);
  706. // make a copy of the datagram iterator so that we can use the main
  707. // iterator for the non-owner update
  708. DatagramIterator _odi(_di);
  709. dclass->receive_update(distobjOV, _odi);
  710. Py_DECREF(distobjOV);
  711. if (PyErr_Occurred()) {
  712. #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
  713. PyGILState_Release(gstate);
  714. #endif
  715. return false;
  716. }
  717. }
  718. }
  719. // now pass the update to the visible view
  720. PyObject *distobj = PyDict_GetItem(doId2do, doId);
  721. Py_DECREF(doId);
  722. Py_DECREF(doId2do);
  723. if (distobj != NULL) {
  724. PyObject *dclass_obj = PyObject_GetAttrString(distobj, "dclass");
  725. nassertr(dclass_obj != NULL, false);
  726. PyObject *dclass_this = PyObject_GetAttrString(dclass_obj, "this");
  727. Py_DECREF(dclass_obj);
  728. nassertr(dclass_this != NULL, false);
  729. DCClass *dclass = (DCClass *)PyLong_AsLong(dclass_this);
  730. Py_DECREF(dclass_this);
  731. // check if we should forward this update to the owner view
  732. DCPacker packer;
  733. packer.set_unpack_data(_di.get_remaining_bytes());
  734. int field_id = packer.raw_unpack_uint16();
  735. DCField *field = dclass->get_field_by_index(field_id);
  736. if (true) {//field->is_broadcast()) {
  737. // It's a good idea to ensure the reference count to distobj is raised
  738. // while we call the update method--otherwise, the update method might
  739. // get into trouble if it tried to delete the object from the doId2do
  740. // map.
  741. Py_INCREF(distobj);
  742. dclass->receive_update(distobj, _di);
  743. Py_DECREF(distobj);
  744. if (PyErr_Occurred()) {
  745. #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
  746. PyGILState_Release(gstate);
  747. #endif
  748. return false;
  749. }
  750. }
  751. }
  752. }
  753. #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
  754. PyGILState_Release(gstate);
  755. #endif
  756. #endif // HAVE_PYTHON
  757. return true;
  758. }
  759. /**
  760. * Unpacks the message and reformats it for user consumption, writing a
  761. * description on the indicated output stream.
  762. */
  763. void CConnectionRepository::
  764. describe_message(ostream &out, const string &prefix,
  765. const Datagram &dg) const {
  766. DCPacker packer;
  767. packer.set_unpack_data(dg.get_message());
  768. CHANNEL_TYPE do_id;
  769. int msg_type;
  770. bool is_update = false;
  771. string full_prefix = "CR::" + prefix;
  772. if (!_client_datagram)
  773. {
  774. unsigned char mcnt = packer.raw_unpack_uint8();
  775. for( ;mcnt > 0; mcnt--)
  776. packer.RAW_UNPACK_CHANNEL(); // msg_channel
  777. packer.RAW_UNPACK_CHANNEL(); // msg_sender
  778. msg_type = packer.raw_unpack_uint16();
  779. is_update = (msg_type == STATESERVER_OBJECT_UPDATE_FIELD);
  780. } else {
  781. msg_type = packer.raw_unpack_uint16();
  782. is_update = (msg_type == CLIENT_OBJECT_UPDATE_FIELD);
  783. }
  784. if (!is_update) {
  785. // figure out the name of the message TODO: print out the arguments to the
  786. // message
  787. string msgName;
  788. #ifdef HAVE_PYTHON
  789. if (_python_repository != (PyObject *)NULL) {
  790. PyObject *msgId = PyLong_FromLong(msg_type);
  791. nassertv(msgId != NULL);
  792. #if PY_MAJOR_VERSION >= 3
  793. PyObject *methodName = PyUnicode_FromString("_getMsgName");
  794. #else
  795. PyObject *methodName = PyString_FromString("_getMsgName");
  796. #endif
  797. nassertv(methodName != NULL);
  798. PyObject *result = PyObject_CallMethodObjArgs(_python_repository, methodName,
  799. msgId, NULL);
  800. nassertv(result != NULL);
  801. #if PY_MAJOR_VERSION >= 3
  802. msgName += string(PyUnicode_AsUTF8(result));
  803. #else
  804. msgName += string(PyString_AsString(result));
  805. #endif
  806. Py_DECREF(methodName);
  807. Py_DECREF(msgId);
  808. Py_DECREF(result);
  809. }
  810. #endif
  811. if (msgName.length() == 0) {
  812. msgName += "unknown message ";
  813. msgName += msg_type;
  814. msgName += "\n";
  815. }
  816. out << full_prefix << ":" << msgName << "\n";
  817. dg.dump_hex(out, 2);
  818. } else {
  819. // It's an update message. Figure out what dclass the object is based on
  820. // its doId, so we can decode the rest of the message.
  821. do_id = packer.raw_unpack_uint32();
  822. DCClass *dclass = NULL;
  823. #ifdef HAVE_PYTHON
  824. if (_python_repository != (PyObject *)NULL) {
  825. PyObject *doId2do =
  826. PyObject_GetAttrString(_python_repository, "doId2do");
  827. nassertv(doId2do != NULL);
  828. #ifdef USE_PYTHON_2_2_OR_EARLIER
  829. PyObject *doId = PyInt_FromLong(do_id);
  830. #else
  831. PyObject *doId = PyLong_FromUnsignedLong(do_id);
  832. #endif
  833. PyObject *distobj = PyDict_GetItem(doId2do, doId);
  834. Py_DECREF(doId);
  835. Py_DECREF(doId2do);
  836. if (distobj != NULL) {
  837. PyObject *dclass_obj = PyObject_GetAttrString(distobj, "dclass");
  838. nassertv(dclass_obj != NULL);
  839. PyObject *dclass_this = PyObject_GetAttrString(dclass_obj, "this");
  840. Py_DECREF(dclass_obj);
  841. nassertv(dclass_this != NULL);
  842. dclass = (DCClass *)PyLong_AsLong(dclass_this);
  843. Py_DECREF(dclass_this);
  844. }
  845. }
  846. #endif // HAVE_PYTHON
  847. int field_id = packer.raw_unpack_uint16();
  848. if (dclass == (DCClass *)NULL) {
  849. out << full_prefix << "update for unknown object " << do_id
  850. << ", field " << field_id << "\n";
  851. } else {
  852. out << full_prefix <<
  853. ":" << dclass->get_name() << "(" << do_id << ").";
  854. DCField *field = dclass->get_field_by_index(field_id);
  855. if (field == (DCField *)NULL) {
  856. out << "unknown field " << field_id << "\n";
  857. } else {
  858. out << field->get_name();
  859. packer.begin_unpack(field);
  860. packer.unpack_and_format(out);
  861. packer.end_unpack();
  862. out << "\n";
  863. }
  864. }
  865. }
  866. }