Move shared boilerplate in servers to Amethyst.Server
All checks were successful
Build & Test / nix-build (push) Successful in 1m9s
All checks were successful
Build & Test / nix-build (push) Successful in 1m9s
This commit is contained in:
parent
770b88a882
commit
1ce9b7c454
@ -37,3 +37,30 @@ defmodule Amethyst.Server.Generic do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmodule Amethyst.Server do
|
||||
@moduledoc """
|
||||
This module includes shared boilerplate code for all stages of the server.
|
||||
"""
|
||||
|
||||
defmacro __using__(_opts) do
|
||||
quote do
|
||||
@spec serve(:gen_tcp.socket()) :: no_return()
|
||||
def serve(client) do
|
||||
{id, data} = Amethyst.Server.Generic.get_packet(client)
|
||||
packet = deserialize(id, data)
|
||||
Logger.debug("Got packet #{inspect(packet)}")
|
||||
handle(packet, client)
|
||||
serve(client)
|
||||
end
|
||||
|
||||
|
||||
def transmit(packet, client) do
|
||||
Logger.debug("Transmitting #{inspect(packet)}")
|
||||
data = serialize(packet)
|
||||
length = byte_size(data) |> Amethyst.Minecraft.Write.varint()
|
||||
:gen_tcp.send(client, length <> data)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -19,16 +19,9 @@ defmodule Amethyst.Server.Stage1 do
|
||||
This module contains the stage 1 (Handshaking) server logic.
|
||||
"""
|
||||
require Logger
|
||||
alias Amethyst.Minecraft.Read
|
||||
use Amethyst.Server
|
||||
|
||||
@spec serve(:gen_tcp.socket()) :: no_return()
|
||||
def serve(client) do
|
||||
{id, data} = Amethyst.Server.Generic.get_packet(client)
|
||||
packet = deserialize(id, data)
|
||||
Logger.debug("Got packet #{inspect(packet)}")
|
||||
handle(packet, client)
|
||||
serve(client)
|
||||
end
|
||||
alias Amethyst.Minecraft.Read
|
||||
|
||||
## DESERIALIZATION
|
||||
# Handshake https://wiki.vg/Protocol#Handshake
|
||||
@ -48,6 +41,11 @@ defmodule Amethyst.Server.Stage1 do
|
||||
raise RuntimeError, "Got unknown packet type #{type}!"
|
||||
end
|
||||
|
||||
## SERIALIZATION
|
||||
def serialize(_) do
|
||||
raise RuntimeError, "No packets can be transmitted while still in the handshake stage!"
|
||||
end
|
||||
|
||||
## HANDLING
|
||||
# Handshake https://wiki.vg/Protocol#Handshake
|
||||
@spec handle(any(), any()) :: no_return()
|
||||
|
@ -20,25 +20,11 @@ defmodule Amethyst.Server.Status do
|
||||
is only asking a bit of information.
|
||||
"""
|
||||
require Logger
|
||||
use Amethyst.Server
|
||||
|
||||
alias Amethyst.Minecraft.Read
|
||||
alias Amethyst.Minecraft.Write
|
||||
|
||||
@spec serve(:gen_tcp.socket()) :: no_return()
|
||||
def serve(client) do
|
||||
{id, data} = Amethyst.Server.Generic.get_packet(client)
|
||||
packet = deserialize(id, data)
|
||||
Logger.debug("Got packet #{inspect(packet)}")
|
||||
handle(packet, client)
|
||||
serve(client)
|
||||
end
|
||||
|
||||
def transmit(packet, client) do
|
||||
Logger.debug("Transmitting #{inspect(packet)}")
|
||||
data = serialize(packet)
|
||||
length = byte_size(data) |> Write.varint()
|
||||
:gen_tcp.send(client, length <> data)
|
||||
end
|
||||
|
||||
## DESERIALIZATION
|
||||
# Status Request https://wiki.vg/Protocol#Status_Request
|
||||
def deserialize(0x00, _) do
|
||||
|
Loading…
Reference in New Issue
Block a user