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, port, crypto) ⇒ Server
constructor
A new instance of Server.
- #run ⇒ Object
- #start_connect(request, port, addr) ⇒ Object
Constructor Details
#initialize(host, port, crypto) ⇒ Server
Returns a new instance of Server.
8 9 10 11 12 13 14 15 |
# File 'lib/dcase/server.rb', line 8 def initialize(host, port, crypto) @server = UDPSocket.new @server.bind(host, 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
17 18 19 |
# File 'lib/dcase/server.rb', line 17 def finalize @server.close unless @server.closed? end |
#handle_data(data, port, addr) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/dcase/server.rb', line 28 def handle_data(data, port, addr) request = UDPSocket.new request.send crypto.decrypt(data), 0, '8.8.8.8', 53 async.start_connect(request, port, addr) end |
#run ⇒ Object
21 22 23 24 25 26 |
# File 'lib/dcase/server.rb', line 21 def run loop do data, (_, port, addr) = @server.recvfrom(16384) handle_data(data, port, addr) end end |
#start_connect(request, port, addr) ⇒ Object
35 36 37 38 39 |
# File 'lib/dcase/server.rb', line 35 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 |