cConnectionRepository.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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. return _bdc.SendMessage(dg);
  359. #endif
  360. #ifdef HAVE_NET
  361. if (_net_conn) {
  362. _cw.send(dg, _net_conn);
  363. return true;
  364. }
  365. #endif // HAVE_NET
  366. #ifdef HAVE_OPENSSL
  367. if (_http_conn) {
  368. if (!_http_conn->send_datagram(dg)) {
  369. distributed_cat.warning()
  370. << "Could not send datagram.\n";
  371. return false;
  372. }
  373. return true;
  374. }
  375. #endif // HAVE_OPENSSL
  376. distributed_cat.warning()
  377. << "Unable to send datagram after connection is closed.\n";
  378. return false;
  379. }
  380. /**
  381. * Send a set of messages to the state server that will be processed
  382. * atomically. For instance, you can do a combined setLocation/setPos and
  383. * prevent race conditions where clients briefly get the setLocation but not
  384. * the setPos, because the state server hasn't processed the setPos yet
  385. */
  386. void CConnectionRepository::
  387. start_message_bundle() {
  388. ReMutexHolder holder(_lock);
  389. // store up network messages until sendMessageBundle is called all updates
  390. // in between must be sent from the same doId (updates must all affect the
  391. // same DistributedObject) it is an error to call this again before calling
  392. // sendMessageBundle
  393. if (get_verbose()) {
  394. nout << "CR::SEND:BUNDLE_START(" << _bundling_msgs << ")" << endl;
  395. }
  396. if (_bundling_msgs == 0) {
  397. _bundle_msgs.clear();
  398. }
  399. ++_bundling_msgs;
  400. }
  401. /**
  402. * Send network messages queued up since startMessageBundle was called.
  403. */
  404. void CConnectionRepository::
  405. send_message_bundle(unsigned int channel, unsigned int sender_channel) {
  406. ReMutexHolder holder(_lock);
  407. nassertv(_bundling_msgs);
  408. --_bundling_msgs;
  409. if (get_verbose()) {
  410. nout << "CR::SEND:BUNDLE_FINISH(" << _bundling_msgs << ")" << endl;
  411. }
  412. // if _bundling_msgs ref count is zero, send the bundle out
  413. if (_bundling_msgs == 0 && get_want_message_bundling()) {
  414. Datagram dg;
  415. // add server header (see PyDatagram.addServerHeader)
  416. dg.add_int8(1);
  417. dg.add_uint64(channel);
  418. dg.add_uint64(sender_channel);
  419. dg.add_uint16(STATESERVER_BOUNCE_MESSAGE);
  420. // add each bundled message
  421. BundledMsgVector::const_iterator bmi;
  422. for (bmi = _bundle_msgs.begin(); bmi != _bundle_msgs.end(); bmi++) {
  423. dg.add_string(*bmi);
  424. }
  425. send_datagram(dg);
  426. }
  427. }
  428. /**
  429. * throw out any msgs that have been queued up for message bundles
  430. */
  431. void CConnectionRepository::
  432. abandon_message_bundles() {
  433. ReMutexHolder holder(_lock);
  434. nassertv(is_bundling_messages());
  435. _bundling_msgs = 0;
  436. _bundle_msgs.clear();
  437. }
  438. /**
  439. *
  440. */
  441. void CConnectionRepository::
  442. bundle_msg(const Datagram &dg) {
  443. ReMutexHolder holder(_lock);
  444. nassertv(is_bundling_messages());
  445. _bundle_msgs.push_back(dg.get_message());
  446. }
  447. /**
  448. * Sends the most recently queued data if enough time has elapsed. This only
  449. * has meaning if set_collect_tcp() has been set to true.
  450. */
  451. bool CConnectionRepository::
  452. consider_flush() {
  453. ReMutexHolder holder(_lock);
  454. if (_simulated_disconnect) {
  455. return false;
  456. }
  457. #ifdef WANT_NATIVE_NET
  458. if(_native)
  459. return true; //Maybe we should just flush here for now?
  460. #endif
  461. #ifdef HAVE_NET
  462. if (_net_conn) {
  463. return _net_conn->consider_flush();
  464. }
  465. #endif // HAVE_NET
  466. #ifdef HAVE_OPENSSL
  467. if (_http_conn) {
  468. return _http_conn->consider_flush();
  469. }
  470. #endif // HAVE_OPENSSL
  471. return false;
  472. }
  473. /**
  474. * Sends the most recently queued data now. This only has meaning if
  475. * set_collect_tcp() has been set to true.
  476. */
  477. bool CConnectionRepository::
  478. flush() {
  479. ReMutexHolder holder(_lock);
  480. if (_simulated_disconnect) {
  481. return false;
  482. }
  483. #ifdef WANT_NATIVE_NET
  484. if(_native)
  485. return _bdc.Flush();
  486. #endif
  487. #ifdef HAVE_NET
  488. if (_net_conn) {
  489. return _net_conn->flush();
  490. }
  491. #endif // HAVE_NET
  492. #ifdef HAVE_OPENSSL
  493. if (_http_conn) {
  494. return _http_conn->flush();
  495. }
  496. #endif // HAVE_OPENSSL
  497. return false;
  498. }
  499. /**
  500. * Closes the connection to the server.
  501. */
  502. void CConnectionRepository::
  503. disconnect() {
  504. ReMutexHolder holder(_lock);
  505. #ifdef WANT_NATIVE_NET
  506. if(_native) {
  507. _bdc.Reset();
  508. _bdc.ClearAddresses();
  509. }
  510. #endif
  511. #ifdef HAVE_NET
  512. if (_net_conn) {
  513. _qcm.close_connection(_net_conn);
  514. _net_conn = NULL;
  515. }
  516. #endif // HAVE_NET
  517. #ifdef HAVE_OPENSSL
  518. if (_http_conn) {
  519. _http_conn->close();
  520. delete _http_conn;
  521. _http_conn = NULL;
  522. }
  523. #endif // HAVE_OPENSSL
  524. _simulated_disconnect = false;
  525. }
  526. /**
  527. * May be called at application shutdown to ensure all threads are cleaned up.
  528. */
  529. void CConnectionRepository::
  530. shutdown() {
  531. disconnect();
  532. #ifdef HAVE_NET
  533. _cw.shutdown();
  534. _qcr.shutdown();
  535. #endif // HAVE_NET
  536. }
  537. /**
  538. * The private implementation of check_datagram(), this gets one datagram if
  539. * it is available.
  540. */
  541. bool CConnectionRepository::
  542. do_check_datagram() {
  543. #ifdef WANT_NATIVE_NET
  544. if(_native) {
  545. return _bdc.GetMessage(_dg);
  546. }
  547. #endif
  548. #ifdef HAVE_NET
  549. if (_net_conn) {
  550. _net_conn->consider_flush();
  551. if (_qcr.get_overflow_flag()) {
  552. throw_event(get_overflow_event_name());
  553. _qcr.reset_overflow_flag();
  554. }
  555. return (_qcr.data_available() && _qcr.get_data(_dg));
  556. }
  557. #endif // HAVE_NET
  558. #ifdef HAVE_OPENSSL
  559. if (_http_conn) {
  560. _http_conn->consider_flush();
  561. return _http_conn->receive_datagram(_dg);
  562. }
  563. #endif // HAVE_OPENSSL
  564. return false;
  565. }
  566. /**
  567. * Directly handles an update message on a field. Python never touches the
  568. * datagram; it just gets its distributed method called with the appropriate
  569. * parameters. Returns true if everything is ok, false if there was an error
  570. * processing the field's update method.
  571. */
  572. bool CConnectionRepository::
  573. handle_update_field() {
  574. #ifdef HAVE_PYTHON
  575. #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
  576. PyGILState_STATE gstate;
  577. gstate = PyGILState_Ensure();
  578. #endif
  579. PStatTimer timer(_update_pcollector);
  580. unsigned int do_id = _di.get_uint32();
  581. if (_python_repository != (PyObject *)NULL)
  582. {
  583. PyObject *doId2do =
  584. PyObject_GetAttrString(_python_repository, "doId2do");
  585. nassertr(doId2do != NULL, false);
  586. #ifdef USE_PYTHON_2_2_OR_EARLIER
  587. PyObject *doId = PyInt_FromLong(do_id);
  588. #else
  589. PyObject *doId = PyLong_FromUnsignedLong(do_id);
  590. #endif
  591. PyObject *distobj = PyDict_GetItem(doId2do, doId);
  592. Py_DECREF(doId);
  593. Py_DECREF(doId2do);
  594. if (distobj != NULL) {
  595. PyObject *dclass_obj = PyObject_GetAttrString(distobj, "dclass");
  596. nassertr(dclass_obj != NULL, false);
  597. PyObject *dclass_this = PyObject_GetAttrString(dclass_obj, "this");
  598. Py_DECREF(dclass_obj);
  599. nassertr(dclass_this != NULL, false);
  600. DCClass *dclass = (DCClass *)PyLong_AsLong(dclass_this);
  601. Py_DECREF(dclass_this);
  602. // If in quiet zone mode, throw update away unless distobj has
  603. // 'neverDisable' attribute set to non-zero
  604. if (_in_quiet_zone) {
  605. PyObject *neverDisable = PyObject_GetAttrString(distobj, "neverDisable");
  606. nassertr(neverDisable != NULL, false);
  607. unsigned int cNeverDisable = PyLong_AsLong(neverDisable);
  608. if (!cNeverDisable) {
  609. // in quiet zone and distobj is disable-able drop update on the
  610. // floor
  611. #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
  612. PyGILState_Release(gstate);
  613. #endif
  614. return true;
  615. }
  616. }
  617. // It's a good idea to ensure the reference count to distobj is raised
  618. // while we call the update method--otherwise, the update method might
  619. // get into trouble if it tried to delete the object from the doId2do
  620. // map.
  621. Py_INCREF(distobj);
  622. dclass->receive_update(distobj, _di);
  623. Py_DECREF(distobj);
  624. if (PyErr_Occurred()) {
  625. #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
  626. PyGILState_Release(gstate);
  627. #endif
  628. return false;
  629. }
  630. }
  631. }
  632. #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
  633. PyGILState_Release(gstate);
  634. #endif
  635. #endif // HAVE_PYTHON
  636. return true;
  637. }
  638. /**
  639. * Directly handles an update message on a field. Supports 'owner' views of
  640. * objects, separate from 'visible' view, and forwards fields to the
  641. * appropriate view(s) based on DC flags. Python never touches the datagram;
  642. * it just gets its distributed method called with the appropriate parameters.
  643. * Returns true if everything is ok, false if there was an error processing
  644. * the field's update method.
  645. */
  646. bool CConnectionRepository::
  647. handle_update_field_owner() {
  648. #ifdef HAVE_PYTHON
  649. #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
  650. PyGILState_STATE gstate;
  651. gstate = PyGILState_Ensure();
  652. #endif
  653. PStatTimer timer(_update_pcollector);
  654. unsigned int do_id = _di.get_uint32();
  655. if (_python_repository != (PyObject *)NULL) {
  656. PyObject *doId2do =
  657. PyObject_GetAttrString(_python_repository, "doId2do");
  658. nassertr(doId2do != NULL, false);
  659. PyObject *doId2ownerView =
  660. PyObject_GetAttrString(_python_repository, "doId2ownerView");
  661. nassertr(doId2ownerView != NULL, false);
  662. #ifdef USE_PYTHON_2_2_OR_EARLIER
  663. PyObject *doId = PyInt_FromLong(do_id);
  664. #else
  665. PyObject *doId = PyLong_FromUnsignedLong(do_id);
  666. #endif
  667. // pass the update to the owner view first
  668. PyObject *distobjOV = PyDict_GetItem(doId2ownerView, doId);
  669. Py_DECREF(doId2ownerView);
  670. if (distobjOV != NULL) {
  671. PyObject *dclass_obj = PyObject_GetAttrString(distobjOV, "dclass");
  672. nassertr(dclass_obj != NULL, false);
  673. PyObject *dclass_this = PyObject_GetAttrString(dclass_obj, "this");
  674. Py_DECREF(dclass_obj);
  675. nassertr(dclass_this != NULL, false);
  676. DCClass *dclass = (DCClass *)PyLong_AsLong(dclass_this);
  677. Py_DECREF(dclass_this);
  678. // check if we should forward this update to the owner view
  679. DCPacker packer;
  680. packer.set_unpack_data(_di.get_remaining_bytes());
  681. int field_id = packer.raw_unpack_uint16();
  682. DCField *field = dclass->get_field_by_index(field_id);
  683. if (field->is_ownrecv()) {
  684. // It's a good idea to ensure the reference count to distobjOV is
  685. // raised while we call the update method--otherwise, the update
  686. // method might get into trouble if it tried to delete the object from
  687. // the doId2do map.
  688. Py_INCREF(distobjOV);
  689. // make a copy of the datagram iterator so that we can use the main
  690. // iterator for the non-owner update
  691. DatagramIterator _odi(_di);
  692. dclass->receive_update(distobjOV, _odi);
  693. Py_DECREF(distobjOV);
  694. if (PyErr_Occurred()) {
  695. #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
  696. PyGILState_Release(gstate);
  697. #endif
  698. return false;
  699. }
  700. }
  701. }
  702. // now pass the update to the visible view
  703. PyObject *distobj = PyDict_GetItem(doId2do, doId);
  704. Py_DECREF(doId);
  705. Py_DECREF(doId2do);
  706. if (distobj != NULL) {
  707. PyObject *dclass_obj = PyObject_GetAttrString(distobj, "dclass");
  708. nassertr(dclass_obj != NULL, false);
  709. PyObject *dclass_this = PyObject_GetAttrString(dclass_obj, "this");
  710. Py_DECREF(dclass_obj);
  711. nassertr(dclass_this != NULL, false);
  712. DCClass *dclass = (DCClass *)PyLong_AsLong(dclass_this);
  713. Py_DECREF(dclass_this);
  714. // check if we should forward this update to the owner view
  715. DCPacker packer;
  716. packer.set_unpack_data(_di.get_remaining_bytes());
  717. int field_id = packer.raw_unpack_uint16();
  718. DCField *field = dclass->get_field_by_index(field_id);
  719. if (true) {//field->is_broadcast()) {
  720. // It's a good idea to ensure the reference count to distobj is raised
  721. // while we call the update method--otherwise, the update method might
  722. // get into trouble if it tried to delete the object from the doId2do
  723. // map.
  724. Py_INCREF(distobj);
  725. dclass->receive_update(distobj, _di);
  726. Py_DECREF(distobj);
  727. if (PyErr_Occurred()) {
  728. #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
  729. PyGILState_Release(gstate);
  730. #endif
  731. return false;
  732. }
  733. }
  734. }
  735. }
  736. #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
  737. PyGILState_Release(gstate);
  738. #endif
  739. #endif // HAVE_PYTHON
  740. return true;
  741. }
  742. /**
  743. * Unpacks the message and reformats it for user consumption, writing a
  744. * description on the indicated output stream.
  745. */
  746. void CConnectionRepository::
  747. describe_message(ostream &out, const string &prefix,
  748. const Datagram &dg) const {
  749. DCPacker packer;
  750. packer.set_unpack_data(dg.get_message());
  751. CHANNEL_TYPE do_id;
  752. int msg_type;
  753. bool is_update = false;
  754. string full_prefix = "CR::" + prefix;
  755. if (!_client_datagram)
  756. {
  757. unsigned char mcnt = packer.raw_unpack_uint8();
  758. for( ;mcnt > 0; mcnt--)
  759. packer.RAW_UNPACK_CHANNEL(); // msg_channel
  760. packer.RAW_UNPACK_CHANNEL(); // msg_sender
  761. msg_type = packer.raw_unpack_uint16();
  762. is_update = (msg_type == STATESERVER_OBJECT_UPDATE_FIELD);
  763. } else {
  764. msg_type = packer.raw_unpack_uint16();
  765. is_update = (msg_type == CLIENT_OBJECT_UPDATE_FIELD);
  766. }
  767. if (!is_update) {
  768. // figure out the name of the message TODO: print out the arguments to the
  769. // message
  770. string msgName;
  771. #ifdef HAVE_PYTHON
  772. if (_python_repository != (PyObject *)NULL) {
  773. PyObject *msgId = PyLong_FromLong(msg_type);
  774. nassertv(msgId != NULL);
  775. #if PY_MAJOR_VERSION >= 3
  776. PyObject *methodName = PyUnicode_FromString("_getMsgName");
  777. #else
  778. PyObject *methodName = PyString_FromString("_getMsgName");
  779. #endif
  780. nassertv(methodName != NULL);
  781. PyObject *result = PyObject_CallMethodObjArgs(_python_repository, methodName,
  782. msgId, NULL);
  783. nassertv(result != NULL);
  784. #if PY_MAJOR_VERSION >= 3
  785. msgName += string(PyUnicode_AsUTF8(result));
  786. #else
  787. msgName += string(PyString_AsString(result));
  788. #endif
  789. Py_DECREF(methodName);
  790. Py_DECREF(msgId);
  791. Py_DECREF(result);
  792. }
  793. #endif
  794. if (msgName.length() == 0) {
  795. msgName += "unknown message ";
  796. msgName += msg_type;
  797. msgName += "\n";
  798. }
  799. out << full_prefix << ":" << msgName << "\n";
  800. dg.dump_hex(out, 2);
  801. } else {
  802. // It's an update message. Figure out what dclass the object is based on
  803. // its doId, so we can decode the rest of the message.
  804. do_id = packer.raw_unpack_uint32();
  805. DCClass *dclass = NULL;
  806. #ifdef HAVE_PYTHON
  807. if (_python_repository != (PyObject *)NULL) {
  808. PyObject *doId2do =
  809. PyObject_GetAttrString(_python_repository, "doId2do");
  810. nassertv(doId2do != NULL);
  811. #ifdef USE_PYTHON_2_2_OR_EARLIER
  812. PyObject *doId = PyInt_FromLong(do_id);
  813. #else
  814. PyObject *doId = PyLong_FromUnsignedLong(do_id);
  815. #endif
  816. PyObject *distobj = PyDict_GetItem(doId2do, doId);
  817. Py_DECREF(doId);
  818. Py_DECREF(doId2do);
  819. if (distobj != NULL) {
  820. PyObject *dclass_obj = PyObject_GetAttrString(distobj, "dclass");
  821. nassertv(dclass_obj != NULL);
  822. PyObject *dclass_this = PyObject_GetAttrString(dclass_obj, "this");
  823. Py_DECREF(dclass_obj);
  824. nassertv(dclass_this != NULL);
  825. dclass = (DCClass *)PyLong_AsLong(dclass_this);
  826. Py_DECREF(dclass_this);
  827. }
  828. }
  829. #endif // HAVE_PYTHON
  830. int field_id = packer.raw_unpack_uint16();
  831. if (dclass == (DCClass *)NULL) {
  832. out << full_prefix << "update for unknown object " << do_id
  833. << ", field " << field_id << "\n";
  834. } else {
  835. out << full_prefix <<
  836. ":" << dclass->get_name() << "(" << do_id << ").";
  837. DCField *field = dclass->get_field_by_index(field_id);
  838. if (field == (DCField *)NULL) {
  839. out << "unknown field " << field_id << "\n";
  840. } else {
  841. out << field->get_name();
  842. packer.begin_unpack(field);
  843. packer.unpack_and_format(out);
  844. packer.end_unpack();
  845. out << "\n";
  846. }
  847. }
  848. }
  849. }