Class: Semlogr::Sinks::Logentries::TcpConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/semlogr/sinks/logentries/tcp_connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, port, ssl) ⇒ TcpConnection

Returns a new instance of TcpConnection.



9
10
11
12
13
# File 'lib/semlogr/sinks/logentries/tcp_connection.rb', line 9

def initialize(host, port, ssl)
  @host = host
  @port = port
  @ssl = ssl
end

Instance Method Details

#closeObject



51
52
53
54
55
56
57
58
59
# File 'lib/semlogr/sinks/logentries/tcp_connection.rb', line 51

def close
  if @socket.respond_to?(:sysclose)
    @socket.sysclose
  elsif @socket.respond_to?(:close)
    @socket.close
  end
ensure
  @socket = nil
end

#connectObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/semlogr/sinks/logentries/tcp_connection.rb', line 29

def connect
  socket = TCPSocket.new(@host, @port)

  configure_socket_keepalive(socket)

  if @ssl
    cert_store = OpenSSL::X509::Store.new
    cert_store.set_default_paths

    ssl_context = OpenSSL::SSL::SSLContext.new
    ssl_context.cert_store = cert_store
    ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER

    socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context)
    socket.hostname = @host if socket.respond_to? :hostname=
    socket.sync_close = true
    socket.connect
  end

  @socket = socket
end

#connected?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/semlogr/sinks/logentries/tcp_connection.rb', line 15

def connected?
  !@socket.nil?
end

#write(data) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/semlogr/sinks/logentries/tcp_connection.rb', line 19

def write(data)
  connect unless connected?

  @socket.write(data)
rescue StandardError
  close

  raise
end