Updated SammiClient
Updated API per suggestion of ImamuraCross on SAMMI Community discord. Added Timeout check when attempting to connect. Added Queue for requests when a request is in the process of running.
This commit is contained in:
parent
d23ac2f7c5
commit
3c874d0218
1 changed files with 51 additions and 2 deletions
|
|
@ -7,15 +7,19 @@ class_name SammiClient
|
|||
## not set the Content-Length header for response headers to the API. Hence it will fail with a
|
||||
## normal Godot HTTPClient.
|
||||
|
||||
|
||||
signal request_completed(response: SammiResponse)
|
||||
|
||||
|
||||
@export_category("SAMMI Connection Information")
|
||||
@export var host: String = "127.0.0.1"
|
||||
@export var port: int = 9450
|
||||
@export var user_agent: String = "GodotSammi/0.1 (%s)" % OS.get_name()
|
||||
@export var timeout_limit: int = 200
|
||||
@export_category("Authentication")
|
||||
@export_custom(PROPERTY_HINT_PASSWORD, "") var password: String = ""
|
||||
|
||||
|
||||
var client: StreamPeerTCP
|
||||
var state: State = State.CLOSED
|
||||
var buffer: PackedByteArray
|
||||
|
|
@ -24,6 +28,9 @@ var response: SammiResponse
|
|||
var query: String = ""
|
||||
var method: Method = Method.GET
|
||||
var body: String = ""
|
||||
var queue: Array[QueueRequest] = []
|
||||
var timeout: int = 0
|
||||
|
||||
|
||||
enum State {
|
||||
CONNECTING,
|
||||
|
|
@ -33,13 +40,18 @@ enum State {
|
|||
CLOSED
|
||||
}
|
||||
|
||||
|
||||
enum Method {
|
||||
GET,
|
||||
POST,
|
||||
}
|
||||
|
||||
|
||||
func request(method: Method, api_path: String, body: String) -> void:
|
||||
assert(state == State.CLOSED, "A request is in the process of being made.")
|
||||
if state != State.CLOSED:
|
||||
var req = QueueRequest.new(method, api_path, body)
|
||||
queue.append(req)
|
||||
return
|
||||
|
||||
self.query = api_path
|
||||
self.body = body
|
||||
|
|
@ -48,6 +60,7 @@ func request(method: Method, api_path: String, body: String) -> void:
|
|||
self.client.connect_to_host(host, port)
|
||||
self.state = State.CONNECTING
|
||||
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
if not client:
|
||||
return
|
||||
|
|
@ -63,9 +76,22 @@ func _process(_delta: float) -> void:
|
|||
_read_response()
|
||||
elif client.get_status() == StreamPeerTCP.Status.STATUS_ERROR:
|
||||
state = State.ERROR
|
||||
print("Error: ", client.get_error())
|
||||
client.disconnect_from_host()
|
||||
client = null
|
||||
_check_queue()
|
||||
elif client.get_status() != StreamPeerTCP.Status.STATUS_CONNECTING:
|
||||
print("AGAIN")
|
||||
if timeout == 0:
|
||||
timeout = Time.get_ticks_msec()
|
||||
elif Time.get_ticks_msec() - timeout >= timeout_limit:
|
||||
state = State.CLOSED
|
||||
print("Timeout")
|
||||
timeout = 0
|
||||
client.disconnect_from_host()
|
||||
client = null
|
||||
_check_queue()
|
||||
return
|
||||
|
||||
|
||||
func _send_request() -> void:
|
||||
var request_str = ""
|
||||
|
|
@ -90,6 +116,7 @@ func _send_request() -> void:
|
|||
request_str += body
|
||||
client.put_data(request_str.to_utf8_buffer())
|
||||
|
||||
|
||||
func _read_response() -> void:
|
||||
client.poll()
|
||||
|
||||
|
|
@ -129,9 +156,19 @@ func _read_response() -> void:
|
|||
state = State.CLOSED
|
||||
headers = []
|
||||
buffer = []
|
||||
timeout = 0
|
||||
client.disconnect_from_host()
|
||||
client = null
|
||||
request_completed.emit(response)
|
||||
_check_queue()
|
||||
|
||||
|
||||
func _check_queue() -> void:
|
||||
if queue.is_empty():
|
||||
return
|
||||
|
||||
var req: QueueRequest = queue.pop_front()
|
||||
request(req.method, req.path, req.body)
|
||||
|
||||
|
||||
class SammiResponse:
|
||||
|
|
@ -157,3 +194,15 @@ class SammiResponse:
|
|||
code = 500
|
||||
error_message = "Unknown Error"
|
||||
query = p_query
|
||||
|
||||
class QueueRequest:
|
||||
extends RefCounted
|
||||
|
||||
var method: Method
|
||||
var path: String
|
||||
var body: String
|
||||
|
||||
func _init(p_method: Method, p_path: String, p_body: String) -> void:
|
||||
method = p_method
|
||||
path = p_path
|
||||
body = p_body
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue