class ClientWrapper

The wrapper for TCPClient.

Public Class Methods

new(port=2000,ip="127.0.0.1") { |self,client| ... } click to toggle source
# File lib/accu-net.rb, line 133
def initialize(port=2000,ip="127.0.0.1")
        @client = TCPSocket.open(ip, port)
        @password,@mode = "password",:double
        yield self,@client if block_given?
        @client.close
end

Public Instance Methods

listen() { |msg| ... } click to toggle source

Listens for a generic network communication and yields to a passed block (if any).

# File lib/accu-net.rb, line 142
def listen()
        msg = @client.gets.chomp
        yield msg if block_given?
        return msg
end
listen_code() click to toggle source

Listens for a network code.

# File lib/accu-net.rb, line 159
def listen_code()
        listen { |msg|
                if msg.slice(0..1) == "C:" then
                        return msg.slice(2..msg.length).to_i
                else
                        return msg.slice(0..1)
                end
        }
end
listen_encrypted() click to toggle source

Listen for an encrypted connection.

# File lib/accu-net.rb, line 170
def listen_encrypted()
        if listen_code == 3 then
                password,source = EncryptDecrypt.decrypt(@password,((listen_lines).join),@mode)
                return source
        else
                raise "Sent code was not 3!"
        end
end
listen_lines() click to toggle source

Listens for multiple lines, appends them together and stops at END.

# File lib/accu-net.rb, line 150
def listen_lines()
        lines = []
        while line = @client.gets.chomp and line != "END" do
                lines << line
        end
        lines
end
send(text) click to toggle source

Sends a plaintext message.

# File lib/accu-net.rb, line 180
def send(text)
        @client.puts text
end
send_code(code) click to toggle source

Sends a code.

# File lib/accu-net.rb, line 203
def send_code(code)
        send("C:#{code}")
end
send_encrypted(lines) click to toggle source

Send over an encrypted multiline message.

# File lib/accu-net.rb, line 195
def send_encrypted(lines)
        secret,text = EncryptDecrypt.encrypt(@password,lines,@mode)
        send_code(3)
        send(text)
        send("END")
end
send_lines(lines) click to toggle source

Sends multiple lines and appends END to end the send.

# File lib/accu-net.rb, line 186
def send_lines(lines)
        array = lines.split("\n")
        array.each do |line|
                send(line)
        end
        send("END")
end
set_password(string) click to toggle source

Set encryption password.

# File lib/accu-net.rb, line 208
def set_password(string)
        warn "Argument 1 is not a string!  Continuing anyway." if not string.is_a? String
        @password = string.to_s
end