Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
8258d56b34 | |||
5a8e0b602d | |||
ae81b7eb0a | |||
b487f98d9e | |||
1dc686215e | |||
c8670ae1b9 |
@ -1142,9 +1142,10 @@ assemble_calldata2(OwnerID, Nonce, Amount, TTL, Gas, GasPrice, Compiled, CallDat
|
||||
read_aci(Path) ->
|
||||
case file:read_file(Path) of
|
||||
{ok, Bin} ->
|
||||
case zx_lib:b_to_ts(Bin) of
|
||||
error -> {error, bad_aci};
|
||||
OK -> OK
|
||||
try
|
||||
{ok, binary_to_term(Bin, [safe])}
|
||||
catch
|
||||
error:badarg -> error
|
||||
end;
|
||||
Error ->
|
||||
Error
|
||||
|
@ -6,8 +6,6 @@
|
||||
|
||||
-export([connect/4, slowly_connect/4]).
|
||||
|
||||
-include("$zx_include/zx_logger.hrl").
|
||||
|
||||
|
||||
connect(Node = {Host, Port}, Request, From, Timeout) ->
|
||||
Timer = erlang:send_after(Timeout, self(), timeout),
|
||||
@ -78,7 +76,7 @@ parse(Received, Sock, From, Timer) ->
|
||||
<<"HTTP/1.1 500 Internal Server Error\r\n", Tail/binary>> ->
|
||||
parse2(500, Tail, Sock, From, Timer);
|
||||
_ ->
|
||||
ok = zx_net:disconnect(Sock),
|
||||
ok = disconnect(Sock),
|
||||
ok = erlang:cancel_timer(Timer, [{async, true}]),
|
||||
gen_server:reply(From, {error, {received, Received}})
|
||||
end.
|
||||
@ -115,7 +113,7 @@ consume2(Length, Received, Sock, From, Timer) ->
|
||||
if
|
||||
Size == Length ->
|
||||
ok = erlang:cancel_timer(Timer, [{async, true}]),
|
||||
ok = zx_net:disconnect(Sock),
|
||||
ok = disconnect(Sock),
|
||||
Result = zj:decode(Received),
|
||||
gen_server:reply(From, Result);
|
||||
Size < Length ->
|
||||
@ -236,3 +234,47 @@ url({Node, Port}, Path) when is_list(Node) ->
|
||||
["https://", Node, ":", integer_to_list(Port), Path];
|
||||
url({Node, Port}, Path) when is_tuple(Node) ->
|
||||
["https://", inet:ntoa(Node), ":", integer_to_list(Port), Path].
|
||||
|
||||
|
||||
|
||||
log(Level, Format, Args) ->
|
||||
Raw = io_lib:format("~w ~w: " ++ Format, [?MODULE, self() | Args]),
|
||||
Entry = unicode:characters_to_list(Raw),
|
||||
logger:log(Level, Entry).
|
||||
|
||||
|
||||
disconnect(Socket) ->
|
||||
case peername(Socket) of
|
||||
{ok, {Addr, Port}} ->
|
||||
Host = inet:ntoa(Addr),
|
||||
disconnect(Socket, Host, Port);
|
||||
{error, Reason} ->
|
||||
log(warning, "Disconnect failed with: ~w", [Reason])
|
||||
end.
|
||||
|
||||
disconnect(Socket, Host, Port) ->
|
||||
case gen_tcp:shutdown(Socket, read_write) of
|
||||
ok ->
|
||||
ok;
|
||||
{error, enotconn} ->
|
||||
receive
|
||||
{tcp_closed, Socket} -> ok
|
||||
after 0 -> ok
|
||||
end;
|
||||
{error, E} ->
|
||||
ok = log(warning, "~ts:~w disconnect failed with: ~w", [Host, Port, E]),
|
||||
receive
|
||||
{tcp_closed, Socket} -> ok
|
||||
after 0 -> ok
|
||||
end
|
||||
end.
|
||||
|
||||
|
||||
peername(Socket) ->
|
||||
case inet:peername(Socket) of
|
||||
{ok, {{0, 0, 0, 0, 0, 65535, X, Y}, Port}} ->
|
||||
<<A:8, B:8, C:8, D:8>> = <<X:16, Y:16>>,
|
||||
{ok, {{A, B, C, D}, Port}};
|
||||
Other ->
|
||||
Other
|
||||
end.
|
||||
|
@ -28,8 +28,6 @@
|
||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
||||
code_change/3, terminate/2]).
|
||||
|
||||
%% TODO: Make logging more flexible
|
||||
-include("$zx_include/zx_logger.hrl").
|
||||
|
||||
|
||||
%%% Type and Record Definitions
|
||||
@ -268,3 +266,11 @@ do_request(Request,
|
||||
do_request(Request, From, State = #s{chain_nodes = {[], Used}}) ->
|
||||
Fresh = lists:reverse(Used),
|
||||
do_request(Request, From, State#s{chain_nodes = {Fresh, []}}).
|
||||
|
||||
|
||||
|
||||
|
||||
log(Level, Format, Args) ->
|
||||
Raw = io_lib:format("~w ~w: " ++ Format, [?MODULE, self() | Args]),
|
||||
Entry = unicode:characters_to_list(Raw),
|
||||
logger:log(Level, Entry).
|
||||
|
Loading…
x
Reference in New Issue
Block a user