Module: Toq::Protocol

Includes:
Raktr::Connection::TLS
Included in:
Client::Handler, Server::Handler
Defined in:
lib/toq/protocol.rb

Overview

Provides helper transport methods for Message transmission.

Author:

Instance Method Summary collapse

Instance Method Details

#on_connectObject

Initializes an SSL session once the connection has been established and sets #status to ‘:ready`.



21
22
23
24
25
26
27
28
29
# File 'lib/toq/protocol.rb', line 21

def on_connect
    start_tls(
        ca:          @opts[:ssl_ca],
        private_key: @opts[:ssl_pkey],
        certificate: @opts[:ssl_cert]
    )

    @status = :ready
end

#on_read(data) ⇒ Object

Receives data from the network.

Rhe data will be chunks of a serialized object which will be buffered until the whole transmission has finished.

It will then unserialize it and pass it to #receive_object.



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/toq/protocol.rb', line 45

def on_read( data )
    (@buf ||= '') << data

    while @buf.size >= 4
        if @buf.size >= 4 + ( size = @buf.unpack( 'N' ).first )
            @buf.slice!( 0, 4 )
            receive_object( unserialize( @buf.slice!( 0, size ) ) )
        else
            break
        end
    end
end

#send_message(msg) ⇒ Object Also known as: send_request, send_response

Parameters:

  • msg (Message)

    Message to send to the peer.



33
34
35
# File 'lib/toq/protocol.rb', line 33

def send_message( msg )
    send_object( msg.prepare_for_tx )
end