Bugfix double Trheads

If a Client connect comes from the same Session multiple Threads and
Allocate / Freememory Problems.
Debug for Thread Cleanup (ltls:CloseTLS Problems) Currently witouth a
fix...
This commit is contained in:
René Linder 2020-12-04 21:03:03 +01:00
parent f5521f2e39
commit 5f81ea4ebf
1 changed files with 56 additions and 25 deletions

View File

@ -310,18 +310,22 @@ Module lhs_web
client_id = ltls::WaitTLSSocket(network_server_id)
Debug "New HTTPS Client:"+Str(client_id)
If client_id > 0
If count_HTTPS_client < conf_max_HTTPS_clients
m_clients(Str(client_id))\client_id = client_id
m_clients(Str(client_id))\client_do_cli = #CLI_DO_WaitDataReceive
m_clients(Str(client_id))\client_do_srv = #SRV_DO_DataReceive
m_clients(Str(client_id))\client_type = #client_HTTPS
;Thread erstellen
m_clients(Str(client_id))\client_thread = CreateThread(@client(), client_id)
count_client(#client_HTTPS, #client_add)
If m_clients(Str(client_id))\client_id = client_id
Debug "Client Thread for socket already exist :"+Str(client_id)
Else
Debug "Max HTTPS Clients reached..."
ltls::CloseTLSSocket(client_id)
EndIf
If count_HTTPS_client < conf_max_HTTPS_clients
m_clients(Str(client_id))\client_id = client_id
m_clients(Str(client_id))\client_do_cli = #CLI_DO_WaitDataReceive
m_clients(Str(client_id))\client_do_srv = #SRV_DO_DataReceive
m_clients(Str(client_id))\client_type = #client_HTTPS
;Thread erstellen
m_clients(Str(client_id))\client_thread = CreateThread(@client(), client_id)
count_client(#client_HTTPS, #client_add)
Else
Debug "Max HTTPS Clients reached..."
ltls::CloseTLSSocket(client_id)
EndIf
EndIf
Else
Break
EndIf
@ -343,18 +347,22 @@ Module lhs_web
client_id = lsocket::WaitSocket(network_server_id)
Debug "New HTTP Client:"+Str(client_id)
If client_id > 0
If count_HTTP_client < conf_max_HTTP_clients
m_clients(Str(client_id))\client_id = client_id
m_clients(Str(client_id))\client_do_cli = #CLI_DO_WaitDataReceive
m_clients(Str(client_id))\client_do_srv = #SRV_DO_DataReceive
m_clients(Str(client_id))\client_type = #client_HTTP
;Thread erstellen
m_clients(Str(client_id))\client_thread = CreateThread(@client(), client_id)
count_client(#client_HTTP, #client_add)
If m_clients(Str(client_id))\client_id = client_id
Debug "Client Thread for socket already exist :"+Str(client_id)
Else
Debug "Max HTTP Clients reached..."
lsocket::CloseSocket(client_id)
EndIf
If count_HTTP_client < conf_max_HTTP_clients
m_clients(Str(client_id))\client_id = client_id
m_clients(Str(client_id))\client_do_cli = #CLI_DO_WaitDataReceive
m_clients(Str(client_id))\client_do_srv = #SRV_DO_DataReceive
m_clients(Str(client_id))\client_type = #client_HTTP
;Thread erstellen
m_clients(Str(client_id))\client_thread = CreateThread(@client(), client_id)
count_client(#client_HTTP, #client_add)
Else
Debug "Max HTTP Clients reached..."
lsocket::CloseSocket(client_id)
EndIf
EndIf
Else
Break
EndIf
@ -533,10 +541,12 @@ Module lhs_web
Debug "Response Content:"+Response(#cha_R_ResponseContentType)
Select Response(#cha_R_ResponseContentType)
Case #response_Memory
Debug "Response Memory"
thread_data_size = Val(Response(#cha_R_MemorySize))
thread_temp_cache_memory = Val(Response(#cha_R_MemoryAdress))
thread_type = Response(#cha_R_ResponseType)
Case #response_string
Debug "Response String"
thread_temp_decode_memory = AllocateMemory(StringByteLength(Response(#cha_R_StringBase64)))
thread_data_size = Base64Decoder(Response(#cha_R_StringBase64), thread_temp_decode_memory, StringByteLength(Response(#cha_R_StringBase64)))
thread_temp_cache_memory = AllocateMemory(thread_data_size)
@ -781,9 +791,16 @@ Module lhs_web
EndIf
If thread_temp_cache_memory <> 0 And thread_buffer_offset <> 0 And thread_data_size <> 0
CopyMemory(thread_temp_cache_memory, thread_buffer_offset, thread_data_size)
Debug thread_temp_cache_memory
FreeMemory(thread_temp_cache_memory)
thread_temp_cache_memory=0
Else
Debug "File Buffer Troubles."
If thread_temp_cache_memory = 0 : Debug "thread_temp_cache_memory = 0" : EndIf
If thread_buffer_offset = 0 : Debug "thread_buffer_offset = 0" : EndIf
If thread_data_size = 0 : Debug "thread_data_size = 0" : EndIf
thread_alive = #False
Break 2
EndIf
; EndIf
@ -810,6 +827,8 @@ Module lhs_web
sent_total + sent
Else
Debug "HTTPS Sent error:"+ltls::ErrorTLSCli(thread_cli_id)
thread_alive = #False
Break 2
EndIf
Until sent_length <= 0
@ -828,6 +847,8 @@ Module lhs_web
sent_total + sent
Else
Debug "HTTP Sent error:"+Str(sent)
thread_alive = #False
Break 2
EndIf
Until sent_length <= 0
@ -861,25 +882,35 @@ Module lhs_web
;}
Until thread_alive = #False
Debug "Thread should now die..."
ResetList(m_clients(Str(thread_cli_id))\datenbuffer())
Debug "Thread kill all Initialized Memory buffers:"
While NextElement(m_clients(Str(thread_cli_id))\datenbuffer())
If m_clients(Str(thread_cli_id))\datenbuffer()\Initialized
Debug "Kill:"+Str(m_clients(Str(thread_cli_id))\datenbuffer()\Buffer)+ " ..."
FreeMemory(m_clients(Str(thread_cli_id))\datenbuffer()\Buffer)
DeleteElement(m_clients(Str(thread_cli_id))\datenbuffer())
Debug "Freed and removed"
Else
Debug "Remove element from List with uninitialized Memory ID: "+Str(m_clients(Str(thread_cli_id))\datenbuffer()\Buffer)
DeleteElement(m_clients(Str(thread_cli_id))\datenbuffer())
Debug "Removed"
EndIf
Wend
If m_clients(Str(thread_cli_id))\client_type = #client_HTTPS
ltls::CloseTLSSocket(thread_cli_id)
Debug "Kill HTTPS Socket:" + Str(thread_cli_id)
;ltls::CloseTLSSocket(thread_cli_id)
count_client(#client_HTTPS, #client_remove)
Debug "Killed."
Else
Debug "Kill HTTP Socket:" + Str(thread_cli_id)
lsocket::CloseSocket(thread_cli_id)
count_client(#client_HTTP, #client_remove)
Debug "Killed."
EndIf
Debug "Remove Client from map..."
DeleteMapElement(m_clients(), Str(thread_cli_id))
Debug "Thread Beendet."
Debug "Removed and thread now died."
EndProcedure