Skip to content

Commit 9df001d

Browse files
authored
Merge pull request #495 from V-Sekai/lint-http-pool
Lint http_pool.gd
2 parents 9d2242f + 9cee7ff commit 9df001d

File tree

7 files changed

+129
-127
lines changed

7 files changed

+129
-127
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ repos:
66
entry: gdlint
77
language: python
88
additional_dependencies: [gdtoolkit==4.3.3]
9-
files: ^addons/godot_uro/godot_uro_helper\.gd$
9+
files: ^addons/godot_uro/(godot_uro_helper|http_pool)\.gd$
1010
types: [gdscript]
1111

1212
# vsk_importer_exporter is ignored due to broken code.

addons/canvas_plane/canvas_3d.gd.uid

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://b3xvbtqpeqgll

addons/godot-xr-tools/hands/animations/left/hand_blend_tree.tres

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ filters = ["Armature/Skeleton3D:Index_Distal_L", "Armature/Skeleton3D:Index_Inte
1919

2020
[resource]
2121
graph_offset = Vector2(-473, -37)
22+
nodes/output/position = Vector2(600, 140)
2223
nodes/Default/node = SubResource("1")
2324
nodes/Default/position = Vector2(-280, 40)
2425
nodes/Fist/node = SubResource("2")
@@ -29,5 +30,4 @@ nodes/Grip/node = SubResource("4")
2930
nodes/Grip/position = Vector2(240, 40)
3031
nodes/Trigger/node = SubResource("5")
3132
nodes/Trigger/position = Vector2(20, 40)
32-
nodes/output/position = Vector2(600, 140)
33-
node_connections = [&"Grip", 0, &"Trigger", &"Grip", 1, &"Fist2", &"Trigger", 0, &"Default", &"Trigger", 1, &"Fist", &"output", 0, &"Grip"]
33+
node_connections = [&"output", 0, &"Grip", &"Grip", 0, &"Trigger", &"Grip", 1, &"Fist2", &"Trigger", 0, &"Default", &"Trigger", 1, &"Fist"]

addons/godot-xr-tools/hands/animations/right/hand_blend_tree.tres

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ filters = ["Armature/Skeleton3D:Index_Distal_R", "Armature/Skeleton3D:Index_Inte
1919

2020
[resource]
2121
graph_offset = Vector2(-364.926, -35.3379)
22+
nodes/output/position = Vector2(600, 140)
2223
nodes/Default/node = SubResource("1")
2324
nodes/Default/position = Vector2(-200, 100)
2425
nodes/Fist/node = SubResource("2")
@@ -29,5 +30,4 @@ nodes/Grip/node = SubResource("4")
2930
nodes/Grip/position = Vector2(240, 40)
3031
nodes/Trigger/node = SubResource("5")
3132
nodes/Trigger/position = Vector2(20, 40)
32-
nodes/output/position = Vector2(600, 140)
33-
node_connections = [&"Grip", 0, &"Trigger", &"Grip", 1, &"Fist2", &"Trigger", 0, &"Default", &"Trigger", 1, &"Fist", &"output", 0, &"Grip"]
33+
node_connections = [&"output", 0, &"Grip", &"Grip", 0, &"Trigger", &"Grip", 1, &"Fist2", &"Trigger", 0, &"Default", &"Trigger", 1, &"Fist"]

addons/godot_uro/http_pool.gd

+122-120
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,24 @@
55

66
extends Node
77

8-
9-
class Future:
10-
signal completed(http: HTTPClient)
11-
8+
signal http_tick
129

1310
var next_request: int = 0
1411
var pending_requests: Dictionary = {} # int -> Future
1512

1613
var http_client_pool: Array[HTTPClient]
1714
var total_http_clients: int = 0
1815

19-
signal http_tick
16+
17+
class Future:
18+
signal completed(http: HTTPClient)
2019

2120

2221
class HTTPState:
22+
signal connection_finished(http_client: HTTPClient)
23+
signal request_finished(success: bool)
24+
signal download_progressed(bytes: int, total_bytes: int)
25+
2326
const YIELD_PERIOD_MS = 50
2427

2528
var out_path: String = ""
@@ -41,11 +44,6 @@ class HTTPState:
4144
var bytes: int
4245
var total_bytes: int
4346

44-
signal _connection_finished(http_client: HTTPClient)
45-
signal _request_finished(success: bool)
46-
47-
signal download_progressed(bytes: int, total_bytes: int)
48-
4947
func _init(p_http_pool: Node, p_http_client: HTTPClient):
5048
self.http = p_http_client
5149
self.http_pool = p_http_pool
@@ -65,121 +63,127 @@ class HTTPState:
6563
http.close()
6664
http = HTTPClient.new()
6765

68-
func http_tick() -> void:
69-
if not sent_request:
70-
if terminated:
71-
if file:
72-
file.close()
73-
_connection_finished.emit(null)
74-
return
75-
76-
if cancelled:
77-
if file:
78-
file.close()
79-
cancelled = false
80-
busy = false
81-
http.close()
82-
_connection_finished.emit(null)
83-
return
84-
85-
var _poll_error: int = http.poll()
86-
status = http.get_status()
87-
88-
if (
89-
status == HTTPClient.STATUS_CONNECTED
90-
or status == HTTPClient.STATUS_REQUESTING
91-
or status == HTTPClient.STATUS_BODY
92-
):
93-
_connection_finished.emit(http)
94-
return
95-
if (
96-
status != HTTPClient.STATUS_CONNECTING
97-
and status != HTTPClient.STATUS_RESOLVING
98-
and status != HTTPClient.STATUS_CONNECTED
99-
):
100-
busy = false
101-
push_error(
102-
(
103-
"GodotUroRequester: could not connect to host: status = %s"
104-
% [str(http.get_status())]
105-
)
106-
)
107-
_connection_finished.emit(null)
108-
return
109-
return
110-
111-
status = http.get_status()
66+
func handle_connection_tick() -> void:
11267
if terminated:
11368
if file:
11469
file.close()
115-
_request_finished.emit(false)
70+
connection_finished.emit(null)
11671
return
11772

118-
if status != HTTPClient.STATUS_REQUESTING and status != HTTPClient.STATUS_BODY:
119-
_request_finished.emit(false)
73+
var poll_error: int = http.poll()
74+
status = http.get_status()
75+
76+
if (
77+
status == HTTPClient.STATUS_CONNECTED
78+
or status == HTTPClient.STATUS_REQUESTING
79+
or status == HTTPClient.STATUS_BODY
80+
):
81+
connection_finished.emit(http)
82+
return
83+
84+
if (
85+
status != HTTPClient.STATUS_CONNECTING
86+
and status != HTTPClient.STATUS_RESOLVING
87+
and status != HTTPClient.STATUS_CONNECTED
88+
):
89+
busy = false
90+
push_error(
91+
(
92+
"GodotUroRequester: could not connect to host: status = %s"
93+
% [str(http.get_status())]
94+
)
95+
)
96+
connection_finished.emit(null)
12097
return
12198

122-
if cancelled:
99+
func handle_request_tick() -> void:
100+
var exit_result = null # Can be true, false, or null (not exiting)
101+
102+
status = http.get_status()
103+
104+
if terminated:
105+
if file:
106+
file.close()
107+
exit_result = false
108+
elif status != HTTPClient.STATUS_REQUESTING and status != HTTPClient.STATUS_BODY:
109+
exit_result = false
110+
elif cancelled:
123111
if file:
124112
file.close()
125113
cancelled = false
126114
busy = false
127115
http.close()
128-
_request_finished.emit(false)
129-
return
130-
131-
if status == HTTPClient.STATUS_REQUESTING:
132-
http.poll()
133-
if status == HTTPClient.STATUS_BODY:
134-
response_code = http.get_response_code()
135-
response_headers = http.get_response_headers_as_dictionary()
136-
137-
bytes = 0
138-
if response_headers.has("Content-Length"):
139-
total_bytes = int(response_headers["Content-Length"])
140-
else:
141-
total_bytes = -1
142-
if not out_path.is_empty():
143-
file = FileAccess.open(out_path, FileAccess.WRITE)
144-
if file.is_null():
116+
exit_result = false
117+
else:
118+
if status == HTTPClient.STATUS_REQUESTING:
119+
http.poll()
120+
if status == HTTPClient.STATUS_BODY:
121+
response_code = http.get_response_code()
122+
response_headers = http.get_response_headers_as_dictionary()
123+
124+
bytes = 0
125+
if response_headers.has("Content-Length"):
126+
total_bytes = int(response_headers["Content-Length"])
127+
else:
128+
total_bytes = -1
129+
if not out_path.is_empty():
130+
file = FileAccess.open(out_path, FileAccess.WRITE)
131+
if file.is_null():
132+
busy = false
133+
status = HTTPClient.STATUS_CONNECTED
134+
exit_result = false
135+
136+
# Only proceed if no exit yet
137+
if exit_result == null:
138+
var last_yield = Time.get_ticks_msec()
139+
var time: int = 0
140+
var should_yield := false
141+
142+
while status == HTTPClient.STATUS_BODY and exit_result == null:
143+
var poll_error: int = http.poll()
144+
145+
var chunk = http.read_response_body_chunk()
146+
response_code = http.get_response_code()
147+
148+
if file:
149+
file.store_buffer(chunk)
150+
else:
151+
response_body.append_array(chunk)
152+
bytes += chunk.size()
153+
self.download_progressed.emit(bytes, total_bytes)
154+
155+
time = Time.get_ticks_msec()
156+
157+
status = http.get_status()
158+
if status == HTTPClient.STATUS_CONNECTION_ERROR and !terminated and !cancelled:
159+
if file:
160+
file.close()
145161
busy = false
146-
status = HTTPClient.STATUS_CONNECTED # failed to write to file
147-
_request_finished.emit(false)
148-
return
149-
150-
var last_yield = Time.get_ticks_msec()
151-
while status == HTTPClient.STATUS_BODY:
152-
var _poll_error: int = http.poll()
153-
154-
var chunk = http.read_response_body_chunk()
155-
response_code = http.get_response_code()
162+
exit_result = false
163+
else:
164+
if status != HTTPClient.STATUS_BODY:
165+
busy = false
166+
exit_result = true
167+
if file:
168+
file.close()
169+
else:
170+
if time - last_yield > YIELD_PERIOD_MS:
171+
should_yield = true
172+
break
173+
174+
if should_yield:
175+
return # Yield without finishing
176+
177+
# Emit result and exit if needed
178+
if exit_result != null:
179+
request_finished.emit(exit_result)
180+
return
156181

157-
if file:
158-
file.store_buffer(chunk)
159-
else:
160-
response_body.append_array(chunk)
161-
bytes += chunk.size()
162-
self.download_progressed.emit(bytes, total_bytes)
163-
164-
var time = Time.get_ticks_msec()
165-
166-
status = http.get_status()
167-
if status == HTTPClient.STATUS_CONNECTION_ERROR and !terminated and !cancelled:
168-
if file:
169-
file.close()
170-
busy = false
171-
_request_finished.emit(false)
172-
return
173-
174-
if status != HTTPClient.STATUS_BODY:
175-
busy = false
176-
_request_finished.emit(true)
177-
if file:
178-
file.close()
179-
return
180-
181-
if time - last_yield > YIELD_PERIOD_MS:
182-
return
182+
func http_tick() -> void:
183+
if not sent_request:
184+
handle_connection_tick()
185+
else:
186+
handle_request_tick()
183187

184188
func connect_http(hostname: String, port: int, use_ssl: bool) -> HTTPClient:
185189
sent_request = false
@@ -228,25 +232,23 @@ class HTTPState:
228232
return null
229233

230234
http_pool.http_tick.connect(self.http_tick)
231-
var ret = await self._connection_finished
235+
var ret = await self.connection_finished
232236
http_pool.http_tick.disconnect(self.http_tick)
233237
return ret
234238

235239
func wait_for_request():
236240
sent_request = true
237241
http_pool.http_tick.connect(self.http_tick)
238-
var ret = await self._request_finished
242+
var ret = await self.request_finished
239243
call_deferred("release")
240244
return ret
241245

242246
func release():
243247
if not http_pool:
244248
return
245-
#print("Do release")
246249
if http_pool.http_tick.is_connected(self.http_tick):
247250
http_pool.http_tick.disconnect(self.http_tick)
248251
if self.http_pool != null and self.http != null:
249-
#print("Release http")
250252
self.http_pool._release_client(self.http)
251253
self.http_pool = null
252254
self.http = null
@@ -270,6 +272,11 @@ func _init(p_http_client_limit: int = 5):
270272
http_client_pool.push_back(HTTPClient.new())
271273

272274

275+
func new_http_state() -> HTTPState:
276+
var http_client: HTTPClient = await _acquire_client()
277+
return HTTPState.new(self, http_client)
278+
279+
273280
func _acquire_client() -> HTTPClient:
274281
if not http_client_pool.is_empty():
275282
return http_client_pool.pop_back()
@@ -289,8 +296,3 @@ func _release_client(http: HTTPClient):
289296
f.completed.emit(http)
290297
else:
291298
http_client_pool.push_back(http)
292-
293-
294-
func new_http_state() -> HTTPState:
295-
var http_client: HTTPClient = await _acquire_client()
296-
return HTTPState.new(self, http_client)

addons/vsk_entities/extensions/infotag.tscn

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[gd_scene load_steps=2 format=3 uid="uid://bpxjakh1f68nw"]
22

3-
[ext_resource type="Script" path="res://addons/vsk_entities/extensions/info_tag.gd" id="1"]
3+
[ext_resource type="Script" uid="uid://dg2tq3ibig4rj" path="res://addons/vsk_entities/extensions/info_tag.gd" id="1"]
44

55
[node name="InfoTag" type="Node3D"]
66
transform = Transform3D(2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 2, 0)

project.godot

-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ NetworkManager="*res://addons/network_manager/network_manager.gd"
9696
ScreenshotManager="*res://addons/sar1_screenshot_manager/screenshot_manager.gd"
9797
ConnectionUtil="*res://addons/gd_util/connection_util.gd"
9898
VSKVersion="*res://addons/vsk_version/vsk_version.gd"
99-
; Telemetry="*res://addons/telemetry/telemetry.gd"
10099

101100
[compression]
102101

0 commit comments

Comments
 (0)