Class: DCase::Server
- Inherits:
-
Object
- Object
- DCase::Server
- Includes:
- Celluloid::IO
- Defined in:
- lib/dcase/server.rb
Instance Attribute Summary collapse
-
#crypto ⇒ Object
Returns the value of attribute crypto.
Instance Method Summary collapse
- #finalize ⇒ Object
- #handle_data(data, port, addr) ⇒ Object
-
#initialize(host, crypto, config) ⇒ Server
constructor
A new instance of Server.
- #run ⇒ Object
- #start_connect(request, port, addr) ⇒ Object
Constructor Details
#initialize(host, crypto, config) ⇒ Server
Returns a new instance of Server.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/dcase/server.rb', line 8 def initialize(host, crypto, config) @config = config @server = UDPSocket.new @server.to_io.setsockopt(:SOCKET, :REUSEADDR, 1) @server.bind(host, @config.port) @crypto = crypto async.run end |
Instance Attribute Details
#crypto ⇒ Object
Returns the value of attribute crypto.
6 7 8 |
# File 'lib/dcase/server.rb', line 6 def crypto @crypto end |
Instance Method Details
#finalize ⇒ Object
20 21 22 |
# File 'lib/dcase/server.rb', line 20 def finalize @server.close unless @server.closed? end |
#handle_data(data, port, addr) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/dcase/server.rb', line 31 def handle_data(data, port, addr) request = UDPSocket.new request.send crypto.decrypt(data), 0, @config.dns_list.sample, 53 async.start_connect(request, port, addr) end |
#run ⇒ Object
24 25 26 27 28 29 |
# File 'lib/dcase/server.rb', line 24 def run loop do data, (_, port, addr) = @server.recvfrom(16384) handle_data(data, port, addr) end end |
#start_connect(request, port, addr) ⇒ Object
38 39 40 41 42 |
# File 'lib/dcase/server.rb', line 38 def start_connect(request, port, addr) data, (_, _port, _addr) = request.recvfrom(16384) @server.send crypto.encrypt(data), 0, addr, port request.close unless request.closed? end |