tags:

views:

135

answers:

2

in an Erlang process, how could i receive from an ssl socket, and at the same time receive from another erlang process with the receive primitive?

the idea is to forward what comes from the socket to another process; and backwards.

my only option so far is to use some time receiving from each end, then switch. that, of course, will delay the processing of the messages received on one interface, while receiving from the other one. do you see any other way to do this? if only Erlang would let me use one process to receive from the socket, and another one to send to the socket...

A: 

You'll anyhow receive the message from (e.g.) an HTTP_Client in a process through the receive statement. You'll be able to delineate the "conversations" easily:

{http, {RequestId, Result}} for the HTTP_Client "conversations"

See here for more details.

jldupont
+2  A: 

Not sure I understand your question; anyway, you can have multiple "clauses" in a receive statement, so it becomes "unblocked" when receiving something from either side:

loop() ->
  receive
    {ssl, Msg} ->  % incoming msg from SSL, send it to process
      Proc ! Msg,
      loop();
    {proc, Msg} -> % incoming msg from process, send it to SSL
      SSL ! Msg,
      loop()
  end.

The important thing is that you need to format your messages in a way that you can differentiate between SSL and process messages with pattern matching.

Zed
that is just what i would like to do. the problem with that is how to receive data from the ssl socket, as it was an erlang message. right now i'm receiving from the socket using ssl:recv, not with the receive primitive. is it possible to receive from a socket with the receive primitive?
fullWindsor
I thought that was the default, when the socket is active.
Zed
you were right, Zed, thanks. only 2 details:i didn't know i had to set the socket to active, with ssl:setopts(Socket, [{active, true}]).also, the pattern is more like {ssl, Ssl_info, Msg}.thanks again.
fullWindsor