Implement being aware of the handshake
All checks were successful
Build & Test / nix-build (push) Successful in 1m6s

This commit is contained in:
Kodi Craft 2024-07-07 22:46:18 +02:00
parent c742fa7c97
commit 5c708a22d2
Signed by: kodi
GPG Key ID: 69D9EED60B242822
2 changed files with 13 additions and 4 deletions

View File

@ -23,7 +23,10 @@ defmodule Amethyst.Server.Generic do
alias Amethyst.Minecraft.Read alias Amethyst.Minecraft.Read
def get_packet(client) do def get_packet(client) do
{[length], ""} = get_varint(client, "")
{:ok, full_packet} = :gen_tcp.recv(client, length)
{[id], data} = Read.start(full_packet) |> Read.varint() |> Read.stop()
{id, data}
end end
defp get_varint(client, acc) do defp get_varint(client, acc) do

View File

@ -18,10 +18,16 @@ defmodule Amethyst.Server.Stage1 do
@moduledoc """ @moduledoc """
This module contains the stage 1 (Handshaking) server logic. This module contains the stage 1 (Handshaking) server logic.
""" """
require Logger
alias Amethyst.Minecraft.Read alias Amethyst.Minecraft.Read
@spec serve(:gen_tcp.socket()) :: no_return()
def serve(client) do def serve(client) do
{id, data} = Amethyst.Server.Generic.get_packet(client)
case deserialize(id, data) do
{:handshake, ver, addr, port, next} -> Logger.info("Got handshake on ver #{ver}, #{addr}:#{port} moving to #{next}")
end
serve(client)
end end
## DESERIALIZATION ## DESERIALIZATION
@ -34,9 +40,9 @@ defmodule Amethyst.Server.Stage1 do
3 -> :transfer 3 -> :transfer
_ -> raise RuntimeError, "Client requested moving to an unknown state!" _ -> raise RuntimeError, "Client requested moving to an unknown state!"
end end
%{type: :handshake, version: ver, address: addr, port: port, next: next} {:handshake, ver, addr, port, next}
end end
def deserialize(<<type, _::binary>>) do def deserialize(type, _) do
raise RuntimeError, "Got unknown packet type #{type}!" raise RuntimeError, "Got unknown packet type #{type}!"
end end
end end