lib/dazeus/connection.rb in dazeus-0.0.1.201306161619 vs lib/dazeus/connection.rb in dazeus-0.0.1.201306161750
- old
+ new
@@ -7,33 +7,52 @@
@socket = create_socket(address)
@cache = []
end
def send(message)
- @socket.sendmsg(dazeusify message)
+ if !@socket.closed?
+ @socket.sendmsg(dazeusify message)
+ else
+ false
+ end
end
def receive
if @cache.length == 0
message = @socket.recvmsg[0]
message = message.force_encoding 'UTF-8'
message = message.strip
while message.length > 0
- digits = ""
- while message[0] =~ /\d/
- digits += message.slice! 0
- end
- @cache.push JSON.parse(message.slice!(0, digits.to_i))
- message = message.strip
+ message = dezeusify(message)
end
end
@cache.shift
end
+ def dezeusify(message)
+ digits = ""
+ while message[0] =~ /\d/
+ digits += message.slice! 0
+ end
+ current = message.slice!(0, digits.to_i)
+ # puts "<- " + current
+ @cache.push JSON.parse(current)
+ message.strip
+ end
+
def dazeusify(message)
msg = JSON.dump(message)
+ # puts "-> " + msg
msg.bytesize.to_s + msg
+ end
+
+ def close
+ @socket.close
+ end
+
+ def closed?
+ @socket.closed?
end
private
def create_socket(address)
if address.start_with? 'tcp://'