Class: Warchat::Network::Connection
- Inherits:
-
Object
- Object
- Warchat::Network::Connection
- Defined in:
- lib/warchat/network/connection.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#on_close ⇒ Object
Returns the value of attribute on_close.
-
#on_receive ⇒ Object
Returns the value of attribute on_receive.
-
#on_send ⇒ Object
Returns the value of attribute on_send.
-
#port ⇒ Object
Returns the value of attribute port.
Instance Method Summary collapse
- #close(reason = "") ⇒ Object
- #handle_requests ⇒ Object
- #handle_responses ⇒ Object
-
#initialize(*args) ⇒ Connection
constructor
A new instance of Connection.
- #is_closed? ⇒ Boolean
- #send_request(request) ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(*args) ⇒ Connection
Returns a new instance of Connection.
10 11 12 13 14 15 16 17 18 |
# File 'lib/warchat/network/connection.rb', line 10 def initialize *args = args.pop if args.last.is_a? Hash self.host = (args.shift or "m.us.wowarmory.com") self.port = (args.shift or 8780) @closed = true @queue = [] @mutex = Mutex.new end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
8 9 10 |
# File 'lib/warchat/network/connection.rb', line 8 def host @host end |
#on_close ⇒ Object
Returns the value of attribute on_close.
8 9 10 |
# File 'lib/warchat/network/connection.rb', line 8 def on_close @on_close end |
#on_receive ⇒ Object
Returns the value of attribute on_receive.
8 9 10 |
# File 'lib/warchat/network/connection.rb', line 8 def on_receive @on_receive end |
#on_send ⇒ Object
Returns the value of attribute on_send.
8 9 10 |
# File 'lib/warchat/network/connection.rb', line 8 def on_send @on_send end |
#port ⇒ Object
Returns the value of attribute port.
8 9 10 |
# File 'lib/warchat/network/connection.rb', line 8 def port @port end |
Instance Method Details
#close(reason = "") ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/warchat/network/connection.rb', line 32 def close reason = "" return if is_closed? on_close and on_close.call(reason) @mutex.synchronize do @closed = true @socket.close end ([@request_thread,@response_thread]-[Thread.current]).each &:join end |
#handle_requests ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/warchat/network/connection.rb', line 65 def handle_requests until is_closed? @mutex.synchronize do until @queue.empty? request = @queue.shift unless is_closed? request.stream @socket @socket.flush on_send and on_send.call(request) end end end sleep 0.01 end rescue Exception => e puts e. puts e.backtrace end |
#handle_responses ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/warchat/network/connection.rb', line 54 def handle_responses until is_closed? response = Response.new(@socket) on_receive and on_receive.call(response) unless is_closed? sleep 0.01 end rescue Exception => e puts e. puts e.backtrace unless e.is_a? IOError end |
#is_closed? ⇒ Boolean
44 45 46 |
# File 'lib/warchat/network/connection.rb', line 44 def is_closed? @closed or @socket.nil? end |
#send_request(request) ⇒ Object
48 49 50 51 52 |
# File 'lib/warchat/network/connection.rb', line 48 def send_request request @mutex.synchronize do @queue << request end end |
#start ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/warchat/network/connection.rb', line 20 def start close @closed = false; @socket = TCPSocket.open(host,port) @socket.sync = false @request_thread = Thread.new &method(:handle_requests) @response_thread = Thread.new &method(:handle_responses) @request_thread['name'] = 'Request Thead' @response_thread['name'] = 'Response Thread' end |