Ripster
I am writing an application that reads incoming packets and I’d like to decode them and route them to the appropriate handler.
The header of each packet is what defines its type.
So for example a user login packet may look like this:
<<
header::integer-little-8,
ulen::unsigned-little-integer-32,
user::binary-size(ulen),
plen::unsigned-little-integer-32,
pass::binary-size(plen)
>>
I’d like to be able to do something like this in my receive function:
<<
header::integer-little-8,
data::bytes
>> = received
{:ok, packet} = Packet.parse(header, data)
:ok = packet.run()
Where Packet.parse/2 is defined at compile time.
defmodule Packet do
defmacro __using__(header) do
# track __CALLER__ and header?
@behaviour Packet
end
@callback parse(BitString) :: {:ok, Map}
@callback def run() :: :ok | {:error, reason}
# Generate these at compile time
def parse(1, data), do: Packets.Login.parse(data)
def parse(2, data), do: Packets.Chat.parse(data)
end
defmodule Packets.Login do
use Packet, header: 1
defstruct :username, :password
def parse(packet_data), do: # decode packet
def run, do: # run code to handle packet
end
Is there a way to do this using macros or a better way that wouldn’t require macros?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Marked As Solved
OvermindDL1
That says they are either on the wrong file or not put in early in the compilation process.
Honestly I’m thinking this is just a bit odd of a design overall regardless, I would probably just hardcode those all in as it’s not like they are going to be changing often anyway and adding new ones is easy. ^.^;
If you really want to go the more complex route (which be sure that you do), I’d really say take a look at my afore-mentioned protocol_ex, your solution might look similar to (typed in-post so likely has syntax errors):
Then just write the implementations like:
Then maybe another one somewhere else:
Etc…
And of course you can add a run function and all sorts as well too. Whatever. (If you need new features in protocol_ex, just ask). It has a lot of other abilities and optimizations and such you can do as well, but as-is it already does what you posted that you want.
Don’t forget to add the compile for protocol_ex to the
mix.exsfile as per the documentation too (or you can always handle it manually too if you want).(EDIT: Updated examples to be actual code rather than just placeholder code)
Also Liked
OvermindDL1
I really should release my library of mine that does that at compile-time… >.>
Hmm, actually you could use my ProtocolEx (it’s like the Protocol Library built into elixir, except it is based on matchers and guards instead of types, and has the ability to be faster too by inlining and more) library if you don’t mind putting your
parsefunctions in implementation modules (you can of course put multiples of the parse functions in a single implementation if you want, so you can group like-functionality.kip
I don’t think you need a macro for this. Something like this should do it. Of course you would probably
encapsulate the header->function mapping differently in real use rather than a module attribute.
mischov
My current favorite library to read for learning about neat code generation is NimbleParsec. It might give you some ideas.
Edit: The Elixir Forum thread for the library is also good- searching it for “quoted expressions” might give you a few more ideas.
Last Post!
OvermindDL1
We try, don’t hesitate to ask!