Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make try_read take an io::IO instead of data::Vector{UInt8} #38

Merged
merged 3 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading