Erlang Robot
3668 단어 erlang
%% @Author: Lee
%% Created: 2014-09-13
%% Description: TODO: Add description to robot.
-module(robot).
-behaviour(gen_fsm).
-export([init/1, handle_event/3, handle_sync_event/4, handle_info/3, terminate/3, code_change/4]).
%% Include Files
-include("../include/def_protocol.hrl").
-include("../include/def_common.hrl").
%% Exports
-export([test_robot/0,start/4,loging/2,sendtoserver/2]).
%% Record
-record(config, {account,ip,port,serverid}).
-record(state,{socket,config}).
%% Macros
-define(TCP_OPTS, [
binary,
{packet, 2},
{reuseaddr, true},
{nodelay, false},
{delay_send, true},
{active, false},
{exit_on_close, false}
]).
%%
start(IP,Port,Num,ServerID)->
InitList = lists:seq(1, Num),
lists:foreach(fun(Index)-> start_test(Index,IP,Port,Num,ServerID) end, InitList).
start_test(Index,IP,Port,Num,ServerID)->
Rebot_Config = #config{account = Index,ip = IP,port = Port,serverid=ServerID},
start_robot(list_to_atom(integer_to_list(Index)),Rebot_Config).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
start_robot(ID,Config)->
gen_fsm:start_link({local,ID},?MODULE, [Config], []).
%% init/1
%% ====================================================================
init([Config]) ->
process_flag(trap_exit,true),
{A,B,C} = now(),
random:seed(A,B,C),
gen_fsm:send_event(self(), {login}),
{ok,loging,#state{config=Config}}.
%% state_name/2
%% ====================================================================
loging({login},State)->
{A,B,C} = erlang:now(),
#state{config=Config} = State,
#config{account=Index,ip=IP,port=Port,serverid=ServerID} = Config,
random:seed(A,B+Index,C),
Account = "1000" ++ integer_to_list(Index),
put(account,Account),
put(serverid,ServerID),
{ok,Socket} = gen_tcp:connect(IP, Port, ?TCP_OPTS),
start_login(Account,ServerID),
{next_state, loging, State#state{socket=Socket}};
loging(_,StateData)->
{next_state, loging, StateData}.
%% handle_event/3
%% ====================================================================
handle_event(Event, StateName, StateData) ->
{next_state, StateName, StateData}.
%% handle_sync_event/4
%% ====================================================================
handle_sync_event(Event, From, StateName, StateData) ->
Reply = ok,
{reply, Reply, StateName, StateData}.
%% handle_info/3
%% ====================================================================
handle_info({sendtoserver,Binary},StateName,#state{socket=Socket}=State)->
gen_tcp:send(Socket, Binary),
{next_state, StateName, State};
handle_info(Info, StateName, StateData) ->
{next_state, StateName, StateData}.
%% terminate/3
%% ====================================================================
terminate(Reason, StateName, StatData) ->
ok.
%% code_change/4
%% ====================================================================
code_change(OldVsn, StateName, StateData, Extra) ->
{ok, StateName, StateData}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Interface %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sendtoserver(Pid,Binary)->
Pid ! {sendtoserver,Binary}.
start_login(Account,ServerID)->
#user_login_c2s{msg=Msg} = #user_login_c2s{},
_Account = list_to_binary(Account),
_AccountLen = erlang:byte_size(_Account),
Binary = <<Msg:?USHORT,_AccountLen:?USHORT,_Account/binary,ServerID:?USHORT>>,
sendtoserver(self(),Binary).
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Instalando o Elixir e o Erlang com o gerenciador de versões asdfEsse tutorial usa o gerenciador de versões asdf, mas fique a vontade para utilizar outros se preferir. Infelizmente o as...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.