Automatically reconnect in singleplayer on "failed to bind socket" errors (#58)

Co-authored-by: luk3yx <luk3yx@users.noreply.github.com>
master
Maksym 2022-05-23 22:21:38 +03:00 committed by GitHub
parent 4047be93db
commit 3306c2ee29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 25 deletions

View File

@ -57,30 +57,12 @@ end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
local maintab = core.settings:get("maintab_LAST") local maintab = core.settings:get("maintab_LAST")
local connect_time = tonumber(core.settings:get("connect_time")) local connect_time = tonumber(core.settings:get("connect_time")) or 0
function ui.update() function ui.update()
ui.overridden = false ui.overridden = false
local formspec = {} local formspec = {}
-- attempt auto restart
--[[if gamedata ~= nil and gamedata.errormessage ~= nil and
core.settings:get_bool("auto_connect") == true and
connect_time and connect_time < os.time() - 30 and
not gamedata.errormessage:find("Kicked") then
if maintab == "local" or maintab == "local_default" then
gamedata.singleplayer = true
gamedata.selected_world =
tonumber(core.settings:get("mainmenu_last_selected_world"))
end
core.settings:set("connect_time", os.time())
gamedata.reconnect_requested = false
gamedata.errormessage = nil
gamedata.do_reconnect = true
core.start()
return
end]]
-- handle errors -- handle errors
if gamedata ~= nil and gamedata.reconnect_requested then if gamedata ~= nil and gamedata.reconnect_requested then
local error_message = core.formspec_escape( local error_message = core.formspec_escape(
@ -91,11 +73,12 @@ function ui.update()
"bgcolor[#0000]", "bgcolor[#0000]",
"background9[0,0;0,0;" .. core.formspec_escape(defaulttexturedir .. "background9[0,0;0,0;" .. core.formspec_escape(defaulttexturedir ..
"bg_common.png") .. ";true;40]", "bg_common.png") .. ";true;40]",
"set_focus[btn_reconnect_yes;true]",
"box[0.5,1.2;13,5;#000]", "box[0.5,1.2;13,5;#000]",
("textarea[0.5,1.2;13,5;;%s;%s]"):format( ("textarea[0.5,1.2;13,5;;%s;%s]"):format(
fgettext("The server has requested a reconnect:"), error_message), fgettext("The server has requested a reconnect:"), error_message),
"button[2,6.6;4,1;btn_reconnect_yes;" .. fgettext("Reconnect") .. "]", "button[2,6.6;4,1;btn_reconnect_yes;" .. fgettext("Reconnect") .. "]",
"button[8,6.6;4,1;btn_reconnect_no;" .. fgettext("Close") .. "]" "button[8,6.6;4,1;btn_reconnect_no;" .. fgettext("Main menu") .. "]"
} }
ui.overridden = true ui.overridden = true
elseif gamedata ~= nil and gamedata.errormessage ~= nil then elseif gamedata ~= nil and gamedata.errormessage ~= nil then
@ -107,12 +90,17 @@ function ui.update()
else else
error_title = fgettext("An error occurred:") error_title = fgettext("An error occurred:")
end end
local restart_btn = "button[5,6.6;4,1;btn_reconnect_no;" .. fgettext("Close") .. "]" local restart_btn
if maintab == "local" or maintab == "local_default" and if (maintab == "local" or maintab == "local_default") and
connect_time and os.time() - connect_time > 30 then core.get_us_time() - connect_time > 30 then
restart_btn = restart_btn =
"button[2,6.6;4,1;btn_reconnect_yes;" .. fgettext("Restart") .. "]" .. "button[2,6.6;4,1;btn_reconnect_yes;" .. fgettext("Restart") .. "]" ..
"button[8,6.6;4,1;btn_reconnect_no;" .. fgettext("Close") .. "]" "button[8,6.6;4,1;btn_reconnect_no;" .. fgettext("Main menu") .. "]" ..
"set_focus[btn_reconnect_yes;true]"
else
restart_btn =
"button[5,6.6;4,1;btn_reconnect_no;" .. fgettext("OK") .. "]" ..
"set_focus[btn_reconnect_no;true]"
end end
formspec = { formspec = {
"formspec_version[3]", "formspec_version[3]",
@ -208,7 +196,7 @@ core.button_handler = function(fields)
gamedata.selected_world = gamedata.selected_world =
tonumber(core.settings:get("mainmenu_last_selected_world")) tonumber(core.settings:get("mainmenu_last_selected_world"))
end end
core.settings:set("connect_time", os.time()) core.settings:set("connect_time", core.get_us_time())
gamedata.reconnect_requested = false gamedata.reconnect_requested = false
gamedata.errormessage = nil gamedata.errormessage = nil
gamedata.do_reconnect = true gamedata.do_reconnect = true
@ -248,3 +236,17 @@ core.event_handler = function(event)
return return
end end
end end
--------------------------------------------------------------------------------
if core.settings:get("just_reconnected") then
core.settings:remove("just_reconnected")
elseif gamedata and gamedata.errormessage == "AsyncErr: Failed to bind socket (port already in use?)" and
(maintab == "local" or maintab == "local_default") and not core.settings:get_bool("enable_server") then
core.settings:set("just_reconnected", "true")
gamedata.singleplayer = true
gamedata.selected_world =
tonumber(core.settings:get("mainmenu_last_selected_world"))
gamedata.errormessage = nil
gamedata.do_reconnect = true
core.start()
end