Skip to content

Commit 2e1d6e2

Browse files
committed
fix bug in thread logic
1 parent 2cb2255 commit 2e1d6e2

2 files changed

Lines changed: 91 additions & 73 deletions

File tree

user/sofplus/addons/!thread.func

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//Usage
2+
//thread_create 100 myloop loopref
3+
//thread_stop $loopref
4+
function thread_init()
5+
{
6+
sp_sc_func_alias thread_create thread_create
7+
sp_sc_func_alias thread_kill thread_kill
8+
set threads_nonce 0
9+
}
10+
11+
//If the thread need be stopped from the outside,supply a ret_cvar to store it.
12+
//This function creates a new thread, with a new nonce.
13+
function thread_create(~freq, ~callback,*)
14+
{
15+
//_t_001_signal
16+
sp_sc_cvar_sset ~signal _t_ $threads_nonce _ signal
17+
set $~signal 1
18+
19+
sp_sc_func_exec thread_loop $threads_nonce $~freq $~callback
20+
21+
sp_sc_flow_if number cvar ~0 > val 0
22+
{
23+
sp_sc_flow_if text cvar ~1 != val ""
24+
{
25+
//optional ret_cvar
26+
set #~1 $threads_nonce
27+
}
28+
}
29+
30+
31+
add threads_nonce 1
32+
}
33+
//The nonce is the index into the 'cvars'.
34+
function thread_loop(~nonce,~freq,~callback)
35+
{
36+
37+
sp_sc_cvar_sset ~signal _t_ $~nonce _ signal
38+
sp_sc_flow_if number cvar $~signal == val 1
39+
{
40+
//run their callback.
41+
sp_sc_func_exec $~callback
42+
sset ~cmd sp_sc_func_exec thread_loop $~nonce $~freq $~callback
43+
44+
//run this function again in X.
45+
sp_sc_timer $~freq #~cmd
46+
}
47+
}
48+
//Strong immutable signal - because we do not set signal to 1 anywhere else.
49+
function thread_kill(~nonce_cvar)
50+
{
51+
sp_sc_cvar_copy ~nonce $~nonce_cvar
52+
//Always one thread exists as old one is killed.
53+
sp_sc_flow_if text cvar ~nonce != val ""
54+
{
55+
sp_sc_cvar_sset ~signal _t_ $~nonce _ signal
56+
set $~signal 0
57+
zero $~nonce_cvar
58+
}
59+
60+
}

user/sofplus/addons/http2.func

Lines changed: 31 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ function http2_init()
77
// udp downloads begin
88
sp_sc_func_exec spf_sc_list_onchange_add_func cl_download_filename http2_dl_filename
99

10-
set _http2_timer 0
10+
11+
zero _http2_prev_thread
1112
set _http2_not_exist_local 0
12-
set _http2_thread_ids 0
1313
set _http2_out_in 1
1414

1515
set _http2_enabled 1
@@ -19,7 +19,7 @@ function http2_init()
1919

2020
function http2_init_print()
2121
{
22-
echo "[HTTP] Version v1.8 Loaded"
22+
echo "[HTTP] Version v1.9 Loaded"
2323
}
2424

2525
//2 -> 4 -> 7 -> 8
@@ -37,26 +37,22 @@ function http2_state_change(*)
3737
sp_sc_flow_if text cvar _sp_cl_info_server != val "localhost"
3838
{
3939
sp_sc_cvar_copy ~state #~1
40-
//echo state is $~state
41-
// fully disconnected
40+
41+
// Fully Disconnected...
4242
sp_sc_flow_if text cvar ~state == val 1
4343
{
44-
sp_sc_func_exec http2_fast_loop $_http2_thread_ids "stop"
44+
thread_kill _http2_prev_thread
4545
set _http2_out_in 1
4646
}
4747
// Attempting Connect...
4848
sp_sc_flow_if text cvar ~state == val 2
4949
{
50-
//echo "state 2"
5150
sp_cl_info_map
52-
//echo HMM: $_sp_cl_info_map
5351
}
54-
// Reconnecting..
52+
// Reconnecting...
5553
sp_sc_flow_if text cvar ~state == val 4
5654
{
57-
//echo "state 4"
5855
sp_cl_info_map
59-
//echo HMM: $_sp_cl_info_map
6056
sp_sc_flow_if text cvar _http2_out_in == val 0
6157
{
6258
// This resets _sp_cl_info_map to ""
@@ -68,8 +64,6 @@ function http2_state_change(*)
6864
// Connecting..
6965
sp_sc_flow_if text cvar ~state == val 7
7066
{
71-
//echo "State 7"
72-
7367
sp_sc_func_exec http2_on_connect
7468
}
7569

@@ -82,83 +76,47 @@ function http2_state_change(*)
8276
sp_sc_cvar_sset ~text "[HTTP] " #_sp_cl_info_map ".zip was downloaded."
8377
echo #~text
8478
}
85-
sp_sc_func_exec http2_fast_loop $_http2_thread_ids "stop"
79+
thread_kill _http2_prev_thread
8680
}
8781
}
8882
}
8983
}
9084
// State == 7
9185
function http2_on_connect()
9286
{
93-
//echo ON CONNECT
87+
9488
set _http2_out_in 0
95-
set _http2_timer 0
9689
set _http2_not_exist_local 0
9790

98-
//Only bounce between 1 and 5
99-
add _http2_thread_ids 1
100-
sp_sc_flow_if number cvar _http2_thread_ids > val 5
101-
{
102-
set _http2_thread_ids 1
103-
}
104-
sp_sc_func_exec http2_fast_loop $_http2_thread_ids "start"
91+
thread_kill _http2_prev_thread
92+
93+
//We do not know how to store it.
94+
thread_create 100 http2_fast_loop _http2_prev_thread
95+
10596
}
10697

