lua-clientsocket.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // simple lua socket library for client
  2. // It's only for demo, limited feature. Don't use it in your project.
  3. // Rewrite socket library by yourself .
  4. #define LUA_LIB
  5. #include <lua.h>
  6. #include <lauxlib.h>
  7. #include <string.h>
  8. #include <stdint.h>
  9. #include <pthread.h>
  10. #include <stdlib.h>
  11. #include <netinet/in.h>
  12. #include <sys/types.h>
  13. #include <sys/socket.h>
  14. #include <arpa/inet.h>
  15. #include <unistd.h>
  16. #include <errno.h>
  17. #include <fcntl.h>
  18. #define CACHE_SIZE 0x1000
  19. static int
  20. lconnect(lua_State *L) {
  21. const char * addr = luaL_checkstring(L, 1);
  22. int port = luaL_checkinteger(L, 2);
  23. int fd = socket(AF_INET,SOCK_STREAM,0);
  24. struct sockaddr_in my_addr;
  25. my_addr.sin_addr.s_addr=inet_addr(addr);
  26. my_addr.sin_family=AF_INET;
  27. my_addr.sin_port=htons(port);
  28. int r = connect(fd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr_in));
  29. if (r == -1) {
  30. return luaL_error(L, "Connect %s %d failed", addr, port);
  31. }
  32. int flag = fcntl(fd, F_GETFL, 0);
  33. fcntl(fd, F_SETFL, flag | O_NONBLOCK);
  34. lua_pushinteger(L, fd);
  35. return 1;
  36. }
  37. static int
  38. lclose(lua_State *L) {
  39. int fd = luaL_checkinteger(L, 1);
  40. close(fd);
  41. return 0;
  42. }
  43. static void
  44. block_send(lua_State *L, int fd, const char * buffer, int sz) {
  45. while(sz > 0) {
  46. int r = send(fd, buffer, sz, 0);
  47. if (r < 0) {
  48. if (errno == EAGAIN || errno == EINTR)
  49. continue;
  50. luaL_error(L, "socket error: %s", strerror(errno));
  51. }
  52. buffer += r;
  53. sz -= r;
  54. }
  55. }
  56. /*
  57. integer fd
  58. string message
  59. */
  60. static int
  61. lsend(lua_State *L) {
  62. size_t sz = 0;
  63. int fd = luaL_checkinteger(L,1);
  64. const char * msg = luaL_checklstring(L, 2, &sz);
  65. block_send(L, fd, msg, (int)sz);
  66. return 0;
  67. }
  68. /*
  69. intger fd
  70. string last
  71. table result
  72. return
  73. boolean (true: data, false: block, nil: close)
  74. string last
  75. */
  76. struct socket_buffer {
  77. void * buffer;
  78. int sz;
  79. };
  80. static int
  81. lrecv(lua_State *L) {
  82. int fd = luaL_checkinteger(L,1);
  83. char buffer[CACHE_SIZE];
  84. int r = recv(fd, buffer, CACHE_SIZE, 0);
  85. if (r == 0) {
  86. lua_pushliteral(L, "");
  87. // close
  88. return 1;
  89. }
  90. if (r < 0) {
  91. if (errno == EAGAIN || errno == EINTR) {
  92. return 0;
  93. }
  94. luaL_error(L, "socket error: %s", strerror(errno));
  95. }
  96. lua_pushlstring(L, buffer, r);
  97. return 1;
  98. }
  99. static int
  100. lusleep(lua_State *L) {
  101. int n = luaL_checknumber(L, 1);
  102. usleep(n);
  103. return 0;
  104. }
  105. // quick and dirty none block stdin readline
  106. #define QUEUE_SIZE 1024
  107. struct queue {
  108. pthread_mutex_t lock;
  109. int head;
  110. int tail;
  111. char * queue[QUEUE_SIZE];
  112. };
  113. static void *
  114. readline_stdin(void * arg) {
  115. struct queue * q = arg;
  116. char tmp[1024];
  117. while (!feof(stdin)) {
  118. if (fgets(tmp,sizeof(tmp),stdin) == NULL) {
  119. // read stdin failed
  120. exit(1);
  121. }
  122. int n = strlen(tmp) -1;
  123. char * str = malloc(n+1);
  124. memcpy(str, tmp, n);
  125. str[n] = 0;
  126. pthread_mutex_lock(&q->lock);
  127. q->queue[q->tail] = str;
  128. if (++q->tail >= QUEUE_SIZE) {
  129. q->tail = 0;
  130. }
  131. if (q->head == q->tail) {
  132. // queue overflow
  133. exit(1);
  134. }
  135. pthread_mutex_unlock(&q->lock);
  136. }
  137. return NULL;
  138. }
  139. static int
  140. lreadstdin(lua_State *L) {
  141. struct queue *q = lua_touserdata(L, lua_upvalueindex(1));
  142. pthread_mutex_lock(&q->lock);
  143. if (q->head == q->tail) {
  144. pthread_mutex_unlock(&q->lock);
  145. return 0;
  146. }
  147. char * str = q->queue[q->head];
  148. if (++q->head >= QUEUE_SIZE) {
  149. q->head = 0;
  150. }
  151. pthread_mutex_unlock(&q->lock);
  152. lua_pushstring(L, str);
  153. free(str);
  154. return 1;
  155. }
  156. static int
  157. lshutdown(lua_State *L) {
  158. int fd = luaL_checkinteger(L,1);
  159. const char *mode = luaL_checkstring(L,2);
  160. int v = 0;
  161. int i;
  162. int read = 1;
  163. int write = 2;
  164. for (i=0;mode[i];i++) {
  165. switch(mode[i]) {
  166. case 'r':
  167. v |= read;
  168. break;
  169. case 'w':
  170. v |= write;
  171. break;
  172. default:
  173. return luaL_error(L, "Invalid mode %c", mode[i]);
  174. }
  175. }
  176. if (v == 0) {
  177. return luaL_error(L, "mode should be r or/and w");
  178. }
  179. if (v == read)
  180. v = SHUT_RD;
  181. else if (v == write)
  182. v = SHUT_WR;
  183. else
  184. v = SHUT_RDWR;
  185. printf("SHUTDOWN %d %d\n", fd, v);
  186. shutdown(fd, v);
  187. return 0;
  188. }
  189. LUAMOD_API int
  190. luaopen_client_socket(lua_State *L) {
  191. luaL_checkversion(L);
  192. luaL_Reg l[] = {
  193. { "connect", lconnect },
  194. { "recv", lrecv },
  195. { "send", lsend },
  196. { "shutdown", lshutdown },
  197. { "close", lclose },
  198. { "usleep", lusleep },
  199. { NULL, NULL },
  200. };
  201. luaL_newlib(L, l);
  202. struct queue * q = lua_newuserdata(L, sizeof(*q));
  203. memset(q, 0, sizeof(*q));
  204. pthread_mutex_init(&q->lock, NULL);
  205. lua_pushcclosure(L, lreadstdin, 1);
  206. lua_setfield(L, -2, "readstdin");
  207. pthread_t pid ;
  208. pthread_create(&pid, NULL, readline_stdin, q);
  209. return 1;
  210. }