int lua_flush(lua_State *L) { epdata_t *epd = get_epd(L); if(!epd) { return 0; } int nargs = lua_gettop(L); _lua_echo(epd, L, nargs, 0); if(epd->status != STEP_PROCESS) { return 0; } if(epd->websocket || epd->status == STEP_SEND) { return 0; } if(network_flush(epd) == 1) { return lua_yield(L, 0); } return 0; }
static int send_then_send(se_ptr_t *ptr) { epdata_t *epd = ptr->data; epd->next_proc = NULL; char *buf = epd->next_out; int len = epd->next_out_len; epd->next_out_len = 0; int have = network_send(epd, buf, len); if(have > 0) { epd->next_out = malloc(have); if(epd->next_out) { memcpy(epd->next_out, buf + (len - have), have); free(buf); if(network_flush(epd) == 1) { epd->next_proc = send_then_send; epd->next_out_len = have; return have; } else { LOGF(ERR, "flush error"); free(epd->next_out); epd->next_out = NULL; return 0; } return 0; } } free(buf); if(lua_resume(epd->L, 0) == LUA_ERRRUN && lua_isstring(epd->L, -1)) { LOGF(ERR, "Lua:error %s", lua_tostring(epd->L, -1)); lua_pop(epd->L, 1); } return 0; }
int main( int argc, char ** argv ) { setvbuf( stdin, NULL, _IONBF, 0 ); if(parse_command_line( argc, argv )) return 1; if(setup_network()) return 1; while( 1 ) { check_input(); network_pump( handle_packet, NULL ); network_flush(); usleep(1000*100); // 100ms, 0.1s } return 0; }
static int send_then_send(se_ptr_t *ptr) { epdata_t *epd = ptr->data; epd->next_proc = NULL; char *buf = epd->next_out; int len = epd->next_out_len; epd->next_out_len = 0; int have = network_send(epd, buf, len); if(have > 0) { epd->next_out = malloc(have); if(epd->next_out) { memcpy(epd->next_out, buf + (len - have), have); free(buf); if(network_flush(epd) == 1) { epd->next_proc = send_then_send; epd->next_out_len = have; return have; } else { LOGF(ERR, "flush error"); free(epd->next_out); epd->next_out = NULL; return 0; } return 0; } } free(buf); lua_f_lua_uthread_resume_in_c(epd->L, 0); return 0; }
static int _lua_echo(epdata_t *epd, lua_State *L, int nargs, int can_yield) { size_t len = 0; int have = 0; epd->next_out = NULL; if(lua_istable(L, 1)) { len = lua_calc_strlen_in_table(L, 1, 2, 0 /* strict */); if(len < 1) { return 0; } char *buf = temp_buf; if(len > 8192) { buf = malloc(len); if(!buf) { return 0; } lua_copy_str_in_table(L, 1, buf); have = network_send(epd, buf, len); if(have > 0 && can_yield) { epd->next_out = malloc(have); memcpy(epd->next_out, buf + (len - have), have); } free(buf); } else { lua_copy_str_in_table(L, 1, buf); have = network_send(epd, buf, len); if(have > 0 && can_yield) { epd->next_out = malloc(have); memcpy(epd->next_out, buf + (len - have), have); } } } else { const char *data = NULL; int i = 0; for(i = 1; i <= nargs; i++) { if(lua_isboolean(L, i)) { char *buf = NULL; if(lua_toboolean(L, i)) { buf = "true"; have = network_send(epd, buf, 4); } else { buf = "false"; have = network_send(epd, buf, 5); } if(have > 0 && can_yield) { epd->next_out = malloc(have); memcpy(epd->next_out, buf + (len - have), have); } } else { data = lua_tolstring(L, i, &len); have = network_send(epd, data, len); if(have > 0 && can_yield) { epd->next_out = malloc(have); memcpy(epd->next_out, data + (len - have), have); } } } } if(epd->next_out) { if(network_flush(epd) == 1) { epd->next_proc = send_then_send; epd->next_out_len = have; return have; } else { LOGF(ERR, "flush error"); free(epd->next_out); epd->next_out = NULL; return 0; } } return 0; }