Skip to content

Commit

Permalink
make try_read take an io::IO instead of data::Vector{UInt8} (#38)
Browse files Browse the repository at this point in the history
* update try_read method to take in an io instead of data

* update try_read method for ConnectionRequestPacket to take in io instead
of data

* minor cleanup
  • Loading branch information
Sid-Bhatia-0 authored Mar 23, 2024
1 parent 9af3bbe commit d806a3e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
24 changes: 4 additions & 20 deletions netcode/serialization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,7 @@ function try_read(io::IO, ::Type{NetcodeAddress})
return NetcodeAddress(address_type, host_ipv4, host_ipv6, port)
end

function try_read(data::Vector{UInt8}, ::Type{ConnectTokenPacket})
if length(data) != SIZE_OF_CONNECT_TOKEN_PACKET
return nothing
end

io = IOBuffer(data)

function try_read(io::IO, ::Type{ConnectTokenPacket})
netcode_version_info = read(io, SIZE_OF_NETCODE_VERSION_INFO)
if netcode_version_info != NETCODE_VERSION_INFO
return nothing
Expand Down Expand Up @@ -188,7 +182,7 @@ function try_read(data::Vector{UInt8}, ::Type{ConnectTokenPacket})
end
end

connect_token_packet = ConnectTokenPacket(
return ConnectTokenPacket(
netcode_version_info,
protocol_id,
create_timestamp,
Expand All @@ -201,17 +195,9 @@ function try_read(data::Vector{UInt8}, ::Type{ConnectTokenPacket})
client_to_server_key,
server_to_client_key,
)

return connect_token_packet
end

function try_read(data::Vector{UInt8}, ::Type{ConnectionRequestPacket})
if length(data) != SIZE_OF_CONNECTION_REQUEST_PACKET
return nothing
end

io = IOBuffer(data)

function try_read(io::IO, ::Type{ConnectionRequestPacket})
packet_type = read(io, TYPE_OF_PACKET_TYPE)
if packet_type != PACKET_TYPE_CONNECTION_REQUEST_PACKET
return nothing
Expand All @@ -236,14 +222,12 @@ function try_read(data::Vector{UInt8}, ::Type{ConnectionRequestPacket})

encrypted_private_connect_token_data = read(io, SIZE_OF_ENCRYPTED_PRIVATE_CONNECT_TOKEN_DATA)

connection_request_packet = ConnectionRequestPacket(
return ConnectionRequestPacket(
packet_type,
netcode_version_info,
protocol_id,
expire_timestamp,
nonce,
encrypted_private_connect_token_data,
)

return connection_request_packet
end
15 changes: 13 additions & 2 deletions netcode/simulate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,14 @@ function start_app_server(app_server_address, room_size)
end

if data[1] == PACKET_TYPE_CONNECTION_REQUEST_PACKET
connection_request_packet = try_read(data, ConnectionRequestPacket)
if length(data) != SIZE_OF_CONNECTION_REQUEST_PACKET
@info "Invalid connection request packet received"
continue
end

io = IOBuffer(data)

connection_request_packet = try_read(io, ConnectionRequestPacket)

if !isnothing(connection_request_packet)
@info "Received PACKET_TYPE_CONNECTION_REQUEST_PACKET"
Expand Down Expand Up @@ -148,7 +155,11 @@ function start_client(auth_server_address, username, password)

response = HTTP.get("http://" * username * ":" * hashed_password * "@" * string(auth_server_address.host) * ":" * string(auth_server_address.port))

connect_token_packet = try_read(copy(response.body), ConnectTokenPacket)
if length(response.body) != SIZE_OF_CONNECT_TOKEN_PACKET
error("Invalid connect token packet received")
end

connect_token_packet = try_read(IOBuffer(response.body), ConnectTokenPacket)
if isnothing(connect_token_packet)
error("Invalid connect token packet received")
end
Expand Down

0 comments on commit d806a3e

Please sign in to comment.