Skip to content

Commit

Permalink
Drop Channel.loop/4, obsoleted by SSHKit.stream/1
Browse files Browse the repository at this point in the history
  • Loading branch information
pmeinhardt committed Dec 14, 2020
1 parent 540b3dc commit 55827b8
Showing 1 changed file with 0 additions and 102 deletions.
102 changes: 0 additions & 102 deletions lib/sshkit/ssh/channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -195,106 +195,4 @@ defmodule SSHKit.SSH.Channel do
def adjust(channel, size) when is_integer(size) do
channel.impl.adjust_window(channel.connection.ref, channel.id, size)
end

@doc """
Loops over channel messages until the channel is closed, or looping is stopped
explicitly.
Expects an accumulator on each call that determines how to proceed:
1. `{:cont, acc}`
The loop will wait for an inbound message. It will then pass the message and
current `acc` to the looping function. `fun`'s return value is the
accumulator for the next cycle.
2. `{:cont, message, acc}`
Sends a message to the remote end of the channel before waiting for a
message as outlined in the `{:cont, acc}` case above. `message` may be one
of the following:
* `{0, data}` or `{1, data}` - sends normal or stderr data to the remote
* `data` - is a shortcut for `{0, data}`
* `:eof` - sends EOF
3. `{:halt, acc}`
Terminates the loop, returning `{:halted, acc}`.
4. `{:suspend, acc}`
Suspends the loop, returning `{:suspended, acc, continuation}`.
`continuation` is a function that accepts a new accumulator value and that,
when called, will resume the loop.
`timeout` specifies the maximum wait time for receiving and sending individual
messages.
Once the final `{:closed, channel}` message is received, the loop will
terminate and return `{:done, acc}`. The channel will be closed if it has
not been closed before.
"""
def loop(channel, timeout \\ :infinity, acc, fun)

def loop(channel, timeout, {:cont, msg, acc}, fun) do
case lsend(channel, msg, timeout) do
:ok -> loop(channel, timeout, {:cont, acc}, fun)
err -> halt(channel, err)
end
end

def loop(channel, timeout, {:cont, acc}, fun) do
case recv(channel, timeout) do
{:ok, msg} ->
if elem(msg, 0) == :closed do
{_, acc} = fun.(msg, acc)
done(channel, acc)
else
:ok = ljust(channel, msg)
loop(channel, timeout, fun.(msg, acc), fun)
end
err -> halt(channel, err)
end
end

def loop(channel, _, {:halt, acc}, _) do
halt(channel, acc)
end

def loop(channel, timeout, {:suspend, acc}, fun) do
suspend(channel, acc, fun, timeout)
end

defp halt(channel, acc) do
:ok = close(channel)
:ok = flush(channel)
{:halted, acc}
end

defp suspend(channel, acc, fun, timeout) do
{:suspended, acc, &loop(channel, timeout, &1, fun)}
end

defp done(_, acc) do
{:done, acc}
end

defp lsend(_, nil, _), do: :ok

defp lsend(channel, :eof, _), do: eof(channel)

defp lsend(channel, {type, data}, timeout) do
send(channel, type, data, timeout)
end

defp lsend(channel, data, timeout) do
send(channel, 0, data, timeout)
end

defp ljust(channel, {:data, _, _, data}) do
adjust(channel, byte_size(data))
end

defp ljust(_, _), do: :ok
end

0 comments on commit 55827b8

Please sign in to comment.