Skip to content

Commit

Permalink
Replace general write int func with specific ones
Browse files Browse the repository at this point in the history
No huge performance win or anything—just makes it more like the reader
  • Loading branch information
composerinteralia committed Apr 22, 2024
1 parent 2796b43 commit 6743d30
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
6 changes: 3 additions & 3 deletions lib/nocturne.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def change_db(db)
@conn.begin_command

@conn.write_packet do |packet|
packet.int(1, Protocol::COM_INIT_DB)
packet.int8(Protocol::COM_INIT_DB)
packet.str(db)
end

Expand All @@ -73,7 +73,7 @@ def ping
@conn.begin_command

@conn.write_packet do |packet|
packet.int(1, Protocol::COM_PING)
packet.int8(Protocol::COM_PING)
end

@conn.read_packet do |payload|
Expand All @@ -89,7 +89,7 @@ def close
@conn.begin_command

@conn.write_packet do |packet|
packet.int(1, Protocol::COM_QUIT)
packet.int8(Protocol::COM_QUIT)
end

@conn.close
Expand Down
22 changes: 12 additions & 10 deletions lib/nocturne/protocol/handshake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ def server_handshake
@options[:read_timeout] = original_read_timeout
end

UNUSED = "\0".b * 23

def ssl_request
@conn.write_packet do |packet|
# TODO don't hardcode all this
packet.int(4, 0x018aaa00) # capabilities (ssl capability set)
packet.int(4, 0xffffff) # max packet size
packet.int(1, 0x2d) # charset
packet.int(23, 0) # unused
packet.int32(0x018aaa00) # capabilities (ssl capability set)
packet.int32(0xffffff) # max packet size
packet.int8(0x2d) # charset
packet.str(UNUSED)
end

@conn.upgrade
Expand All @@ -78,18 +80,18 @@ def ssl_request
def client_handshake
@conn.write_packet do |packet|
# TODO don't hardcode all this
packet.int(4, 0x018aa200) # capabilities
packet.int(4, 0xffffff) # max packet size
packet.int(1, 0x2d) # charset
packet.int(23, 0) # unused
packet.int32(0x018aa200) # capabilities (ssl capability set)
packet.int32(0xffffff) # max packet size
packet.int8(0x2d) # charset
packet.str(UNUSED)

packet.nulstr(@options[:username] || "root")

if @auth_plugin_name == "mysql_native_password" && password?
packet.int(1, 20)
packet.int8(20)
packet.str(mysql_native_password(@auth_plugin_data))
else
packet.int(1, 0)
packet.int8(0)
end

packet.nulstr(@auth_plugin_name)
Expand Down
2 changes: 1 addition & 1 deletion lib/nocturne/protocol/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def query(sql)
@conn.begin_command

@conn.write_packet do |packet|
packet.int(1, Protocol::COM_QUERY)
packet.int8(Protocol::COM_QUERY)
packet.str(sql)
end

Expand Down
15 changes: 9 additions & 6 deletions lib/nocturne/write/packet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ def empty?
@buffer.empty?
end

def int(bytes, value)
while bytes > 0
@buffer << (value & 0xff)
value >>= 8
bytes -= 1
end
def int8(value)
@buffer << (value & 0xFF)
end

def int32(value)
@buffer << (value & 0xFF)
@buffer << ((value >> 8) & 0xFF)
@buffer << ((value >> 16) & 0xFF)
@buffer << ((value >> 24) & 0xFF)
end

def str(value)
Expand Down

0 comments on commit 6743d30

Please sign in to comment.