Skip to content

Commit

Permalink
Add a blocking read with timeout
Browse files Browse the repository at this point in the history
The timeout defaults to infinity. read/2 will return the next available
read buffer before the timeout.

recv/2 works the other way: it will wait up until the timeout and
accumulate any messages.
  • Loading branch information
msantos committed Aug 19, 2012
1 parent c74cb04 commit 04fade9
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/vert_console.erl
Expand Up @@ -58,6 +58,7 @@
close/1,

write/2,
read/1, read/2,
send/2,
recv/2,
flush/1,
Expand Down Expand Up @@ -115,6 +116,19 @@ send(Ref, Data) when is_pid(Ref), is_list(Data) ->
send(Ref, Data) when is_pid(Ref), is_binary(Data) ->
write(Ref, <<Data/binary, $\n>>).

read(Ref) ->
read(Ref, infinity).
read(Ref, Timeout) ->
receive
{vert_console, Ref, Buf} ->
{ok, Buf};
{vert_console_error, Ref, Error} ->
Error
after
Timeout ->
{error, timeout}
end.

recv(Ref, Timeout) when is_pid(Ref), Timeout > 0 ->
recv_loop(Ref, Timeout, []).

Expand Down Expand Up @@ -192,7 +206,6 @@ handle_info({vert_console, Data}, #state{pid = Pid} = State) ->
{noreply, State};

handle_info({vert_console_error, Error}, #state{pid = Pid} = State) ->
error_logger:error_report([wtf, Error]),
Pid ! {vert_console_error, self(), Error},
{stop, normal, State};

Expand Down

0 comments on commit 04fade9

Please sign in to comment.