Karchnu 2020-07-11 16:08:46 +02:00
parent 9c9a3231bd
commit f6c0498e46
3 changed files with 81 additions and 77 deletions

View File

@ -3,10 +3,13 @@ class InstanceStorage
property is_json : Hash(Int32, Bool) property is_json : Hash(Int32, Bool)
property fd_to_websocket : Hash(Int32, WebSocket) property fd_to_websocket : Hash(Int32, WebSocket)
# So the GC won't close it.
property fd_to_socket : Hash(Int32, TCPSocket)
def initialize def initialize
# fdlist_client = [] of TCPSocket
@is_json = Hash(Int32,Bool).new @is_json = Hash(Int32,Bool).new
@fd_to_websocket = Hash(Int32, WebSocket).new @fd_to_websocket = Hash(Int32, WebSocket).new
@fd_to_socket = Hash(Int32, TCPSocket).new
end end
def remove_fd (fdclient : Int32) def remove_fd (fdclient : Int32)
@ -16,5 +19,8 @@ class InstanceStorage
# 2. remove the client from fd_to_websocket # 2. remove the client from fd_to_websocket
@fd_to_websocket.select! do |fd, ws| fd != fdclient end @fd_to_websocket.select! do |fd, ws| fd != fdclient end
# 3. remove the client from fd_to_socket
@fd_to_socket.select! do |fd, ws| fd != fdclient end
end end
end end

View File

@ -47,6 +47,8 @@ class HTTP::WebSocket
rescue IO::EOFError rescue IO::EOFError
close close
return Error.new return Error.new
rescue
return Error.new
end end
case info.opcode case info.opcode
@ -113,7 +115,7 @@ class WebSocket < HTTP::WebSocket
getter? closed = false getter? closed = false
def finalize def finalize
# puts "WrappedTCPFileDescriptor garbage collection!!" puts "WebSocket garbage collection!!"
# super # super
end end
end end
@ -121,7 +123,7 @@ end
class WrappedTCPFileDescriptor < TCPSocket class WrappedTCPFileDescriptor < TCPSocket
# do not close the connection when garbage collected!! # do not close the connection when garbage collected!!
def finalize def finalize
# puts "WrappedTCPFileDescriptor garbage collection!!" puts "WrappedTCPFileDescriptor garbage collection!!"
# super # super
end end
end end

View File

