mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-04 23:40:01 +07:00
SimpleHttp gets a connection timeout (#6812)
* SimpleHttp gets a connection timeout
This commit is contained in:
@ -8,25 +8,28 @@ import java.io.InputStreamReader
|
||||
import java.net.*
|
||||
import java.nio.charset.Charset
|
||||
|
||||
private typealias SendRequestCallback = (success: Boolean, result: String, code: Int?)->Unit
|
||||
|
||||
object SimpleHttp {
|
||||
fun sendGetRequest(url: String, action: (success: Boolean, result: String, code: Int?)->Unit) {
|
||||
sendRequest(Net.HttpMethods.GET, url, "", action)
|
||||
fun sendGetRequest(url: String, timeout: Int = 5000, action: SendRequestCallback) {
|
||||
sendRequest(Net.HttpMethods.GET, url, "", timeout, action)
|
||||
}
|
||||
|
||||
fun sendRequest(method: String, url: String, content: String, action: (success: Boolean, result: String, code: Int?)->Unit) {
|
||||
fun sendRequest(method: String, url: String, content: String, timeout: Int = 5000, action: SendRequestCallback) {
|
||||
var uri = URI(url)
|
||||
if (uri.host == null) uri = URI("http://$url")
|
||||
|
||||
val urlObj: URL
|
||||
try {
|
||||
urlObj = uri.toURL()
|
||||
} catch (t:Throwable){
|
||||
} catch (t: Throwable) {
|
||||
action(false, "Bad URL", null)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
with(urlObj.openConnection() as HttpURLConnection) {
|
||||
requestMethod = method // default is GET
|
||||
connectTimeout = timeout
|
||||
if (UncivGame.isCurrentInitialized())
|
||||
setRequestProperty("User-Agent", "Unciv/${UncivGame.Current.version}-GNU-Terry-Pratchett")
|
||||
else
|
||||
|
@ -4,10 +4,10 @@ import com.badlogic.gdx.Net
|
||||
import java.io.FileNotFoundException
|
||||
import java.lang.Exception
|
||||
|
||||
class UncivServerFileStorage(val serverUrl:String): FileStorage {
|
||||
class UncivServerFileStorage(val serverUrl: String, val timeout: Int = 30000) : FileStorage {
|
||||
override fun saveFileData(fileName: String, data: String, overwrite: Boolean) {
|
||||
SimpleHttp.sendRequest(Net.HttpMethods.PUT, "$serverUrl/files/$fileName", data) {
|
||||
success, result, code ->
|
||||
SimpleHttp.sendRequest(Net.HttpMethods.PUT, fileUrl(fileName), data, timeout) {
|
||||
success, result, _ ->
|
||||
if (!success) {
|
||||
println(result)
|
||||
throw Exception(result)
|
||||
@ -17,7 +17,7 @@ class UncivServerFileStorage(val serverUrl:String): FileStorage {
|
||||
|
||||
override fun loadFileData(fileName: String): String {
|
||||
var fileData = ""
|
||||
SimpleHttp.sendGetRequest("$serverUrl/files/$fileName"){
|
||||
SimpleHttp.sendGetRequest(fileUrl(fileName), timeout = timeout){
|
||||
success, result, code ->
|
||||
if (!success) {
|
||||
println(result)
|
||||
@ -37,7 +37,7 @@ class UncivServerFileStorage(val serverUrl:String): FileStorage {
|
||||
}
|
||||
|
||||
override fun deleteFile(fileName: String) {
|
||||
SimpleHttp.sendRequest(Net.HttpMethods.DELETE, "$serverUrl/files/$fileName", "") {
|
||||
SimpleHttp.sendRequest(Net.HttpMethods.DELETE, fileUrl(fileName), "", timeout) {
|
||||
success, result, code ->
|
||||
if (!success) {
|
||||
when (code) {
|
||||
@ -48,4 +48,5 @@ class UncivServerFileStorage(val serverUrl:String): FileStorage {
|
||||
}
|
||||
}
|
||||
|
||||
private fun fileUrl(fileName: String) = "$serverUrl/files/$fileName"
|
||||
}
|
||||
|
@ -137,8 +137,15 @@ class OptionsPopup(
|
||||
(previousScreen.game.screen as BaseScreen).openOptionsPopup(tabs.activePage)
|
||||
}
|
||||
|
||||
private fun successfullyConnectedToServer(action: (Boolean, String, Int?) -> Unit){
|
||||
SimpleHttp.sendGetRequest("${settings.multiplayerServer}/isalive", action)
|
||||
private fun successfullyConnectedToServer(action: (Boolean) -> Unit) {
|
||||
launchCrashHandling("TestIsAlive") {
|
||||
SimpleHttp.sendGetRequest("${settings.multiplayerServer}/isalive") {
|
||||
success, _, _ ->
|
||||
postCrashHandlingRunnable {
|
||||
action(success)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//region Page builders
|
||||
@ -307,7 +314,7 @@ class OptionsPopup(
|
||||
}
|
||||
popup.open(true)
|
||||
|
||||
successfullyConnectedToServer { success, _, _ ->
|
||||
successfullyConnectedToServer { success ->
|
||||
popup.addGoodSizedLabel(if (success) "Success!" else "Failed!").row()
|
||||
popup.addCloseButton()
|
||||
}
|
||||
|
Reference in New Issue
Block a user