sq_nanomsg.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. #ifdef __cplusplus
  2. extern "C" {
  3. #endif
  4. //#ifdef USE_NANOMSG
  5. #include "squirrel.h"
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <stdlib.h> /* for malloc */
  9. #include <assert.h> /* for a few sanity tests */
  10. #include "NanoMsg.h"
  11. static const SQChar SQ_LIBNAME[] = _SC("NanoMsg");
  12. SQ_OPT_STRING_STRLEN();
  13. static const SQChar SSL_CTX_Tag[] = _SC("sq_NanoMsg_ctx");
  14. static SQRESULT get_NanoMsg_instance(HSQUIRRELVM v, SQInteger idx, sq_sqlite3_sdb **sdb)
  15. {
  16. SQRESULT _rc_;
  17. if((_rc_ = sq_getinstanceup(v,idx,(SQUserPointer*)sdb,(void*)SQLite3_TAG)) < 0) return _rc_;
  18. if(!*sdb) return sq_throwerror(v, _SC("database is closed"));
  19. return _rc_;
  20. }
  21. #define GET_NanoMsg_INSTANCE() SQ_GET_INSTANCE(v, 1, NANOMSG, NANOMSG_Tag) \
  22. if(self == NULL) return sq_throwerror(v, _SC("NanoMsg object already closed"));
  23. #define GET_NanoMsg_INSTANCE_at(idx) SQ_GET_INSTANCE(v, idx, NANOMSG, NANOMSG_Tag)
  24. static SQRESULT NanoMsg_release_hook(SQUserPointer p, SQInteger size, HSQUIRRELVM v)
  25. {
  26. NANOMSG *self = (NANOMSG*)p;
  27. if(self) NanoMsg_free(self);
  28. return 0;
  29. }
  30. static SQRESULT NanoMsg_free(HSQUIRRELVM v)
  31. {
  32. SQ_FUNC_VARS_NO_TOP(v);
  33. GET_ssl_INSTANCE();
  34. NanoMsg_release_hook(self, 0, v);
  35. sq_setinstanceup(v, 1, 0);
  36. return 0;
  37. }
  38. static SQRESULT NanoMsg_constructor(HSQUIRRELVM v, NANOMSG *NanoMsg, int free_on_gc)
  39. {
  40. if(!NanoMsg)
  41. return sq_throwerror(v, _SC("Could'nt create an NanoMsg object."));
  42. sq_pushstring(v, SQ_LIBNAME, -1);
  43. if(sq_getonroottable(v) == SQ_OK){
  44. sq_pushstring(v, NanoMsg_NAME, -1);
  45. if(sq_get(v, -2) == SQ_OK){
  46. if(sq_createinstance(v, -1) == SQ_OK){
  47. sq_setinstanceup(v, -1, ssl);
  48. if(free_on_gc) sq_setreleasehook(v,-1, NanoMsg_release_hook);
  49. return 1;
  50. }
  51. }
  52. }
  53. return SQ_ERROR;
  54. }
  55. static SQRESULT sq_NanoMsg_allocmsg(HSQUIRRELVM v){
  56. SQ_FUNC_VARS_NO_TOP(v);
  57. GET_NanoMsg_INSTANCE();
  58. SQ_GET_INTEGER(v, 2, int_size);
  59. SQ_GET_INTEGER(v, 3, int_the_type);
  60. // void *nn_allocmsg (size_t size, int type)
  61. return 0;
  62. }
  63. static SQRESULT sq_NanoMsg_bind(HSQUIRRELVM v){
  64. SQ_FUNC_VARS_NO_TOP(v);
  65. GET_NanoMsg_INSTANCE();
  66. SQ_GET_STRING(v, 2, str_addr);
  67. // int nn_bind (int s, const char *addr)
  68. sq_pushinteger(v, nn_bind (self->sock, str_addr));
  69. return 1;
  70. }
  71. static SQRESULT sq_NanoMsg_close(HSQUIRRELVM v){
  72. SQ_FUNC_VARS_NO_TOP(v);
  73. GET_NanoMsg_INSTANCE();
  74. // int nn_close (int s)
  75. sq_pushinteger(v, nn_close (self->sock));
  76. return 1;
  77. }
  78. static SQRESULT sq_NanoMsg_connect(HSQUIRRELVM v){
  79. SQ_FUNC_VARS_NO_TOP(v);
  80. GET_NanoMsg_INSTANCE();
  81. SQ_GET_STRING(v, 2, str_addr);
  82. // int nn_connect (int s, const char *addr)
  83. sq_pushinteger(v, nn_connect (self->sock, str_addr));
  84. return 1;
  85. }
  86. static SQRESULT sq_NanoMsg_constructor(HSQUIRRELVM v){
  87. SQ_FUNC_VARS_NO_TOP(v);
  88. GET_NanoMsg_INSTANCE();
  89. SQ_GET_INTEGER(v, 2, int_domain);
  90. SQ_GET_INTEGER(v, 3, int_protocol);
  91. // int nn_socket (int domain, int protocol)
  92. return 0;
  93. }
  94. static SQRESULT sq_NanoMsg_destructor(HSQUIRRELVM v){
  95. SQ_FUNC_VARS_NO_TOP(v);
  96. GET_NanoMsg_INSTANCE();
  97. // int nn_close (int s)
  98. return 0;
  99. }
  100. static SQRESULT sq_NanoMsg_device(HSQUIRRELVM v){
  101. SQ_FUNC_VARS_NO_TOP(v);
  102. GET_NanoMsg_INSTANCE();
  103. GET_NanoMsg_INSTANCE_at(v, 2);
  104. GET_NanoMsg_INSTANCE_at(v, 3);
  105. // int nn_device (int s1, int s2)
  106. sq_pushinteger(v, nn_device (self->sock, str_addr));
  107. return 1;
  108. }
  109. static SQRESULT sq_NanoMsg_errno(HSQUIRRELVM v){
  110. SQ_FUNC_VARS_NO_TOP(v);
  111. GET_NanoMsg_INSTANCE();
  112. // int nn_errno (void)
  113. return 0;
  114. }
  115. static SQRESULT sq_NanoMsg_freemsg(HSQUIRRELVM v){
  116. SQ_FUNC_VARS_NO_TOP(v);
  117. GET_NanoMsg_INSTANCE();
  118. SQ_GET_STRING(v, 2, msg);
  119. // int nn_freemsg (void *msg)
  120. return 0;
  121. }
  122. static SQRESULT sq_NanoMsg_getsockopt(HSQUIRRELVM v){
  123. SQ_FUNC_VARS_NO_TOP(v);
  124. GET_NanoMsg_INSTANCE();
  125. SQ_GET_INTEGER(v, 2, int_level);
  126. SQ_GET_INTEGER(v, 3, int_option);
  127. SQ_GET_STRING(v, 4, optval);
  128. SQ_GET_STRING(v, 5, optvallen);
  129. // int nn_getsockopt (int s, int level, int option, void *optval, size_t *optvallen)
  130. return 0;
  131. }
  132. static SQRESULT sq_NanoMsg_pollfd(HSQUIRRELVM v){
  133. SQ_FUNC_VARS_NO_TOP(v);
  134. GET_NanoMsg_INSTANCE();
  135. GET_NanoMsg_INSTANCE_at(v, 2, fds);
  136. GET_NanoMsg_INSTANCE_at(v, 3, int_timeout);
  137. // int nn_poll (struct nn_pollfd *fds, int nfds, int timeout)
  138. return 0;
  139. }
  140. static SQRESULT sq_NanoMsg_reallocmsg(HSQUIRRELVM v){
  141. SQ_FUNC_VARS_NO_TOP(v);
  142. GET_NanoMsg_INSTANCE();
  143. SQ_GET_STRING(v, 2, msg);
  144. SQ_GET_STRING(v, 3, size);
  145. // void *nn_reallocmsg (void *msg, size_t size)
  146. return 0;
  147. }
  148. static SQRESULT sq_NanoMsg_recv(HSQUIRRELVM v){
  149. SQ_FUNC_VARS_NO_TOP(v);
  150. GET_NanoMsg_INSTANCE();
  151. SQ_GET_INTEGER(v, 2, int_flags);
  152. // int nn_recv (int s, void *buf, size_t len, int flags)
  153. return 0;
  154. }
  155. static SQRESULT sq_NanoMsg_recvmsg(HSQUIRRELVM v){
  156. SQ_FUNC_VARS_NO_TOP(v);
  157. GET_NanoMsg_INSTANCE();
  158. SQ_GET_INTEGER(v, 2, int_flags);
  159. // int nn_recvmsg (int s, struct nn_msghdr *msghdr, int flags)
  160. return 0;
  161. }
  162. static SQRESULT sq_NanoMsg_send(HSQUIRRELVM v){
  163. SQ_FUNC_VARS_NO_TOP(v);
  164. GET_NanoMsg_INSTANCE();
  165. SQ_GET_STRING(v, 2, str_buf);
  166. SQ_GET_INTEGER(v, 3, int_flags);
  167. // int nn_send (int s, const void *buf, size_t len, int flags)
  168. return 0;
  169. }
  170. static SQRESULT sq_NanoMsg_sendmsg(HSQUIRRELVM v){
  171. SQ_FUNC_VARS_NO_TOP(v);
  172. GET_NanoMsg_INSTANCE();
  173. SQ_GET_STRING(v, 2, msghdr);
  174. SQ_GET_STRING(v, 3, flags);
  175. // int nn_sendmsg (int s, const struct nn_msghdr *msghdr, int flags)
  176. return 0;
  177. }
  178. static SQRESULT sq_NanoMsg_setsockopt(HSQUIRRELVM v){
  179. SQ_FUNC_VARS_NO_TOP(v);
  180. GET_NanoMsg_INSTANCE();
  181. SQ_GET_INTEGER(v, 2, int_level);
  182. SQ_GET_INTEGER(v, 3, int_option);
  183. SQ_GET_STRING(v, 4, optval);
  184. SQ_GET_STRING(v, 5, optvallen);
  185. // int nn_setsockopt (int s, int level, int option, const void *optval, size_t optvallen)
  186. return 0;
  187. }
  188. static SQRESULT sq_NanoMsg_shutdown(HSQUIRRELVM v){
  189. SQ_FUNC_VARS_NO_TOP(v);
  190. GET_NanoMsg_INSTANCE();
  191. SQ_GET_INTEGER(v, 2, int_how);
  192. // int nn_shutdown (int s, int how)
  193. return 0;
  194. }
  195. static SQRESULT sq_NanoMsg_socket(HSQUIRRELVM v){
  196. SQ_FUNC_VARS_NO_TOP(v);
  197. GET_NanoMsg_INSTANCE();
  198. SQ_GET_INTEGER(v, 2, int_domain);
  199. SQ_GET_INTEGER(v, 3, int_protocol);
  200. // int nn_socket (int domain, int protocol)
  201. return 0;
  202. }
  203. static SQRESULT sq_NanoMsg_strerror(HSQUIRRELVM v){
  204. SQ_FUNC_VARS_NO_TOP(v);
  205. GET_NanoMsg_INSTANCE();
  206. SQ_GET_INTEGER(v, 2, int_errnum);
  207. // const char *nn_strerror (int errnum)
  208. return 0;
  209. }
  210. static SQRESULT sq_NanoMsg_symbol(HSQUIRRELVM v){
  211. SQ_FUNC_VARS_NO_TOP(v);
  212. GET_NanoMsg_INSTANCE();
  213. SQ_GET_INTEGER(v, 2, int_i);
  214. SQ_GET_STRING(v, 3, value);
  215. // const char *nn_symbol (int i, int *value)
  216. return 0;
  217. }
  218. static SQRESULT sq_NanoMsg_symbol_info(HSQUIRRELVM v){
  219. SQ_FUNC_VARS_NO_TOP(v);
  220. GET_NanoMsg_INSTANCE();
  221. SQ_GET_INTEGER(v, 2, int_i);
  222. // int nn_symbol_info (int i, struct nn_symbol_properties *buf, int buflen)
  223. return 0;
  224. }
  225. static SQRESULT sq_NanoMsg_tcpmuxd(HSQUIRRELVM v){
  226. SQ_FUNC_VARS_NO_TOP(v);
  227. GET_NanoMsg_INSTANCE();
  228. SQ_GET_INTEGER(v, 2, int_port);
  229. // int nn_tcpmuxd (int port)
  230. return 0;
  231. }
  232. static SQRESULT sq_NanoMsg_term(HSQUIRRELVM v){
  233. SQ_FUNC_VARS_NO_TOP(v);
  234. GET_NanoMsg_INSTANCE();
  235. // void nn_term (void)
  236. return 0;
  237. }
  238. #define _DECL_NANOMSG_FUNC(name,nparams,pmask) {_SC(#name),sq_NanoMsg_##name,nparams,pmask}
  239. static SQRegFunction NanoMsg_obj_funcs[]={
  240. _DECL_NANOMSG_FUNC(allocmsg, 3, _SC(".ii")),
  241. _DECL_NANOMSG_FUNC(bind, 2, _SC(".s")),
  242. _DECL_NANOMSG_FUNC(close, 1, _SC(".")),
  243. _DECL_NANOMSG_FUNC(connect, 2, _SC(".s")),
  244. _DECL_NANOMSG_FUNC(constructor, 3, _SC(".ii")),
  245. _DECL_NANOMSG_FUNC(destructor, 1, _SC(".")),
  246. _DECL_NANOMSG_FUNC(device, 3, _SC(".ii")),
  247. _DECL_NANOMSG_FUNC(errno, 1, _SC(".")),
  248. _DECL_NANOMSG_FUNC(freemsg, 2, _SC("..")),
  249. _DECL_NANOMSG_FUNC(getsockopt, 5, _SC(".ii..")),
  250. _DECL_NANOMSG_FUNC(pollfd, 3, _SC("..i")),
  251. _DECL_NANOMSG_FUNC(reallocmsg, 3, _SC("...")),
  252. _DECL_NANOMSG_FUNC(recv, 2, _SC(".i")),
  253. _DECL_NANOMSG_FUNC(recvmsg, 2, _SC(".i")),
  254. _DECL_NANOMSG_FUNC(send, 3, _SC(".si")),
  255. _DECL_NANOMSG_FUNC(sendmsg, 3, _SC("...")),
  256. _DECL_NANOMSG_FUNC(setsockopt, 5, _SC(".ii..")),
  257. _DECL_NANOMSG_FUNC(shutdown, 2, _SC(".i")),
  258. _DECL_NANOMSG_FUNC(socket, 3, _SC(".ii")),
  259. _DECL_NANOMSG_FUNC(strerror, 2, _SC(".i")),
  260. _DECL_NANOMSG_FUNC(symbol, 3, _SC(".i.")),
  261. _DECL_NANOMSG_FUNC(symbol_info, 2, _SC(".i")),
  262. _DECL_NANOMSG_FUNC(tcpmuxd, 2, _SC(".i")),
  263. _DECL_NANOMSG_FUNC(term, 1, _SC(".")),
  264. {0,0}
  265. };
  266. #undef _DECL_NANOMSG_FUNC
  267. typedef struct {
  268. const SQChar *Str;
  269. SQInteger Val;
  270. } KeyIntType, * KeyIntPtrType;
  271. static KeyIntType NanoMsg_constants[] = {
  272. #define MK_CONST(c) {_SC(#c), c}
  273. //MK_CONST(SSL_SESSION_ID_SIZE),
  274. MK_CONST(v, NN_NS_LIMIT);
  275. MK_CONST(v, NN_SNDPRIO);
  276. MK_CONST(v, NN_NS_OPTION_LEVEL);
  277. MK_CONST(v, NN_NS_TRANSPORT_OPTION);
  278. MK_CONST(v, NN_UNIT_MILLISECONDS);
  279. MK_CONST(v, NN_RECONNECT_IVL_MAX);
  280. MK_CONST(v, NN_NS_SOCKET_OPTION);
  281. MK_CONST(v, NN_TYPE_STR);
  282. MK_CONST(v, NN_SOL_SOCKET);
  283. MK_CONST(v, NN_NS_FLAG);
  284. MK_CONST(v, NN_RCVFD);
  285. MK_CONST(v, NN_LINGER);
  286. MK_CONST(v, NN_SNDFD);
  287. MK_CONST(v, SP_HDR);
  288. MK_CONST(v, NN_TYPE_INT);
  289. MK_CONST(v, NN_POLLIN);
  290. MK_CONST(v, NN_RCVBUF);
  291. MK_CONST(v, NN_SNDBUF);
  292. MK_CONST(v, NN_NS_TRANSPORT);
  293. MK_CONST(v, NN_IPV4ONLY);
  294. MK_CONST(v, NN_DOMAIN);
  295. MK_CONST(v, NN_NS_EVENT);
  296. MK_CONST(v, NN_SOCKADDR_MAX);
  297. MK_CONST(v, NN_RCVTIMEO);
  298. MK_CONST(v, NN_NS_OPTION_UNIT);
  299. MK_CONST(v, NN_NS_PROTOCOL);
  300. MK_CONST(v, NN_SNDTIMEO);
  301. MK_CONST(v, NN_NS_DOMAIN);
  302. MK_CONST(v, NN_SOCKET_NAME);
  303. MK_CONST(v, NN_PROTOCOL);
  304. MK_CONST(v, NN_NS_VERSION);
  305. MK_CONST(v, PROTO_SP);
  306. MK_CONST(v, NN_TYPE_NONE);
  307. MK_CONST(v, NN_NS_OPTION_TYPE);
  308. MK_CONST(v, NN_NS_ERROR);
  309. MK_CONST(v, NN_POLLOUT);
  310. MK_CONST(v, NN_DONTWAIT);
  311. MK_CONST(v, NN_MSG);
  312. MK_CONST(v, NN_UNIT_PRIORITY);
  313. MK_CONST(v, NN_UNIT_NONE);
  314. MK_CONST(v, NN_RECONNECT_IVL);
  315. MK_CONST(v, NN_RCVPRIO);
  316. MK_CONST(v, AF_SP_RAW);
  317. MK_CONST(v, NN_UNIT_BYTES);
  318. MK_CONST(v, NN_UNIT_BOOLEAN);
  319. MK_CONST(v, NN_NS_NAMESPACE);
  320. MK_CONST(v, AF_SP);
  321. {0,0}
  322. };
  323. /* This defines a function that opens up your library. */
  324. SQRESULT sqext_register_NanoMsg (HSQUIRRELVM v) {
  325. //add a namespace NanoMsg
  326. sq_pushstring(v, SQ_LIBNAME, -1);
  327. sq_newtable(v);
  328. sq_insert_reg_funcs(v, NanoMsg_obj_funcs);
  329. //add constants
  330. KeyIntPtrType KeyIntPtr;
  331. for (KeyIntPtr = NanoMsg_constants; KeyIntPtr->Str; KeyIntPtr++) {
  332. sq_pushstring(v, KeyIntPtr->Str, -1); //first the key
  333. sq_pushinteger(v, KeyIntPtr->Val); //then the value
  334. sq_newslot(v, -3, SQFalse); //store then
  335. }
  336. sq_newslot(v,-3,SQFalse); //add NanoMsg table to the root table
  337. return SQ_OK;
  338. }
  339. #ifdef __cplusplus
  340. }
  341. #endif //USE_NANOMSG
  342. #endif