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
|
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.
|
This module contains the stage 1 (Handshaking) server logic.
|
||||||
"""
|
"""
|
||||||
require Logger
|
require Logger
|
||||||
alias Amethyst.Minecraft.Read
|
use Amethyst.Server
|
||||||
|
|
||||||
@spec serve(:gen_tcp.socket()) :: no_return()
|
alias Amethyst.Minecraft.Read
|
||||||
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
|
|
||||||
|
|
||||||
## DESERIALIZATION
|
## DESERIALIZATION
|
||||||
# Handshake https://wiki.vg/Protocol#Handshake
|
# Handshake https://wiki.vg/Protocol#Handshake
|
||||||
@ -48,6 +41,11 @@ defmodule Amethyst.Server.Stage1 do
|
|||||||
raise RuntimeError, "Got unknown packet type #{type}!"
|
raise RuntimeError, "Got unknown packet type #{type}!"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
## SERIALIZATION
|
||||||
|
def serialize(_) do
|
||||||
|
raise RuntimeError, "No packets can be transmitted while still in the handshake stage!"
|
||||||
|
end
|
||||||
|
|
||||||
## HANDLING
|
## HANDLING
|
||||||
# Handshake https://wiki.vg/Protocol#Handshake
|
# Handshake https://wiki.vg/Protocol#Handshake
|
||||||
@spec handle(any(), any()) :: no_return()
|
@spec handle(any(), any()) :: no_return()
|
||||||
|
@ -20,25 +20,11 @@ defmodule Amethyst.Server.Status do
|
|||||||
is only asking a bit of information.
|
is only asking a bit of information.
|
||||||
"""
|
"""
|
||||||
require Logger
|
require Logger
|
||||||
|
use Amethyst.Server
|
||||||
|
|
||||||
alias Amethyst.Minecraft.Read
|
alias Amethyst.Minecraft.Read
|
||||||
alias Amethyst.Minecraft.Write
|
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
|
## DESERIALIZATION
|
||||||
# Status Request https://wiki.vg/Protocol#Status_Request
|
# Status Request https://wiki.vg/Protocol#Status_Request
|
||||||
def deserialize(0x00, _) do
|
def deserialize(0x00, _) do
|
||||||
|
Loading…
Reference in New Issue
Block a user