lib/eye/server.rb in reel-eye-0.3.2 vs lib/eye/server.rb in reel-eye-0.4
- old
+ new
@@ -1,13 +1,13 @@
require 'celluloid/io'
require 'celluloid/autostart'
class Eye::Server
include Celluloid::IO
-
+
attr_reader :socket_path, :server
-
+
def initialize(socket_path)
@socket_path = socket_path
@server = begin
UNIXServer.open(socket_path)
rescue Errno::EADDRINUSE
@@ -19,18 +19,26 @@
def run
loop { async.handle_connection @server.accept }
end
def handle_connection(socket)
- command, *args = socket.readline.strip.split('|')
+ text = socket.read
+
+ begin
+ command, *args = Marshal.load(text)
+ rescue => ex
+ error "Failed socket read #{ex.message}"
+ return
+ end
+
response = command(command, *args)
socket.write(Marshal.dump(response))
-
+
rescue Errno::EPIPE
# client timeouted
# do nothing
-
+
ensure
socket.close
end
def command(cmd, *args)
@@ -41,10 +49,10 @@
File.delete(@socket_path) if @socket_path
rescue
end
finalizer :close_socket
-
+
def close_socket
@server.close if @server
unlink_socket_file
end
\ No newline at end of file