Class: MTProto::Transport::TCPConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/mtproto/transport/tcp_connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, codec_class: AbridgedPacketCodec, wait_timeout: 60, read_timeout: 3) ⇒ TCPConnection

Returns a new instance of TCPConnection.



12
13
14
15
16
17
18
19
20
# File 'lib/mtproto/transport/tcp_connection.rb', line 12

def initialize(host, port, codec_class: AbridgedPacketCodec, wait_timeout: 60, read_timeout: 3)
  @host = host
  @port = port
  @codec_class = codec_class
  @wait_timeout = wait_timeout
  @read_timeout = read_timeout
  @socket = nil
  @codec = nil
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



9
10
11
# File 'lib/mtproto/transport/tcp_connection.rb', line 9

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



9
10
11
# File 'lib/mtproto/transport/tcp_connection.rb', line 9

def port
  @port
end

#read_timeoutObject

Returns the value of attribute read_timeout.



10
11
12
# File 'lib/mtproto/transport/tcp_connection.rb', line 10

def read_timeout
  @read_timeout
end

#wait_timeoutObject

Returns the value of attribute wait_timeout.



10
11
12
# File 'lib/mtproto/transport/tcp_connection.rb', line 10

def wait_timeout
  @wait_timeout
end

Instance Method Details

#connect!Object



22
23
24
25
26
27
28
29
# File 'lib/mtproto/transport/tcp_connection.rb', line 22

def connect!
  return if connected?

  @socket = TCPSocket.new(@host, @port)
  @codec = @codec_class.new(@socket)

  write_init_tag
end

#connected?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/mtproto/transport/tcp_connection.rb', line 46

def connected?
  not not_connected?
end

#disconnect!Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/mtproto/transport/tcp_connection.rb', line 31

def disconnect!
  return unless @socket

  @socket.close
rescue StandardError
  nil
ensure
  @socket = nil
  @codec = nil
end

#not_connected?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/mtproto/transport/tcp_connection.rb', line 42

def not_connected?
  @socket.nil? || @socket.closed?
end

#receiveObject

Raises:



56
57
58
59
60
61
62
63
64
# File 'lib/mtproto/transport/tcp_connection.rb', line 56

def receive(&)
  raise NotConnectedError, 'Not connected' unless connected?

  if block_given?
    receive_loop(&)
  else
    receive_once
  end
end

#send(packet) ⇒ Object

Raises:



50
51
52
53
54
# File 'lib/mtproto/transport/tcp_connection.rb', line 50

def send(packet)
  raise NotConnectedError, 'Not connected' unless connected?

  @codec.send(packet)
end