@ -131,9 +131,13 @@ def ws_http_upgrade(client)
end end
# registering the client into storing structures to avoid being garbage collected # registering the client into storing structures to avoid being garbage collected
Context.context.fd_to_socket[client.fd] = client
Context.context.fd_to_websocket[client.fd] = wsclient Context.context.fd_to_websocket[client.fd] = wsclient
req_service req_service
rescue e
puts "#{CRED}Exception in ws_http_upgrade#{CRESET}: #{CBLUE}#{e}#{CRESET}"
raise "DROP IT"
end end
def handle_new_clients(service, server) def handle_new_clients(service, server)
@ -145,58 +149,39 @@ def handle_new_clients(service, server)
# 2. upgrade HTTP connections and get the service name in the URI path. # 2. upgrade HTTP connections and get the service name in the URI path.
req_service = ws_http_upgrade client req_service = ws_http_upgrade client
puts "requested service: #{req_service}"
# 3. connect to the service via ipc_connection_switched # 3. connect to the service via ipc_connection_switched
LibIPC.ipc_connection_switched service.pointer, req_service serverfd = 0
# puts "ipc_connection_switched: OK" # LibIPC.ipc_connection_switched service.pointer, req_service, client.fd, Pointer(Libc::Int).null
req_service_fd = service.context.pollfd[service.context.size - 1].fd LibIPC.ipc_connection_switched service.pointer, req_service, client.fd, pointerof (serverfd)
# 4. add client fd in the context, type = switched via ipc_ctx_fd_type proc_cb_in = ->ws_cb_in(Int32, Pointer(LibIPC::Message))
service << client.fd proc_cb_out = ->ws_cb_out(Int32, Pointer(LibIPC::Message))
# service.pp
LibIPC.ipc_ctx_fd_type service.pointer, client.fd, LibIPC::ConnectionType::Switched
# puts "ipc_ctx_fd_type: OK"
# 5. add both fd in switched via ipc_switching_add # 4. change client callbacks via ipc_switching_callbacks
LibIPC.ipc_ctx_switching_add service.pointer, client.fd, req_service_fd
# service.pp
# puts "ipc_switching_add: OK"
proc_cb_in = ->my_cb_in(Int32, Pointer(LibIPC::Message))
proc_cb_out = ->my_cb_out(Int32, Pointer(LibIPC::Message))
# 6. change client callbacks via ipc_switching_callbacks
# only the client callbacks are changed, since the associated server is a simple libipc service # only the client callbacks are changed, since the associated server is a simple libipc service
puts "#{CGREEN}BEFORE ipc_switching_callbacks#{CRESET}"
LibIPC.ipc_switching_callbacks service.pointer, client.fd, proc_cb_in, proc_cb_out LibIPC.ipc_switching_callbacks service.pointer, client.fd, proc_cb_in, proc_cb_out
puts "ipc_switching_callbacks: OK"
puts "#{CBLUE}new client:#{CRESET} #{client.fd}" unless CLI.verbosity == 0 puts "#{CBLUE}new client:#{CRESET} #{client.fd}" unless CLI.verbosity == 0
rescue e rescue e
puts "Exception: #{CRED}#{e}#{CRESET}" puts "Exception in handle_new_client: #{CRED}#{e}#{CRESET}"
unless client.nil? unless client.nil?
client.not_nil!.close client.close
end end
end end
def my_cb_out(fd : Int32, pm : Pointer(LibIPC::Message)) def ws_cb_out(fd : Int32, pm : Pointer(LibIPC::Message))
puts "OUT fd is #{fd}" puts "OUT fd is #{fd}"
wsclient = Context.context.fd_to_websocket[fd] wsclient = Context.context.fd_to_websocket[fd]
message = IPC::Message.new pm message = IPC::Message.new pm
pp! message # pp! message
if Context.context.is_json[fd] if Context.context.is_json[fd]
puts "OUT: this is a JSON client"
buf = message.to_json buf = message.to_json
else else
puts "OUT: this is NOT a JSON client (fake, always JSON right now)" buf = message.to_packet
# buf = message.to_packet
buf = message.to_json
end end
puts "OUT: SENDING"
wsclient.send buf wsclient.send buf
return LibIPC::IPCCB::NoError return LibIPC::IPCCB::NoError
@ -206,72 +191,83 @@ rescue e
return LibIPC::IPCCB::Error return LibIPC::IPCCB::Error
end end
def my_cb_in(fd : Int32, pm : LibIPC::Message*) def ws_cb_in(fd : Int32, pm : LibIPC::Message*)
puts "IN fd is #{fd}" puts "IN fd is #{fd}"
message = IPC::Message.new pm
wsclient = Context.context.fd_to_websocket[fd] wsclient = Context.context.fd_to_websocket[fd]
message = wsclient.run_once puts
puts
puts "before run_once"
message = nil
begin
message = wsclient.run_once
puts "after the run_once"
rescue e
puts "#{CRED}run_once FAILED#{CRESET}: #{e}"
# Context.context.remove_fd fd
return LibIPC::IPCCB::Error
end
if wsclient.closed? if wsclient.closed?
puts "#{CBLUE}client is closed#{CRESET}" puts "#{CBLUE}client is closed#{CRESET}"
Context.context.remove_fd fd # Context.context.remove_fd fd
return LibIPC::IPCCB::Closing return LibIPC::IPCCB::Closing
end end
if message.nil? if message.nil?
puts "#{CRED}message is nil#{CRESET}" puts "#{CRED}message is nil#{CRESET}"
Context.context.remove_fd fd # Context.context.remove_fd fd
return LibIPC::IPCCB::Closing return LibIPC::IPCCB::Closing
end end
case message case message
when WebSocket::Error
puts "#{CRED}An error occured#{CRESET}"
Context.context.remove_fd fd
return LibIPC::IPCCB::Error
when WebSocket::Ping
puts "#{CBLUE}Received a ping message#{CRESET}"
Context.context.remove_fd fd
return LibIPC::IPCCB::Ignore
when WebSocket::Pong
puts "#{CBLUE}Received a pong message#{CRESET}"
return LibIPC::IPCCB::Ignore
when WebSocket::Close
puts "#{CBLUE}Received a close message#{CRESET}"
Context.context.remove_fd fd
return LibIPC::IPCCB::Closing
when WebSocket::NotFinal
puts "#{CBLUE}Received only part of a message: NOT IMPLEMENTED#{CRESET}"
Context.context.remove_fd fd
return LibIPC::IPCCB::Error
when Bytes
# TODO: when receiving a binary message
# we should test the format and maybe its content
puts "#{CBLUE}Received a binary message: NOT IMPLEMENTED, YET#{CRESET}"
Context.context.remove_fd fd
return LibIPC::IPCCB::Error
when String when String
puts "#{CBLUE}Received a String message#{CRESET}" if Context.context.is_json[fd]
ipc_message = IPC::Message.from_json(message)
ipc_message.copy_to_message_pointer pm
return LibIPC::IPCCB::NoError
end
else
# every other option should be dropped
case message
when WebSocket::Error
puts "#{CRED}An error occured#{CRESET}"
# Context.context.remove_fd fd
return LibIPC::IPCCB::Error
when WebSocket::Ping
puts "#{CBLUE}Received a ping message#{CRESET}"
# Context.context.remove_fd fd
return LibIPC::IPCCB::Ignore
when WebSocket::Pong
puts "#{CBLUE}Received a pong message#{CRESET}"
return LibIPC::IPCCB::Ignore
when WebSocket::Close
puts "#{CBLUE}Received a close message#{CRESET}"
# Context.context.remove_fd fd
return LibIPC::IPCCB::Closing
when WebSocket::NotFinal
puts "#{CBLUE}Received only part of a message: NOT IMPLEMENTED#{CRESET}"
# Context.context.remove_fd fd
return LibIPC::IPCCB::Error
when Bytes
# TODO: when receiving a binary message
# we should test the format and maybe its content
puts "#{CBLUE}Received a binary message: NOT IMPLEMENTED, YET#{CRESET}"
# Context.context.remove_fd fd
return LibIPC::IPCCB::Error
else
puts "#{CRED}Received a websocket message with unknown type#{CRESET}"
end
end end
if Context.context.is_json[fd] && message.is_a?(String) return LibIPC::IPCCB::Error
puts "#{CRED}CECI EST DU JSON#{CRESET}"
message = IPC::Message.from_json(message).to_packet
end
pp! message
# TODO: take the received message and convert it in pm
return LibIPC::IPCCB::NoError
rescue e rescue e
puts "#{CRED}Exception (receiving a message)#{CRESET} #{e}" puts "#{CRED}Exception (receiving a message)#{CRESET} #{e}"
tcp = WrappedTCPFileDescriptor.new(fd: fd, family: Socket::Family::INET) # tcp = WrappedTCPFileDescriptor.new(fd: fd, family: Socket::Family::INET)
tcp.close # tcp.close
Context.context.remove_fd fd Context.context.remove_fd fd
return LibIPC::IPCCB::Error return LibIPC::IPCCB::Error
end end