-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from ImNotAVirus/documentation
Ready for v0.1.1
- Loading branch information
Showing
66 changed files
with
2,244 additions
and
511 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
# TODO | ||
# Changelog | ||
|
||
## 0.1.0 | ||
|
||
Pre-release, this is not a production ready release ! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
# TODOLIST | ||
# Todo list | ||
|
||
- [ ] Fix Endpoint start_link and child_spec | ||
- [ ] Serializers for `ElvenGard.Network.Socket` | ||
- [ ] Rewrite the doc for `ElvenGard.Network.Socket` | ||
- [ ] Use `c:handle_error/2` in `ElvenGard.Network.Endpoint.Protocol` | ||
- [ ] Check if the lib is working in clustered mode | ||
- Abstract the message handler and provide a generic way to handle all transport (ranch, gen_tcp, gen_udp, ...) | ||
- Refacto ElvenGard.Network.PacketSerializer | ||
- Refacto ElvenGard.Network.Endpoint.Protocol | ||
- Document all `use` according to the bests practices | ||
- Add telemetry | ||
- use @derive for packet serialization/deserialization instead of `@serializable` and `@deserializable` ?? | ||
- mix task `elven_network.new` to create a project structure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Used by "mix format" | ||
[ | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"], | ||
import_deps: [:elvengard_network] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
_build/ | ||
deps/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Config | ||
|
||
config :login_server, LoginServer.Endpoint, | ||
listener_name: :login_server, | ||
transport: :ranch_tcp, | ||
transport_opts: [ip: "127.0.0.1", port: 3000], | ||
protocol: LoginServer.Endpoint.Protocol | ||
|
||
config :login_server, LoginServer.Endpoint.Protocol, | ||
packet_handler: LoginServer.Endpoint.PacketHandler, | ||
network_codec: LoginServer.Endpoint.NetworkCodec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
defmodule LoginServer do | ||
@moduledoc """ | ||
Documentation for `LoginServer`. | ||
""" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule LoginServer.Application do | ||
# See https://hexdocs.pm/elixir/Application.html | ||
# for more information on OTP Applications | ||
@moduledoc false | ||
|
||
use Application | ||
|
||
@impl true | ||
def start(_type, _args) do | ||
children = [ | ||
LoginServer.Endpoint | ||
] | ||
|
||
# See https://hexdocs.pm/elixir/Supervisor.html | ||
# for other strategies and supported options | ||
opts = [strategy: :one_for_one, name: LoginServer.Supervisor] | ||
Supervisor.start_link(children, opts) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule LoginServer.ClientPackets do | ||
@moduledoc """ | ||
Documentation for LoginServer.ClientPackets | ||
""" | ||
|
||
use ElvenGard.Network.PacketSerializer | ||
|
||
alias LoginServer.Types.StringType | ||
|
||
## Ping packet | ||
|
||
@deserializable true | ||
defpacket "PING", as: PingRequest | ||
|
||
## Login packet | ||
|
||
@deserializable true | ||
defpacket "LOGIN", as: LoginRequest do | ||
field :username, StringType | ||
field :password, StringType | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule LoginServer.Endpoint do | ||
@moduledoc """ | ||
Documentation for LoginServer.Endpoint | ||
""" | ||
|
||
use ElvenGard.Network.Endpoint, otp_app: :login_server | ||
|
||
require Logger | ||
|
||
## Callbacks | ||
|
||
@impl true | ||
def handle_start(config) do | ||
host = get_in(config, [:transport_opts, :socket_opts, :ip]) | ||
port = get_in(config, [:transport_opts, :socket_opts, :port]) | ||
Logger.info("LoginServer started on #{:inet.ntoa(host)}:#{port}") | ||
end | ||
end |
43 changes: 43 additions & 0 deletions
43
examples/login_server/lib/login_server/endpoint/network_codec.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
defmodule LoginServer.Endpoint.NetworkCodec do | ||
@moduledoc """ | ||
Documentation for LoginServer.Endpoint.NetworkCodec | ||
""" | ||
|
||
@behaviour ElvenGard.Network.NetworkCodec | ||
|
||
alias LoginServer.ClientPackets | ||
|
||
## Behaviour impls | ||
|
||
@impl true | ||
def next(raw, _socket) do | ||
case String.split(raw, "\n", parts: 2) do | ||
[packet] -> {packet, ""} | ||
[packet, rest] -> {packet, rest} | ||
end | ||
end | ||
|
||
@impl true | ||
def decode(raw, socket) do | ||
case String.split(raw, " ", parts: 2) do | ||
[packet_id] -> ClientPackets.deserialize(packet_id, "", socket) | ||
[packet_id, params] -> ClientPackets.deserialize(packet_id, params, socket) | ||
end | ||
end | ||
|
||
@impl true | ||
def encode(struct, _socket) when is_struct(struct) do | ||
# %LoginSucceed{world_info: %WorldInfo{host: "127.0.0.1", port: 5000}} | ||
struct | ||
# {"SUCCESS", ["127.0.0.1", ":", "5000"]} | ||
|> struct.__struct__.serialize() | ||
# ["SUCCESS", ["127.0.0.1", ":", "5000"]] | ||
|> Tuple.to_list() | ||
# ["SUCCESS", " ", ["127.0.0.1", ":", "5000"]] | ||
|> Enum.intersperse(" ") | ||
# [["SUCCESS", " ", ["127.0.0.1", ":", "5000"]], "\n"] | ||
|> then(&[&1 | "\n"]) | ||
|
||
# This will be serialized into `SUCCESS 127.0.0.1:5000\n` | ||
end | ||
end |
46 changes: 46 additions & 0 deletions
46
examples/login_server/lib/login_server/endpoint/packet_handler.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
defmodule LoginServer.Endpoint.PacketHandler do | ||
@moduledoc """ | ||
LoginServer.Endpoint.PacketHandler | ||
""" | ||
|
||
@behaviour ElvenGard.Network.PacketHandler | ||
|
||
alias ElvenGard.Network.Socket | ||
|
||
alias LoginServer.ClientPackets.{PingRequest, LoginRequest} | ||
alias LoginServer.PacketViews | ||
|
||
## Handlers | ||
|
||
@impl true | ||
def handle_packet(%PingRequest{}, socket) do | ||
render = PacketViews.render(:pong_response, %{time: DateTime.utc_now()}) | ||
:ok = Socket.send(socket, render) | ||
{:cont, socket} | ||
end | ||
|
||
def handle_packet(%LoginRequest{username: username, password: password}, socket) do | ||
render = | ||
if auth_using_db(username, password) do | ||
PacketViews.render(:login_succeed, %{world: get_worlds_from_manager()}) | ||
else | ||
PacketViews.render(:login_failed, %{reason: "Bad credentials :/`"}) | ||
end | ||
|
||
:ok = Socket.send(socket, render) | ||
{:halt, socket} | ||
end | ||
|
||
## Fake functions | ||
|
||
defp auth_using_db(username, password) do | ||
case {username, password} do | ||
{"admin", "password"} -> true | ||
_ -> false | ||
end | ||
end | ||
|
||
defp get_worlds_from_manager() do | ||
%{host: "127.0.0.1", port: 5000} | ||
end | ||
end |
35 changes: 35 additions & 0 deletions
35
examples/login_server/lib/login_server/endpoint/protocol.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule LoginServer.Endpoint.Protocol do | ||
@moduledoc """ | ||
Documentation for LoginServer.Endpoint.Protocol | ||
""" | ||
|
||
use ElvenGard.Network.Endpoint.Protocol | ||
|
||
require Logger | ||
|
||
alias ElvenGard.Network.Socket | ||
|
||
## Callbacks | ||
|
||
@impl true | ||
def handle_init(%Socket{} = socket) do | ||
Logger.info("New connection: #{socket.id}") | ||
|
||
%Socket{transport: transport, transport_pid: transport_pid} = socket | ||
:ok = transport.setopts(transport_pid, packet: :line, reuseaddr: true) | ||
|
||
{:ok, socket} | ||
end | ||
|
||
@impl true | ||
def handle_message(message, %Socket{} = socket) do | ||
Logger.debug("New message from #{socket.id}: #{inspect(message)}") | ||
{:ok, socket} | ||
end | ||
|
||
@impl true | ||
def handle_halt(reason, %Socket{} = socket) do | ||
Logger.info("#{socket.id} is now disconnected (reason: #{inspect(reason)})") | ||
{:ok, socket} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule LoginServer.PacketViews do | ||
@moduledoc """ | ||
Documentation for LoginServer.PacketViews | ||
""" | ||
|
||
use ElvenGard.Network.View | ||
|
||
alias LoginServer.ServerPackets.{PongResponse, LoginFailed, LoginSucceed} | ||
alias LoginServer.SubPackets.WorldInfo | ||
|
||
@impl true | ||
def render(:pong_response, %{time: time}) do | ||
%PongResponse{time: time} | ||
end | ||
|
||
def render(:login_failed, %{reason: reason}) do | ||
%LoginFailed{reason: reason} | ||
end | ||
|
||
def render(:login_succeed, %{world: world}) do | ||
%LoginSucceed{world: %WorldInfo{host: world.host, port: world.port}} | ||
end | ||
end |
Oops, something went wrong.