Skip to content

Commit

Permalink
If SERVER_SRS_HOST_AUTO is enabled only send the port in the autoconn…
Browse files Browse the repository at this point in the history
…ect message

The receiver can then use net.get_server_host() to include the IP address of the server
  • Loading branch information
Camble committed Jul 11, 2024
1 parent e7d329f commit 269f62b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 53 deletions.
40 changes: 15 additions & 25 deletions Scripts/DCS-SRS-AutoConnectGameGUI.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ SRSAuto.unicast = true
local HOST_PLAYER_ID = 1

SRSAuto.MESSAGE_PREFIX = "SRS Running @ " -- DO NOT MODIFY!!!
SRSAuto.MESSAGE_PREFIX_PORT = "SRS Running on " -- DO NOT MODIFY!!!

package.path = package.path..";.\\LuaSocket\\?.lua;"
package.cpath = package.cpath..";.\\LuaSocket\\?.dll;"
Expand All @@ -67,26 +68,33 @@ function SRSAuto.log(str)
end
end

function SRSAuto.sendAutoConnectMessage(id)
SRSAuto.log(string.format("Sending auto connect message to player %d on connect ", id))
if SRSAuto.SERVER_SRS_HOST_AUTO then
net.send_chat_to(SRSAuto.MESSAGE_PREFIX_PORT .. SRSAuto.SERVER_SRS_PORT, id)
else
net.send_chat_to(SRSAuto.MESSAGE_PREFIX .. SRSAuto.SERVER_SRS_HOST .. ":" .. SRSAuto.SERVER_SRS_PORT, id)
end
end

-- Register callbacks --

SRSAuto.onPlayerConnect = function(id)
if not DCS.isServer() then
return
end
if SRSAuto.SERVER_SEND_AUTO_CONNECT and id ~= HOST_PLAYER_ID then
SRSAuto.log(string.format("Sending auto connect message to player %d on connect ", id))
net.send_chat_to(string.format(SRSAuto.MESSAGE_PREFIX .. "%s", SRSAuto.SERVER_SRS_HOST..":"..SRSAuto.SERVER_SRS_PORT), id)
end
if SRSAuto.SERVER_SEND_AUTO_CONNECT and id ~= HOST_PLAYER_ID then
sendAutoConnectMessage(id)
end
end

SRSAuto.onPlayerChangeSlot = function(id)
if not DCS.isServer() then
return
end
if SRSAuto.SERVER_SEND_AUTO_CONNECT and id ~= HOST_PLAYER_ID then
SRSAuto.log(string.format("Sending auto connect message to player %d on switch ", id))
net.send_chat_to(string.format(SRSAuto.MESSAGE_PREFIX .. "%s", SRSAuto.SERVER_SRS_HOST..":"..SRSAuto.SERVER_SRS_PORT), id)
end
sendAutoConnectMessage(id)
end
end

SRSAuto.trimStr = function(_str)
Expand Down Expand Up @@ -119,24 +127,6 @@ end
local _lastSent = 0

SRSAuto.onMissionLoadBegin = function()
local _status, _result = pcall( function()
if SRSAuto.SERVER_SRS_HOST_AUTO then
local ipLookupUrl = "https://ipv4.icanhazip.com"
local T, code, headers, status = socket.http.request(ipLookupUrl)

if T == nil or code == nil or code < 200 or code >= 300 then
if code == nil then code = "??" end
SRSAuto.log("Failed to lookup IP from "..ipLookupUrl..". Http Status: " .. code)
else
SRSAuto.SERVER_SRS_HOST = T
SRSAuto.log("SET IP automatically to "..SRSAuto.SERVER_SRS_HOST)
end
end
end)

if not _status then
SRSAuto.log('ERROR: ' .. _result)
end
end

SRSAuto.onSimulationFrame = function()
Expand Down
62 changes: 34 additions & 28 deletions Scripts/DCS-SRS/Scripts/DCS-SRSGameGUI.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ SRS.sendUpdate = function(playerID)
socket.try(SRS.UDPSendSocket:sendto(_jsonUpdate, "127.0.0.1", 9087))
end

SRS.MESSAGE_PREFIX_OLD = "This server is running SRS on - " -- DO NOT MODIFY!!!
SRS.MESSAGE_PREFIX = "SRS Running @ " -- DO NOT MODIFY!!!
SRS.MESSAGE_PATTERN_OLDER = "This server is running SRS on - (%d+%.%d+%.%d+%.%d+:%d+)" -- DO NOT MODIFY!!!
SRS.MESSAGE_PATTERN_OLD = "SRS Running @ (%d+%.%d+%.%d+%.%d+:%d+)" -- DO NOT MODIFY!!!
SRS.MESSAGE_PATTERN = "SRS Running on (%d+)" -- DO NOT MODIFY!!!

function string.startsWith(string, prefix)
return string.sub(string, 1, string.len(prefix)) == prefix
Expand All @@ -112,16 +113,15 @@ function string.trim(_str)
return string.format( "%s", _str:match( "^%s*(.-)%s*$" ) )
end

function SRS.isAutoConnectMessage(msg)
return string.startsWith(string.trim(msg), SRS.MESSAGE_PREFIX) or string.startsWith(string.trim(msg), SRS.MESSAGE_PREFIX_OLD)
function SRS.getPortFromMessage(msg)
return msg:match(SRS.MESSAGE_PATTERN)
end

function SRS.getHostFromMessage(msg)
if string.startsWith(string.trim(msg), SRS.MESSAGE_PREFIX_OLD) then
return string.trim(string.sub(msg, string.len(SRS.MESSAGE_PREFIX_OLD) + 1))
else
return string.trim(string.sub(msg, string.len(SRS.MESSAGE_PREFIX) + 1))
end
local host = msg:match(SRS.MESSAGE_PATTERN_OLD)
if host ~= nil then return host end

return msg:match(SRS.MESSAGE_PATTERN_OLDER)
end

-- Register callbacks --
Expand Down Expand Up @@ -307,27 +307,33 @@ SRS.handleRadio = function(msg)
end

SRS.onChatMessage = function(msg, from)
-- Only accept auto connect message coming from host.
if SRS.CLIENT_ACCEPT_AUTO_CONNECT and from == 1 then
local host = nil
local port = SRS.getPortFromMessage(msg)
if port ~= nil then
local ip = net.get_server_host()
host = ip .. ':' .. port
else
host = SRS.getHostFromMessage(msg)
end
if host == nil then
SRS.log("Error getting host from message: " .. msg)
return
end

SRS.log(string.format("Got SRS Auto Connect message: %s", host))

-- Only accept auto connect message coming from host.
if SRS.CLIENT_ACCEPT_AUTO_CONNECT
and from == 1
and SRS.isAutoConnectMessage(msg) then
local host = SRS.getHostFromMessage(msg)
SRS.log(string.format("Got SRS Auto Connect message: %s", host))

local enabled = OptionsData.getPlugin("DCS-SRS","srsAutoLaunchEnabled")
if srs and enabled then
local path = srs.get_srs_path()
if path ~= "" then

net.log("Trying to Launch SRS @ "..path)
srs.start_srs(host)
end

end
SRS.sendConnect(host)
end
local enabled = OptionsData.getPlugin("DCS-SRS", "srsAutoLaunchEnabled")
if srs and enabled then
local path = srs.get_srs_path()
if path ~= "" then
net.log("Trying to Launch SRS @ " .. path)
srs.start_srs(host)
end
end
SRS.sendConnect(host)
end

-- MESSAGE FROM MYSELF
if from == net.get_my_player_id() then
Expand Down

0 comments on commit 269f62b

Please sign in to comment.