Class: ProcessWanker::NetConnection
- Inherits:
-
Object
- Object
- ProcessWanker::NetConnection
show all
- Includes:
- Log
- Defined in:
- lib/net/net_connection.rb
Constant Summary
Constants included
from Log
Log::DEBUG, Log::ERROR, Log::INFO, Log::WARN
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Log
debug, error, info, log, set_level, warn
Constructor Details
#initialize(ssl_connection) ⇒ NetConnection
Returns a new instance of NetConnection.
34
35
36
37
38
39
|
# File 'lib/net/net_connection.rb', line 34
def initialize(ssl_connection)
@write_mutex=Mutex.new
@ssl_connection=ssl_connection
@read_thread = Thread.new { read_proc }
@user = ssl_connection.peer_cert.subject.to_a.select { |x| x[0]=="CN" }.map { |x| x[1] }[0]
end
|
Instance Attribute Details
#ssl_connection ⇒ Object
Returns the value of attribute ssl_connection.
25
26
27
|
# File 'lib/net/net_connection.rb', line 25
def ssl_connection
@ssl_connection
end
|
#user ⇒ Object
Returns the value of attribute user.
26
27
28
|
# File 'lib/net/net_connection.rb', line 26
def user
@user
end
|
Instance Method Details
#disconnect ⇒ Object
91
92
93
94
95
96
|
# File 'lib/net/net_connection.rb', line 91
def disconnect()
on_close()
if(Thread.current != @read_thread)
@read_thread.join
end
end
|
#on_close ⇒ Object
136
137
138
139
140
141
|
# File 'lib/net/net_connection.rb', line 136
def on_close()
ProcessWanker::with_logged_rescue("on_close",Log::DEBUG) do
@ssl_connection.close if(@ssl_connection)
end
@ssl_connection=nil
end
|
#on_msg(msg) ⇒ Object
149
150
151
|
# File 'lib/net/net_connection.rb', line 149
def on_msg(msg)
end
|
#read_connection ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/net/net_connection.rb', line 116
def read_connection()
begin
length=@ssl_connection.read(4)
raise "closed" if(length.length != 4)
length=length.unpack("N")[0]
data=@ssl_connection.read(length)
raise "closed" if(data.length != length)
msg=Marshal.load(data)
on_msg(msg)
rescue Exception => e
on_close()
end
end
|
#read_proc ⇒ Object
104
105
106
107
108
|
# File 'lib/net/net_connection.rb', line 104
def read_proc
while(@ssl_connection)
read_connection()
end
end
|
#send_msg(msg) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/net/net_connection.rb', line 57
def send_msg(msg)
@write_mutex.synchronize do
debug("sending message #{msg.inspect}")
begin
data=Marshal.dump(msg)
length=[data.length].pack("N")
@ssl_connection.write(length + data)
rescue Exception => e
on_close()
end
end
end
|
#wait ⇒ Object
47
48
49
|
# File 'lib/net/net_connection.rb', line 47
def wait
@read_thread.join
end
|