fixup
This commit is contained in:
@@ -10,17 +10,19 @@ defmodule AlterProxy do
|
|||||||
backlog: 100
|
backlog: 100
|
||||||
])
|
])
|
||||||
|
|
||||||
accept_loop(listen_socket)
|
spawn(__MODULE__, :accept_loop, [listen_socket])
|
||||||
|
listen_socket
|
||||||
end
|
end
|
||||||
|
|
||||||
defp accept_loop(listen_socket) do
|
def accept_loop(listen_socket) do
|
||||||
{:ok, socket} = :gen_tcp.accept(listen_socket)
|
case :gen_tcp.accept(listen_socket) do
|
||||||
|
{:ok, socket} ->
|
||||||
# 爲每個客戶端創建新進程處理
|
|
||||||
spawn(__MODULE__, :handle_client, [socket])
|
spawn(__MODULE__, :handle_client, [socket])
|
||||||
|
|
||||||
# 繼續等待下一個連接
|
|
||||||
accept_loop(listen_socket)
|
accept_loop(listen_socket)
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
IO.puts("接受連接失敗: #{reason}")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_client(socket) do
|
def handle_client(socket) do
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ defmodule AlterProxy.Application do
|
|||||||
children = [
|
children = [
|
||||||
# Starts a worker by calling: AlterProxy.Worker.start_link(arg)
|
# Starts a worker by calling: AlterProxy.Worker.start_link(arg)
|
||||||
# {AlterProxy.Worker, arg}
|
# {AlterProxy.Worker, arg}
|
||||||
|
{Manager, name: Manager}
|
||||||
]
|
]
|
||||||
|
|
||||||
# See https://hexdocs.pm/elixir/Supervisor.html
|
# See https://hexdocs.pm/elixir/Supervisor.html
|
||||||
|
|||||||
26
lib/broker.ex
Normal file
26
lib/broker.ex
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
defmodule Broker do
|
||||||
|
use GenServer
|
||||||
|
|
||||||
|
def init(opts) do
|
||||||
|
{:ok, listen_socket} =
|
||||||
|
:gen_tcp.listen(opts[:port], [
|
||||||
|
:binary,
|
||||||
|
reuseaddr: true,
|
||||||
|
active: false,
|
||||||
|
backlog: 100
|
||||||
|
])
|
||||||
|
|
||||||
|
{:ok, opts}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp accept_loop(listen_socket) do
|
||||||
|
case :gen_tcp.accept(listen_socket) do
|
||||||
|
{:ok, socket} ->
|
||||||
|
spawn(__MODULE__, :handle_client, [socket])
|
||||||
|
accept_loop(listen_socket)
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
IO.puts("接受連接失敗: #{reason}")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
17
lib/manager.ex
Normal file
17
lib/manager.ex
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
defmodule Manager do
|
||||||
|
use DynamicSupervisor
|
||||||
|
|
||||||
|
def start_link(init_arg) do
|
||||||
|
DynamicSupervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def init(_init_arg) do
|
||||||
|
DynamicSupervisor.init(strategy: :one_for_one)
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_broker(port) do
|
||||||
|
spec = {Broker, port}
|
||||||
|
DynamicSupervisor.start_child(__MODULE__, spec)
|
||||||
|
end
|
||||||
|
end
|
||||||
43
lib/tcp_handler.ex
Normal file
43
lib/tcp_handler.ex
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
defmodule TcpHandler do
|
||||||
|
def start(port) do
|
||||||
|
# 監聽端口
|
||||||
|
{:ok, listen_socket} =
|
||||||
|
:gen_tcp.listen(port, [
|
||||||
|
:binary,
|
||||||
|
packet: :line,
|
||||||
|
reuseaddr: true,
|
||||||
|
active: false,
|
||||||
|
backlog: 100
|
||||||
|
])
|
||||||
|
|
||||||
|
accept_loop(listen_socket)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp accept_loop(listen_socket) do
|
||||||
|
case :gen_tcp.accept(listen_socket) do
|
||||||
|
{:ok, socket} ->
|
||||||
|
spawn(__MODULE__, :handle_client, [socket])
|
||||||
|
accept_loop(listen_socket)
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
IO.puts("接受連接失敗: #{reason}")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_client(socket) do
|
||||||
|
# 接收數據
|
||||||
|
case :gen_tcp.recv(socket, 0) do
|
||||||
|
{:ok, data} ->
|
||||||
|
IO.puts("收到: #{data}")
|
||||||
|
:gen_tcp.send(socket, "Echo: #{data}")
|
||||||
|
# 繼續接收
|
||||||
|
handle_client(socket)
|
||||||
|
|
||||||
|
{:error, :closed} ->
|
||||||
|
IO.puts("客戶端斷開連接")
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
IO.puts("接收數據失敗: #{reason}")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user