Skip to content

Commit

Permalink
Support OTP-19-rc2
Browse files Browse the repository at this point in the history
  • Loading branch information
lpgauth committed Jun 14, 2016
1 parent 9265c3d commit 678d8ed
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
3 changes: 2 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
]}.

{erl_opts, [
debug_info
debug_info,
{platform_define, "19", 'UDP_HEADER'}
]}.

{profiles, [
Expand Down
10 changes: 4 additions & 6 deletions src/statsderl_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
]).

-record(state, {
header :: binary(),
header :: iodata(),
socket :: inet:socket()
}).

Expand Down Expand Up @@ -70,11 +70,9 @@ loop(State) ->
udp_header(Hostname, Port, BaseKey) ->
case statsderl_utils:getaddrs(Hostname) of
{ok, {A, B, C, D}} ->
{ok, iolist_to_binary([
[((Port) bsr 8) band 16#ff, (Port) band 16#ff],
[A band 16#ff, B band 16#ff, C band 16#ff, D band 16#ff],
statsderl_utils:base_key(BaseKey)
])};
Header = statsderl_udp:header({A, B, C, D}, Port),
BaseKey2 = statsderl_utils:base_key(BaseKey),
{ok, [Header, BaseKey2]};
{error, Reason} ->
{error, Reason}
end.
44 changes: 44 additions & 0 deletions src/statsderl_udp.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
-module(statsderl_udp).
-include("statsderl.hrl").

-compile(inline).
-compile({inline_size, 512}).

-export([
header/2,
send/3
]).

-define(INET_AF_INET, 1).
-define(INT16(X), [((X) bsr 8) band 16#ff, (X) band 16#ff]).

%% public
-spec header(inet:ip_address(), inet:port_number()) -> iodata().

-ifdef(UDP_HEADER).

header(IP, Port) ->
[?INET_AF_INET, ?INT16(Port) | ip4_to_bytes(IP)].

-else.

header(IP, Port) ->
[?INT16(Port) | ip4_to_bytes(IP)].

-endif.

-spec send(inet:socket(), iodata(), iodata()) ->
ok | {error, term()}.

send(Socket, Header, Data) ->
try
true = erlang:port_command(Socket, [Header, Data]),
ok
catch
Error:Reason ->
{error, {Error, Reason}}
end.

%% private
ip4_to_bytes({A, B, C, D}) ->
[A band 16#ff, B band 16#ff, C band 16#ff, D band 16#ff].

0 comments on commit 678d8ed

Please sign in to comment.