Class: NiceSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/nice_socket.rb

Defined Under Namespace

Classes: BadHeader, BadMessage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tcp_socket = nil) ⇒ NiceSocket

Returns a new instance of NiceSocket.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/nice_socket.rb', line 6

def initialize(tcp_socket=nil)
  @tcp_socket = tcp_socket
  @outbox = Queue.new

  @inbox = Queue.new
  @on_recv = lambda { |msg| @inbox << msg } # by default, stick the messages in the inbox

  @closing = false

  start_sender
  start_receiver
end

Instance Attribute Details

#inboxObject (readonly)

Returns the value of attribute inbox.



4
5
6
# File 'lib/nice_socket.rb', line 4

def inbox
  @inbox
end

Class Method Details

.connect(domain, port) ⇒ Object



28
29
30
# File 'lib/nice_socket.rb', line 28

def self.connect(domain, port)
  new(TCPSocket.new(domain, port))
end

Instance Method Details

#recvObject



24
25
26
# File 'lib/nice_socket.rb', line 24

def recv
  @inbox.pop
end

#send(msg) ⇒ Object



19
20
21
22
# File 'lib/nice_socket.rb', line 19

def send(msg)
  @outbox << msg.to_s
  msg
end