jnnks

jnnks

Need help using Erlang library Quicer in Elixir

I want to use the library GitHub - qzhuyan/quicer which is written in Erlang.
The readme contains a sample snippet:

application:ensure_all_started(quicer),
Port = 4567,
LOptions = [ {certfile, "cert.pem"}
           , {keyfile,  "key.pem"}
           , {alpn, ["sample"]}
           , {peer_bidi_stream_count, 1}
             ],
{ok, L} = quicer:listen(Port, LOptions),
{ok, Conn} = quicer:accept(L, [], 120000),
{ok, Conn} = quicer:handshake(Conn),
{ok, Stm} = quicer:accept_stream(Conn, []),
receive {quic, <<"ping">>, Stm, _Props} -> ok end,
{ok, 4} = quicer:send(Stm, <<"pong">>),
quicer:close_listener(L).

which I translated to the following:

:application.ensure_all_started(:quicer)

    port = 4567
    l_options = [
      {:certfile, "cert.pem"},
      {:keyfile, "key.pem"},
      {:alpn, ["sample"]},
      {:peer_bidi_stream_count, 1}
    ]
    # {:error, :badarg}
    {:ok, listener} = :quicer.listen(port, l_options)
    {:ok, conn} = :quicer.accept(listener, [], 120_000)
    {:ok, conn} = :quicer.handshake(conn)
    {:ok, stm} = :quicer.accept_stream(conn, [])

    receive do
      {:quic, <<"ping">>, stm, _props} -> :ok
    end

    {:ok, 4} = :quicer.send(stm, <<"pong">>)
    :quicer.close_listener(listener)

Something seems to be wrong, as there is a badarg (marked above).
ElixirLS Dialyzer is reporting the following:

The call quicer:listen
         (_port@1 :: 4567,
          _l_options@1 ::
              [{'alpn', [<<_:48>>, ...]} |
               {'certfile', <<_:64>>} |
               {'keyfile', <<_:56>>} |
               {'peer_bidi_stream_count', 1},
               ...]) breaks the contract 
          (listen_on(), listen_opts()) ->
             {'ok', listener_handler()} |
             {'error', 'listener_open_error', atom_reason()} |
             {'error', 'listener_start_error', atom_reason()}

What am I missing?
PS: ChatGPT came to the same translation :slight_smile:

Most Liked

paulanthonywilson

paulanthonywilson

I think the problem is the strings. Remember the thing where strings are lists of characters in Erlang?

Try using single quotes in place of double quotes in the translation. eg

{:certfile, 'cert.pem'}
al2o3cr

al2o3cr

Like in the last ChatGPT Erlang-to-Elixir translation, the robot doesn’t do a good job spotting the difference between binaries and charlists. :man_shrugging:

Dialyzer is complaining about the binaries in your options, since listen_opts is defined as:

alpn() is defined as string() - so charlists

file:filename() is defined in stdlib as… string() - so charlists

al2o3cr

al2o3cr

There are many places inside of :quicer.listen’s NIF that can return {:error, :badarg}:

Passing an empty list of options results in {:error, :badarg} because of checks for required parameters, for instance the ones on line 302-316.

One other thing that jumps out: the change to add support for certfile and keyfile (instead of just cert and key) isn’t in a released package, it was just added a bit more than a month ago. If you’ve installed quicer from Hex, try using cert and key (the old names) instead.

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement