Erlang gen_tcp补充(2)

这次讲的是gen_tcp:send 的阻塞 与 直接调用 erlang:port_command的非阻塞。
————————————————————————————————————————————————————————
gen_tcp.erl  源码在 kernel
gen_tcp.erl
send(S, Packet) when is_port(S) ->
    case inet_db:lookup_socket(S) of
{ok, Mod} ->
    Mod:send(S, Packet);  %%此处Mod模块是inet_tcp.erl 
Error ->
    Error
    end.

inet_tcp.erl
%%
%% Send data on a socket
%% 调用prim_inet模块
%%
send(Socket, Packet, Opts) -> prim_inet:send(Socket, Packet, Opts). 
send(Socket, Packet) -> prim_inet:send(Socket, Packet, []).



prim_inet.erl
send(S, Data, OptList) when is_port(S), is_list(OptList) ->
    ?DBG_FORMAT("prim_inet:send(~p, ~p)~n", [S,Data]),
    try erlang:port_command(S, Data, OptList) of  %%提交数据
	false -> % Port busy and nosuspend option passed
	    ?DBG_FORMAT("prim_inet:send() -> {error,busy}~n", []),
	    {error,busy};
	true ->
	    receive     %% <strong>gen_tcp:send默认是在send后会等driver给他反馈,这个等待是阻塞的</strong>
		{inet_reply,S,Status} ->
		    ?DBG_FORMAT("prim_inet:send() -> ~p~n", [Status]),
		    Status
	    end
    catch
	error:_Error ->
	    ?DBG_FORMAT("prim_inet:send() -> {error,einval}~n", []),
	     {error,einval}
    end.

erlang:port_command 建议用此函数直接提交数据
其他文章: http://blog.yufeng.info/archives/1581

你可能感兴趣的:(erlang)