Module: SimpleRPC::SocketProtocol::Simple

Defined in:
lib/simplerpc/socket_protocol.rb

Overview

Sends string buffers back and forth using a simple protocol.

This method is significantly slower, but significantly more secure, than SocketProtocol::Stream, and is used for the auth handshake.

Class Method Summary collapse

Class Method Details

.recv(s, timeout = nil) ⇒ Object

Receive a buffer

Raises:

  • (Errno::ETIMEDOUT)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/simplerpc/socket_protocol.rb', line 80

def self.recv(s, timeout = nil)
    raise Errno::ETIMEDOUT unless IO.select([s], [], [], timeout)
    buflen = s.gets.to_s.chomp.to_i

    return nil if buflen <= 0

    buf = ''
    recieved = 0
    while recieved < buflen && (x = IO.select([s], [], [], timeout)) do
      str = s.read(buflen - recieved)
      buf += str
      recieved += str.length
    end
    raise Errno::ETIMEDOUT unless x

    return buf
end

.send(s, buf, timeout = nil) ⇒ Object

Send a buffer

Raises:

  • (Errno::ETIMEDOUT)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/simplerpc/socket_protocol.rb', line 62

def self.send(s, buf, timeout = nil)
    # Dump into buffer
    buflen = buf.length

    # Send buffer length
    raise Errno::ETIMEDOUT unless IO.select([], [s], [], timeout)
    s.puts(buflen)

    # Send buffer
    sent = 0
    while sent < buflen && (x = IO.select([], [s], [], timeout)) do
      sent += s.write(buf[sent..-1])
    end
    raise Errno::ETIMEDOUT unless x

end