Sha256: b561b1c5f8afdc743b906a3e31c092638c94124f7f2482a20d64bb1d98e263b2

Contents?: true

Size: 1.29 KB

Versions: 10

Compression:

Stored size: 1.29 KB

Contents

require 'forwardable'
require 'socket'
require 'stringio'
require 'zlib'

module Weskit::MP
  class Connection
    extend Forwardable

    attr_reader :adapter, :options, :socket
    def_delegators :adapter, :buffer, :message, :read, :write

    def close
      @socket.close
    end

    def defaults
      {
        :debug   => false,
        :host    => 'server.wesnoth.org',
        :port    => 15000,
        :version => '1.10.0'
      }
    end

    def initialize worker, options = {}
      @options = defaults.merge options

      @adapter = Adapter.new self, @options[:debug]
      @worker  = worker
    end

    def open
      @socket = socket_for destination_socket
      @worker.login
      
      if block_given?
        result = yield
        close ; result
      end
    end

    private

    def destination_socket
      @socket  = socket_for @options
      redirect = read.redirect

      @socket.close
      redirect
    end

    def socket_for hash
      @socket = TCPSocket.new hash[:host], hash[:port]

      socket_handshake
      socket_init

      @socket
    end

    def socket_handshake
      @socket.send "\x00" * 4, 0
      @socket.read 4
    end

    def socket_init
      read

      @worker.verify_response :version
      message 'version', {:version => @options[:version]}
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
weskit-0.3.6 lib/weskit/mp/connection.rb
weskit-0.3.5 lib/weskit/mp/connection.rb
weskit-0.3.4 lib/weskit/mp/connection.rb
weskit-0.3.3 lib/weskit/mp/connection.rb
weskit-0.3.2 lib/weskit/mp/connection.rb
weskit-0.3.1 lib/weskit/mp/connection.rb
weskit-0.3.0 lib/weskit/mp/connection.rb
weskit-0.2.1 lib/weskit/mp/connection.rb
weskit-0.2.0 lib/weskit/mp/connection.rb
weskit-0.1.0 lib/weskit/mp/connection.rb