receive from a socket to a lua string */
| 996 | receive from a socket to a lua string |
| 997 | */ |
| 998 | int SocketAPM_recv(lua_State *L) { |
| 999 | binding_argcheck(L, 2); |
| 1000 | |
| 1001 | SocketAPM * ud = *check_SocketAPM(L, 1); |
| 1002 | |
| 1003 | const uint16_t count = get_uint16_t(L, 2); |
| 1004 | uint8_t *data = (uint8_t*)malloc(count); |
| 1005 | if (data == nullptr) { |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | const auto ret = ud->recv(data, count, 0); |
| 1010 | if (ret < 0) { |
| 1011 | free(data); |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
| 1015 | int retcount = 1; |
| 1016 | |
| 1017 | // push data to lua string |
| 1018 | lua_pushlstring(L, (const char *)data, ret); |
| 1019 | |
| 1020 | // also push the address and port if available |
| 1021 | uint32_t ip_addr; |
| 1022 | uint16_t port; |
| 1023 | if (ud->last_recv_address(ip_addr, port)) { |
| 1024 | *new_uint32_t(L) = ip_addr; |
| 1025 | lua_pushinteger(L, port); |
| 1026 | retcount += 2; |
| 1027 | } |
| 1028 | |
| 1029 | free(data); |
| 1030 | |
| 1031 | return retcount; |
| 1032 | } |
| 1033 | |
| 1034 | /* |
| 1035 | TCP socket accept() call |
nothing calls this directly
no test coverage detected