Module: Revent::RRCon

Defined in:
lib/revent/r_r.rb

Overview

Included by both Revent::RRServer and Revent::RRClient.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sessionObject

The place to store something associated with this connection



11
12
13
# File 'lib/revent/r_r.rb', line 11

def session
  @session
end

Instance Method Details

#on_message(message) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/revent/r_r.rb', line 48

def on_message(message)
  @user.synchronize do
    if @user_is_server
      @user.on_message(self, message)
    else
      @user.on_message(message)
    end
  end
rescue
end

#post_initObject



18
19
20
# File 'lib/revent/r_r.rb', line 18

def post_init
  @data = ''
end

#receive_data(data) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/revent/r_r.rb', line 22

def receive_data(data)
  @data << data
  while true
    io = StringIO.new(@data)
    message = Marshal.load(io)
    @data.slice!(0, io.pos)
    on_message(message)
  end
rescue
end

#remote_ipObject



59
60
61
62
63
64
65
66
# File 'lib/revent/r_r.rb', line 59

def remote_ip
  peername = get_peername
  return @last_ip if peername.nil?

  a = get_peername[2, 6].unpack('nC4')
  a.delete_at(0)
  @last_ip = a.join('.')
end

#send_message(message) ⇒ Object




44
45
46
# File 'lib/revent/r_r.rb', line 44

def send_message(message)
  send_data(Marshal.dump(message))
end

#unbindObject



33
34
35
36
37
38
39
40
# File 'lib/revent/r_r.rb', line 33

def unbind
  if @user_is_server
    @user.cons.synchronize { @user.cons.delete(self) }
    @user.on_unbind(self)
  else
    @user.on_unbind
  end
end

#user=(server_or_client) ⇒ Object



13
14
15
16
# File 'lib/revent/r_r.rb', line 13

def user=(server_or_client)
  @user_is_server = server_or_client.respond_to?(:start_server)
  @user = server_or_client
end