10798
// Pros: Works even on linux server because doesn't depend on serverstatus
10899
// A loop to get the mapname spamming sp_cl_info_map
109-
function http2_fast_loop(~thread_id, ~mode)
100+
function http2_fast_loop()
110101
{
111-
set ~thread _http2_thread_ids_$~thread_id
112-
sp_sc_flow_if text cvar ~mode == val "start"
113-
{
114-
//echo START
115-
set $~thread 1
116-
set ~mode "cont"
117-
}
118-
sp_sc_flow_if text cvar ~mode == val "stop"
119-
{
120-
//echo STOP
121-
set $~thread 0
122-
}
123-
sp_sc_flow_if text cvar ~mode == val "cont"
102+
sp_cl_info_map
103+
104+
sp_sc_flow_if text cvar _sp_cl_info_map != val ""
124105
{
125-
//echo CONT
126-
// Thread guaranteed to die.
127-
sp_sc_flow_if text cvar $~thread == val 1
128-
{
129-
130-
// is empty for a few frames before. perfect.
131-
sp_cl_info_map
132-
//echo MAP : $_sp_cl_info_map
133-
sp_sc_flow_if text cvar _sp_cl_info_map != val ""
134-
{
135-
//echo Map variable appears at : $_http2_timer
136-
//echo Map is $_sp_cl_info_map
137-
sp_sc_func_exec http2_spawn_dl $~thread_id
138-
}
139-
add _http2_timer 100
140-
sset ~cmd sp_sc_func_exec http2_fast_loop $~thread_id "cont"
141-
sp_sc_timer 100 #~cmd
142-
}
143-
}
144-
}
145-
// Called by fast_loop if mapname seemed to change.
146-
function http2_spawn_dl(~thread_id)
147-
{
148-
// Stop this thread, we have got the mapname we needed. Only a new connect creates new thread.
149-
sp_sc_func_exec http2_fast_loop $~thread_id "stop"
106+
thread_kill _http2_prev_thread
150107

151-
// Begin download.
152-
sp_sc_cvar_append ~full_path #_sp_cl_info_map ".bsp"
108+
// Begin download.
109+
sp_sc_cvar_append ~full_path #_sp_cl_info_map ".bsp"
153110

154-
sp_sc_file_find ~found maps/$~full_path file
155-
sp_sc_flow_if text cvar ~found_0 == val "0"
156-
{
157-
// We have duration of configstrings to download map ( should freeze here possibly )
158-
set _http2_not_exist_local 1
159-
sp_sc_cvar_sset ~text "[HTTP] Checking repo for " #_sp_cl_info_map ".zip"
160-
echo #~text
161-
sp_sc_cvar_save http_tmp ~full_path
111+
sp_sc_file_find ~found maps/$~full_path file
112+
sp_sc_flow_if text cvar ~found_0 == val "0"
113+
{
114+
// We have duration of configstrings to download map ( should freeze here possibly )
115+
set _http2_not_exist_local 1
116+
sp_sc_cvar_sset ~text "[HTTP] Checking repo for " #_sp_cl_info_map ".zip"
117+
echo #~text
118+
sp_sc_cvar_save http_tmp ~full_path
119+
}
162120
}
163121
}
164122

@@ -193,4 +151,4 @@ function http2_dl_filename(*)
193151
}
194152
}
195153

196-
}
154+
}

0 commit comments

Comments
 (0)