Erl_Interface: msg.from pid is not valid to reply to the sender

I’m experimenting with erl_interface to create a (server, for now) C Node and do some experiments.

I was able to setup the C Node correctly, accept the connection from an Elixir node and receive a message but I found a strange behaviour.

When I call ei_xreceive_msg, I expect the msg struct to be populated with a from pid I can use to reply to the sender, but if I try to use that as argument in ei_send, the call fails. If, however, I explicitly send a tuple with the sender pid as first element, i.e. on the Elixir side I basically do

:global.whereis_name(:my_c_node_process)
|> send({self(), :ping})

that pid actually works to reply back to the sender.

What am I missing? Is the msg.from field used for something different from what I am thinking?

1 Like

ei_xreceive_msg provides no information about the sender. If you wanted to receive the sender’s pid, that pid would need to be a part of the term sent to the C node, (e.g. similarly how you do it in your send/2 example). It would be your responsibility to extract the pid from the term received by ei_xreceive_msg. The from and to fields of the struct are used for a different purpose - when msgtype is ERL_SEND, ERL_REG_SEND, ERL_LINK, ERL_UNLINK, ERL_EXIT, they contain information about the lined/unlinked/registered pid. See the docs.

2 Likes

@saleyn thanks for the help, that’s clearer. One thing I don’t understand though (and I don’t see it explained in the docs) is the difference between ERL_SEND and ERL_REG_SEND message type - how can I trigger a registered send from the Elixir/Erlang side?
If I understand correctly in that case then the msg.from field would contain the pid of the sender.

This is just curiosity at this point, since I get that in the general case I just have to pass the pid of the caller to reply to it.

1 Like