CARVIEW |
Select Language
HTTP/2 200
date: Fri, 10 Oct 2025 23:15:04 GMT
content-type: text/html; charset=UTF-8
server: cloudflare
x-frame-options: DENY
x-content-type-options: nosniff
x-xss-protection: 1;mode=block
vary: accept-encoding
cf-cache-status: DYNAMIC
content-encoding: gzip
set-cookie: _csrf-frontend=2342302d93d065c4560d664b0f800ca3f7b74e48cf2e5121a78732b889416d26a%3A2%3A%7Bi%3A0%3Bs%3A14%3A%22_csrf-frontend%22%3Bi%3A1%3Bs%3A32%3A%222mEb4sXkdTwEfKRU14Si5TOTX5pnbhkm%22%3B%7D; HttpOnly; Path=/
cf-ray: 98c9d14f2e01d817-BLR
001_Connection_Communication.rb - Pastebin.com
SHARE
TWEET

001_Connection_Communication.rb
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'socket'
- require 'io/wait'
- class Connection
- class Disconnected < Exception; end
- class ProtocolError < StandardError; end
- def self.open(host, port)
- # XXX: Non-blocking connect.
- begin
- socket = TCPSocket.open(host, port)
- connection = Connection.new(socket)
- yield connection
- end
- end
- def initialize(socket)
- @socket = socket
- @recv_parser = Parser.new
- @recv_records = []
- @discard_records = 0
- end
- def update
- if @socket.nread>0
- recvd = @socket.recv(4096)
- raise Disconnected.new("server disconnected") if recvd.empty?
- @recv_parser.parse(recvd) {|record| @recv_records << record}
- end
- # Process at most one record so that any control flow in the block doesn't cause us to lose records.
- if !@recv_records.empty?
- record = @recv_records.shift
- if record.disconnect?
- reason = record.str() rescue "unknown error"
- raise Disconnected.new(reason)
- end
- if @discard_records == 0
- begin
- yield record
- rescue
- raise # compat
- else
- raise ProtocolError.new("Unconsumed input: #{record}") if !record.empty?
- end
- else
- @discard_records -= 1
- end
- end
- end
- def can_send?
- return !IO.select(nil, [@socket],nil).nil?
- end
- def send
- # XXX: Non-blocking send.
- # but note we don't update often so we need some sort of drained?
- # for the send buffer so that we can delay starting the battle.
- writer = RecordWriter.new
- yield writer
- @socket.write_nonblock(writer.line!)
- end
- def discard(n)
- raise "Cannot discard #{n} messages." if n < 0
- @discard_records += n
- end
- def dispose
- @socket.close
- @parser = nil
- end
- end
- class Parser
- def initialize
- @buffer = ""
- end
- def parse(data)
- return if data.empty?
- lines = data.split("\n", -1)
- lines[0].insert(0, @buffer)
- @buffer = lines.pop
- lines.each do |line|
- yield RecordParser.new(line) if !line.empty?
- end
- end
- end
- class RecordParser
- def initialize(data)
- @fields = []
- field = ""
- escape = false
- # each_char and chars don't exist.
- for i in (0...data.length)
- char = data[i].chr
- if char == "," && !escape
- @fields << field
- field = ""
- elsif char == "\\" && !escape
- escape = true
- else
- field += char
- escape = false
- end
- end
- @fields << field
- @fields.reverse!
- end
- def empty?; return @fields.empty? end
- def disconnect?
- if @fields.last == "disconnect"
- @fields.pop
- return true
- else
- return false
- end
- end
- def nil_or(t)
- raise Connection::ProtocolError.new("Expected nil or #{t}, got EOL") if @fields.empty?
- if @fields.last.empty?
- @fields.pop
- return nil
- else
- return self.send(t)
- end
- end
- def bool
- raise Connection::ProtocolError.new("Expected bool, got EOL") if @fields.empty?
- field = @fields.pop
- if field == "true"
- return true
- elsif field == "false"
- return false
- else
- raise Connection::ProtocolError.new("Expected bool, got #{field}")
- end
- end
- def int
- raise Connection::ProtocolError.new("Expected int, got EOL") if @fields.empty?
- field = @fields.pop
- begin
- return Integer(field)
- rescue
- raise Connection::ProtocolError.new("Expected int, got #{field}")
- end
- end
- def str
- raise Connection::ProtocolError.new("Expected str, got EOL") if @fields.empty?
- @fields.pop
- end
- def sym
- raise Connection::ProtocolError.new("Expected sym, got EOL") if @fields.empty?
- @fields.pop.to_sym
- end
- def to_s; @fields.reverse.join(", ") end
- end
- class RecordWriter
- def initialize
- @fields = []
- end
- def line!
- line = @fields.map {|field| escape!(field)}.join(",")
- line += "\n"
- @fields = []
- return line
- end
- def escape!(s)
- t=s.clone(freeze: false)
- t.gsub!("\\", "\\\\")
- t.gsub!(",", "\,")
- return t
- end
- def nil_or(t, o)
- if o.nil?
- @fields << ""
- else
- self.send(t, o)
- end
- end
- def bool(b); @fields << b.to_s end
- def int(i); @fields << i.to_s end
- def str(s) @fields << s end
- def sym(s); @fields << s.to_s end
- end
Advertisement
Add Comment
Please, Sign In to add comment
-
⭐⭐⭐Make $1500 in 20 minutes⭐⭐
Java | 1 sec ago | 0.10 KB
-
⭐⭐Exchange Exploit⭐⭐ E
JavaScript | 2 sec ago | 0.24 KB
-
✅⭐ Make huge profits on trading ⭐⭐ 0
JavaScript | 13 sec ago | 0.24 KB
-
⭐⭐⭐MAKE $900 INSTANTLY⭐⭐
Java | 14 sec ago | 0.10 KB
-
⭐✅ MAKE $2500 IN 15 MIN⭐⭐⭐ 7
JavaScript | 24 sec ago | 0.24 KB
-
⭐✅ Exploit 2500$ in 15 Minutes⭐⭐⭐ 7
JavaScript | 35 sec ago | 0.24 KB
-
✅⭐ Make huge profits on trading ⭐⭐ A
JavaScript | 39 sec ago | 0.24 KB
-
⭐✅ MAKE $2500 IN 15 MIN⭐⭐⭐ O
JavaScript | 50 sec ago | 0.24 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand