Erlang gen_tcp缺少数据包?

时间:2013-05-11 15:04:13

标签: tcp erlang imap pop3

我正在为Erlang开发一个邮件客户端适配器。我尝试执行fetch命令时遇到问题,Erlang无法获取正文内容。

当我尝试通过netcat使用命令时,这是从我的终端输出的:

4 FETCH 2 BODY[2]
* 2 FETCH (BODY[2] {1135}

                <div>
                    test content
                </div>
            )
4 OK FETCH completed.

gen_tcp服务器能够接收的唯一输出是这个二进制文件:

<<"* 2 FETCH (BODY[2] {1135}\r\n">>

源代码在这里:

-module(mailconnector).

-behaviour(gen_server).

-export([start_link/2, stop/0]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

start_link(Host, imap) ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [Host, 143], []).

stop() ->
    gen_server:call(?MODULE, stop).

init([Host, Port]) ->
    {ok, Sock} = gen_tcp:connect(Host, Port, [binary, {packet, 0}, {active, true}]),
    {ok, {Sock, 0}}.

handle_call(stop, _From, State) ->
    {stop, normal, ok, State};

handle_call({login, Username, Password}, _From, State) ->
    {NewState, Output} = action(State, string:join(["LOGIN", Username, Password], " ")),
    case Output of
        {ok, Response, Data} -> Result = {Response, Data};
        _ -> Result = false
    end,
    {reply, Result, NewState};

handle_call(list, _From, State) ->
    {NewState, Resp} = action(State, "LIST \"\" \"*\""),
    {reply, Resp, NewState};

handle_call({select, MailBox}, _From, State) ->
    {NewState, Output} = action(State, string:join(["SELECT", MailBox], " ")),
    case Output of
        {ok, Response, Data} -> Result = {Response, Data};
        _ -> Result = false
    end,
    {reply, Result, NewState};

handle_call({fetch, Num}, _From, State) ->
    {NewState, Output} = action(State, string:join(["FETCH", Num, "BODY[1]"], " ")),
    case Output of
        {ok, Response, Data} -> Result = {Response, Data};
        _ -> Result = false
    end,
    {reply, Result, NewState};

handle_call(_Command, _From, _State) ->
    {reply, not_valid, _State}.

handle_cast(_Command, State) ->
    {noreply, State}.

handle_info(_Info, State) ->
    {noreply, State}.

terminate(_Reason, _State) ->
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

action(_State = {Socket, Counter}, Command) ->
    NewCount = Counter+1,
    CounterAsList = lists:flatten(io_lib:format("~p ", [NewCount])),
    Message = list_to_binary(lists:concat([CounterAsList, Command, "\r\n"])),
    io:format("~p~n", [Message]),
    gen_tcp:send(Socket, Message),
    {{Socket, NewCount}, listener(Socket, NewCount)}.

listener(_Sock, Count) ->
    receive
    {_, _, Reply} ->
        io:format("RECEIVED: ~p~n", [Reply]),
        Messages = string:tokens(binary_to_list(Reply), "\r\n"),
        io:format("~p~n", [Messages]),
        find_message(Messages, Count)
    after 5000 ->
        timeout
    end.

process_message(Message, Count) ->
    StringCount = lists:flatten(io_lib:format("~p", [Count])),
    case [MCount|PureMessage] = string:tokens(Message, " ") of
        _M when StringCount == MCount ->
            {ok, string:join(PureMessage, " ")};
        _ -> [_Command|Output] = PureMessage, {data, string:join(Output, " ")}
    end.

find_message(Messages, Count) ->
    find_message(Messages, Count, []).

find_message([], _, _) ->
 false;    

find_message([H|T], Count, Data) ->
    case process_message(H, Count) of
        {ok, Message} -> {ok, Message, lists:reverse(Data)};
        {data, Output} -> find_message(T, Count, [Output|Data])
    end.

非常感谢你的帮助。

2 个答案:

答案 0 :(得分:1)

以下只是一个评论,而不是我认为上面提供的问题的答案。

由于您使用的是标准行为之一(gen_server),因此可以假设您打算编写符合OTP的应用程序。如果是这样,除非您准备好处理所有可能的系统消息以及应用程序,否则不应直接使用接收表达式。对于gen_server或gen_fsm,非系统消息由handle_info / 2回调函数处理。您可以使用State变量来保存您正在处理的命令(例如登录)的指示符,并为每个命令分别设置一个子句:

 handle_info({tcp,Socket,Reply}, #state{pending = login} = State) ->
 ...;
 handle_info({tcp,Socket,Reply}, #state{pending = list} = State) ->
 ...;

...然而,这将成为一个穷人的有限状态机,所以你最好把它移植到gen_fsm行为回调模块,在那里你有一个单独的状态(即wait_for_login)。然后你可以使用handle_info / 3:

 handle_info({tcp,Socket,Reply}, wait_for_login, State) ->
 ...;
 handle_info({tcp,Socket,Reply}, wait_for_list, State) ->
 ...;

答案 1 :(得分:0)

如果您处于active模式,最佳方法是使用模式handle_info{tcp, Socket, Msg}(或您的自定义接收函数)中接收流,并将其存储在缓冲区直到满足特定模式匹配或具有特定长度,然后根据需要刷新缓冲区。

正如@rvirding所说,你不能确定你的所有消息都将在一个数据包中被接收,所以你必须处理可能的多个数据包。否则,您必须使用passive模式和函数gen_tcp:recv/2,但请记住此函数正在阻止。

我建议你阅读:http://learnyousomeerlang.com/buckets-of-sockets