Class: MTProto::Transport::TCPConnection
- Inherits:
-
Object
- Object
- MTProto::Transport::TCPConnection
- Defined in:
- lib/mtproto/transport/tcp_connection.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#read_timeout ⇒ Object
Returns the value of attribute read_timeout.
-
#wait_timeout ⇒ Object
Returns the value of attribute wait_timeout.
Instance Method Summary collapse
- #connect! ⇒ Object
- #connected? ⇒ Boolean
- #disconnect! ⇒ Object
-
#initialize(host, port, codec_class: AbridgedPacketCodec, wait_timeout: 60, read_timeout: 3) ⇒ TCPConnection
constructor
A new instance of TCPConnection.
- #not_connected? ⇒ Boolean
- #receive ⇒ Object
- #send(packet) ⇒ Object
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
#host ⇒ Object (readonly)
Returns the value of attribute host.
9 10 11 |
# File 'lib/mtproto/transport/tcp_connection.rb', line 9 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
9 10 11 |
# File 'lib/mtproto/transport/tcp_connection.rb', line 9 def port @port end |
#read_timeout ⇒ Object
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_timeout ⇒ Object
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
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
42 43 44 |
# File 'lib/mtproto/transport/tcp_connection.rb', line 42 def not_connected? @socket.nil? || @socket.closed? end |
#receive ⇒ Object
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
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 |