Skip to content

Commit

Permalink
feat(reg): api get_registration_name
Browse files Browse the repository at this point in the history
  • Loading branch information
qzhuyan committed Aug 29, 2023
1 parent cca97a0 commit 195f7f5
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions c_src/quicer_ctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ typedef struct QuicerRegistrationCTX
{
ErlNifEnv *env;
HQUIC Registration;
char name[UINT8_MAX + 1];
} QuicerRegistrationCTX;

/*
Expand Down
1 change: 1 addition & 0 deletions c_src/quicer_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,7 @@ static ErlNifFunc nif_funcs[] = {
{ "new_registration", 2, new_registration2, 0},
{ "shutdown_registration", 1, shutdown_registration_x, 0},
{ "shutdown_registration", 3, shutdown_registration_x, 0},
{ "get_registration_name", 1, get_registration_name1, 0},
{ "listen", 2, listen2, 0},
{ "start_listener", 3, start_listener3, 0},
{ "stop_listener", 1, stop_listener1, 0},
Expand Down
24 changes: 24 additions & 0 deletions c_src/quicer_reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ new_registration2(ErlNifEnv *env,
{
ERROR_TUPLE_2(ATOM_ERROR_NOT_ENOUGH_MEMORY);
}

if (0 >= enif_get_string(
env, ename, r_ctx->name, UINT8_MAX + 1, ERL_NIF_LATIN1))
{
deinit_r_ctx(r_ctx);
destroy_r_ctx(r_ctx);
ERROR_TUPLE_2(ATOM_BADARG);
}

// Open Registration
if (QUIC_FAILED(
status = MsQuic->RegistrationOpen(&RegConfig, &r_ctx->Registration)))
Expand Down Expand Up @@ -118,3 +127,18 @@ shutdown_registration_x(ErlNifEnv *env, int argc, const ERL_NIF_TERM *argv)

return ATOM_OK;
}

ERL_NIF_TERM
get_registration_name1(ErlNifEnv *env,
__unused_parm__ int argc,
const ERL_NIF_TERM argv[])
{
QuicerRegistrationCTX *r_ctx = NULL;
ERL_NIF_TERM ectx = argv[0];
if (!enif_get_resource(env, ectx, ctx_reg_t, (void **)&r_ctx))
{
return ERROR_TUPLE_2(ATOM_BADARG);
}

return SUCCESS(enif_make_string(env, r_ctx->name, ERL_NIF_LATIN1));
}
3 changes: 3 additions & 0 deletions c_src/quicer_reg.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ new_registration2(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
ERL_NIF_TERM
shutdown_registration_x(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);

ERL_NIF_TERM
get_registration_name1(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);

#endif // QUICER_REG_H_
7 changes: 7 additions & 0 deletions src/quicer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
, new_registration/2
, shutdown_registration/1
, shutdown_registration/3
, get_registration_name/1
, reg_open/0
, reg_open/1
, reg_close/0
Expand Down Expand Up @@ -161,6 +162,12 @@ shutdown_registration(Handle) ->
shutdown_registration(Handle, IsSilent, ErrCode) ->
quicer_nif:shutdown_registration(Handle, IsSilent, ErrCode).

%% @doc get registration name
-spec get_registration_name(Handle) ->
quicer_nif:get_registration_name(Handle).
get_registration_name(Handle) ->
quicer_nif:get_registration_name(Handle).

%% @doc GRegistraion should be opened before calling traffic APIs.
%%
%% This is called automatically when quicer application starts with
Expand Down
5 changes: 5 additions & 0 deletions src/quicer_nif.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
, new_registration/2
, shutdown_registration/1
, shutdown_registration/3
, get_registration_name/1
, listen/2
, start_listener/3
, stop_listener/1
Expand Down Expand Up @@ -126,6 +127,10 @@ shutdown_registration(_Handle) ->
shutdown_registration(_Handle, _IsSilent, _ErrorCode) ->
erlang:nif_error(nif_library_not_loaded).

-spec get_registration_name(reg_handle()) -> {ok, string()} | {error, badarg}.
get_registration_name(_Handle) ->
erlang:nif_error(nif_library_not_loaded).

-spec listen(listen_on(), listen_opts()) ->
{ok, listener_handle()} |
{error, listener_open_error, atom_reason()} |
Expand Down
12 changes: 10 additions & 2 deletions test/quicer_reg_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ all() ->
nomatch =/= string:prefix(atom_to_list(F), "tc_")
].


%%--------------------------------------------------------------------
%% @spec TestCase() -> Info
%% Info = [tuple()]
Expand All @@ -142,7 +141,8 @@ all() ->
tc_new_reg(_Config) ->
Name = atom_to_list(?FUNCTION_NAME),
Profile = quic_execution_profile_low_latency,
{ok, _Reg} = quicer:new_registration(Name, Profile),
{ok, Reg} = quicer:new_registration(Name, Profile),
quicer:shutdown_registration(Reg),
ok.

tc_shutdown_reg_1(_Config) ->
Expand Down Expand Up @@ -182,3 +182,11 @@ tc_shutdown_with_reason(_Config) ->
Profile = quic_execution_profile_low_latency,
{ok, Reg} = quicer:new_registration(Name, Profile),
ok = quicer:shutdown_registration(Reg, false, 123).

tc_get_reg_name(_Config) ->
Name = atom_to_list(?FUNCTION_NAME),
Profile = quic_execution_profile_low_latency,
{ok, Reg} = quicer:new_registration(Name, Profile),
?assertEqual({ok, Name}, quicer:get_registration_name(Reg)),
ok = quicer:shutdown_registration(Reg),
?assertEqual({ok, Name}, quicer:get_registration_name(Reg)).

0 comments on commit 195f7f5

Please sign in to comment.