Working send_chat() poggers

This commit is contained in:
ChillerDragon 2022-11-01 10:37:24 +01:00
parent 07ac2e07b8
commit cd0d5da9fa
3 changed files with 86 additions and 1 deletions

View file

@ -4,6 +4,7 @@ require_relative 'bytes'
class NetChunk
attr_reader :next, :data, :msg, :sys, :flags
@@sent_vital_chunks = 4 # BIG TODO: SEND READY AND SHIT WITH PROPER HEADER
def initialize(data)
@next = nil
@ -19,6 +20,54 @@ class NetChunk
@next = data[chunk_end..] if data.size > chunk_end
end
##
# Create int array ready to be send over the network
#
# Given the flags hash (vital/resend)
# the size
# the sequence number
#
# It will create a 3 byte chunk header
# represented as an Array of 3 integers
def self.create_vital_header(flags, size, seq = nil)
@@sent_vital_chunks += 1
if seq.nil?
seq = @@sent_vital_chunks
end
flag_bits = '00'
flag_bits[0] = flags[:resend] ? '1' : '0'
flag_bits[1] = flags[:vital] ? '1' : '0'
size_bits = size.to_s(2).rjust(12, '0')
# size_bits[0..5]
# size_bits[6..]
seq_bits = seq.to_s(2).rjust(10, '0')
# seq_bits[0..1]
# seq_bits[2..]
# The vital chunk header is 3 bytes
# containing flags, size and sequence
# in the following format
#
# f=flag
# s=size
# q=sequence
#
# ffss ssss qqss ssss qqqq qqqq
header_bits =
flag_bits +
size_bits[0..5] +
seq_bits[0..1] +
size_bits[6..] +
seq_bits[2..]
header_bits.chars.groups_of(8).map do |eigth_bits|
eigth_bits.join('').to_i(2)
end
end
def parse_header(data)
# flags
flags = data[0].unpack("B*").first
@ -103,5 +152,7 @@ def todo_make_this_an_rspec_test
p chunks[0].sys == true
end
# todo_make_this_an_rspec_test
def test2
p NetChunk.create_vital_header({vital: true}, 20, 5) == [64, 20, 5]
end

View file

@ -90,6 +90,14 @@ NET_CONNSTATE_ERROR = 5
NET_MAX_PACKETSIZE = 1400
CHAT_NONE = 0
CHAT_ALL = 1
CHAT_TEAM = 2
CHAT_WHISPER = 3
NUM_CHATS = 4
TARGET_SERVER = -1
PACKET_HEADER_SIZE = 7
CHUNK_HEADER_SIZE = 3

View file

@ -122,6 +122,29 @@ class TwClient
@netbase.send_packet([0x40, 0x01, 0x04, 0x27])
end
##
# Turns int into network byte
#
# Takes a NETMSGTYPE_CL_* integer
# and returns a byte that can be send over
# the network
#
# todo: does not support system flag yet
def pack_msg_id(msg_id)
msg_id << 1
end
def send_chat(str)
@netbase.send_packet(
NetChunk.create_vital_header({vital: true}, 4 + str.length) +
[
pack_msg_id(NETMSGTYPE_CL_SAY),
CHAT_ALL,
64 # should use TARGET_SERVER (-1) instead of hacking 64 in here
] +
str.chars.map(&:ord) + [0x00])
end
def send_input
header = [0x10, 0x0A, 01] + str_bytes(@token)
random_compressed_input = [
@ -307,6 +330,9 @@ class TwClient
if @ticks % 8 == 0
send_ctrl_keepalive
end
# if @ticks % 20 == 0
# send_chat("hello world")
# end
end
def disconnect