SisMaker 4 роки тому
джерело
коміт
47ca3bc368
6 змінених файлів з 226 додано та 83 видалено
  1. +5
    -17
      c_src/ePort/ePort.cpp
  2. +8
    -2
      rebar.config
  3. +0
    -64
      src/ePort.erl
  4. +22
    -0
      src/ePortMsg.erl
  5. +189
    -0
      src/ePortSrv.erl
  6. +2
    -0
      src/ePort_sup.erl

+ 5
- 17
c_src/ePort/ePort.cpp Переглянути файл

@ -8,16 +8,15 @@
#include <time.h>
#include <stdarg.h>
#include <syslog.h>
# include <stdio.h>
#include <stdlib.h>
#define BUFF_LEN 128
std::map<u_int64_t, int> reqCache;
std::map<int, int> 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);

+ 8
- 2
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"}]}.

+ 0
- 64
src/ePort.erl Переглянути файл

@ -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.

+ 22
- 0
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)}}.

+ 189
- 0
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}.

+ 2
- 0
src/ePort_sup.erl Переглянути файл

@ -1,3 +1,5 @@
-module(ePort_sup).
-behaviour(supervisor).
-export([start_link/0]).

Завантаження…
Відмінити
Зберегти