Hello,
Actually I’m making a dynamic TCP protocol for a game with elixir, all the messages are already writed on another language I just need to handle every message in elixir and send back an answer to this message, actually my “model” to handle recieve its like this:
Get data bytes from the socket |> Read the message Id, the message length and main data |> contruct the message by reflection
(So here I decided to use a existing solution created by hanshine (It took me a long time to understand the code):
- Generate the messages at compile time based on a file, like this:
https://github.com/McEx/McProtocol/blob/master/lib/packet.ex - And get contruct it with the message id, like this:
https://github.com/McEx/McProtocol/blob/master/lib/packet/in.ex
)
But I also found a simplistic solution that is use Kernel.apply/3
and String.to_existing_atom/1
, I’m not sure what of the two approach are better for this kind of problem (if you know a better solution feel free to tell me please).
After this “reflection” I need to call another reflection for handle the logic of this message and return the messages to send, the problem is that are so many messages sended by the client, to organize it I need many files to keep the project clean, so the project can looks like
handlers(folder)
->login(folder)
|> login_handler.ex
->game(folder)
|>fight(folder)
|>fight_handler.ex
|>inventory(folder)
|>entities(folder)
.....
How I can handle this kind of reflection? For example call the by an id like this (I dont know if is a valid elixir code this is a code more like c#
) (this is like call de definition by attribute, but I dont find something like this on elixir)
# handlers/connection/ping.ex
@messageid 1 #I dont know if its possible but its a code more like c#, so just need the message id and call with elixir in some way no matter the directory/module name(dunno if its possible)
def handle_ping_message(state, message) do
{:send, PongMessage}
end
or like a pattern matching like this
# handlers/connection/ping.ex
def handle_ping_message(message = %{PingMessage}) do # sended as argument and pattern match over all proyect
{:send, PongMessage}
end
Again, I don’t know if this is possible (or correct) in elixir, just I illustrate how it looks in my mind, I want to know how implement something like this on elixir
Thats my problem! I hope someone can help me to solve this and a way to improve the actual “solution” that I have
Greetings!