Sha256: d66eb205351e2af32b3edf0ea0c8c80d0e51e5aa8d9106f3c5866ebfcc6c6bc9
Contents?: true
Size: 1.07 KB
Versions: 6
Compression:
Stored size: 1.07 KB
Contents
require 'rubygems' require 'json' require 'socket' class DemoSocket def initialize(socket) @tcp_socket = socket @hostname = @tcp_socket.peeraddr[2] @addr = @tcp_socket.peeraddr[3] end attr_reader :hostname, :addr def self.connect(addr, port) new TCPSocket.new(addr, port) end def close @tcp_socket.close end def send(data) msg = data.to_json begin puts "<<< #{msg.inspect}" if ENV['DEMO_DEBUG'] @tcp_socket.send([msg.size].pack('N'), 0) @tcp_socket.send(msg, 0) rescue Errno::ECONNRESET, Errno::EPIPE => e raise DeadSocket end end class SocketError < StandardError ; end class BadMessage < SocketError ; end class DeadSocket < SocketError ; end def recv header = @tcp_socket.recv(4) raise BadMessage if header.size < 4 expected_length = header.unpack('N')[0] msg = @tcp_socket.recv(expected_length) raise BadMessage if msg.size < expected_length puts ">>> #{msg.inspect}" if ENV['DEMO_DEBUG'] JSON.parse(msg) rescue Errno::ECONNRESET raise DeadSocket end end
Version data entries
6 entries across 6 versions & 1 rubygems
Version | Path |
---|---|
bum-0.0.17 | lib/demo_socket.rb |
bum-0.0.16 | lib/demo_socket.rb |
bum-0.0.15 | lib/demo_socket.rb |
bum-0.0.14 | lib/demo_socket.rb |
bum-0.0.13 | lib/demo_socket.rb |
bum-0.0.12 | lib/demo_socket.rb |