websocketd: simpler protocol
parent
608a467f89
commit
48201acea4
|
@ -55,25 +55,25 @@ end
|
||||||
|
|
||||||
class InstanceStorage
|
class InstanceStorage
|
||||||
property service : IPC::SwitchingService
|
property service : IPC::SwitchingService
|
||||||
property fdlist : Hash(Int32,String)
|
property is_client : Hash(Int32,Bool)
|
||||||
property socklist : Hash(Int32, TCPSocket)
|
|
||||||
property wssocklist : Hash(Int32, WebSocket)
|
|
||||||
property switchtable : Hash(Int32, Int32)
|
property switchtable : Hash(Int32, Int32)
|
||||||
property connectionlist : Hash(Int32, IPC::Connection)
|
property fdtotcpsocket : Hash(Int32, TCPSocket)
|
||||||
|
property fdtows : Hash(Int32, WebSocket)
|
||||||
|
property fdtoipcconnection : Hash(Int32, IPC::Connection)
|
||||||
|
|
||||||
def initialize (@service : IPC::SwitchingService)
|
def initialize (@service : IPC::SwitchingService)
|
||||||
# fdlist_client = [] of TCPSocket
|
# fdlist_client = [] of TCPSocket
|
||||||
@fdlist = Hash(Int32,String).new
|
@is_client = Hash(Int32,Bool).new
|
||||||
@socklist = Hash(Int32, TCPSocket).new
|
@fdtotcpsocket = Hash(Int32, TCPSocket).new
|
||||||
@wssocklist = Hash(Int32, WebSocket).new
|
@fdtows = Hash(Int32, WebSocket).new
|
||||||
@switchtable = Hash(Int32, Int32).new
|
@switchtable = Hash(Int32, Int32).new
|
||||||
@connectionlist = Hash(Int32, IPC::Connection).new
|
@fdtoipcconnection = Hash(Int32, IPC::Connection).new
|
||||||
end
|
end
|
||||||
|
|
||||||
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]?
|
||||||
tcpfdc = @socklist[fdclient]
|
tcpfdc = @fdtotcpsocket[fdclient]
|
||||||
|
|
||||||
# 2. closing the TCP connections
|
# 2. closing the TCP connections
|
||||||
tcpfdc.close unless tcpfdc.closed?
|
tcpfdc.close unless tcpfdc.closed?
|
||||||
|
@ -86,18 +86,18 @@ class InstanceStorage
|
||||||
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 is_client
|
||||||
@fdlist = @fdlist.select do |fd,v| fd != fdclient end
|
@is_client = @is_client.select do |fd,v| fd != fdclient end
|
||||||
|
|
||||||
unless fdservice.nil?
|
unless fdservice.nil?
|
||||||
service = @connectionlist[fdservice]
|
service = @fdtoipcconnection[fdservice]
|
||||||
service.close
|
service.close
|
||||||
@service.remove_fd (fdservice)
|
@service.remove_fd (fdservice)
|
||||||
@connectionlist = @connectionlist.select do |k, v|
|
@fdtoipcconnection = @fdtoipcconnection.select do |k, v|
|
||||||
k != fdservice
|
k != fdservice
|
||||||
end
|
end
|
||||||
|
|
||||||
@fdlist = @fdlist.select do |fd,v| fd != fdservice end
|
@is_client = @is_client.select do |fd,v| fd != fdservice end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -122,9 +122,7 @@ def websocket_client_connection(client, context : InstanceStorage)
|
||||||
end
|
end
|
||||||
|
|
||||||
# FIXME: check they actually wanted to upgrade to websocket
|
# FIXME: check they actually wanted to upgrade to websocket
|
||||||
|
|
||||||
key = request.headers["Sec-WebSocket-Key"]
|
key = request.headers["Sec-WebSocket-Key"]
|
||||||
|
|
||||||
response_key = Digest::SHA1.base64digest key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
|
response_key = Digest::SHA1.base64digest key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
|
||||||
# puts response_key
|
# puts response_key
|
||||||
|
|
||||||
|
@ -138,50 +136,55 @@ def websocket_client_connection(client, context : InstanceStorage)
|
||||||
|
|
||||||
headers = headers.map { |key, value| "#{key}: #{value[0]}\r\n" }.join
|
headers = headers.map { |key, value| "#{key}: #{value[0]}\r\n" }.join
|
||||||
|
|
||||||
|
# requested service, fd
|
||||||
|
req_service = request.path.lchop
|
||||||
|
if req_service.empty?
|
||||||
|
client.close
|
||||||
|
puts "should send a PATH"
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
websocket_connection_procedure req_service, client.fd, context
|
||||||
|
|
||||||
# puts "#{headers_header}\n#{headers.to_s}\r\n"
|
# puts "#{headers_header}\n#{headers.to_s}\r\n"
|
||||||
client.send "#{headers_header}\n#{headers.to_s}\r\n"
|
client.send "#{headers_header}\n#{headers.to_s}\r\n"
|
||||||
|
|
||||||
wsclient = WebSocket.new client
|
wsclient = WebSocket.new client
|
||||||
wsclient.send "are we websocket yet?"
|
|
||||||
|
|
||||||
# the client is still not connected to a service
|
# the client is still not connected to a service
|
||||||
context.fdlist[client.fd] = "not connected"
|
context.is_client[client.fd] = true
|
||||||
|
|
||||||
# listen to the client's file descriptor
|
# listen to the client's file descriptor
|
||||||
context.service << client.fd
|
context.service << client.fd
|
||||||
# puts "#{CBLUE}new client: #{client.fd}#{CRESET}"
|
# puts "#{CBLUE}new client: #{client.fd}#{CRESET}"
|
||||||
|
|
||||||
# registering the client into storing structures to avoid being garbage collected
|
# registering the client into storing structures to avoid being garbage collected
|
||||||
context.socklist[client.fd] = client
|
context.fdtotcpsocket[client.fd] = client
|
||||||
context.wssocklist[client.fd] = wsclient
|
context.fdtows[client.fd] = wsclient
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def closing_client (fdclient : Int, context : InstanceStorage)
|
def closing_client (fdclient : Int, context : InstanceStorage)
|
||||||
# puts "closing the client #{fdclient}"
|
puts "#{CBLUE}closing the client:#{CRESET} #{fdclient}"
|
||||||
context.remove_fd fdclient
|
context.remove_fd fdclient
|
||||||
end
|
end
|
||||||
|
|
||||||
# first message from the client: requested service name
|
# first message from the client: requested service name
|
||||||
# 1. extracting the requested service from the message
|
# 1. connection to the service
|
||||||
# 2. connection to the service
|
# 2. listening on the service fd
|
||||||
# 3. listening on the service fd
|
# 3. bounding both file descriptors (through switchtable hash)
|
||||||
# 4. bounding both file descriptors (through switchtable hash)
|
# 4. indicating that the client is connected (in is_client)
|
||||||
# 5. indicating that the client is connected (in fdlist)
|
def websocket_connection_procedure (requested_service : String, clientfd : Int32, context : InstanceStorage)
|
||||||
# 6. sending an ack to the client
|
|
||||||
def websocket_connection_procedure (message : String, clientfd : Int32, context : InstanceStorage)
|
|
||||||
begin
|
begin
|
||||||
# 1. reading the service name from the received message
|
|
||||||
requested_service = message
|
|
||||||
|
|
||||||
# 2. establishing a connection to the service
|
# 2. establishing a connection to the service
|
||||||
newservice = IPC::Connection.new requested_service
|
newservice = IPC::Connection.new requested_service
|
||||||
context.connectionlist[newservice.fd] = newservice
|
context.fdtoipcconnection[newservice.fd] = newservice
|
||||||
|
|
||||||
# 3. listening on the service fd
|
# 3. listening on the client fd and the service fd
|
||||||
context.service << newservice.fd
|
context.service << newservice.fd
|
||||||
|
|
||||||
# cannot perform automatic switching due to websockets headers
|
# cannot perform automatic switching due to websockets headers
|
||||||
|
# future version of the libipc lib should include some workaround, probably
|
||||||
# service.switch.add fdclient, newservice.fd
|
# service.switch.add fdclient, newservice.fd
|
||||||
|
|
||||||
# 4. bounding the client and the service fd
|
# 4. bounding the client and the service fd
|
||||||
|
@ -189,12 +192,8 @@ def websocket_connection_procedure (message : String, clientfd : Int32, context
|
||||||
context.switchtable[newservice.fd] = clientfd
|
context.switchtable[newservice.fd] = clientfd
|
||||||
|
|
||||||
# 5. the client is then connected, send it a message
|
# 5. the client is then connected, send it a message
|
||||||
context.fdlist[clientfd] = "connected"
|
context.is_client[clientfd] = true
|
||||||
context.fdlist[newservice.fd] = "service"
|
context.is_client[newservice.fd] = false
|
||||||
|
|
||||||
# 6. ack
|
|
||||||
wsclient = context.wssocklist[clientfd]
|
|
||||||
wsclient.send "OK\n"
|
|
||||||
rescue e
|
rescue e
|
||||||
puts "#{CRED}Exception during connection to the service:#{CRESET} #{e}"
|
puts "#{CRED}Exception during connection to the service:#{CRESET} #{e}"
|
||||||
closing_client clientfd, context
|
closing_client clientfd, context
|
||||||
|
@ -203,11 +202,10 @@ end
|
||||||
|
|
||||||
def websocket_switching_procedure (activefd : Int, context : InstanceStorage)
|
def websocket_switching_procedure (activefd : Int, context : InstanceStorage)
|
||||||
begin
|
begin
|
||||||
|
if context.is_client[activefd]
|
||||||
if context.fdlist[activefd] == "connected"
|
|
||||||
# The client is a WebSocket on top of a TCP connection
|
# The client is a WebSocket on top of a TCP connection
|
||||||
client = context.socklist[activefd]
|
client = context.fdtotcpsocket[activefd]
|
||||||
wsclient = context.wssocklist[activefd]
|
wsclient = context.fdtows[activefd]
|
||||||
begin
|
begin
|
||||||
message = wsclient.read
|
message = wsclient.read
|
||||||
rescue e
|
rescue e
|
||||||
|
@ -235,32 +233,30 @@ def websocket_switching_procedure (activefd : Int, context : InstanceStorage)
|
||||||
|
|
||||||
# XXX: this is not a TCP fd, but since behind the scene this is compatible, I'm hacking a bit
|
# XXX: this is not a TCP fd, but since behind the scene this is compatible, I'm hacking a bit
|
||||||
serv = WrappedTCPFileDescriptor.new(fd: fdservice, family: Socket::Family::INET)
|
serv = WrappedTCPFileDescriptor.new(fd: fdservice, family: Socket::Family::INET)
|
||||||
#serv = context.connectionlist[fdservice]
|
#serv = context.fdtoipcconnection[fdservice]
|
||||||
serv.send message
|
serv.send message
|
||||||
|
|
||||||
elsif context.fdlist[activefd] == "service"
|
else
|
||||||
# puts "switching: service to client"
|
# puts "switching: service to client"
|
||||||
# service => client
|
# service => client
|
||||||
|
|
||||||
fdclient = context.switchtable[activefd]
|
fdclient = context.switchtable[activefd]
|
||||||
wsclient = context.wssocklist[fdclient]
|
wsclient = context.fdtows[fdclient]
|
||||||
|
|
||||||
serv = context.connectionlist[activefd]
|
serv = context.fdtoipcconnection[activefd]
|
||||||
message = serv.read
|
message = serv.read
|
||||||
# puts "received message from service: #{message.to_s}"
|
# puts "received message from service: #{message.to_s}"
|
||||||
buf = message.to_buffer
|
buf = message.to_buffer
|
||||||
# print_hexa String.new(buf), "\033[31m Message to send to the client \033[00m "
|
# print_hexa String.new(buf), "\033[31m Message to send to the client \033[00m "
|
||||||
wsclient.send buf
|
wsclient.send buf
|
||||||
else
|
|
||||||
raise "cannot understand if the fd is a client or a service"
|
|
||||||
end
|
end
|
||||||
rescue e
|
rescue e
|
||||||
puts "#{CRED}Exception during message transfer:#{CRESET} #{e}"
|
puts "#{CRED}Exception during message transfer:#{CRESET} #{e}"
|
||||||
if context.fdlist[activefd] == "service"
|
if context.is_client[activefd]
|
||||||
|
closing_client activefd, context
|
||||||
|
else
|
||||||
clientfd = context.switchtable[activefd]
|
clientfd = context.switchtable[activefd]
|
||||||
closing_client clientfd, context
|
closing_client clientfd, context
|
||||||
elsif context.fdlist[activefd] == "service"
|
|
||||||
closing_client activefd, context
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -289,39 +285,8 @@ service.loop do |event|
|
||||||
end
|
end
|
||||||
|
|
||||||
# 2. active fd != server fd
|
# 2. active fd != server fd
|
||||||
# is this a service or a client?
|
|
||||||
activefd = event.connection.fd
|
activefd = event.connection.fd
|
||||||
if context.fdlist[activefd] == "not connected"
|
|
||||||
|
|
||||||
# since it's an external communication
|
|
||||||
# we have to read the message here, it's not handled by default in libipc
|
|
||||||
wsclient = context.wssocklist[activefd]
|
|
||||||
begin
|
|
||||||
message = wsclient.read
|
|
||||||
rescue e
|
|
||||||
puts "#{CRED}Exception (receiving a message)#{CRESET} #{e}"
|
|
||||||
closing_client activefd, context
|
|
||||||
next
|
|
||||||
end
|
|
||||||
|
|
||||||
if message.nil?
|
|
||||||
puts "#{CBLUE}disconnection of client#{CRESET} #{event.connection.fd}"
|
|
||||||
closing_client activefd, context
|
|
||||||
next
|
|
||||||
end
|
|
||||||
|
|
||||||
# requested service, fd
|
|
||||||
websocket_connection_procedure String.new(message), activefd, context
|
|
||||||
|
|
||||||
elsif context.fdlist[activefd] == "connected"
|
|
||||||
# the service is sending a message
|
|
||||||
websocket_switching_procedure activefd, context
|
websocket_switching_procedure activefd, context
|
||||||
elsif context.fdlist[activefd] == "service"
|
|
||||||
# the service is sending a message
|
|
||||||
websocket_switching_procedure activefd, context
|
|
||||||
else
|
|
||||||
raise "should not happen: client not recorded in fdlist"
|
|
||||||
end
|
|
||||||
|
|
||||||
when IPC::Event::Switch
|
when IPC::Event::Switch
|
||||||
puts "\033[36mIPC::Event::Switch#{CRESET}: from fd #{event.connection.fd}"
|
puts "\033[36mIPC::Event::Switch#{CRESET}: from fd #{event.connection.fd}"
|
||||||
|
|
Reference in New Issue