[erlang-questions] how does an erlang Socket/Port look from inside?

Dror Mein drormein@REDACTED
Wed Mar 26 18:20:35 CET 2014


It works! thank you!



On Wednesday, March 26, 2014 7:00 PM, Fred Hebert <mononcqc@REDACTED> wrote:
 
Hi, I have a solution.

As part of recon (which allows to use strings and whatnot as ports) to
diagnose issues, I have developed a term_to_port function that uses the
serialized binary form of a port and uses binary_to_term/1 to make it
into a real Erlang term:

https://github.com/ferd/recon/blob/master/src/recon_lib.erl#L157-177

    %% @doc Transforms a given term to a port
    -spec term_to_port(recon:port_term()) -> port().
    term_to_port(Port) when is_port(Port) -> Port;
    term_to_port(Name) when is_atom(Name) -> whereis(Name);
    term_to_port("#Port<0."++Id) ->
        N = list_to_integer(lists:sublist(Id, length(Id)-1)), % drop trailing '>'
        term_to_port(N);
    term_to_port(N) when is_integer(N) ->
        %% We rebuild the term from the int received:
        %% http://www.erlang.org/doc/apps/erts/erl_ext_dist.html#id86892
        Name = iolist_to_binary(atom_to_list(node())),
        NameLen = iolist_size(Name),
        Vsn = binary:last(term_to_binary(self())),
        Bin = <<131, % term encoding value
                102, % port tag
                100, % atom ext tag, used for node name
                NameLen:2/unit:8,
                Name:NameLen/binary,
                N:4/unit:8, % actual counter value
                Vsn:8>>, % version
        binary_to_term(Bin).

I'm guessing this could be useful in your case here, if gen_tcp:listen
doesn't work (and it should totally work)

Regards,
Fred.


On 03/26, Dror Mein wrote:
> Hey all,
> 
> I want to eunit+meck test a simple telnet client which uses the guard is_port(Port).
> how can I pass something in meck:expect/4 that will pass the guard?
> 
> is it a record/reference that I can make up?
> can I use hd(erlang:ports()) -> #Port<0.0> without blowing up?
> Thanks

> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140326/b71b87b3/attachment.htm>


More information about the erlang-questions mailing list