tolerate multiple netconn callbacks for a single send job

This commit is contained in:
devsaurus 2017-11-12 17:41:29 +01:00
parent 7448d21afa
commit 0b998e5612
1 changed files with 9 additions and 2 deletions

View File

@ -61,6 +61,7 @@ typedef struct lnet_userdata {
bool connecting; bool connecting;
int hold; int hold;
size_t num_held; size_t num_held;
size_t num_send;
int cb_connect_ref; int cb_connect_ref;
int cb_disconnect_ref; int cb_disconnect_ref;
int cb_reconnect_ref; int cb_reconnect_ref;
@ -290,8 +291,13 @@ static void lnet_netconn_callback(struct netconn *netconn, enum netconn_evt evt,
ud->client.connecting = false; ud->client.connecting = false;
post_net_connected(ud); post_net_connected(ud);
} else if (len > 0) { } else if (len > 0) {
// data sent, trigger Lua callback // we're potentially called back from netconn several times for a single send job
post_net_sent(ud); // keep track of them in num_send and postpone the Lua callback until all data is sent
ud->client.num_send -= len;
if (ud->client.num_send == 0) {
// all data sent, trigger Lua callback
post_net_sent(ud);
}
} }
break; break;
@ -608,6 +614,7 @@ int net_send( lua_State *L ) {
} }
data = luaL_checklstring(L, stack++, &datalen); data = luaL_checklstring(L, stack++, &datalen);
if (!data || datalen == 0) return luaL_error(L, "no data to send"); if (!data || datalen == 0) return luaL_error(L, "no data to send");
ud->client.num_send = datalen;
if (lua_isfunction(L, stack) || lua_islightfunction(L, stack)) { if (lua_isfunction(L, stack) || lua_islightfunction(L, stack)) {
lua_pushvalue(L, stack++); lua_pushvalue(L, stack++);
luaL_unref(L, LUA_REGISTRYINDEX, ud->client.cb_sent_ref); luaL_unref(L, LUA_REGISTRYINDEX, ud->client.cb_sent_ref);