Obsolete
/
ipcd
Archived
3
0
Fork 0

Websocketd: correctly removes the client.

master
Philippe PITTOLI 2019-08-03 09:50:03 +02:00
parent 15db08db0b
commit 16daa80d5d
1 changed files with 24 additions and 27 deletions

View File

@ -72,39 +72,33 @@ class InstanceStorage
def remove_fd (fdclient : Int32) def remove_fd (fdclient : Int32)
# 1. closing both the client and the service # 1. closing both the client and the service
fdservice = @switchtable[fdclient] fdservice = @switchtable[fdclient]?
# puts "1. fds to close: #{fdclient} and #{fdservice}"
tcpfdc = @socklist[fdclient] tcpfdc = @socklist[fdclient]
service = @connectionlist[fdservice]
# 2. closing the TCP connections # 2. closing the TCP connections
# puts "2.1 closing fd #{fdclient}"
tcpfdc.close unless tcpfdc.closed? tcpfdc.close unless tcpfdc.closed?
# puts "2.2 closing fd #{fdservice}"
service.close
# 3. removing the client and the service fds from the loop check # 3. removing the client and the service fds from the loop check
# puts "3.1. removing #{fdclient} from @service"
@service.remove_fd (fdclient) @service.remove_fd (fdclient)
# puts "3.2. removing #{fdservice} from @service"
@service.remove_fd (fdservice)
# 4. removing the service IPC::Connection from connectionlist
# puts "4. removing fdservice from @connectionlist"
@connectionlist.select do |k, v|
k != fdservice
end
# 5. removing both the client and the service from the switchtable # 5. removing both the client and the service from the switchtable
# puts "5. removing both fd from @switchable" @switchtable = @switchtable.select do |fdc, fds|
@switchtable.select do |fdc, fds|
fdc != fdclient && fds != fdclient fdc != fdclient && fds != fdclient
end end
# 6. removing the client and the service from fdlist # 6. removing the client and the service from fdlist
# puts "6. removing both fd from fdlist" @fdlist = @fdlist.select do |fd,v| fd != fdclient end
@fdlist.select do |fd,v| fd != fdclient end
@fdlist.select do |fd,v| fd != fdservice end unless fdservice.nil?
service = @connectionlist[fdservice]
service.close
@service.remove_fd (fdservice)
@connectionlist = @connectionlist.select do |k, v|
k != fdservice
end
@fdlist = @fdlist.select do |fd,v| fd != fdservice end
end
end end
end end
@ -120,13 +114,11 @@ def websocket_client_connection(client, context : InstanceStorage)
# pp! request # pp! request
if request.nil? if request.nil?
puts "#{CRED}eEQUEST IS NIL#{CRESET}" raise "#REQUEST IS NIL"
return
end end
if request.is_a? HTTP::Request::BadRequest if request.is_a? HTTP::Request::BadRequest
puts "#{CRED}BAD REQUEST DAZE~#{CRESET}" raise "BAD REQUEST DAZE~"
return
end end
# FIXME: check they actually wanted to upgrade to websocket # FIXME: check they actually wanted to upgrade to websocket
@ -280,8 +272,13 @@ service.loop do |event|
# 1. accept new websocket clients # 1. accept new websocket clients
if server.fd == event.connection.fd if server.fd == event.connection.fd
client = server.accept client = server.accept
websocket_client_connection client, context begin
puts "#{CBLUE}new client:#{CRESET} #{client.fd}" websocket_client_connection client, context
puts "#{CBLUE}new client:#{CRESET} #{client.fd}"
rescue e
puts "Exception: #{CRED}#{e}#{CRESET}"
client.close
end
next next
end end
@ -296,7 +293,7 @@ service.loop do |event|
message = wsclient.read message = wsclient.read
if message.nil? if message.nil?
puts "#{CBLUE}disconnection of client #{event.connection.fd}#{CRESET}" puts "#{CBLUE}disconnection of client#{CRESET} #{event.connection.fd}"
closing_client activefd, context closing_client activefd, context
next next
end end