enet.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /**
  2. *
  3. * Copyright (C) 2011 by Leaf Corcoran
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include <stdlib.h>
  24. #include <string.h>
  25. extern "C" {
  26. #define LUA_COMPAT_ALL
  27. #include "lua.h"
  28. #include "lualib.h"
  29. #include "lauxlib.h"
  30. #include <enet/enet.h>
  31. }
  32. #define check_host(l, idx)\
  33. *(ENetHost**)luaL_checkudata(l, idx, "enet_host")
  34. #define check_peer(l, idx)\
  35. *(ENetPeer**)luaL_checkudata(l, idx, "enet_peer")
  36. /**
  37. * Parse address string, eg:
  38. * *:5959
  39. * 127.0.0.1:*
  40. * website.com:8080
  41. */
  42. static void parse_address(lua_State *l, const char *addr_str, ENetAddress *address) {
  43. int host_i = 0, port_i = 0;
  44. char host_str[128] = {0};
  45. char port_str[32] = {0};
  46. int scanning_port = 0;
  47. char *c = (char *)addr_str;
  48. while (*c != 0) {
  49. if (host_i >= 128 || port_i >= 32 ) luaL_error(l, "Hostname too long");
  50. if (scanning_port) {
  51. port_str[port_i++] = *c;
  52. } else {
  53. if (*c == ':') {
  54. scanning_port = 1;
  55. } else {
  56. host_str[host_i++] = *c;
  57. }
  58. }
  59. c++;
  60. }
  61. host_str[host_i] = '\0';
  62. port_str[port_i] = '\0';
  63. if (host_i == 0) luaL_error(l, "Failed to parse address");
  64. if (port_i == 0) luaL_error(l, "Missing port in address");
  65. if (strcmp("*", host_str) == 0) {
  66. address->host = ENET_HOST_ANY;
  67. } else {
  68. if (enet_address_set_host(address, host_str) != 0) {
  69. luaL_error(l, "Failed to resolve host name");
  70. }
  71. }
  72. if (strcmp("*", port_str) == 0) {
  73. address->port = ENET_PORT_ANY;
  74. } else {
  75. address->port = atoi(port_str);
  76. }
  77. }
  78. /**
  79. * Find the index of a given peer for which we only have the pointer.
  80. */
  81. static size_t find_peer_index(lua_State *l, ENetHost *enet_host, ENetPeer *peer) {
  82. size_t peer_index;
  83. for (peer_index = 0; peer_index < enet_host->peerCount; peer_index++) {
  84. if (peer == &(enet_host->peers[peer_index]))
  85. return peer_index;
  86. }
  87. luaL_error (l, "enet: could not find peer id!");
  88. return peer_index;
  89. }
  90. static void push_peer(lua_State *l, ENetPeer *peer) {
  91. // try to find in peer table
  92. lua_getfield(l, LUA_REGISTRYINDEX, "enet_peers");
  93. lua_pushlightuserdata(l, peer);
  94. lua_gettable(l, -2);
  95. if (lua_isnil(l, -1)) {
  96. // printf("creating new peer\n");
  97. lua_pop(l, 1);
  98. *(ENetPeer**)lua_newuserdata(l, sizeof(void*)) = peer;
  99. luaL_getmetatable(l, "enet_peer");
  100. lua_setmetatable(l, -2);
  101. lua_pushlightuserdata(l, peer);
  102. lua_pushvalue(l, -2);
  103. lua_settable(l, -4);
  104. }
  105. lua_remove(l, -2); // remove enet_peers
  106. }
  107. static void push_event(lua_State *l, ENetEvent *event) {
  108. lua_newtable(l); // event table
  109. if (event->peer) {
  110. push_peer(l, event->peer);
  111. lua_setfield(l, -2, "peer");
  112. }
  113. switch (event->type) {
  114. case ENET_EVENT_TYPE_CONNECT:
  115. lua_pushinteger(l, event->data);
  116. lua_setfield(l, -2, "data");
  117. lua_pushstring(l, "connect");
  118. break;
  119. case ENET_EVENT_TYPE_DISCONNECT:
  120. lua_pushinteger(l, event->data);
  121. lua_setfield(l, -2, "data");
  122. lua_pushstring(l, "disconnect");
  123. break;
  124. case ENET_EVENT_TYPE_RECEIVE:
  125. lua_pushlstring(l, (const char *)event->packet->data, event->packet->dataLength);
  126. lua_setfield(l, -2, "data");
  127. lua_pushinteger(l, event->channelID);
  128. lua_setfield(l, -2, "channel");
  129. lua_pushstring(l, "receive");
  130. enet_packet_destroy(event->packet);
  131. break;
  132. case ENET_EVENT_TYPE_NONE:
  133. lua_pushstring(l, "none");
  134. break;
  135. }
  136. lua_setfield(l, -2, "type");
  137. }
  138. /**
  139. * Read a packet off the stack as a string
  140. * idx is position of string
  141. */
  142. static ENetPacket *read_packet(lua_State *l, int idx, enet_uint8 *channel_id) {
  143. size_t size;
  144. int argc = lua_gettop(l);
  145. const void *data = luaL_checklstring(l, idx, &size);
  146. ENetPacket *packet;
  147. enet_uint32 flags = ENET_PACKET_FLAG_RELIABLE;
  148. *channel_id = 0;
  149. if (argc >= idx+2 && !lua_isnil(l, idx+2)) {
  150. const char *flag_str = luaL_checkstring(l, idx+2);
  151. if (strcmp("unsequenced", flag_str) == 0) {
  152. flags = ENET_PACKET_FLAG_UNSEQUENCED;
  153. } else if (strcmp("reliable", flag_str) == 0) {
  154. flags = ENET_PACKET_FLAG_RELIABLE;
  155. } else if (strcmp("unreliable", flag_str) == 0) {
  156. flags = 0;
  157. } else {
  158. luaL_error(l, "Unknown packet flag: %s", flag_str);
  159. }
  160. }
  161. if (argc >= idx+1 && !lua_isnil(l, idx+1)) {
  162. *channel_id = luaL_checkint(l, idx+1);
  163. }
  164. packet = enet_packet_create(data, size, flags);
  165. if (packet == NULL) {
  166. luaL_error(l, "Failed to create packet");
  167. }
  168. return packet;
  169. }
  170. /**
  171. * Create a new host
  172. * Args:
  173. * address (nil for client)
  174. * [peer_count = 64]
  175. * [channel_count = 1]
  176. * [in_bandwidth = 0]
  177. * [out_bandwidth = 0]
  178. */
  179. static int host_create(lua_State *l) {
  180. ENetHost *host;
  181. size_t peer_count = 64, channel_count = 1;
  182. enet_uint32 in_bandwidth = 0, out_bandwidth = 0;
  183. int have_address = 1;
  184. ENetAddress address;
  185. if (lua_gettop(l) == 0 || lua_isnil(l, 1)) {
  186. have_address = 0;
  187. } else {
  188. parse_address(l, luaL_checkstring(l, 1), &address);
  189. }
  190. switch (lua_gettop(l)) {
  191. case 5:
  192. if (!lua_isnil(l, 5)) out_bandwidth = luaL_checkint(l, 5);
  193. case 4:
  194. if (!lua_isnil(l, 4)) in_bandwidth = luaL_checkint(l, 4);
  195. case 3:
  196. if (!lua_isnil(l, 3)) channel_count = luaL_checkint(l, 3);
  197. case 2:
  198. if (!lua_isnil(l, 2)) peer_count = luaL_checkint(l, 2);
  199. }
  200. // printf("host create, peers=%d, channels=%d, in=%d, out=%d\n",
  201. // peer_count, channel_count, in_bandwidth, out_bandwidth);
  202. host = enet_host_create(have_address ? &address : NULL, peer_count,
  203. channel_count, in_bandwidth, out_bandwidth);
  204. if (host == NULL) {
  205. lua_pushnil (l);
  206. lua_pushstring(l, "enet: failed to create host (already listening?)");
  207. return 2;
  208. }
  209. *(ENetHost**)lua_newuserdata(l, sizeof(void*)) = host;
  210. luaL_getmetatable(l, "enet_host");
  211. lua_setmetatable(l, -2);
  212. return 1;
  213. }
  214. static int linked_version(lua_State *l) {
  215. lua_pushfstring(l, "%d.%d.%d",
  216. ENET_VERSION_GET_MAJOR(enet_linked_version()),
  217. ENET_VERSION_GET_MINOR(enet_linked_version()),
  218. ENET_VERSION_GET_PATCH(enet_linked_version()));
  219. return 1;
  220. }
  221. /**
  222. * Serice a host
  223. * Args:
  224. * timeout
  225. *
  226. * Return
  227. * nil on no event
  228. * an event table on event
  229. */
  230. static int host_service(lua_State *l) {
  231. ENetHost *host = check_host(l, 1);
  232. ENetEvent event;
  233. int timeout = 0, out;
  234. if (lua_gettop(l) > 1)
  235. timeout = luaL_checkint(l, 2);
  236. out = enet_host_service(host, &event, timeout);
  237. if (out == 0) return 0;
  238. if (out < 0) return luaL_error(l, "Error during service");
  239. push_event(l, &event);
  240. return 1;
  241. }
  242. /**
  243. * Dispatch a single event if available
  244. */
  245. static int host_check_events(lua_State *l) {
  246. ENetHost *host = check_host(l, 1);
  247. ENetEvent event;
  248. int out = enet_host_check_events(host, &event);
  249. if (out == 0) return 0;
  250. if (out < 0) return luaL_error(l, "Error checking event");
  251. push_event(l, &event);
  252. return 1;
  253. }
  254. /**
  255. * Enables an adaptive order-2 PPM range coder for the transmitted data of
  256. * all peers.
  257. */
  258. static int host_compress_with_range_coder(lua_State *l) {
  259. ENetHost *host = check_host(l, 1);
  260. int result = enet_host_compress_with_range_coder (host);
  261. if (result == 0) {
  262. lua_pushboolean (l, 1);
  263. } else {
  264. lua_pushboolean (l, 0);
  265. }
  266. return 1;
  267. }
  268. /**
  269. * Connect a host to an address
  270. * Args:
  271. * the address
  272. * [channel_count = 1]
  273. * [data = 0]
  274. */
  275. static int host_connect(lua_State *l) {
  276. ENetHost *host = check_host(l, 1);
  277. ENetAddress address;
  278. ENetPeer *peer;
  279. enet_uint32 data = 0;
  280. size_t channel_count = 1;
  281. parse_address(l, luaL_checkstring(l, 2), &address);
  282. switch (lua_gettop(l)) {
  283. case 4:
  284. if (!lua_isnil(l, 4)) data = luaL_checkint(l, 4);
  285. case 3:
  286. if (!lua_isnil(l, 3)) channel_count = luaL_checkint(l, 3);
  287. }
  288. // printf("host connect, channels=%d, data=%d\n", channel_count, data);
  289. peer = enet_host_connect(host, &address, channel_count, data);
  290. if (peer == NULL) {
  291. return luaL_error(l, "Failed to create peer");
  292. }
  293. push_peer(l, peer);
  294. return 1;
  295. }
  296. static int host_flush(lua_State *l) {
  297. ENetHost *host = check_host(l, 1);
  298. enet_host_flush(host);
  299. return 0;
  300. }
  301. static int host_broadcast(lua_State *l) {
  302. ENetHost *host = check_host(l, 1);
  303. enet_uint8 channel_id;
  304. ENetPacket *packet = read_packet(l, 2, &channel_id);
  305. enet_host_broadcast(host, channel_id, packet);
  306. return 0;
  307. }
  308. // Args: limit:number
  309. static int host_channel_limit(lua_State *l) {
  310. ENetHost *host = check_host(l, 1);
  311. int limit = luaL_checkint(l, 2);
  312. enet_host_channel_limit(host, limit);
  313. return 0;
  314. }
  315. static int host_bandwidth_limit(lua_State *l) {
  316. ENetHost *host = check_host(l, 1);
  317. enet_uint32 in_bandwidth = luaL_checkint(l, 2);
  318. enet_uint32 out_bandwidth = luaL_checkint(l, 2);
  319. enet_host_bandwidth_limit(host, in_bandwidth, out_bandwidth);
  320. return 0;
  321. }
  322. static int host_socket_get_address(lua_State *l) {
  323. ENetHost *host = check_host(l, 1);
  324. ENetAddress address;
  325. enet_socket_get_address (host->socket, &address);
  326. lua_pushfstring(l, "%d.%d.%d.%d:%d",
  327. ((address.host) & 0xFF),
  328. ((address.host >> 8) & 0xFF),
  329. ((address.host >> 16) & 0xFF),
  330. (address.host >> 24& 0xFF),
  331. address.port);
  332. return 1;
  333. }
  334. static int host_total_sent_data(lua_State *l) {
  335. ENetHost *host = check_host(l, 1);
  336. lua_pushinteger (l, host->totalSentData);
  337. return 1;
  338. }
  339. static int host_total_received_data(lua_State *l) {
  340. ENetHost *host = check_host(l, 1);
  341. lua_pushinteger (l, host->totalReceivedData);
  342. return 1;
  343. }
  344. static int host_service_time(lua_State *l) {
  345. ENetHost *host = check_host(l, 1);
  346. lua_pushinteger (l, host->serviceTime);
  347. return 1;
  348. }
  349. static int host_peer_count(lua_State *l) {
  350. ENetHost *host = check_host(l, 1);
  351. lua_pushinteger (l, host->peerCount);
  352. return 1;
  353. }
  354. static int host_get_peer(lua_State *l) {
  355. ENetHost *host = check_host(l, 1);
  356. int peer_index = luaL_checkint(l, 2) - 1;
  357. if (peer_index < 0 || ((size_t) peer_index) >= host->peerCount) {
  358. luaL_argerror (l, 2, "Invalid peer index");
  359. }
  360. ENetPeer *peer = &(host->peers[peer_index]);
  361. push_peer (l, peer);
  362. return 1;
  363. }
  364. static int host_gc(lua_State *l) {
  365. ENetHost *host = check_host(l, 1);
  366. enet_host_destroy(host);
  367. return 0;
  368. }
  369. static int peer_tostring(lua_State *l) {
  370. ENetPeer *peer = check_peer(l, 1);
  371. char host_str[128];
  372. enet_address_get_host_ip(&peer->address, host_str, 128);
  373. lua_pushstring(l, host_str);
  374. lua_pushstring(l, ":");
  375. lua_pushinteger(l, peer->address.port);
  376. lua_concat(l, 3);
  377. return 1;
  378. }
  379. static int peer_ping(lua_State *l) {
  380. ENetPeer *peer = check_peer(l, 1);
  381. enet_peer_ping(peer);
  382. return 0;
  383. }
  384. static int peer_throttle_configure(lua_State *l) {
  385. ENetPeer *peer = check_peer(l, 1);
  386. enet_uint32 interval = luaL_checkint(l, 2);
  387. enet_uint32 acceleration = luaL_checkint(l, 3);
  388. enet_uint32 deceleration = luaL_checkint(l, 4);
  389. enet_peer_throttle_configure(peer, interval, acceleration, deceleration);
  390. return 0;
  391. }
  392. static int peer_round_trip_time(lua_State *l) {
  393. ENetPeer *peer = check_peer(l, 1);
  394. if (lua_gettop(l) > 1) {
  395. enet_uint32 round_trip_time = luaL_checkint(l, 2);
  396. peer->roundTripTime = round_trip_time;
  397. }
  398. lua_pushinteger (l, peer->roundTripTime);
  399. return 1;
  400. }
  401. static int peer_last_round_trip_time(lua_State *l) {
  402. ENetPeer *peer = check_peer(l, 1);
  403. if (lua_gettop(l) > 1) {
  404. enet_uint32 round_trip_time = luaL_checkint(l, 2);
  405. peer->lastRoundTripTime = round_trip_time;
  406. }
  407. lua_pushinteger (l, peer->lastRoundTripTime);
  408. return 1;
  409. }
  410. static int peer_ping_interval(lua_State *l) {
  411. ENetPeer *peer = check_peer(l, 1);
  412. if (lua_gettop(l) > 1) {
  413. enet_uint32 interval = luaL_checkint(l, 2);
  414. enet_peer_ping_interval (peer, interval);
  415. }
  416. lua_pushinteger (l, peer->pingInterval);
  417. return 1;
  418. }
  419. static int peer_timeout(lua_State *l) {
  420. ENetPeer *peer = check_peer(l, 1);
  421. enet_uint32 timeout_limit = 0;
  422. enet_uint32 timeout_minimum = 0;
  423. enet_uint32 timeout_maximum = 0;
  424. switch (lua_gettop(l)) {
  425. case 4:
  426. if (!lua_isnil(l, 4)) timeout_maximum = luaL_checkint(l, 4);
  427. case 3:
  428. if (!lua_isnil(l, 3)) timeout_minimum = luaL_checkint(l, 3);
  429. case 2:
  430. if (!lua_isnil(l, 2)) timeout_limit = luaL_checkint(l, 2);
  431. }
  432. enet_peer_timeout (peer, timeout_limit, timeout_minimum, timeout_maximum);
  433. lua_pushinteger (l, peer->timeoutLimit);
  434. lua_pushinteger (l, peer->timeoutMinimum);
  435. lua_pushinteger (l, peer->timeoutMaximum);
  436. return 3;
  437. }
  438. static int peer_disconnect(lua_State *l) {
  439. ENetPeer *peer = check_peer(l, 1);
  440. enet_uint32 data = lua_gettop(l) > 1 ? luaL_checkint(l, 2) : 0;
  441. enet_peer_disconnect(peer, data);
  442. return 0;
  443. }
  444. static int peer_disconnect_now(lua_State *l) {
  445. ENetPeer *peer = check_peer(l, 1);
  446. enet_uint32 data = lua_gettop(l) > 1 ? luaL_checkint(l, 2) : 0;
  447. enet_peer_disconnect_now(peer, data);
  448. return 0;
  449. }
  450. static int peer_disconnect_later(lua_State *l) {
  451. ENetPeer *peer = check_peer(l, 1);
  452. enet_uint32 data = lua_gettop(l) > 1 ? luaL_checkint(l, 2) : 0;
  453. enet_peer_disconnect_later(peer, data);
  454. return 0;
  455. }
  456. static int peer_index(lua_State *l) {
  457. ENetPeer *peer = check_peer(l, 1);
  458. size_t peer_index = find_peer_index (l, peer->host, peer);
  459. lua_pushinteger (l, peer_index + 1);
  460. return 1;
  461. }
  462. static int peer_state(lua_State *l) {
  463. ENetPeer *peer = check_peer(l, 1);
  464. switch (peer->state) {
  465. case (ENET_PEER_STATE_DISCONNECTED):
  466. lua_pushstring (l, "disconnected");
  467. break;
  468. case (ENET_PEER_STATE_CONNECTING):
  469. lua_pushstring (l, "connecting");
  470. break;
  471. case (ENET_PEER_STATE_ACKNOWLEDGING_CONNECT):
  472. lua_pushstring (l, "acknowledging_connect");
  473. break;
  474. case (ENET_PEER_STATE_CONNECTION_PENDING):
  475. lua_pushstring (l, "connection_pending");
  476. break;
  477. case (ENET_PEER_STATE_CONNECTION_SUCCEEDED):
  478. lua_pushstring (l, "connection_succeeded");
  479. break;
  480. case (ENET_PEER_STATE_CONNECTED):
  481. lua_pushstring (l, "connected");
  482. break;
  483. case (ENET_PEER_STATE_DISCONNECT_LATER):
  484. lua_pushstring (l, "disconnect_later");
  485. break;
  486. case (ENET_PEER_STATE_DISCONNECTING):
  487. lua_pushstring (l, "disconnecting");
  488. break;
  489. case (ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT):
  490. lua_pushstring (l, "acknowledging_disconnect");
  491. break;
  492. case (ENET_PEER_STATE_ZOMBIE):
  493. lua_pushstring (l, "zombie");
  494. break;
  495. default:
  496. lua_pushstring (l, "unknown");
  497. }
  498. return 1;
  499. }
  500. static int peer_connect_id(lua_State *l) {
  501. ENetPeer *peer = check_peer(l, 1);
  502. lua_pushinteger (l, peer->connectID);
  503. return 1;
  504. }
  505. static int peer_reset(lua_State *l) {
  506. ENetPeer *peer = check_peer(l, 1);
  507. enet_peer_reset(peer);
  508. return 0;
  509. }
  510. static int peer_receive(lua_State *l) {
  511. ENetPeer *peer = check_peer(l, 1);
  512. ENetPacket *packet;
  513. enet_uint8 channel_id = 0;
  514. if (lua_gettop(l) > 1) {
  515. channel_id = luaL_checkint(l, 2);
  516. }
  517. packet = enet_peer_receive(peer, &channel_id);
  518. if (packet == NULL) return 0;
  519. lua_pushlstring(l, (const char *)packet->data, packet->dataLength);
  520. lua_pushinteger(l, channel_id);
  521. enet_packet_destroy(packet);
  522. return 2;
  523. }
  524. /**
  525. * Send a lua string to a peer
  526. * Args:
  527. * packet data, string
  528. * channel id
  529. * flags ["reliable", nil]
  530. *
  531. */
  532. static int peer_send(lua_State *l) {
  533. ENetPeer *peer = check_peer(l, 1);
  534. enet_uint8 channel_id;
  535. ENetPacket *packet = read_packet(l, 2, &channel_id);
  536. // printf("sending, channel_id=%d\n", channel_id);
  537. enet_peer_send(peer, channel_id, packet);
  538. return 0;
  539. }
  540. static const struct luaL_Reg enet_funcs [] = {
  541. {"host_create", host_create},
  542. {"linked_version", linked_version},
  543. {NULL, NULL}
  544. };
  545. static const struct luaL_Reg enet_host_funcs [] = {
  546. {"service", host_service},
  547. {"check_events", host_check_events},
  548. {"compress_with_range_coder", host_compress_with_range_coder},
  549. {"connect", host_connect},
  550. {"flush", host_flush},
  551. {"broadcast", host_broadcast},
  552. {"channel_limit", host_channel_limit},
  553. {"bandwidth_limit", host_bandwidth_limit},
  554. {"socket_get_address", host_socket_get_address},
  555. // additional convenience functions (mostly accessors)
  556. {"total_sent_data", host_total_sent_data},
  557. {"total_received_data", host_total_received_data},
  558. {"service_time", host_service_time},
  559. {"peer_count", host_peer_count},
  560. {"get_peer", host_get_peer},
  561. {NULL, NULL}
  562. };
  563. static const struct luaL_Reg enet_peer_funcs [] = {
  564. {"disconnect", peer_disconnect},
  565. {"disconnect_now", peer_disconnect_now},
  566. {"disconnect_later", peer_disconnect_later},
  567. {"reset", peer_reset},
  568. {"ping", peer_ping},
  569. {"receive", peer_receive},
  570. {"send", peer_send},
  571. {"throttle_configure", peer_throttle_configure},
  572. {"ping_interval", peer_ping_interval},
  573. {"timeout", peer_timeout},
  574. // additional convenience functions to member variables
  575. {"index", peer_index},
  576. {"state", peer_state},
  577. {"connect_id", peer_connect_id},
  578. {"round_trip_time", peer_round_trip_time},
  579. {"last_round_trip_time", peer_last_round_trip_time},
  580. {NULL, NULL}
  581. };
  582. int luaopen_enet(lua_State *l) {
  583. enet_initialize();
  584. atexit(enet_deinitialize);
  585. // create metatables
  586. luaL_newmetatable(l, "enet_host");
  587. lua_newtable(l); // index
  588. luaL_register(l, NULL, enet_host_funcs);
  589. lua_setfield(l, -2, "__index");
  590. lua_pushcfunction(l, host_gc);
  591. lua_setfield(l, -2, "__gc");
  592. luaL_newmetatable(l, "enet_peer");
  593. lua_newtable(l);
  594. luaL_register(l, NULL, enet_peer_funcs);
  595. lua_setfield(l, -2, "__index");
  596. lua_pushcfunction(l, peer_tostring);
  597. lua_setfield(l, -2, "__tostring");
  598. // set up peer table
  599. lua_newtable(l);
  600. lua_newtable(l); // metatable
  601. lua_pushstring(l, "v");
  602. lua_setfield(l, -2, "__mode");
  603. lua_setmetatable(l, -2);
  604. lua_setfield(l, LUA_REGISTRYINDEX, "enet_peers");
  605. luaL_register(l, "enet", enet_funcs);
  606. // return the enet table created with luaL_register
  607. return 1;
  608. }