diff options
Diffstat (limited to 'lib/net/tcpserver.ex')
-rw-r--r-- | lib/net/tcpserver.ex | 17 |
1 files changed, 4 insertions, 13 deletions
diff --git a/lib/net/tcpserver.ex b/lib/net/tcpserver.ex index e5ee996..7c758c1 100644 --- a/lib/net/tcpserver.ex +++ b/lib/net/tcpserver.ex @@ -6,7 +6,6 @@ defmodule SNet.TCPServer do Task.start_link(__MODULE__, :accept, [port]) end - @doc """ Starts accepting connections on the given `port`. """ @@ -14,22 +13,14 @@ defmodule SNet.TCPServer do {:ok, socket} = :gen_tcp.listen(port, [:binary, packet: 2, active: false, reuseaddr: true]) Logger.info "Accepting connections on port #{port}" - loop_acceptor(socket) + loop_acceptor(socket, port) end - defp loop_acceptor(socket) do + defp loop_acceptor(socket, my_port) do {:ok, client} = :gen_tcp.accept(socket) - {:ok, pid} = DynamicSupervisor.start_child(SNet.ConnSupervisor, {SNet.TCPConn, %{socket: client}}) + {:ok, pid} = DynamicSupervisor.start_child(SNet.ConnSupervisor, {SNet.TCPConn, %{socket: client, my_port: my_port}}) :ok = :gen_tcp.controlling_process(client, pid) - loop_acceptor(socket) + loop_acceptor(socket, my_port) end - - def add_peer(ip, port) do - {:ok, client} = :gen_tcp.connect(ip, port, [:binary, packet: 2, active: false]) - {:ok, pid} = DynamicSupervisor.start_child(SNet.ConnSupervisor, {SNet.TCPConn, %{socket: client}}) - :ok = :gen_tcp.controlling_process(client, pid) - pid - end - end |