diff --git a/c_src/ePort/ePort.cpp b/c_src/ePort/ePort.cpp index 5a0c850..2208745 100644 --- a/c_src/ePort/ePort.cpp +++ b/c_src/ePort/ePort.cpp @@ -8,16 +8,15 @@ #include #include -#include # include #include #define BUFF_LEN 128 -std::map reqCache; +std::map reqCache; bool gIsBigEndian; char gCmdBuff[BUFF_LEN] = {}; int gIndex = 0; -u_int64_t gReqId = 0; +uint64_t gReqId = 0; void LOG(const char* ms, ... ) { char wzLog[100] = {0}; @@ -35,12 +34,9 @@ void LOG(const char* ms, ... ) { FILE *file = fopen("debug.log", "a+"); fwrite(buffer, 1, strlen(buffer), file); fclose(file); - -// syslog(LOG_INFO,wzLog); return; } - int checkEndian(void) { union { uint16_t i; @@ -51,9 +47,7 @@ int checkEndian(void) { } uint16_t swapByteOrder(uint16_t ui) { - LOG("IMY***********************swapByteOrder111 %d", ui); ui = ((ui & 0xff00) >> 8) | ((ui & 0x00ff) << 8); - LOG("IMY***********************swapByteOrder222 %d", ui); return ui; } @@ -77,7 +71,6 @@ std::ostream &writeLen(std::ostream &s, uint16_t len) { return s; } - int main(void) { gIsBigEndian = checkEndian(); @@ -124,14 +117,9 @@ int main(void) { memset(gCmdBuff, 0, BUFF_LEN); ei_encode_version(gCmdBuff, &gIndex); ei_encode_tuple_header(gCmdBuff, &gIndex, 3); - ei_encode_char(gCmdBuff, &gIndex, cmdId); - ei_encode_ulong(gCmdBuff, &gIndex, sdkCmdId); - ei_encode_ulong(gCmdBuff, &gIndex, sdkCmd); - //ei_encode_tuple_header(gCmdBuff, &gIndex, 2); - //ei_encode_ulong(gCmdBuff, &gIndex, gReqId); - //ei_encode_ulong(gCmdBuff, &gIndex, 12369); - //ei_encode_string(gCmdBuff, &gIndex, "127.0.0.1"); - + ei_encode_ulong(gCmdBuff, &gIndex, gReqId); + ei_encode_ulong(gCmdBuff, &gIndex, 12369); + ei_encode_string(gCmdBuff, &gIndex, "127.0.0.1"); LOG("IMY************222222112222* %d", gIndex); writeLen(std::cout, uint16_t(gIndex)); std::cout.write(gCmdBuff, gIndex); diff --git a/rebar.config b/rebar.config index c21c2b2..5f7960e 100644 --- a/rebar.config +++ b/rebar.config @@ -2,6 +2,12 @@ {deps, []}. {shell, [ - % {config, "config/sys.config"}, - {apps, [ePort]} + % {config, "config/sys.config"}, + {apps, [ePort]} ]}. + +{pre_hooks, + [{"", compile, "escript c_src/erlNpc compile"}]}. + +{post_hooks, + [{"", clean, "escript c_src/erlNpc clean"}]}. diff --git a/src/ePort.erl b/src/ePort.erl deleted file mode 100644 index 075ce86..0000000 --- a/src/ePort.erl +++ /dev/null @@ -1,64 +0,0 @@ --module(ePort). - --behavior(gen_server). - - - --compile([export_all]). - --define(PORT_NAME, "./priv/tporti"). - -start_link() -> - gen_server:start_link(). - - -control(Port, Data) -> - Port ! {self(), {command, Data}}, - receive - {Port, {data, AnswerData}} -> - io:format("IMY******************111 ~p", [AnswerData]), - AnswerData; - {Port, {exit_status, _Status}} -> - io:format("IMY******************2222 ~p", [_Status]), - erlang:error(port_exit) - after 1000 -> - io:format("IMY******************333 ~p", [no_receive]), - no_receive - end. - -open() -> - Opts = [{packet, 2}, binary, exit_status, use_stdio], - erlang:open_port({spawn_executable, ?PORT_NAME}, Opts). - - -tt() -> - case get(ppp) of - undefined -> - P = open(), - put(ppp, P); - _ -> - ignore - end, - - control(get(ppp), term_to_binary({$g, 12432, 4324})). - -tt(CmdId) -> - case get(ppp) of - undefined -> - P = open(), - put(ppp, P); - _ -> - ignore - end, - case CmdId of - $g -> - control(get(ppp), term_to_binary({CmdId, 12432, 4324})); - $u -> - control(get(ppp), term_to_binary({CmdId, 12432, 4324})); - $t -> - ignore; - _ -> - control(get(ppp), term_to_binary({CmdId, 12432, 4324})) - end. - - diff --git a/src/ePortMsg.erl b/src/ePortMsg.erl new file mode 100644 index 0000000..8926d8b --- /dev/null +++ b/src/ePortMsg.erl @@ -0,0 +1,22 @@ +-module(ePortMsg). + +-export([ + port_call/2, + port_cast/2 +]). + +port_call(Port, Msg) -> + Port ! {self(), {command, term_to_binary(Msg)}}, + receive + {Port, {data, DataBin}} -> + {ok, binary_to_term(DataBin)}; + {Port, closed} -> + {error, closed}; + {'EXIT', Port, Reason} -> + {'EXIT', Reason} + after 4000 -> + timeout + end. + +port_cast(Port, Msg) -> + Port ! {self(), {command, term_to_binary(Msg)}}. \ No newline at end of file diff --git a/src/ePortSrv.erl b/src/ePortSrv.erl new file mode 100644 index 0000000..6b0161a --- /dev/null +++ b/src/ePortSrv.erl @@ -0,0 +1,189 @@ +-module(ePortSrv). + +-behavior(gen_server). + +-define(PORT_NAME, "./priv/ePort"). +-define(ERR(Args), io:format("IMY***************** ~p~n", [Args])). +-define(CATCH(Expression), ( + try Expression + catch + Error:Reason -> + ?ERR([?MODULE, ?LINE, {"Error", Error}, {"Reason", Reason}, {"Stack", erlang:get_stacktrace()}, {"Expression", ??Expression}]), + {'EXIT',{Error, Reason}} + end +)). + +-record(state, {port = undefined}). + +-compile(export_all). + +%%%================================EXPORT================================ +-export([ + start_link/0 +]). + +-export([ + init/1 + , handle_call/3 + , handle_cast/2 + , handle_info/2 + , terminate/2 + , code_change/3 +]). + +open() -> + Opts = [{packet, 2}, binary, exit_status, use_stdio], + erlang:open_port({spawn_executable, ?PORT_NAME}, Opts). + + +tt() -> + case get(ppp) of + undefined -> + P = open(), + put(ppp, P); + _ -> + ignore + end, + + ePortMsg:port_call(get(ppp), term_to_binary({$g, 12432, 4324})). + +tt(CmdId) -> + case get(ppp) of + undefined -> + P = open(), + put(ppp, P); + _ -> + ignore + end, + case CmdId of + $g -> + ePortMsg:port_call(get(ppp), term_to_binary({CmdId, 12432, 4324})); + $u -> + ePortMsg:port_call(get(ppp), term_to_binary({CmdId, 12432, 4324})); + $t -> + ignore; + _ -> + ePortMsg:port_call(get(ppp), term_to_binary({CmdId, 12432, 4324})) + end. + +%%----------------------------------------------------------------- +%% start_link/0 +%% @doc 启动gen_server进程 +%% ``` +%% +%% ''' +%% @end +%% ----------------------------------------------------------------- +-spec start_link() ->gen:start_ret(). +%% ----------------------------------------------------------------- +start_link() -> + gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). + +%%%================================behaviour functions================================ +%%----------------------------------------------------------------- +%% init/0 +%% @doc 初始化 +%% ``` +%% +%% ''' +%% @end +%%-------------------------------------------------------------------- +-spec init(Args :: term()) -> + {ok, State :: term()} | {ok, State :: term(), timeout() | hibernate} | + {stop, Reason :: term()} | ignore. +%%-------------------------------------------------------------------- +init([]) -> + process_flag(trap_exit,true), + Port = open(), + {ok, #state{port = Port}}. + +%%----------------------------------------------------------------- +%% handle_call/3 +%% @doc +%% ``` +%% +%% ''' +%% @end +%%-------------------------------------------------------------------- +-spec handle_call(Request :: term(), From :: {pid(), Tag :: term()}, + State :: term()) -> + {reply, Reply :: term(), NewState :: term()} | + {reply, Reply :: term(), NewState :: term(), timeout() | hibernate} | + {noreply, NewState :: term()} | + {noreply, NewState :: term(), timeout() | hibernate} | + {stop, Reason :: term(), Reply :: term(), NewState :: term()} | + {stop, Reason :: term(), NewState :: term()}. +%%-------------------------------------------------------------------- +handle_call({func, F, Args}, _From, State) -> + Result = ?CATCH(util_kit:catch_apply(F,Args)), + {reply, Result, State}; +handle_call(Request, _From, State) -> + ?ERR(["handle_call function clause", {"request", Request}]), + Reply = ok, + {reply, Reply, State}. + +%%----------------------------------------------------------------- +%% handle_cast/2 +%% @doc +%% ``` +%% +%% ''' +%% @end +%%-------------------------------------------------------------------- +-spec handle_cast(Request :: term(), State :: term()) -> + {noreply, NewState :: term()} | + {noreply, NewState :: term(), timeout() | hibernate} | + {stop, Reason :: term(), NewState :: term()}. +%%-------------------------------------------------------------------- +handle_cast(Msg, State) -> + ?ERR(["handle_cast function clause", {"request", Msg}]), + {noreply, State}. + +%%----------------------------------------------------------------- +%% handle_info/2 +%% @doc +%% ``` +%% +%% ''' +%% @end +%%-------------------------------------------------------------------- +-spec handle_info(Info :: timeout | term(), State :: term()) -> + {noreply, NewState :: term()} | + {noreply, NewState :: term(), timeout() | hibernate} | + {stop, Reason :: term(), NewState :: term()}. +%%-------------------------------------------------------------------- +handle_info(Info, State) -> + ?ERR(["handle_info function clause", {"request", Info}]), + {noreply, State}. + +%%----------------------------------------------------------------- +%% terminate/2 +%% @doc +%% ``` +%% +%% ''' +%% @end +%%-------------------------------------------------------------------- +-spec terminate(Reason :: (normal | shutdown | {shutdown, term()} |term()), State :: term()) -> + term(). +%%-------------------------------------------------------------------- +terminate(Reason, #state{port = Port} = State) -> + ?ERR(["server_terminate_for", {"Reason", Reason}, {"State", State}, {"Dictionary", element(2,process_info(self(),dictionary))}]), + port_close(Port), + ok. + +%%----------------------------------------------------------------- +%% code_change/3 +%% @doc +%% ``` +%% +%% ''' +%% @end +%%-------------------------------------------------------------------- +-spec code_change(OldVsn :: (term() | {down, term()}), State :: term(), Extra :: term()) -> + {ok, NewState :: term()} | {error, Reason :: term()}. +%%-------------------------------------------------------------------- +code_change(_OldVsn, State, _Extra) -> + {ok, State}. + + diff --git a/src/ePort_sup.erl b/src/ePort_sup.erl index a128819..50bd22c 100644 --- a/src/ePort_sup.erl +++ b/src/ePort_sup.erl @@ -1,3 +1,5 @@ +-module(ePort_sup). + -behaviour(supervisor). -export([start_link/0]).