echo server
This commit is contained in:
18
lib/protohackers.ex
Normal file
18
lib/protohackers.ex
Normal file
@@ -0,0 +1,18 @@
|
||||
defmodule Protohackers do
|
||||
@moduledoc """
|
||||
Documentation for `Protohackers`.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Hello world.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Protohackers.hello()
|
||||
:world
|
||||
|
||||
"""
|
||||
def hello do
|
||||
:world
|
||||
end
|
||||
end
|
20
lib/protohackers/application.ex
Normal file
20
lib/protohackers/application.ex
Normal file
@@ -0,0 +1,20 @@
|
||||
defmodule Protohackers.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 = [
|
||||
{Protohackers.EchoServer, port: 5001},
|
||||
{Protohackers.PrimeTimeServer, port: 5002}
|
||||
]
|
||||
|
||||
# See https://hexdocs.pm/elixir/Supervisor.html
|
||||
# for other strategies and supported options
|
||||
opts = [strategy: :one_for_one, name: Protohackers.Supervisor]
|
||||
Supervisor.start_link(children, opts)
|
||||
end
|
||||
end
|
69
lib/protohackers/echo_server.ex
Normal file
69
lib/protohackers/echo_server.ex
Normal file
@@ -0,0 +1,69 @@
|
||||
defmodule Protohackers.EchoServer do
|
||||
use GenServer
|
||||
|
||||
require Logger
|
||||
|
||||
@spec start_link(keyword()) :: GenServer.on_start()
|
||||
def start_link(opts) do
|
||||
GenServer.start_link(__MODULE__, opts)
|
||||
end
|
||||
|
||||
defstruct [:listen_socket, :supervisor]
|
||||
|
||||
@impl true
|
||||
def init(opts) do
|
||||
port = Keyword.fetch!(opts, :port)
|
||||
{:ok, supervisor} = Task.Supervisor.start_link(max_children: 100)
|
||||
|
||||
listen_options = [
|
||||
ifaddr: {0, 0, 0, 0},
|
||||
mode: :binary,
|
||||
active: false,
|
||||
reuseaddr: true,
|
||||
exit_on_close: false
|
||||
]
|
||||
|
||||
case :gen_tcp.listen(port, listen_options) do
|
||||
{:ok, listen_socket} ->
|
||||
Logger.info("Started echo server on port #{port}")
|
||||
state = %__MODULE__{listen_socket: listen_socket, supervisor: supervisor}
|
||||
{:ok, state, {:continue, :accept}}
|
||||
|
||||
{:error, reason} ->
|
||||
{:stop, reason}
|
||||
end
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_continue(:accept, %__MODULE__{} = state) do
|
||||
case :gen_tcp.accept(state.listen_socket) do
|
||||
{:ok, socket} ->
|
||||
Task.Supervisor.start_child(state.supervisor, fn -> handle_connection(socket) end)
|
||||
{:noreply, state, {:continue, :accept}}
|
||||
|
||||
{:error, reason} ->
|
||||
{:stop, reason}
|
||||
end
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
defp handle_connection(socket) do
|
||||
case recv_until_closed(socket, _buffer = "", _buffered_size = 0) do
|
||||
{:ok, data} -> :gen_tcp.send(socket, data)
|
||||
{:error, reason} -> Logger.error("Failed to receive data: #{inspect(reason)}")
|
||||
end
|
||||
|
||||
:gen_tcp.close(socket)
|
||||
end
|
||||
|
||||
@limit _100_kb = 1024 * 100
|
||||
defp recv_until_closed(socket, buffer, buffered_size) do
|
||||
case :gen_tcp.recv(socket, 0, 10_000) do
|
||||
{:ok, data} when buffered_size + byte_size(data) > @limit -> {:error, :buffer_overflow}
|
||||
{:ok, data} -> recv_until_closed(socket, [buffer, data], buffered_size + byte_size(data))
|
||||
{:error, :closed} -> {:ok, buffer}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
end
|
69
lib/protohackers/prime_time_server.ex
Normal file
69
lib/protohackers/prime_time_server.ex
Normal file
@@ -0,0 +1,69 @@
|
||||
defmodule Protohackers.PrimeTimeServer do
|
||||
use GenServer
|
||||
|
||||
require Logger
|
||||
|
||||
@spec start_link(keyword()) :: GenServer.on_start()
|
||||
def start_link(opts) do
|
||||
GenServer.start_link(__MODULE__, opts)
|
||||
end
|
||||
|
||||
defstruct [:listen_socket, :supervisor]
|
||||
|
||||
@impl true
|
||||
def init(opts) do
|
||||
port = Keyword.fetch!(opts, :port)
|
||||
{:ok, supervisor} = Task.Supervisor.start_link(max_children: 100)
|
||||
|
||||
listen_options = [
|
||||
ifaddr: {0, 0, 0, 0},
|
||||
mode: :binary,
|
||||
active: false,
|
||||
reuseaddr: true,
|
||||
exit_on_close: false
|
||||
]
|
||||
|
||||
case :gen_tcp.listen(port, listen_options) do
|
||||
{:ok, listen_socket} ->
|
||||
Logger.info("Started prime time server on port #{port}")
|
||||
state = %__MODULE__{listen_socket: listen_socket, supervisor: supervisor}
|
||||
{:ok, state, {:continue, :accept}}
|
||||
|
||||
{:error, reason} ->
|
||||
{:stop, reason}
|
||||
end
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_continue(:accept, %__MODULE__{} = state) do
|
||||
case :gen_tcp.accept(state.listen_socket) do
|
||||
{:ok, socket} ->
|
||||
Task.Supervisor.start_child(state.supervisor, fn -> handle_connection(socket) end)
|
||||
{:noreply, state, {:continue, :accept}}
|
||||
|
||||
{:error, reason} ->
|
||||
{:stop, reason}
|
||||
end
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
defp handle_connection(socket) do
|
||||
case recv_until_closed(socket, _buffer = "", _buffered_size = 0) do
|
||||
{:ok, data} -> :gen_tcp.send(socket, data)
|
||||
{:error, reason} -> Logger.error("Failed to receive data: #{inspect(reason)}")
|
||||
end
|
||||
|
||||
:gen_tcp.close(socket)
|
||||
end
|
||||
|
||||
@limit _100_kb = 1024 * 100
|
||||
defp recv_until_closed(socket, buffer, buffered_size) do
|
||||
case :gen_tcp.recv(socket, 0, 10_000) do
|
||||
{:ok, data} when buffered_size + byte_size(data) > @limit -> {:error, :buffer_overflow}
|
||||
{:ok, data} -> recv_until_closed(socket, [buffer, data], buffered_size + byte_size(data))
|
||||
{:error, :closed} -> {:ok, buffer}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user