Class: Reactomatic::TcpConnection

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ TcpConnection

Returns a new instance of TcpConnection.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/reactomatic/tcp_connection.rb', line 3

def initialize(opts = {})
  @opts = opts

  @reactor = opts[:reactor] || Reactomatic.reactor
  @socket = opts[:socket]
  @write_buffer = opts[:write_buffer] || Buffer.new
  @read_count = 0
  @write_count = 0
  @read_eof = false
  @lock = Monitor.new

  on_initialize
  register if @socket
end

Instance Method Details

#closeObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/reactomatic/tcp_connection.rb', line 42

def close
  @lock.synchronize do
    if @socket
      @reactor.deregister(@socket)
      @socket.close
      @socket = nil
      on_disconnect
    end
  end

  nil
end

#connect(host, port) ⇒ Object



26
27
28
# File 'lib/reactomatic/tcp_connection.rb', line 26

def connect(host, port)
  raise 'Not implemented yet.'
end

#reactorObject

Public methods.



22
23
24
# File 'lib/reactomatic/tcp_connection.rb', line 22

def reactor
  return @reactor
end

#send_data(data) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/reactomatic/tcp_connection.rb', line 30

def send_data(data)
  @lock.synchronize do
    return nil if @socket.nil?

    @write_buffer.append(data)
    write_nonblock
    register
  end

  nil
end