class ApnsKit::Connection
Attributes
connected[R]
http[R]
uri[R]
Public Class Methods
new(uri, certificate)
click to toggle source
# File lib/apns_kit/connection.rb, line 16 def initialize(uri, certificate) @uri = uri @certificate = certificate @connected = false @mutex = Mutex.new end
Public Instance Methods
close()
click to toggle source
# File lib/apns_kit/connection.rb, line 29 def close shutdown if !@thread.nil? end
open()
click to toggle source
# File lib/apns_kit/connection.rb, line 23 def open if !connected && (@thread.nil? || @thread.stop?) start end end
ping()
click to toggle source
# File lib/apns_kit/connection.rb, line 33 def ping if @http ApnsKit.log_debug("Sending ping") @http.ping("whatever") end end
Private Instance Methods
close_connection!()
click to toggle source
# File lib/apns_kit/connection.rb, line 99 def close_connection! @mutex.synchronize do ApnsKit.log_info("Closing connection") @socket.close if @socket @connected = false @http = nil ApnsKit.log_info("Connection closed") end end
setup_connection!()
click to toggle source
# File lib/apns_kit/connection.rb, line 42 def setup_connection! @mutex.synchronize do ApnsKit.log_info("Setting up connection") ctx = @certificate.ssl_context tcp = TCPSocket.new(@uri.host, @uri.port) @socket = OpenSSL::SSL::SSLSocket.new(tcp, ctx) @socket.sync_close = true @socket.hostname = @uri.hostname @socket.connect @connected = true @http = HTTP2::Client.new @http.on(:frame) do |bytes| ApnsKit.log_debug("Sending bytes: #{bytes.unpack("H*").first}") @socket.print bytes @socket.flush end ping ApnsKit.log_info("Connection established") end end
shutdown()
click to toggle source
# File lib/apns_kit/connection.rb, line 93 def shutdown @thread.exit @thread.join close_connection! end
start()
click to toggle source
# File lib/apns_kit/connection.rb, line 68 def start setup_connection! @thread = Thread.new { loop do begin if @socket.closed? close_connection! ApnsKit.log_warn("Socket was closed") break elsif !@socket.eof? data = @socket.readpartial(1024) ApnsKit.log_debug("Received bytes: #{data.unpack("H*").first}") @http << data end rescue => e close_connection! ApnsKit.log_warn("#{e.class} exception: #{e.message} - closing socket") e.backtrace.each { |l| ApnsKit.log_debug(l) } raise e end end } return true end