-
Notifications
You must be signed in to change notification settings - Fork 2
Feat server advertisement #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
760d805
Commit Server IP override
akirilov 1c350aa
Fix hardcoded IP
akirilov ca16e0d
Apply suggestions from code review
akirilov c63caeb
Moving server advertiser to a separate goroutine in the backend init
akirilov 7b56418
Update logger for advertiser
akirilov 04a97c7
Remove unnecessary waits
akirilov 95afab3
Make the advertisement server a const
akirilov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package advertiser | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "net/http" | ||
| "runtime" | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "github.com/JacksonTheMaster/StationeersServerUI/v5/src/config" | ||
| "github.com/JacksonTheMaster/StationeersServerUI/v5/src/logger" | ||
| "github.com/JacksonTheMaster/StationeersServerUI/v5/src/managers/detectionmgr" | ||
| "github.com/JacksonTheMaster/StationeersServerUI/v5/src/managers/gamemgr" | ||
| ) | ||
|
|
||
| const StationeersAdvertisementEndpoint = "http://40.82.200.175:8081/Ping" | ||
|
|
||
| type ServerAdMessage struct { | ||
| SessionId int | ||
| Name string | ||
| Password bool | ||
| Version string | ||
| Address string | ||
| Port string | ||
| Players int | ||
| MaxPlayers int | ||
| Type int | ||
| } | ||
|
|
||
| type ServerAdResponse struct { | ||
| SessionId int | ||
| Status string | ||
| } | ||
|
|
||
| func StartAdvertiser() { | ||
| if config.GetServerVisible() { | ||
| logger.Advertiser.Warn("Server advertisement is enabled. Disable it in the config and restart SSUI to use manual advertisement. Skipping for now...") | ||
| return | ||
| } | ||
| go func() { | ||
| sessionId := -1 | ||
| for { | ||
| // Only advertise if we are running | ||
| if gamemgr.InternalIsServerRunning() { | ||
| // Get max players | ||
| maxplayers, err := strconv.Atoi(config.GetServerMaxPlayers()) | ||
| if err != nil { | ||
| logger.Advertiser.Errorf("ServerAdvertiser failed to convert max players number to int: %s", config.GetServerMaxPlayers()) | ||
| return | ||
| } | ||
| // Get connected players | ||
| detector := detectionmgr.GetDetector() | ||
| players := len(detectionmgr.GetPlayers(detector)) | ||
| // Get platform | ||
| platform := 0 | ||
| switch runtime.GOOS { | ||
| case "windows": | ||
| platform = 1 | ||
| case "linux": | ||
| platform = 2 | ||
| } | ||
| adMessage := ServerAdMessage{ | ||
| SessionId: sessionId, | ||
| Name: config.GetServerName(), | ||
| Password: config.GetServerPassword() != "", | ||
| Version: config.GetExtractedGameVersion(), | ||
| Address: config.GetOverrideAdvertisedIp(), | ||
| Port: config.GetGamePort(), | ||
| Players: players, | ||
| MaxPlayers: maxplayers, | ||
| Type: platform, | ||
| } | ||
| body, err := json.Marshal(adMessage) | ||
| if err != nil { | ||
| logger.Advertiser.Errorf("ServerAdvertiser failed to Serialize to JSON from native Go struct type: %v", err) | ||
| return | ||
|
akirilov marked this conversation as resolved.
|
||
| } | ||
| // Send advertisement | ||
| resp, err := http.Post(StationeersAdvertisementEndpoint, "application/json", bytes.NewBuffer(body)) | ||
| // Check for errors | ||
| if err != nil { | ||
| logger.Advertiser.Errorf("ServerAdvertiser failed to send request: %v", err) | ||
| return | ||
|
akirilov marked this conversation as resolved.
|
||
| } | ||
| defer resp.Body.Close() | ||
| // Check the status | ||
| if resp.StatusCode != 200 { | ||
| logger.Advertiser.Warnf("ServerAdvertiser received non-200 status: %d", resp.StatusCode) | ||
| } | ||
| // Read the response and update our sessionId if needed | ||
| adResponse := ServerAdResponse{} | ||
| err = json.NewDecoder(resp.Body).Decode(&adResponse) | ||
| if err != nil { | ||
| logger.Advertiser.Errorf("Failed to decode response body: %v", err) | ||
| return | ||
|
akirilov marked this conversation as resolved.
|
||
| } | ||
| if adResponse.Status != "Success" { | ||
| logger.Advertiser.Warnf("ServerAdvertiser received unexpected status: %s", adResponse.Status) | ||
| } | ||
| sessionId = adResponse.SessionId | ||
| } else { | ||
| // Reset sessionid for the next run | ||
| sessionId = -1 | ||
| } | ||
| // Sleep for 30 seconds to follow the standard advertisement timer | ||
| time.Sleep(30 * time.Second) | ||
| } | ||
| }() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.