enet.pyx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. # enet.pyx
  2. #
  3. # DESCRIPTION
  4. #
  5. # Python ENET Wrapper implemented in pyrexc.
  6. #
  7. # RATIONALE
  8. #
  9. # Ling Lo's pyenet.c module had a problem with dropping a connection after
  10. # a short amount of time. Having seen other Python <-> C interfaces
  11. # defined in pyrexc, I decided it probably has a much better time of
  12. # surviving time.
  13. #
  14. # Hopefully no one will be too mad with the option of choice?
  15. #
  16. # LICENSE
  17. #
  18. # Copyright (C) 2003, Scott Robinson (scott@tranzoa.com)
  19. #
  20. # Redistribution and use in source and binary forms, with or without
  21. # modification, are permitted provided that the following conditions are
  22. # met:
  23. #
  24. # Redistributions of source code must retain the above copyright notice,
  25. # this list of conditions and the following disclaimer.
  26. #
  27. # Redistributions in binary form must reproduce the above copyright
  28. # notice, this list of conditions and the following disclaimer in the
  29. # documentation and/or other materials provided with the distribution.
  30. #
  31. # The names of its contributors may not be used to endorse or promote
  32. # products derived from this software without specific prior written
  33. # permission.
  34. #
  35. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  36. # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  37. # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  38. # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  39. # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  40. # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  41. # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  42. # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  43. # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  44. # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  45. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. #
  47. # CHANGELOG
  48. #
  49. # Sat Nov 1 00:36:02 PST 2003 Scott Robinson <scott@tranzoa.com>
  50. # Began developing test interface after a day of coding...
  51. #
  52. # Mon Nov 3 08:46:33 PST 2003 Scott Robinson <scott@tranzoa.com>
  53. # Added documentation to all classes, functions, and attributes.
  54. # While adding documentation, added accessors to a few more attributes.
  55. # Cleaned up a few methods to match proper pyrex behavior.
  56. # Removed a few, and added a couple of obvious todos for the future.
  57. # Fixed Address.__getattr__ extra \0s in the case .host.
  58. #
  59. # Fri Feb 13 18:18:04 PST 2004 Scott Robinson <scott@tranzoa.com>
  60. # Added Socket class for use with select and poll.
  61. #
  62. import atexit
  63. # SECTION
  64. # C declarations and definitions for the interface.
  65. cdef extern from "Python.h" :
  66. object PyBuffer_FromMemory (void *ptr, int size)
  67. object PyString_FromString (char *v)
  68. object PyString_FromStringAndSize (char *v, int len)
  69. cdef extern from "enet/types.h" :
  70. ctypedef unsigned char enet_uint8
  71. ctypedef unsigned short enet_uint16
  72. ctypedef unsigned int enet_uint32
  73. ctypedef unsigned int size_t
  74. cdef extern from "enet/enet.h" :
  75. cdef enum :
  76. ENET_HOST_ANY = 0
  77. # TODO: Handle Windows situation.
  78. ctypedef int ENetSocket
  79. ctypedef struct ENetAddress :
  80. enet_uint32 host
  81. enet_uint16 port
  82. ctypedef enum ENetPacketFlag :
  83. ENET_PACKET_FLAG_RELIABLE = (1 << 0)
  84. ctypedef struct ENetPacket :
  85. size_t referenceCount
  86. enet_uint32 flags
  87. enet_uint8 *data
  88. size_t dataLength
  89. ctypedef enum ENetPeerState :
  90. ENET_PEER_STATE_DISCONNECTED = 0
  91. ENET_PEER_STATE_CONNECTING = 1
  92. ENET_PEER_STATE_ACKNOWLEDGING_CONNECT = 2
  93. ENET_PEER_STATE_CONNECTED = 3
  94. ENET_PEER_STATE_DISCONNECTING = 4
  95. ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT = 5
  96. ENET_PEER_STATE_ZOMBIE = 6
  97. cdef enum :
  98. ENET_PEER_PACKET_LOSS_SCALE = (1 << 16)
  99. ctypedef struct ENetPeer :
  100. ENetAddress address
  101. ENetPeerState state
  102. enet_uint32 packetLoss
  103. enet_uint32 packetThrottleAcceleration
  104. enet_uint32 packetThrottleDeceleration
  105. enet_uint32 packetThrottleInterval
  106. enet_uint32 roundTripTime
  107. ctypedef struct ENetHost :
  108. ENetSocket socket
  109. ENetAddress address
  110. ctypedef enum ENetEventType :
  111. ENET_EVENT_TYPE_NONE = 0
  112. ENET_EVENT_TYPE_CONNECT = 1
  113. ENET_EVENT_TYPE_DISCONNECT = 2
  114. ENET_EVENT_TYPE_RECEIVE = 3
  115. ctypedef struct ENetEvent :
  116. ENetEventType type
  117. ENetPeer *peer
  118. enet_uint8 channelID
  119. ENetPacket *packet
  120. int enet_initialize ()
  121. void enet_deinitialize ()
  122. int enet_address_set_host (ENetAddress *address, char *hostName)
  123. int enet_address_get_host (ENetAddress *address, char *hostName, size_t nameLength)
  124. ENetPacket * enet_packet_create (void *dataContents, size_t dataLength, enet_uint32 flags)
  125. void enet_packet_destroy (ENetPacket *packet)
  126. int enet_packet_resize (ENetPacket *packet, size_t dataLength)
  127. ENetHost * enet_host_create (ENetAddress *address, size_t peerCount, enet_uint32 incomingBandwidth, enet_uint32 outgoingBandwidth)
  128. void enet_host_destroy (ENetHost *host)
  129. ENetPeer * enet_host_connect (ENetHost *host, ENetAddress *address, size_t channelCount)
  130. int enet_host_service (ENetHost *host, ENetEvent *event, enet_uint32 timeout)
  131. void enet_host_flush (ENetHost *host)
  132. void enet_host_broadcast (ENetHost *host, enet_uint8 channelID, ENetPacket *packet)
  133. void enet_host_bandwidth_limit (ENetHost *host, enet_uint32 incomingBandwidth, enet_uint32 outgoingBandwidth)
  134. int enet_peer_send (ENetPeer *peer, enet_uint8 channelID, ENetPacket *packet)
  135. ENetPacket * enet_peer_receive (ENetPeer *peer, enet_uint8 channelID)
  136. void enet_peer_ping (ENetPeer *peer)
  137. void enet_peer_reset (ENetPeer *peer)
  138. void enet_peer_disconnect (ENetPeer *peer)
  139. void enet_peer_disconnect_now (ENetPeer *peer)
  140. void enet_peer_throttle_configure (ENetPeer *peer, enet_uint32 interval, enet_uint32 acceleration, enet_uint32 deacceleration)
  141. # SECTION
  142. # Enumerations and constants.
  143. HOST_ANY = ENET_HOST_ANY
  144. PACKET_FLAG_RELIABLE = ENET_PACKET_FLAG_RELIABLE
  145. PEER_STATE_DISCONNECT = ENET_PEER_STATE_DISCONNECTED
  146. PEER_STATE_CONNECTING = ENET_PEER_STATE_CONNECTING
  147. PEER_STATE_ACKNOWLEDGING_CONNECT = ENET_PEER_STATE_ACKNOWLEDGING_CONNECT
  148. PEER_STATE_CONNECTED = ENET_PEER_STATE_CONNECTED
  149. PEER_STATE_DISCONNECTING = ENET_PEER_STATE_DISCONNECTING
  150. PEER_STATE_ACKNOWLEDGING_DISCONNECT = ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT
  151. PEER_STATE_ZOMBIE = ENET_PEER_STATE_ZOMBIE
  152. PEER_PACKET_LOSS_SCALE = ENET_PEER_PACKET_LOSS_SCALE
  153. EVENT_TYPE_NONE = ENET_EVENT_TYPE_NONE
  154. EVENT_TYPE_CONNECT = ENET_EVENT_TYPE_CONNECT
  155. EVENT_TYPE_DISCONNECT = ENET_EVENT_TYPE_DISCONNECT
  156. EVENT_TYPE_RECEIVE = ENET_EVENT_TYPE_RECEIVE
  157. # SECTION
  158. # Python exposed class definitions.
  159. cdef class Socket :
  160. """Socket (int socket)
  161. DESCRIPTION
  162. An ENet socket.
  163. Can be used with select and poll."""
  164. cdef ENetSocket _enet_socket
  165. def fileno (self) :
  166. return self._enet_socket
  167. cdef class Address :
  168. """Address (str address, int port)
  169. ATTRIBUTES
  170. str host Hostname referred to by the Address.
  171. int port Port referred to by the Address.
  172. DESCRIPTION
  173. An ENet address and port pair.
  174. When instantiated, performs a resolution upon 'address'. However, if 'address' is None, enet.HOST_ANY is assumed."""
  175. cdef ENetAddress _enet_address
  176. def __init__ (self, address, port) :
  177. self.host = address
  178. self.port = port
  179. def __getattr__ (self, name) :
  180. if name == "host" :
  181. if self._enet_address.host == ENET_HOST_ANY :
  182. return "*"
  183. elif self._enet_address.host :
  184. maxhostname = 257 # We'll follow Solaris' standard.
  185. host = PyString_FromStringAndSize (NULL, maxhostname)
  186. if enet_address_get_host (&self._enet_address, host, maxhostname) :
  187. raise IOError ("Resolution failure!")
  188. return PyString_FromString (host)
  189. else :
  190. assert (not ENET_HOST_ANY)
  191. elif name == "port" :
  192. return self._enet_address.port
  193. else :
  194. return AttributeError ("Address object has no attribute '" + name + "'")
  195. def __setattr__ (self, name, value) :
  196. if name == "host" :
  197. if not value or value == "*":
  198. self._enet_address.host = ENET_HOST_ANY
  199. else :
  200. if enet_address_set_host (&self._enet_address, value) :
  201. raise IOError ("Resolution failure!")
  202. elif name == "port" :
  203. self._enet_address.port = value
  204. else :
  205. return AttributeError ("Address object has no attribute '" + name + "'")
  206. def __str__ (self) :
  207. return "%s:%u" % (self.host, self.port)
  208. cdef class Packet :
  209. """Packet ([dataContents, int flags])
  210. ATTRIBUTES
  211. str data Contains the data for the packet.
  212. int flags Flags modifying delivery of the Packet:
  213. enet.PACKET_FLAG_RELIABLE Packet must be received by the target peer and resend attempts should be made until the packet is delivered.
  214. DESCRIPTION
  215. An ENet data packet that may be sent to or received from a peer."""
  216. cdef ENetPacket *_enet_packet
  217. def __init__ (self, char *dataContents = "", flags = 0) :
  218. if dataContents or flags :
  219. self._enet_packet = enet_packet_create (dataContents, len (dataContents), flags)
  220. if not self._enet_packet :
  221. raise MemoryError ("Unable to create packet structure!")
  222. def __dealloc__ (self) :
  223. if self._enet_packet and not self._enet_packet.referenceCount :
  224. # WARNING: referenceCount is an internal structure. Is there a better way of doing this?
  225. enet_packet_destroy (self._enet_packet)
  226. def __getattr__ (self, name) :
  227. if self._enet_packet :
  228. if name == "flags" :
  229. return self._enet_packet.flags
  230. elif name == "data" :
  231. # TODO: Find out why the PyBuffer interface is cutting off data!
  232. #return PyBuffer_FromMemory (self._enet_packet.data, self._enet_packet.dataLength)
  233. return PyString_FromStringAndSize (<char *> self._enet_packet.data, self._enet_packet.dataLength)
  234. elif name == "dataLength" :
  235. return len (self.data)
  236. else :
  237. raise AttributeError ("Packet object has no attribute '" + name + "'")
  238. else :
  239. raise MemoryError ("Empty Packet object accessed!")
  240. cdef class Peer :
  241. """Peer ()
  242. ATTRIBUTES
  243. Address address
  244. int state The peer's current state.
  245. enet.PEER_STATE_DISCONNECT
  246. .PEER_STATE_CONNECTING
  247. .PEER_STATE_CONNECTED
  248. .PEER_STATE_DISCONNECTING
  249. .PEER_STATE_ACKNOWLEDGING_DISCONNECT
  250. .PEER_STATE_ZOMBIE
  251. int packetLoss Mean packet loss of reliable packets as a ratio with respect to the constant enet.PEER_PACKET_LOSS_SCALE.
  252. int packetThrottleAcceleration
  253. int packetThrottleDeceleration
  254. int packetThrottleInterval
  255. int roundTripTime Mean round trip time (RTT), in milliseconds, between sending a reliable packet and receiving its acknowledgement.
  256. DESCRIPTION
  257. An ENet peer which data packets may be sent or received from.
  258. This class should never be instantiated directly, but rather via enet.Host.connect or enet.Event.Peer."""
  259. cdef ENetPeer *_enet_peer
  260. def send (self, channelID, Packet packet) :
  261. """send (int channelID, Packet packet)
  262. Queues a packet to be sent."""
  263. if self._enet_peer and packet._enet_packet :
  264. return enet_peer_send (self._enet_peer, channelID, packet._enet_packet)
  265. def receive (self, channelID) :
  266. """receive (int channelID)
  267. Attempts to dequeue any incoming queued packet."""
  268. if self._enet_peer :
  269. packet = Packet ()
  270. (<Packet> packet)._enet_packet = enet_peer_receive (self._enet_peer, channelID)
  271. if packet._enet_packet :
  272. return packet
  273. else :
  274. return None
  275. def reset (self) :
  276. """reset ()
  277. Forcefully disconnects a peer."""
  278. if self._enet_peer :
  279. enet_peer_reset (self._enet_peer)
  280. def ping (self) :
  281. """ping ()
  282. Sends a ping request to a peer."""
  283. if self._enet_peer :
  284. enet_peer_ping (self._enet_peer)
  285. def disconnect (self) :
  286. """disconnect ()
  287. Request a disconnection from a peer."""
  288. if self._enet_peer :
  289. enet_peer_disconnect (self._enet_peer)
  290. def __getattr__ (self, name) :
  291. if self._enet_peer :
  292. if name == "address" :
  293. address = Address (0, 0)
  294. (<Address> address)._enet_address = self._enet_peer.address
  295. return address
  296. elif name == "state" :
  297. return self._enet_peer.state
  298. elif name == "packetLoss" :
  299. return self._enet_peer.packetLoss
  300. elif name == "packetThrottleInterval" :
  301. return self._enet_peer.packetThrottleInterval
  302. elif name == "packetThrottleAcceleration" :
  303. return self._enet_peer.packetThrottleAcceleration
  304. elif name == "packetThrottleDeceleration" :
  305. return self._enet_peer.packetThrottleDeceleration
  306. elif name == "roundTripTime" :
  307. return self._enet_peer.roundTripTime
  308. else :
  309. raise AttributeError ("Peer object has no attribute '" + name + "'")
  310. else :
  311. raise MemoryError ("Empty Peer object accessed!")
  312. def __setattr__ (self, name, value) :
  313. if self._enet_peer :
  314. if name == "packetThrottleInterval" :
  315. enet_peer_throttle_configure (self._enet_peer, value, self._enet_peer.packetThrottleAcceleration, self._enet_peer.packetThrottleDeceleration)
  316. elif name == "packetThrottleAcceleration" :
  317. enet_peer_throttle_configure (self._enet_peer, self._enet_peer.packetThrottleInterval, value, self._enet_peer.packetThrottleDeceleration)
  318. elif name == "packetThrottleDeceleration" :
  319. enet_peer_throttle_configure (self._enet_peer, self._enet_peer.packetThrottleInterval, self._enet_peer.packetThrottleAcceleration, value)
  320. else :
  321. raise AttributeError ("Peer object has no attribute '" + name + "'")
  322. else :
  323. raise MemoryError ("Empty Peer object accessed!")
  324. cdef class Event :
  325. """Event ()
  326. ATTRIBUTES
  327. int type Type of the event.
  328. enet.EVENT_TYPE_NONE
  329. .EVENT_TYPE_CONNECT
  330. .EVENT_TYPE_DISCONNECT
  331. .EVENT_TYPE_RECEIVE
  332. Peer peer Peer that generated a connect, disconnect or receive event.
  333. int channelID
  334. Packet packet
  335. DESCRIPTION
  336. An ENet event as returned by enet.Host.service.
  337. This class should never be instantiated directly."""
  338. cdef ENetEvent _enet_event
  339. def __getattr__ (self, name) :
  340. if name == "type" :
  341. return self._enet_event.type
  342. elif name == "peer" :
  343. peer = Peer ()
  344. (<Peer> peer)._enet_peer = self._enet_event.peer
  345. return peer
  346. elif name == "channelID" :
  347. return self._enet_event.channelID
  348. elif name == "packet" :
  349. packet = Packet ()
  350. (<Packet> packet)._enet_packet = self._enet_event.packet
  351. return packet
  352. else :
  353. raise AttributeError ("Event object has no attribute '" + name + "'")
  354. def __setattr__ (self, name, value) :
  355. if name == "type" or name == "peer" or name == "channelID" or name == "packet" :
  356. raise AttributeError ("Attribute '" + name +"' on Event object is read-only.")
  357. else :
  358. raise AttributeError ("Event object has no attribute '" + name + "'")
  359. cdef class Host :
  360. """Host (Address address, int peerCount, int incomingBandwidth, int outgoingBandwidth)
  361. ATTRIBUTES
  362. Address address Internet address of the host.
  363. Socket socket The socket the host services.
  364. int incomingBandwidth Downstream bandwidth of the host.
  365. int outgoingBandwidth Upstream bandwidth of the host.
  366. DESCRIPTION
  367. An ENet host for communicating with peers.
  368. If 'address' is None, then the Host will be client only."""
  369. cdef ENetHost *_enet_host
  370. cdef enet_uint32 _enet_incomingBandwidth
  371. cdef enet_uint32 _enet_outgoingBandwidth
  372. def __init__ (self, Address address = None, peerCount = 0, incomingBandwidth = 0, outgoingBandwidth = 0) :
  373. (self._enet_incomingBandwidth, self._enet_outgoingBandwidth) = (incomingBandwidth, outgoingBandwidth)
  374. if address :
  375. self._enet_host = enet_host_create (&address._enet_address, peerCount, incomingBandwidth, outgoingBandwidth)
  376. else :
  377. self._enet_host = enet_host_create (NULL, peerCount, incomingBandwidth, outgoingBandwidth)
  378. if not self._enet_host :
  379. raise MemoryError ("Unable to create host structure!")
  380. def __dealloc__ (self) :
  381. if self._enet_host :
  382. enet_host_destroy (self._enet_host)
  383. def connect (self, Address address, channelCount) :
  384. """Peer connect (Address address, int channelCount)
  385. Initiates a connection to a foreign host."""
  386. if self._enet_host :
  387. peer = Peer ()
  388. (<Peer> peer)._enet_peer = enet_host_connect (self._enet_host, &address._enet_address, channelCount)
  389. if not (<Peer> peer)._enet_peer :
  390. raise IOError ("Connection failure!")
  391. return peer
  392. def service (self, timeout) :
  393. """Event service (int timeout)
  394. Waits for events on the host specified and shuttles packets between the host and its peers."""
  395. if self._enet_host :
  396. event = Event ()
  397. result = enet_host_service (self._enet_host, &(<Event> event)._enet_event, timeout)
  398. if result < 0 :
  399. raise IOError ("Servicing error - probably disconnected.")
  400. else :
  401. return event
  402. def flush (self) :
  403. """flush ()
  404. Sends any queued packets on the host specified to its designated peers."""
  405. if self._enet_host :
  406. enet_host_flush (self._enet_host)
  407. def broadcast (self, channelID, Packet packet) :
  408. """broadcast (int channelID, Packet packet)
  409. Queues a packet to be sent to all peers associated with the host."""
  410. if self._enet_host and packet._enet_packet :
  411. enet_host_broadcast (self._enet_host, channelID, packet._enet_packet)
  412. def __getattr__ (self, name) :
  413. # TODO: Add 'peers'.
  414. if name == "address" and self._enet_host :
  415. address = Address (0, 0)
  416. (<Address> address)._enet_address = self._enet_host.address
  417. return address
  418. elif name == "incomingBandwidth" :
  419. return self._enet_incomingBandwidth
  420. elif name == "outgoingBandwidth" :
  421. return self._enet_outgoingBandwidth
  422. elif name == "socket" :
  423. socket = Socket ()
  424. (<Socket> socket)._enet_socket = self._enet_host.socket
  425. return socket
  426. else :
  427. raise AttributeError ("Host object has no attribute '" + name + "'")
  428. def __setattr__ (self, name, value) :
  429. if name == "incomingBandwidth" :
  430. self._enet_incomingBandwidth = value
  431. enet_host_bandwidth_limit (self._enet_host, self._enet_incomingBandwidth, self._enet_outgoingBandwidth)
  432. elif name == "outgoingBandwidth" :
  433. self._enet_outgoingBandwidth = value
  434. enet_host_bandwidth_limit (self._enet_host, self._enet_incomingBandwidth, self._enet_outgoingBandwidth)
  435. else :
  436. raise AttributeError ("Host object has no attribute '" + name + "'")
  437. # SECTION
  438. # Testing
  439. #
  440. # TODO
  441. # Switch to using the unittest framework.
  442. class test :
  443. """test ()
  444. DESCRIPTION
  445. A very simple testing class that will change between releases. This is for the maintainer only."""
  446. def check (self, host) :
  447. event = host.service (0)
  448. if event.type == EVENT_TYPE_NONE :
  449. pass
  450. elif event.type == EVENT_TYPE_CONNECT :
  451. print "%s connected to %s via %s." % (host, event.peer.address, event.peer)
  452. elif event.type == EVENT_TYPE_DISCONNECT :
  453. print "%s disconnected from %s via %s." % (host, event.peer.address, event.peer)
  454. elif event.type == EVENT_TYPE_RECEIVE :
  455. print "%s received %s containing '%s' from %s via %s." % (host, event.packet, event.packet.data, event.peer.address, event.peer)
  456. else :
  457. print "%s received invalid event %s of type %u." % (host, event, event.type)
  458. def test (self) :
  459. print "Starting services..."
  460. host1 = Host (None, 1, 0, 0)
  461. host2 = Host (Address ("localhost", 6666), 1, 0, 0)
  462. print "Connecting %s (client) to %s (server)..." % (host1, host2)
  463. peer1 = host1.connect (Address ("localhost", 6666), 1)
  464. print "Entering service loop..."
  465. count = 0
  466. while 1 :
  467. self.check (host1)
  468. self.check (host2)
  469. count = count + 1
  470. if not (count % 10000) :
  471. print "Sending broadcast..."
  472. host1.broadcast (0, Packet ("SuperJoe"))
  473. # SECTION
  474. # Ensure ENET is properly initialized and de-initialized.
  475. def _enet_atexit () :
  476. enet_deinitialize ()
  477. enet_initialize ()
  478. atexit.register (_enet_atexit)