Class: EventMachine::RubySockets::TcpClient

Inherits:
EM::Connection
  • Object
show all
Includes:
TcpConnection
Defined in:
lib/em-ruby-sockets/tcp_client.rb

Instance Method Summary collapse

Methods included from TcpConnection

#error?, #local_address, #remote_address, #stream_file_data

Instance Method Details

#disconnectObject Also known as: close_connection, close_connection_after_writing

Terminates the connection regardless it’s been sucessfuly connected or not. The callback on_disconnected will be called with argument nil.



141
142
143
144
145
146
147
148
# File 'lib/em-ruby-sockets/tcp_client.rb', line 141

def disconnect
  return if @error
  @error = true
  detach
  @io.close  unless @io.closed?
  @buffer.clear
  on_disconnected nil
end

#get_outbound_data_sizeObject

Returns the number of bytes to be sent by the socket.



177
178
179
# File 'lib/em-ruby-sockets/tcp_client.rb', line 177

def get_outbound_data_size
  @buffer.size
end

#ioObject

Access to the Ruby socket itself. A good place for socket settings is the post_init method.



113
114
115
# File 'lib/em-ruby-sockets/tcp_client.rb', line 113

def io
  @io
end

#on_connectedObject

Called upon connection is established.

This method must be defined in the user’s inherinted class.



191
192
# File 'lib/em-ruby-sockets/tcp_client.rb', line 191

def on_connected
end

#on_connection_error(cause) ⇒ Object

Called when the connection attemp failed. cause can be:

  • :pending_connect_timeout

    Connection not established within pending_connect_timeout value.

  • exception

    An exception.

This method must be defined in the user’s inherinted class.



199
200
# File 'lib/em-ruby-sockets/tcp_client.rb', line 199

def on_connection_error cause
end

#on_disconnected(cause) ⇒ Object

Called when the connection is terminated by the peer (and it was already established) or when it is terminated locally (by calling disconnect so cause becomes nil).

cause can be:

  • nil

    Local disconnection via disconnect method.

  • :remote_close

    Remote normal close.

  • exception

    Some other cause.

This method must be defined in the user’s inherinted class.



218
219
# File 'lib/em-ruby-sockets/tcp_client.rb', line 218

def on_disconnected cause
end

#pending_connect_timeoutObject

Gets the duration after which a TCP connection in a connecting state will fail. Returns nil if not set.



172
173
174
# File 'lib/em-ruby-sockets/tcp_client.rb', line 172

def pending_connect_timeout
  @pending_connect_timeout
end

#pending_connect_timeout=(duration) ⇒ Object Also known as: set_pending_connect_timeout

Sets the duration after which a TCP connection in a connecting state will fail. After the given duration on_connection_error() is called with argument :pending_connect_timeout.

Arguments:

  • duration

    Value in seconds (float).



158
159
160
161
162
163
164
165
166
167
# File 'lib/em-ruby-sockets/tcp_client.rb', line 158

def pending_connect_timeout= duration
  return false  if @pending_connect_timeout

  EM.add_timer(@pending_connect_timeout = duration.to_f) do
    unless @error or @connected
      connection_terminated :pending_connect_timeout
    end
  end
  @pending_connect_timeout
end

#post_initObject

Executed upon instance initialization and before the connection is attempted. Good place for defining custom attributes.

This method must be defined in the user’s inherinted class.



185
186
# File 'lib/em-ruby-sockets/tcp_client.rb', line 185

def post_init
end

#receive_data(data) ⇒ Object

Called when TCP data is received in the socket.

This method must be defined in the user’s inherinted class.



205
206
# File 'lib/em-ruby-sockets/tcp_client.rb', line 205

def receive_data data
end

#send_data(data) ⇒ Object

Sends the given data to the remote peer. It can be safely called before the connection is established (the data is then stored within an internal buffer). Returns true if the connection is established or trying to connect. False if the connection has been closed or if an unrecoverable error occurred while writting into the socket.

Arguments:

  • data

    a String.

Raises:

  • (::ArgumentError)


126
127
128
129
130
131
132
133
134
135
136
# File 'lib/em-ruby-sockets/tcp_client.rb', line 126

def send_data data
  raise ::ArgumentError, "argument must be a String"  unless data.is_a?(::String)
  return false  if @error

  if @connected
    try_send_data data
  else
    @buffer << data
  end
  true
end