| 116 | } |
| 117 | |
| 118 | static int l_connect (lua_State *L) { |
| 119 | const char *host = luaL_checkstring(L, 1); |
| 120 | auto port = static_cast<uint16_t>(luaL_checkinteger(L, 2)); |
| 121 | const char *const opts[] = { "tcp", "udp", nullptr }; |
| 122 | bool udp = luaL_checkoption(L, 3, "tcp", opts); |
| 123 | |
| 124 | StandaloneSocket& ss = pushsocket(L); |
| 125 | |
| 126 | if (udp) { |
| 127 | ss.sock = ss.sched.addSocket(); |
| 128 | const bool host_is_ip_addr = ss.sock->peer.ip.fromString(host); |
| 129 | ss.sock->peer.port = soup::Endianness::toNetwork(soup::native_u16_t(port)); |
| 130 | #if SOUP_WINDOWS |
| 131 | ss.sock->init(ss.sock->peer.ip.isV4() ? AF_INET : AF_INET6, SOCK_DGRAM); /* init socket right away so we can use udpServerSend as client */ |
| 132 | #else |
| 133 | ss.sock->init(AF_INET6, SOCK_DGRAM); /* init socket right away so we can use udpServerSend as client */ |
| 134 | #endif |
| 135 | ss.udp = true; |
| 136 | ss.recvLoopUdp(*ss.sock); |
| 137 | if (!host_is_ip_addr) { |
| 138 | auto spTask = ss.sched.add<soup::ResolveIpAddrTask>(host); |
| 139 | ss.sched.tick(); |
| 140 | lua_assert(!spTask->isWorkDone()); |
| 141 | if (lua_isyieldable(L)) { |
| 142 | const auto ctx = reinterpret_cast<lua_KContext>(spTask.get()); |
| 143 | spTask.reset(); |
| 144 | return lua_yieldk(L, 0, ctx, connectudpcont); |
| 145 | } |
| 146 | do { |
| 147 | soup::os::sleep(1); |
| 148 | ss.sched.tick(); |
| 149 | } while (!spTask->isWorkDone()); |
| 150 | return restconnectudp(L, spTask.get()); |
| 151 | } |
| 152 | return 1; |
| 153 | } |
| 154 | |
| 155 | if (!lua_isyieldable(L)) { |
| 156 | ss.sock = ss.sched.addSocket(); |
| 157 | if (l_unlikely(!ss.sock->connect(host, port))) |
| 158 | return 0; |
| 159 | ss.recvLoop(); |
| 160 | return 1; |
| 161 | } |
| 162 | |
| 163 | auto pTask = ss.sched.add<soup::netConnectTask>(host, port).get(); |
| 164 | ss.sched.tick(); |
| 165 | lua_assert(!pTask->isWorkDone()); |
| 166 | return lua_yieldk(L, 0, reinterpret_cast<lua_KContext>(pTask), connectcont); |
| 167 | } |
| 168 | |
| 169 | static int l_send (lua_State *L) { |
| 170 | size_t len; |
nothing calls this directly
no test coverage detected