├── Docs └── README.md ├── .travis.yml ├── config ├── vm.args └── sys.config ├── apps └── game_server │ ├── src │ ├── game_util.erl │ ├── game_massage.csv │ ├── game_web │ │ ├── web_data.erl │ │ ├── game_ws_util.erl │ │ ├── game_web.erl │ │ ├── game_ws_routing.erl │ │ ├── game_ws_login.erl │ │ ├── game_web_sup.erl │ │ └── game_ws_handler.erl │ ├── game_cache │ │ ├── ets_data.erl │ │ ├── game_ets.erl │ │ ├── game_ets_sup.erl │ │ └── game_ets_svr.erl │ ├── game_proto_util.erl │ ├── game_server.app.src │ ├── game_handler │ │ ├── maininterface_handler.erl │ │ └── common_handler.erl │ ├── game_db │ │ ├── mnesia_data.erl │ │ ├── game_mn.erl │ │ ├── game_mnesia_sup.erl │ │ └── game_mnesia.erl │ ├── game_log │ │ ├── game_debug.erl │ │ └── game_log.erl │ ├── game_role │ │ ├── game_role_sup.erl │ │ └── game_role.erl │ ├── game_server_app.erl │ ├── game_massage.erl │ ├── game_server_sup.erl │ ├── game_simulation.erl │ └── game_protobuf │ │ ├── maininterface_pb.erl │ │ └── common_pb.erl │ ├── include │ ├── game_sup.hrl │ ├── game_user.hrl │ ├── maininterface_pb.hrl │ └── common_pb.hrl │ ├── priv │ └── protobuf │ │ ├── maininterface.proto │ │ └── common.proto │ └── rebar.config ├── .gitignore ├── test └── prop_hello.erl ├── README.md ├── rebar.lock ├── rebar.config └── LICENSE /Docs/README.md: -------------------------------------------------------------------------------- 1 | # Doc -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: erlang 2 | otp_release: 3 | - 21.1 -------------------------------------------------------------------------------- /config/vm.args: -------------------------------------------------------------------------------- 1 | -name 'game_server@127.0.0.1' 2 | 3 | -setcookie game_server_cookie 4 | 5 | -boot start_sasl 6 | 7 | %% using multi pollthreads and multi pollsets 8 | +IOt 8 +IOp 8 9 | 10 | +K true 11 | +A30 12 | -------------------------------------------------------------------------------- /apps/game_server/src/game_util.erl: -------------------------------------------------------------------------------- 1 | -module(game_util). 2 | 3 | -author("Naupio Z.Y. Huang"). 4 | 5 | -export([ 6 | unixtime/0 7 | ]). 8 | 9 | unixtime() -> 10 | {GSec, Sec, _MiSec} = erlang:timestamp(), 11 | round(GSec * math:pow(10,6) + Sec). -------------------------------------------------------------------------------- /apps/game_server/src/game_massage.csv: -------------------------------------------------------------------------------- 1 | 11,loginReq,common 2 | 12,loginResp,common 3 | 4 | 101,heartbeatReq,common 5 | 102,heartbeatResp,common 6 | 7 | 1001,helloReq,common 8 | 1002,worldResp,common 9 | 10 | 2001,userInfoReq,maininterface 11 | 2002,userInfoResp,maininterface 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .rebar3 2 | _* 3 | .eunit 4 | *.o 5 | *.beam 6 | *.plt 7 | *.swp 8 | *.swo 9 | .erlang.cookie 10 | ebin 11 | log 12 | erl_crash.dump 13 | .rebar 14 | logs 15 | _build 16 | .idea 17 | *.iml 18 | rebar3.crashdump 19 | mf_logs/* 20 | !mf_logs/README.md 21 | Mnesia.* 22 | mnesia_db_data 23 | .DS_Store -------------------------------------------------------------------------------- /apps/game_server/src/game_web/web_data.erl: -------------------------------------------------------------------------------- 1 | -module(web_data). 2 | 3 | -author("Nuapio Z.Y. Huang"). 4 | 5 | -export([ 6 | get_websocket_port/0 7 | ]). 8 | 9 | get_websocket_port() -> 10 | % 19910. 11 | Port = application:get_env(game_config, websocket_port, 19910), 12 | Port. -------------------------------------------------------------------------------- /apps/game_server/include/game_sup.hrl: -------------------------------------------------------------------------------- 1 | %% game sup hrl file 2 | 3 | -author("Nuapio Z.Y. Huang"). 4 | 5 | -define(SupChildSpecById(Module), 6 | #{ id => Module, 7 | start => {Module, start_link, []}, 8 | restart => permanent, 9 | shudown => 30000, 10 | type => supervisor, 11 | modules => [Module] 12 | }). -------------------------------------------------------------------------------- /apps/game_server/src/game_cache/ets_data.erl: -------------------------------------------------------------------------------- 1 | -module(ets_data). 2 | 3 | -author("Nuapio Z.Y. Huang"). 4 | 5 | -include("game_user.hrl"). 6 | 7 | -export([ 8 | get_init_ets_list/0 9 | ]). 10 | 11 | get_init_ets_list() -> 12 | [ 13 | {ets_user_online 14 | ,[named_table, ordered_set, public, {keypos, #r_online.user_id}, {write_concurrency,true}, {read_concurrency,true}]} 15 | ]. 16 | 17 | -------------------------------------------------------------------------------- /apps/game_server/src/game_proto_util.erl: -------------------------------------------------------------------------------- 1 | -module(game_proto_util). 2 | 3 | % -include("common_pb.hrl"). 4 | % -include("game_user.hrl"). 5 | % -include("maininterface_pb.hrl"). 6 | 7 | -export([proto_cast_change/3]). 8 | 9 | proto_cast_change(AnMap, RecordName, Fields) -> 10 | Values = lists:map(fun(Field) -> maps:get(Field, AnMap, undefined) end, Fields), 11 | list_to_tuple([RecordName | Values]) 12 | . -------------------------------------------------------------------------------- /apps/game_server/priv/protobuf/maininterface.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package maininterface.proto; 4 | 5 | message userInfoReq { 6 | uint64 user_id = 1; 7 | } 8 | message userInfoResp { 9 | uint64 user_id = 1; 10 | string user_name = 2; 11 | uint32 user_img_id = 3; 12 | uint32 user_rank = 4; 13 | uint64 user_account_experience = 5; 14 | uint64 user_gold = 6; 15 | uint64 user_diamond = 7; 16 | } -------------------------------------------------------------------------------- /apps/game_server/src/game_cache/game_ets.erl: -------------------------------------------------------------------------------- 1 | -module(game_ets). 2 | 3 | -author("Naupio Z.Y. Huang"). 4 | 5 | -export([ 6 | ets_start/0 7 | ]). 8 | 9 | ets_start() -> 10 | lists:foreach(fun({EtsName, Options}) -> 11 | supervisor:start_child(game_ets_sup, [{EtsName, Options}]) 12 | end, ets_data:get_init_ets_list()), 13 | game_debug:debug(info, "~n cache ets is starting succeed!!! ~n"), 14 | ok. -------------------------------------------------------------------------------- /apps/game_server/src/game_server.app.src: -------------------------------------------------------------------------------- 1 | {application, game_server, 2 | [{description, "An game_server application"}, 3 | {vsn, "0.2.0"}, 4 | {registered, []}, 5 | {mod, { game_server_app, []}}, 6 | {applications, 7 | [kernel, 8 | stdlib, 9 | sasl, 10 | lager, 11 | cowboy 12 | ]}, 13 | {env,[]}, 14 | {modules, []}, 15 | 16 | {maintainers, []}, 17 | {licenses, ["Apache 2.0"]}, 18 | {links, []} 19 | ]}. 20 | -------------------------------------------------------------------------------- /test/prop_hello.erl: -------------------------------------------------------------------------------- 1 | -module(prop_hello). 2 | -include_lib("proper/include/proper.hrl"). 3 | 4 | %%%%%%%%%%%%%%%%%% 5 | %%% Properties %%% 6 | %%%%%%%%%%%%%%%%%% 7 | prop_test() -> 8 | ?FORALL(Type, term(), 9 | begin 10 | boolean(Type) 11 | end). 12 | 13 | %%%%%%%%%%%%%%% 14 | %%% Helpers %%% 15 | %%%%%%%%%%%%%%% 16 | boolean(_) -> true. 17 | 18 | %%%%%%%%%%%%%%%%%% 19 | %%% Generators %%% 20 | %%%%%%%%%%%%%%%%%% 21 | mytype() -> term(). 22 | -------------------------------------------------------------------------------- /apps/game_server/src/game_web/game_ws_util.erl: -------------------------------------------------------------------------------- 1 | -module(game_ws_util). 2 | 3 | -author("Naupio Z.Y. Huang"). 4 | 5 | -export([ 6 | ws_send/2 7 | ]). 8 | 9 | ws_send(WsPid, RecordData) -> 10 | RecordName = element(1, RecordData), 11 | Cmd = game_massage:msg_code(RecordName), 12 | Module = game_massage:decoder_for(Cmd), 13 | Bin = Module:encode_msg(RecordData), 14 | BinRecordData = <>, 15 | case Cmd of 16 | 102 -> 17 | notdoing; 18 | _ -> 19 | game_debug:debug(info,"wwwwwww WsPid: ~p, protobuf send: ~p wwwwwww ~n", [WsPid, RecordData]) 20 | end, 21 | WsPid ! {send_binary, BinRecordData}. -------------------------------------------------------------------------------- /apps/game_server/src/game_handler/maininterface_handler.erl: -------------------------------------------------------------------------------- 1 | -module(maininterface_handler). 2 | 3 | -author("Naupio Z.Y. Huang"). 4 | 5 | -include("maininterface_pb.hrl"). 6 | 7 | -export([ 8 | handle/2 9 | ]). 10 | 11 | %% heartbeat for keep user alive. 12 | handle(#userInfoReq{}, #{ws_pid := WsPid} = State) -> 13 | UserInfo = game_proto_util:proto_cast_change(State, userInfoResp, record_info(fields, userInfoResp)), 14 | game_ws_util:ws_send(WsPid, UserInfo), 15 | {noreply, State}; 16 | 17 | handle(Record, State) -> 18 | game_debug:debug(info,"~n~n module *~p* unknow *Record* message: ~p with *State* ~p ~n~n", [?MODULE,Record, State]), 19 | {noreply, State}. 20 | -------------------------------------------------------------------------------- /apps/game_server/src/game_web/game_web.erl: -------------------------------------------------------------------------------- 1 | -module(game_web). 2 | 3 | -author("Naupio Z.Y. Huang"). 4 | 5 | -export([network_start/0]). 6 | 7 | network_start() -> 8 | ok = websocket_start(), 9 | WsPort = web_data:get_websocket_port(), 10 | game_debug:debug(info, "~n network websocket with port-~w is starting succeed!!! ~n",[WsPort]), 11 | ok. 12 | 13 | websocket_start() -> 14 | Dispatch = cowboy_router:compile([ 15 | {'_', [{"/", game_ws_handler, []}]} 16 | ]), 17 | WsPort = web_data:get_websocket_port(), 18 | {ok, _} = cowboy:start_clear(websocket_handler_listener, 19 | [{port, WsPort}], 20 | #{env => #{dispatch => Dispatch}} 21 | ), 22 | ok. -------------------------------------------------------------------------------- /apps/game_server/src/game_db/mnesia_data.erl: -------------------------------------------------------------------------------- 1 | -module(mnesia_data). 2 | 3 | -author("Nuapio Z.Y. Huang"). 4 | 5 | -include("game_user.hrl"). 6 | 7 | -export([ 8 | get_init_table_list/0, 9 | get_all_exit_tables/0 10 | ]). 11 | 12 | get_init_table_list() -> 13 | [ 14 | {r_unique, [ {type, ordered_set}, {disc_copies, [node()]}, {attributes, record_info(fields, r_unique)} ] }, 15 | {r_account, [ {type, ordered_set}, {disc_copies, [node()]}, {attributes, record_info(fields, r_account)} ] }, 16 | {r_user, [ {type, ordered_set}, {disc_copies, [node()]}, {attributes, record_info(fields, r_user)} ] } 17 | ]. 18 | 19 | get_all_exit_tables() -> 20 | mnesia:system_info(tables). -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pigame 2 | [![Build Status](https://www.travis-ci.org/Naupio/pigame.svg?branch=master)](https://www.travis-ci.org/Naupio/pigame) 3 | 4 | Just a game server template for Erlang/OTP. 5 | 6 | 7 | # Telegram Group 8 | [OpenBeam:](https://t.me/joinchat/FO_0mxQWbf90Z-2rjCscfw) 9 | `https://t.me/joinchat/FO_0mxQWbf90Z-2rjCscfw` 10 | 11 | # QQ Group 12 | Group ID: **298923900** 13 | 14 | # LICENSE 15 | - [Apache 2.0](./LICENSE) 16 | - Copyright (c) 2018-2020 [Naupio Z.Y Huang](https://github.com/Naupio) 17 | 18 | # Build 19 | `$ rebar3 compile` 20 | 21 | # Run Shell 22 | `$ rebar3 shell` 23 | 24 | # Release 25 | `$ rebar3 as prod release` 26 | 27 | # Run Release 28 | `_build/prod/rel/game_server/bin/game_server foreground` 29 | -------------------------------------------------------------------------------- /apps/game_server/priv/protobuf/common.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package common.proto; 4 | 5 | enum LoginStatus { 6 | FAILED = 0; 7 | SUCCEEDED = 1; 8 | NOTFOUND = 2; 9 | } 10 | 11 | // 登录协议 12 | message loginReq { 13 | string cookie = 1; 14 | } 15 | 16 | message loginResp { 17 | LoginStatus result = 1; 18 | uint64 user_id = 2; 19 | } 20 | 21 | // *************************** 22 | // * use for logined 23 | // *************************** 24 | 25 | // hello-world test 26 | message helloReq { 27 | string msg = 1 ; 28 | } 29 | message worldResp { 30 | string msg = 1; 31 | } 32 | 33 | // hearbeat protocol,30 secends one time 34 | message heartbeatReq {} 35 | 36 | message heartbeatResp { 37 | uint64 unixtime = 1; 38 | } -------------------------------------------------------------------------------- /apps/game_server/src/game_cache/game_ets_sup.erl: -------------------------------------------------------------------------------- 1 | -module(game_ets_sup). 2 | 3 | -author("Naupio Z.Y. Huang"). 4 | 5 | -behaviour(supervisor). 6 | 7 | -define(SERVER, ?MODULE). 8 | 9 | -export([start_link/0]). 10 | -export([init/1]). 11 | 12 | start_link() -> 13 | supervisor:start_link({local, ?SERVER}, ?MODULE, []). 14 | 15 | init(_Args) -> 16 | SupFlags = #{strategy => simple_one_for_one, intensity => 10, period => 1}, 17 | ChildSpecs = [#{id => game_ets_svr, 18 | start => {game_ets_svr, start_link, []}, 19 | restart => permanent, 20 | shutdown => 30000, 21 | type => worker, 22 | modules => [game_ets_svr]}], 23 | {ok, {SupFlags, ChildSpecs}}. -------------------------------------------------------------------------------- /apps/game_server/src/game_web/game_ws_routing.erl: -------------------------------------------------------------------------------- 1 | -module(game_ws_routing). 2 | 3 | -author("Naupio Z.Y. Huang"). 4 | 5 | -export([ 6 | cmd_routing/3 7 | ]). 8 | 9 | cmd_routing(Cmd, Bin, #{ws_pid := WsPid} = State) -> 10 | ProtocolModule = game_massage:decoder_for(Cmd), 11 | RecordName = game_massage:msg_type(Cmd), 12 | RecordData = ProtocolModule:decode_msg(Bin, RecordName), 13 | case Cmd of 14 | 101 -> 15 | notdoing; 16 | _ -> 17 | game_debug:debug(info,"wwwwwww WsPid : ~p, protobuf recevie: ~p ~n wwwwwww", [WsPid, RecordData]) 18 | end, 19 | HandlerModule = list_to_atom(lists:concat([hd(string:split(atom_to_list(ProtocolModule),"_",trailing)), "_handler"])), 20 | HandlerModule:handle(RecordData, State). -------------------------------------------------------------------------------- /apps/game_server/include/game_user.hrl: -------------------------------------------------------------------------------- 1 | %% game user hrl file. 2 | 3 | -author("Nuapio Z.Y. Huang"). 4 | 5 | -record(r_unique, { unique_key, unique_value} ). 6 | 7 | -record(r_account, { user_account = "" 8 | , user_id = 0 } ). 9 | 10 | -record(r_user, { user_id = 0 11 | , user_state = #{} }). 12 | 13 | -record(r_online, { 14 | user_id = 0, 15 | user_pid = none 16 | }). 17 | 18 | -define(default_img_id, rand:uniform(6)). 19 | -define(default_user_state(UserId), 20 | #{user_id => UserId 21 | , user_name => integer_to_binary(UserId) 22 | , user_img_id => ?default_img_id 23 | , user_rank => 0 24 | , user_gold => 0 25 | , user_diamond => 0 26 | , user_account_experience => 0 27 | , user_pid => none 28 | , ws_pid => none 29 | , check_online_count => 0} 30 | ). 31 | -------------------------------------------------------------------------------- /apps/game_server/src/game_log/game_debug.erl: -------------------------------------------------------------------------------- 1 | -module(game_debug). 2 | 3 | -author("Naupio Z.Y. Huang"). 4 | 5 | -export([ 6 | debug/2, debug/3 7 | ]). 8 | 9 | debug(info, String) -> 10 | game_log:info(String); 11 | debug(warning, String) -> 12 | game_log:warning(String); 13 | debug(error, String) -> 14 | game_log:error(String); 15 | debug(info_error, String) -> 16 | debug(info, String), 17 | debug(error, String). 18 | 19 | 20 | debug(info, FormatStr, ArgsList) -> 21 | game_log:info(FormatStr, ArgsList); 22 | debug(warning, FormatStr, ArgsList) -> 23 | game_log:warning(FormatStr, ArgsList); 24 | debug(error, FormatStr, ArgsList) -> 25 | game_log:error(FormatStr, ArgsList); 26 | debug(info_error,FormatStr, ArgsList) -> 27 | debug(info, FormatStr, ArgsList), 28 | debug(error, FormatStr, ArgsList). -------------------------------------------------------------------------------- /apps/game_server/rebar.config: -------------------------------------------------------------------------------- 1 | {erl_opts, [ 2 | debug_info, 3 | {i, "./_build/default/plugins/gpb/include"} 4 | 5 | ]}. 6 | 7 | {deps, []}. 8 | 9 | {src_dirs, ["src", "src/*"]}. 10 | 11 | {plugins, [ 12 | {pb_msgcodegen, {git, "https://github.com/Naupio/pb_msgcodegen", {tag, "0.1.0"}}}, 13 | {rebar3_gpb_plugin, "2.12.1"} 14 | ]}. 15 | 16 | {gpb_opts, [ 17 | {i, "priv/protobuf/"}, 18 | {module_name_suffix, "_pb"}, 19 | % {o, "path/to/out_dir"}, %% both .erl and .hrl are generated here 20 | {o_erl, "src/game_protobuf"}, 21 | {o_hrl, "include/"}, 22 | {strings_as_binaries, true}, 23 | type_specs, 24 | {packed, true} 25 | ]}. 26 | 27 | {provider_hooks, 28 | [{pre, [ 29 | {compile, pb_msgcodegen}, 30 | {compile, {protobuf, compile}}, 31 | {clean, {protobuf, clean}} 32 | ]} 33 | ]}. -------------------------------------------------------------------------------- /apps/game_server/src/game_role/game_role_sup.erl: -------------------------------------------------------------------------------- 1 | -module(game_role_sup). 2 | 3 | -author("Naupio Z.Y. Huang"). 4 | 5 | -behaviour(supervisor). 6 | 7 | -define(SERVER, ?MODULE). 8 | 9 | -export([start_link/0]). 10 | -export([init/1]). 11 | 12 | start_link() -> 13 | {ok, SupPid} = supervisor:start_link({local, ?SERVER}, ?MODULE, []), 14 | game_debug:debug(info, "~n game_role_sup is starting succeed!!! ~n"), 15 | {ok, SupPid} 16 | . 17 | 18 | init(_Args) -> 19 | SupFlags = #{strategy => simple_one_for_one, intensity => 1, period => 5}, 20 | ChildSpecs = [#{id => game_role, 21 | start => {game_role, start_link, []}, 22 | restart => transient, 23 | shutdown => 30000, 24 | type => worker, 25 | modules => [game_role]}], 26 | {ok, {SupFlags, ChildSpecs}}. -------------------------------------------------------------------------------- /apps/game_server/src/game_server_app.erl: -------------------------------------------------------------------------------- 1 | %%%------------------------------------------------------------------- 2 | %% @doc game_server public API 3 | %% @end 4 | %%%------------------------------------------------------------------- 5 | 6 | -module(game_server_app). 7 | 8 | -author("Nuapio Z.Y. Huang"). 9 | 10 | -behaviour(application). 11 | 12 | %% Application callbacks 13 | -export([start/2, stop/1]). 14 | 15 | %%==================================================================== 16 | %% API 17 | %%==================================================================== 18 | 19 | start(_StartType, _StartArgs) -> 20 | {ok, SupPid} = game_server_sup:start_link(), 21 | 22 | % any start hear 23 | ok = game_ets:ets_start(), 24 | 25 | {ok, SupPid}. 26 | 27 | %%-------------------------------------------------------------------- 28 | stop(_State) -> 29 | ok. 30 | 31 | %%==================================================================== 32 | %% Internal functions 33 | %%==================================================================== -------------------------------------------------------------------------------- /apps/game_server/src/game_handler/common_handler.erl: -------------------------------------------------------------------------------- 1 | -module(common_handler). 2 | 3 | -author("Naupio Z.Y. Huang"). 4 | 5 | -include("common_pb.hrl"). 6 | 7 | -export([ 8 | handle/2 9 | ]). 10 | 11 | %% heartbeat for keep user alive. 12 | handle(#heartbeatReq{}, #{ws_pid := WsPid} = State) -> 13 | UnixTime = game_util:unixtime(), 14 | RecordData = #heartbeatResp{unixtime = UnixTime}, 15 | game_ws_util:ws_send(WsPid, RecordData), 16 | {noreply, State#{check_online_count := 0}}; 17 | 18 | %% Test proto for Hello World。 19 | handle(#helloReq{msg = OptionalString}, #{ws_pid := WsPid} = State) -> 20 | case OptionalString of 21 | undefined -> 22 | RecordData = #worldResp{}, 23 | game_ws_util:ws_send(WsPid, RecordData); 24 | _ -> 25 | RecordData = #worldResp{msg = OptionalString}, 26 | game_ws_util:ws_send(WsPid, RecordData) 27 | end, 28 | {noreply, State}; 29 | 30 | handle(Record, State) -> 31 | game_debug:debug(info,"~n~n module *~p* unknow *Record* message: ~p with *State* ~p ~n~n", [?MODULE,Record, State]), 32 | {noreply, State}. -------------------------------------------------------------------------------- /apps/game_server/src/game_log/game_log.erl: -------------------------------------------------------------------------------- 1 | -module(game_log). 2 | 3 | -author("Naupio Z.Y. Huang"). 4 | 5 | -export([ 6 | info/1, info/2, 7 | debug/1, debug/2, 8 | warning/1, warning/2, 9 | error/1, error/2 10 | ]). 11 | 12 | info(String) -> 13 | lager:info(String). 14 | % error_logger:info_msg(String). 15 | 16 | info(FormatStr,ArgsList) -> 17 | lager:info(FormatStr,ArgsList). 18 | % error_logger:info_msg(FormatStr,ArgsList). 19 | 20 | error(String) -> 21 | lager:error(String). 22 | % error_logger:error_msg(String). 23 | 24 | error(FormatStr,ArgsList) -> 25 | lager:error(FormatStr,ArgsList). 26 | % error_logger:error_msg(FormatStr,ArgsList). 27 | 28 | warning(String) -> 29 | lager:warning(String). 30 | % error_logger:warning_msg(String). 31 | 32 | warning(FormatStr,ArgsList) -> 33 | lager:warning(FormatStr,ArgsList). 34 | % error_logger:warning_msg(FormatStr,ArgsList). 35 | 36 | debug(String) -> 37 | lager:debug(String). 38 | % info(String). 39 | 40 | debug(FormatStr,ArgsList) -> 41 | lager:debug(FormatStr,ArgsList). 42 | % info(FormatStr,ArgsList). -------------------------------------------------------------------------------- /apps/game_server/src/game_massage.erl: -------------------------------------------------------------------------------- 1 | -module(game_massage). 2 | 3 | -export([msg_type/1, msg_code/1, decoder_for/1]). 4 | 5 | -spec msg_type(non_neg_integer()) -> atom(). 6 | 7 | msg_type(11) -> loginReq; 8 | msg_type(12) -> loginResp; 9 | msg_type(101) -> heartbeatReq; 10 | msg_type(102) -> heartbeatResp; 11 | msg_type(1001) -> helloReq; 12 | msg_type(1002) -> worldResp; 13 | msg_type(2001) -> userInfoReq; 14 | msg_type(2002) -> userInfoResp; 15 | msg_type(_) -> undefined. 16 | 17 | -spec msg_code(atom()) -> non_neg_integer(). 18 | 19 | msg_code(loginReq) -> 11; 20 | msg_code(loginResp) -> 12; 21 | msg_code(heartbeatReq) -> 101; 22 | msg_code(heartbeatResp) -> 102; 23 | msg_code(helloReq) -> 1001; 24 | msg_code(worldResp) -> 1002; 25 | msg_code(userInfoReq) -> 2001; 26 | msg_code(userInfoResp) -> 2002. 27 | 28 | -spec decoder_for(non_neg_integer()) -> module(). 29 | 30 | 31 | decoder_for(11) -> common_pb; 32 | decoder_for(12) -> common_pb; 33 | decoder_for(101) -> common_pb; 34 | decoder_for(102) -> common_pb; 35 | decoder_for(1001) -> common_pb; 36 | decoder_for(1002) -> common_pb; 37 | decoder_for(2001) -> maininterface_pb; 38 | decoder_for(2002) -> maininterface_pb. -------------------------------------------------------------------------------- /apps/game_server/src/game_web/game_ws_login.erl: -------------------------------------------------------------------------------- 1 | -module(game_ws_login). 2 | 3 | -author("Naupio Z.Y. Huang"). 4 | 5 | -include("common_pb.hrl"). 6 | -include("game_user.hrl"). 7 | 8 | -export([ 9 | login/3 10 | ]). 11 | 12 | login(Cmd, Bin, #{ws_pid := WsPid} = WsState) -> 13 | Module = game_massage:decoder_for(Cmd), 14 | RecordName = game_massage:msg_type(Cmd), 15 | RecordData = Module:decode_msg(Bin, RecordName), 16 | game_debug:debug(info,"wwwwwww WsPid : ~p, protobuf recevie: ~p ~n wwwwwww", [WsPid, RecordData]), 17 | NewWsState = login_handler(RecordData, WsState), 18 | NewWsState. 19 | 20 | login_handler(#loginReq{cookie = Cookie}, #{ws_pid := WsPid} = WsState) -> 21 | UserId = game_mn:get_user_id_by_account(Cookie), 22 | case ets:lookup(ets_user_online, UserId) of 23 | [#r_online{user_id=UserId, user_pid=UserPid}] -> 24 | gen_server:cast(UserPid, {reconnect, WsPid}), 25 | WsState#{logined => true, user_pid => UserPid}; 26 | [] -> 27 | {ok, UserPid} = supervisor:start_child(game_role_sup, [[UserId, WsPid]]), 28 | WsState#{logined => true, user_pid => UserPid} 29 | end. -------------------------------------------------------------------------------- /apps/game_server/include/maininterface_pb.hrl: -------------------------------------------------------------------------------- 1 | %% -*- coding: utf-8 -*- 2 | %% Automatically generated, do not edit 3 | %% Generated by gpb_compile version 4.10.1 4 | 5 | -ifndef(maininterface_pb). 6 | -define(maininterface_pb, true). 7 | 8 | -define(maininterface_pb_gpb_version, "4.10.1"). 9 | 10 | -ifndef('USERINFOREQ_PB_H'). 11 | -define('USERINFOREQ_PB_H', true). 12 | -record(userInfoReq, 13 | {user_id = 0 :: non_neg_integer() | undefined % = 1, 32 bits 14 | }). 15 | -endif. 16 | 17 | -ifndef('USERINFORESP_PB_H'). 18 | -define('USERINFORESP_PB_H', true). 19 | -record(userInfoResp, 20 | {user_id = 0 :: non_neg_integer() | undefined, % = 1, 32 bits 21 | user_name = <<>> :: iodata() | undefined, % = 2 22 | user_img_id = 0 :: non_neg_integer() | undefined, % = 3, 32 bits 23 | user_rank = 0 :: non_neg_integer() | undefined, % = 4, 32 bits 24 | user_account_experience = 0 :: non_neg_integer() | undefined, % = 5, 32 bits 25 | user_gold = 0 :: non_neg_integer() | undefined, % = 6, 32 bits 26 | user_diamond = 0 :: non_neg_integer() | undefined % = 7, 32 bits 27 | }). 28 | -endif. 29 | 30 | -endif. 31 | -------------------------------------------------------------------------------- /apps/game_server/src/game_db/game_mn.erl: -------------------------------------------------------------------------------- 1 | -module(game_mn). 2 | 3 | -author("Naupio Z.Y. Huang"). 4 | 5 | -include("game_user.hrl"). 6 | 7 | -export([ 8 | get_user_id_by_account/1 9 | , put_user_id_by_account/2 10 | , save_user_state/1 11 | , get_user_state/1 12 | ]). 13 | 14 | -define(default_id_start, 10000). 15 | 16 | get_user_id_by_account(Cookie) -> 17 | case catch mnesia:dirty_read(r_account, Cookie) of 18 | [#r_account{user_account = Cookie, user_id = UserId}|_] -> 19 | UserId; 20 | _ -> 21 | UserId = ?default_id_start + mnesia:dirty_update_counter(r_unique, r_user, 1), 22 | put_user_id_by_account(Cookie, UserId), 23 | UserId 24 | end. 25 | 26 | put_user_id_by_account(Cookie, UserId) -> 27 | mnesia:dirty_write(r_account, #r_account{user_account = Cookie, user_id = UserId}). 28 | 29 | 30 | get_user_state(UserId) -> 31 | case catch mnesia:dirty_read(r_user, UserId) of 32 | [#r_user{user_id = UserId, user_state = UserState}|_] -> 33 | maps:merge(UserState, ?default_user_state(UserId)); 34 | _ -> %% first login 35 | ?default_user_state(UserId) 36 | end. 37 | 38 | save_user_state(#{user_id := UserId} = UserState) -> 39 | mnesia:dirty_write(r_user, #r_user{user_id= UserId, user_state = UserState}). -------------------------------------------------------------------------------- /apps/game_server/src/game_cache/game_ets_svr.erl: -------------------------------------------------------------------------------- 1 | -module(game_ets_svr). 2 | 3 | -author("Naupio Z.Y. Huang"). 4 | 5 | -behaviour(gen_server). 6 | 7 | %% callback function 8 | -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). 9 | 10 | %% api 11 | -export([start_link/1]). 12 | 13 | start_link({EtsName, Options}) -> 14 | gen_server:start_link({local, list_to_atom(lists:concat([EtsName,"_svr"]))} 15 | , ?MODULE, [EtsName, Options], []). 16 | 17 | init([EtsName, Options]) -> 18 | ets:new(EtsName, Options), 19 | State = #{ets_name => EtsName, ets_options => Options}, 20 | {ok, State}. 21 | 22 | handle_call(_Msg, _From, _State) -> 23 | game_debug:debug(info,"~n~n module *~p* unknow *CALL* message: ~p which *From*: ~p with *State* ~p ~n~n", [?MODULE,_Msg, _From, _State]), 24 | {noreply, _State}. 25 | 26 | handle_cast(_Msg, _State) -> 27 | game_debug:debug(info,"~n~n module *~p* unknow *CAST* message: ~p with *State* ~p ~n~n", [?MODULE,_Msg, _State]), 28 | {noreply, _State}. 29 | 30 | handle_info(_Msg, _State) -> 31 | game_debug:debug(info,"~n~n module *~p* unknow *INFO* message: ~p with *State* ~p ~n~n", [?MODULE, _Msg, _State]), 32 | {noreply, _State}. 33 | 34 | terminate(_Reson, _State) -> 35 | ok. 36 | 37 | code_change(_OldVsn, _State, _Extra) -> 38 | ok. 39 | -------------------------------------------------------------------------------- /apps/game_server/src/game_web/game_web_sup.erl: -------------------------------------------------------------------------------- 1 | %%%------------------------------------------------------------------- 2 | %% @doc game_web top level supervisor. 3 | %% @end 4 | %%%------------------------------------------------------------------- 5 | 6 | -module(game_web_sup). 7 | 8 | -author("Nuapio Z.Y. Huang"). 9 | 10 | -behaviour(supervisor). 11 | 12 | %% API 13 | -export([start_link/0]). 14 | 15 | %% Supervisor callbacks 16 | -export([init/1]). 17 | 18 | -define(SERVER, ?MODULE). 19 | 20 | %%==================================================================== 21 | %% API functions 22 | %%==================================================================== 23 | 24 | start_link() -> 25 | supervisor:start_link({local, ?SERVER}, ?MODULE, []). 26 | 27 | %%==================================================================== 28 | %% Supervisor callbacks 29 | %%==================================================================== 30 | 31 | %% Child :: #{id => Id, start => {M, F, A}} 32 | %% Optional keys are restart, shutdown, type, modules. 33 | %% Before OTP 18 tuples must be used to specify a child. e.g. 34 | %% Child :: {Id,StartFunc,Restart,Shutdown,Type,Modules} 35 | init([]) -> 36 | ok = game_web:network_start(), 37 | {ok, {{one_for_all, 0, 1}, []}}. 38 | 39 | %%==================================================================== 40 | %% Internal functions 41 | %%==================================================================== 42 | -------------------------------------------------------------------------------- /apps/game_server/src/game_db/game_mnesia_sup.erl: -------------------------------------------------------------------------------- 1 | %%%------------------------------------------------------------------- 2 | %% @doc game_mnesia top level supervisor. 3 | %% @end 4 | %%%------------------------------------------------------------------- 5 | 6 | -module(game_mnesia_sup). 7 | 8 | -author("Nuapio Z.Y. Huang"). 9 | 10 | -behaviour(supervisor). 11 | 12 | %% API 13 | -export([start_link/0]). 14 | 15 | %% Supervisor callbacks 16 | -export([init/1]). 17 | 18 | -define(SERVER, ?MODULE). 19 | 20 | %%==================================================================== 21 | %% API functions 22 | %%==================================================================== 23 | 24 | start_link() -> 25 | supervisor:start_link({local, ?SERVER}, ?MODULE, []). 26 | 27 | %%==================================================================== 28 | %% Supervisor callbacks 29 | %%==================================================================== 30 | 31 | %% Child :: #{id => Id, start => {M, F, A}} 32 | %% Optional keys are restart, shutdown, type, modules. 33 | %% Before OTP 18 tuples must be used to specify a child. e.g. 34 | %% Child :: {Id,StartFunc,Restart,Shutdown,Type,Modules} 35 | init([]) -> 36 | ok = game_mnesia:mnesia_start(), 37 | {ok, {{one_for_all, 0, 1}, []}}. 38 | 39 | %%==================================================================== 40 | %% Internal functions 41 | %%==================================================================== 42 | -------------------------------------------------------------------------------- /apps/game_server/src/game_server_sup.erl: -------------------------------------------------------------------------------- 1 | %%%------------------------------------------------------------------- 2 | %% @doc game_server top level supervisor. 3 | %% @end 4 | %%%------------------------------------------------------------------- 5 | 6 | -module(game_server_sup). 7 | 8 | -behaviour(supervisor). 9 | 10 | -author("Nuapio Z.Y. Huang"). 11 | 12 | -include("game_sup.hrl"). 13 | 14 | %% API 15 | -export([start_link/0]). 16 | 17 | %% Supervisor callbacks 18 | -export([init/1]). 19 | 20 | -define(SERVER, ?MODULE). 21 | 22 | 23 | %%==================================================================== 24 | %% API functions 25 | %%==================================================================== 26 | 27 | start_link() -> 28 | supervisor:start_link({local, ?SERVER}, ?MODULE, []). 29 | 30 | %%==================================================================== 31 | %% Supervisor callbacks 32 | %%==================================================================== 33 | 34 | %% Child :: {Id,StartFunc,Restart,Shutdown,Type,Modules} 35 | init([]) -> 36 | {ok, { {one_for_one, 10, 1}, [ 37 | ?SupChildSpecById(game_ets_sup), 38 | ?SupChildSpecById(game_mnesia_sup), 39 | ?SupChildSpecById(game_role_sup), 40 | ?SupChildSpecById(game_web_sup) 41 | ]} }. 42 | 43 | %%==================================================================== 44 | %% Internal functions 45 | %%==================================================================== 46 | -------------------------------------------------------------------------------- /rebar.lock: -------------------------------------------------------------------------------- 1 | {"1.1.0", 2 | [{<<"cowboy">>,{pkg,<<"cowboy">>,<<"2.7.0">>},0}, 3 | {<<"cowlib">>,{pkg,<<"cowlib">>,<<"2.8.0">>},1}, 4 | {<<"goldrush">>,{pkg,<<"goldrush">>,<<"0.1.9">>},1}, 5 | {<<"gproc">>,{pkg,<<"gproc">>,<<"0.8.0">>},0}, 6 | {<<"jsx">>,{pkg,<<"jsx">>,<<"2.10.0">>},0}, 7 | {<<"lager">>,{pkg,<<"lager">>,<<"3.8.0">>},0}, 8 | {<<"poolboy">>,{pkg,<<"poolboy">>,<<"1.5.2">>},0}, 9 | {<<"ranch">>,{pkg,<<"ranch">>,<<"1.7.1">>},1}, 10 | {<<"recen">>, 11 | {git,"https://github.com/ferd/recon", 12 | {ref,"a1551ddaf4ec2982a0e1451d5fea635b07243dee"}}, 13 | 0}, 14 | {<<"recon">>,{pkg,<<"recon">>,<<"2.5.1">>},0}]}. 15 | [ 16 | {pkg_hash,[ 17 | {<<"cowboy">>, <<"91ED100138A764355F43316B1D23D7FF6BDB0DE4EA618CB5D8677C93A7A2F115">>}, 18 | {<<"cowlib">>, <<"FD0FF1787DB84AC415B8211573E9A30A3EBE71B5CBFF7F720089972B2319C8A4">>}, 19 | {<<"goldrush">>, <<"F06E5D5F1277DA5C413E84D5A2924174182FB108DABB39D5EC548B27424CD106">>}, 20 | {<<"gproc">>, <<"CEA02C578589C61E5341FCE149EA36CCEF236CC2ECAC8691FBA408E7EA77EC2F">>}, 21 | {<<"jsx">>, <<"77760560D6AC2B8C51FD4C980E9E19B784016AA70BE354CE746472C33BEB0B1C">>}, 22 | {<<"lager">>, <<"3402B9A7E473680CA179FC2F1D827CAB88DD37DD1E6113090C6F45EF05228A1C">>}, 23 | {<<"poolboy">>, <<"392B007A1693A64540CEAD79830443ABF5762F5D30CF50BC95CB2C1AAAFA006B">>}, 24 | {<<"ranch">>, <<"6B1FAB51B49196860B733A49C07604465A47BDB78AA10C1C16A3D199F7F8C881">>}, 25 | {<<"recon">>, <<"430FFA60685AC1EFDFB1FE4C97B8767C92D0D92E6E7C3E8621559BA77598678A">>}]} 26 | ]. 27 | -------------------------------------------------------------------------------- /config/sys.config: -------------------------------------------------------------------------------- 1 | [ 2 | { game_config, [ 3 | {websocket_port, 19910} 4 | ]}, 5 | 6 | % {sasl, [ 7 | % {sasl_error_logger, {file, "logs"}}, 8 | % {errlog_type, error}, 9 | % {error_logger_mf_dir,"./mf_logs"}, 10 | % {error_logger_mf_maxbytes,104857600}, % 100 MB 11 | % {error_logger_mf_maxfiles, 10} 12 | % ]}, 13 | 14 | {mnesia, [ 15 | % {mkdir, "/tmp/mf_logs"}, 16 | {dir, "/tmp/mnesia_db_data"} 17 | ] 18 | }, 19 | 20 | {lager, [ 21 | {colored, true}, 22 | {crash_log, "/tmp/mf_logs/crash.log"}, 23 | {crash_log_msg_size, 65536}, 24 | {crash_log_size, 10485760}, 25 | {crash_log_date, "$D0"}, 26 | {crash_log_count, 10}, 27 | {handlers, [ 28 | {lager_console_backend, [{level, error}, 29 | {formatter, lager_default_formatter}, 30 | {formatter_config, [date," ", time, color, " [",severity,"] ", pid,"@",module,":",function,":",line,"\r\n", message, "\e[0m\r\n\r\n"]} 31 | ]}, 32 | {lager_file_backend, [{level, error}, {file, "/tmp/mf_logs/error.log"}, {size, 10485760}, {date, "$D0"}, {count, 10}]}, 33 | {lager_file_backend, [{level, warning}, {file, "/tmp/mf_logs/warning.log"}, {size, 10485760}, {date, "$D0"}, {count, 10}]}, 34 | {lager_file_backend, [{level, info}, {file, "/tmp/mf_logs/info.log"}, {size, 10485760}, {date, "$D0"}, {count, 10}]} 35 | ]} 36 | ]} 37 | 38 | ]. -------------------------------------------------------------------------------- /apps/game_server/include/common_pb.hrl: -------------------------------------------------------------------------------- 1 | %% -*- coding: utf-8 -*- 2 | %% Automatically generated, do not edit 3 | %% Generated by gpb_compile version 4.10.1 4 | 5 | -ifndef(common_pb). 6 | -define(common_pb, true). 7 | 8 | -define(common_pb_gpb_version, "4.10.1"). 9 | 10 | -ifndef('LOGINREQ_PB_H'). 11 | -define('LOGINREQ_PB_H', true). 12 | -record(loginReq, 13 | {cookie = <<>> :: iodata() | undefined % = 1 14 | }). 15 | -endif. 16 | 17 | -ifndef('LOGINRESP_PB_H'). 18 | -define('LOGINRESP_PB_H', true). 19 | -record(loginResp, 20 | {result = 'FAILED' :: 'FAILED' | 'SUCCEEDED' | 'NOTFOUND' | integer() | undefined, % = 1, enum LoginStatus 21 | user_id = 0 :: non_neg_integer() | undefined % = 2, 32 bits 22 | }). 23 | -endif. 24 | 25 | -ifndef('HELLOREQ_PB_H'). 26 | -define('HELLOREQ_PB_H', true). 27 | -record(helloReq, 28 | {msg = <<>> :: iodata() | undefined % = 1 29 | }). 30 | -endif. 31 | 32 | -ifndef('WORLDRESP_PB_H'). 33 | -define('WORLDRESP_PB_H', true). 34 | -record(worldResp, 35 | {msg = <<>> :: iodata() | undefined % = 1 36 | }). 37 | -endif. 38 | 39 | -ifndef('HEARTBEATREQ_PB_H'). 40 | -define('HEARTBEATREQ_PB_H', true). 41 | -record(heartbeatReq, 42 | { 43 | }). 44 | -endif. 45 | 46 | -ifndef('HEARTBEATRESP_PB_H'). 47 | -define('HEARTBEATRESP_PB_H', true). 48 | -record(heartbeatResp, 49 | {unixtime = 0 :: non_neg_integer() | undefined % = 1, 32 bits 50 | }). 51 | -endif. 52 | 53 | -endif. 54 | -------------------------------------------------------------------------------- /apps/game_server/src/game_db/game_mnesia.erl: -------------------------------------------------------------------------------- 1 | -module(game_mnesia). 2 | 3 | -author("Naupio Z.Y. Huang"). 4 | 5 | -export([mnesia_start/0]). 6 | 7 | mnesia_start() -> 8 | case mnesia:system_info(use_dir) of 9 | true -> 10 | game_debug:debug(info, "~n mnesia schema is exited!!! ~n"), 11 | mnesia:start(), 12 | game_debug:debug(info, "~n database mnesia is starting succeed!!! ~n"), 13 | check_mn_table(), 14 | ok; 15 | false -> 16 | game_debug:debug(info, "~n creating schema!!! ~n"), 17 | mnesia:stop(), 18 | ok = mnesia:delete_schema([node()]), 19 | ok = mnesia:create_schema([node()]), 20 | mnesia:start(), 21 | game_debug:debug(info, "~n database mnesia is starting succeed!!! ~n"), 22 | create_mn_table(), 23 | ok 24 | end. 25 | 26 | create_mn_table() -> 27 | InitTL = mnesia_data:get_init_table_list(), 28 | lists:foreach(fun({Name, Args}) -> 29 | mnesia:create_table(Name, Args) 30 | end, InitTL), 31 | ok. 32 | 33 | check_mn_table() -> 34 | AllExitTables = mnesia_data:get_all_exit_tables(), 35 | InitTL = mnesia_data:get_init_table_list(), 36 | lists:foreach(fun({Name, Args}) -> 37 | case lists:member(Name, AllExitTables) of 38 | true -> 39 | case mnesia:wait_for_tables([Name], 5000) of 40 | ok -> ok; 41 | Err-> 42 | game_log:error("~n wait mnesia table ~p error ~p ~n", [Name, Err]) 43 | end; 44 | false -> 45 | mnesia:create_table(Name, Args) 46 | end 47 | end, InitTL), 48 | ok. -------------------------------------------------------------------------------- /apps/game_server/src/game_simulation.erl: -------------------------------------------------------------------------------- 1 | -module(game_simulation). 2 | 3 | -author("Naupio Z.Y. Huang"). 4 | 5 | -include("common_pb.hrl"). 6 | -include("maininterface_pb.hrl"). 7 | 8 | -export([ 9 | multi_user_start/1, 10 | create_login_user/1 11 | ]). 12 | 13 | multi_user_start(Num) -> 14 | lists:foreach(fun(Index) -> 15 | spawn(fun() -> create_login_user(integer_to_list(Index)) end) 16 | end 17 | , lists:seq(1,Num)). 18 | 19 | create_login_user(Cookie) -> 20 | Cmd = 11, 21 | LoginBin = common_pb:encode_msg(#loginReq{cookie = Cookie}), 22 | State = #{ws_pid => self()}, 23 | NewState = game_ws_login:login(Cmd, LoginBin, State), 24 | 25 | UserPid = maps:get(user_pid, NewState), 26 | WsPid = maps:get(ws_pid, NewState), 27 | erlang:send_after(5000, self(), heartbeat), 28 | 29 | loop_receive(Cookie,UserPid,WsPid). 30 | 31 | loop_receive(Cookie,UserPid,WsPid) -> 32 | receive 33 | heartbeat -> 34 | HeartbeatBin = common_pb:encode_msg(#heartbeatReq{}), 35 | gen_server:cast(UserPid, {cmd_routing, 101, HeartbeatBin}), 36 | erlang:send_after(5000, self(), heartbeat), 37 | loop_receive(Cookie,UserPid,WsPid); 38 | {send_binary, <> = Msg} -> 39 | case Cmd of 40 | 102 -> 41 | notdoing; 42 | _ -> 43 | game_debug:debug(info, "~n !!!!!!!!!!!!!! user: ~p, receive msg: ~p , time: ~p !!!!!!!!!!!!!! ~n" 44 | , [Cookie, Msg, time()]) 45 | end, 46 | loop_receive(Cookie,UserPid,WsPid); 47 | Msg -> 48 | game_debug:debug(info, "~n !!!!!!!!!!!!!! user: ~p, receive msg: ~p , time: ~p !!!!!!!!!!!!!! ~n" 49 | , [Cookie, Msg, time()]), 50 | loop_receive(Cookie,UserPid,WsPid) 51 | end. -------------------------------------------------------------------------------- /rebar.config: -------------------------------------------------------------------------------- 1 | {erl_opts, [ 2 | debug_info, 3 | {i, "./apps/game_server/include/"}, 4 | {parse_transform, lager_transform} 5 | ]}. 6 | 7 | {minimum_otp_vsn, "22.0"}. 8 | 9 | {plugins, [rebar3_hex, rebar3_appup_plugin]}. 10 | 11 | {project_plugins, [rebar3_proper]}. 12 | 13 | {shell, [ 14 | {apps, [game_server]}, 15 | {config, "./config/sys.config"} 16 | ]}. 17 | 18 | {src_dirs, ["src", "src/*"]}. 19 | 20 | {deps, [ 21 | {cowboy, "2.7.0"}, 22 | {gproc, "0.8.0"}, 23 | {poolboy, "1.5.2"}, 24 | {jsx, "2.10.0"}, 25 | {lager, "3.8.0"}, 26 | {recon, "2.5.1"} 27 | ]}. 28 | 29 | {pre_hooks, [{compile, "mkdir -p /tmp/mnesia_db_data"}, 30 | {compile, "mkdir -p /tmp/mf_logs"} 31 | ]}. 32 | 33 | {provider_hooks, [ 34 | {pre, [{tar, {appup, tar}}]}, 35 | {post, [{compile, {appup, compile}}, 36 | {clean, {appup, clean}}]} 37 | ]}. 38 | 39 | {dist_node, [ 40 | {setcookie, 'game_server_cookie'}, 41 | {name, 'game_server@127.0.0.1'} 42 | ]}. 43 | 44 | {relx, [{release, { game_server, "0.2.0" }, 45 | [ 46 | sasl, 47 | mnesia, 48 | game_server 49 | ]}, 50 | 51 | {sys_config, "./config/sys.config"}, 52 | {vm_args, "./config/vm.args"}, 53 | 54 | {dev_mode, true}, 55 | {include_erts, false}, 56 | 57 | {overlay, [ 58 | {mkdir, "/tmp/mnesia_db_data"}, 59 | {mkdir, "/tmp/mf_logs"} 60 | ]}, 61 | {extended_start_script, true}] 62 | }. 63 | 64 | {profiles, [ 65 | {prod, [{relx, [ 66 | {dev_mode, false}, 67 | {include_src, false}, 68 | {include_erts, true}, 69 | {system_libs, true} 70 | ] 71 | } 72 | ]}, 73 | {native, [ 74 | {erl_opts, [{native, o3}]} 75 | ]}, 76 | {test, [ 77 | {deps, [ 78 | {proper, "1.3.0"} 79 | ]} 80 | ]} 81 | ] 82 | } 83 | . 84 | -------------------------------------------------------------------------------- /apps/game_server/src/game_web/game_ws_handler.erl: -------------------------------------------------------------------------------- 1 | -module(game_ws_handler). 2 | 3 | -author("Nuapio Z.Y. Huang"). 4 | 5 | -export([ 6 | init/2 7 | ,websocket_init/1 8 | ,websocket_handle/2 9 | ,websocket_info/2 10 | ,terminate/3 11 | ]). 12 | 13 | init(Req, _) -> 14 | State = #{ws_pid => none, logined => false, user_pid => none}, 15 | {cowboy_websocket, Req, State}. 16 | 17 | websocket_init(State) -> 18 | WsPid = self(), 19 | game_debug:debug(info,"wwwwwww WsPid: ~p, websocket connected wwwwwww ~n", [WsPid]), 20 | NewState = State#{ws_pid := WsPid}, 21 | {ok, NewState}. 22 | 23 | websocket_handle({text, <<"@heart">>}, #{ws_pid := WsPid} = State) -> 24 | game_debug:debug(info,"wwwwwww WsPid: ~p, text recevie: ~p wwwwwww ~n", [WsPid, <<"@heart">>]), 25 | Resp = integer_to_binary(game_util:unixtime()), 26 | {reply, {text, Resp}, State}; 27 | 28 | websocket_handle({text, Req}, #{ws_pid := WsPid} = State) -> 29 | game_debug:debug(info,"wwwwwww WsPid: ~p, text recevie: ~p wwwwwww ~n", [WsPid, Req]), 30 | Resp = Req, 31 | {reply, {text, Resp}, State}; 32 | 33 | websocket_handle({binary, Req}, #{logined := IsLogined} = State) -> 34 | <> = Req, 35 | case IsLogined of 36 | true -> 37 | UserPid = maps:get(user_pid, State), 38 | gen_server:cast(UserPid, {cmd_routing, Cmd, Bin}), 39 | {ok, State}; 40 | false -> 41 | NewState = game_ws_login:login(Cmd, Bin, State), 42 | {ok, NewState} 43 | end; 44 | 45 | websocket_handle(_Frame, State) -> 46 | {ok, State}. 47 | 48 | websocket_info({send_binary, Resp}, State) -> 49 | {reply, {binary, Resp}, State}; 50 | 51 | websocket_info(_Info, #{ws_pid := WsPid} = State) -> 52 | game_debug:debug(info,"Wwwwwwww WsPid: ~p, websocket unkown info wwwwwww ~n", [WsPid]), 53 | {reply, {text, <<"unkown info !">>}, State}. 54 | 55 | terminate(_Info, _Req, #{ws_pid := WsPid 56 | % , user_pid := UserPid, logined := IsLogined 57 | } = _State) -> 58 | % case {IsLogined, is_pid(UserPid)} of 59 | % {true,true} -> 60 | % case is_process_alive(UserPid) of 61 | % true -> exit(UserPid, normal); 62 | % _ -> notdoing 63 | % end; 64 | % _ -> notdoing 65 | % end, 66 | game_debug:debug(info,"wwwwwww WsPid: ~p, websocket terminated wwwwwww ~n", [WsPid]), 67 | ok. -------------------------------------------------------------------------------- /apps/game_server/src/game_role/game_role.erl: -------------------------------------------------------------------------------- 1 | -module(game_role). 2 | 3 | -author("Naupio Z.Y. Huang"). 4 | 5 | -include("game_user.hrl"). 6 | -include("common_pb.hrl") . 7 | -include("maininterface_pb.hrl") . 8 | 9 | -behaviour(gen_server). 10 | 11 | %% callback function 12 | -export([init/1, handle_call/3, handle_cast/2, handle_info/2, 13 | terminate/2, code_change/3]). 14 | 15 | %% API 16 | -export([start_link/1, stop/2]). 17 | 18 | -define(SERVER, ?MODULE). 19 | 20 | -define(SAVE_TIME, 5000). 21 | -define(CHECK_ONLINE_TIME, 30000). 22 | -define(CHECK_ONLINE_LIMIT, 3). 23 | 24 | stop(Pid, Reson) -> 25 | Pid ! {stop, Reson}. 26 | 27 | start_link([UserId, WsPid]) -> 28 | gen_server:start_link(?MODULE, [UserId, WsPid], []). 29 | 30 | init([UserId, WsPid]) -> 31 | State = game_mn:get_user_state(UserId), 32 | NewState = State#{user_pid := self(), ws_pid := WsPid, check_online_count := 0, statem_pid => none}, 33 | erlang:send_after(?SAVE_TIME, self(), save_user_state), 34 | erlang:send_after(?CHECK_ONLINE_TIME, self(), check_online), 35 | {ok, NewState, 0}. 36 | 37 | handle_call(_Msg, _From, _State) -> 38 | game_debug:debug(info,"~n~n module *~p* unknow *CALL* message: ~p which *From*: ~p with *State* ~p ~n~n", [?MODULE,_Msg, _From, _State]), 39 | {noreply, _State}. 40 | 41 | handle_cast({reconnect, WsPid}, #{user_id := UserId} = State) -> 42 | game_ws_util:ws_send(WsPid, #loginResp{result='SUCCEEDED', user_id = UserId}), 43 | UserInfo = game_proto_util:proto_cast_change(State, userInfoResp, record_info(fields, userInfoResp)), 44 | game_ws_util:ws_send(WsPid, UserInfo), 45 | {noreply, State#{check_online_count := 0, ws_pid := WsPid}}; 46 | handle_cast({cmd_routing, Cmd, Bin}, State) -> 47 | game_ws_routing:cmd_routing(Cmd, Bin, State); 48 | handle_cast({change_gold, ChangeGold}, #{user_gold := UserGold} = State) -> 49 | {noreply, State#{user_gold := UserGold+ChangeGold}}; 50 | handle_cast(_Msg, _State) -> 51 | game_debug:debug(info,"~n~n module *~p* unknow *CAST* message: ~p with *State* ~p ~n~n", [?MODULE,_Msg, _State]), 52 | {noreply, _State}. 53 | 54 | handle_info({stop, Reson} , State) -> 55 | {stop, Reson, State#{statem_pid := none}}; 56 | handle_info(check_online, #{check_online_count := COC} = State) -> 57 | case COC >= ?CHECK_ONLINE_LIMIT of 58 | true -> 59 | {stop,normal,State}; 60 | false -> 61 | erlang:send_after(?CHECK_ONLINE_TIME, self(), check_online), 62 | {noreply, State#{check_online_count := COC+1}} 63 | end; 64 | handle_info(timeout, State) -> 65 | #{user_id := UserId, 66 | ws_pid := WsPid 67 | } = State, 68 | ets:insert(ets_user_online, #r_online{user_id = UserId, user_pid = self()}), 69 | game_ws_util:ws_send(WsPid, #loginResp{result='SUCCEEDED', user_id = UserId}), 70 | UserInfo = game_proto_util:proto_cast_change(State, userInfoResp, record_info(fields, userInfoResp)), 71 | game_ws_util:ws_send(WsPid, UserInfo), 72 | NewState = State#{user_tired := 100, user_tired_limit := 100}, 73 | {noreply, NewState}; 74 | handle_info(save_user_state, State) -> 75 | game_mn:save_user_state(State), 76 | erlang:send_after(?SAVE_TIME, self(), save_user_state), 77 | {noreply, State}; 78 | handle_info(_Msg, _State) -> 79 | game_debug:debug(info,"~n~n module *~p* unknow *INFO* message: ~p with *State* ~p ~n~n", [?MODULE, _Msg, _State]), 80 | {noreply, _State}. 81 | 82 | terminate(_Reson, #{user_id := UserId, user_pid := UserPid, ws_pid := WsPid} = State) -> 83 | game_mn:save_user_state(State), 84 | case is_process_alive(WsPid) of 85 | true -> exit(WsPid, kill); 86 | false -> notdoing 87 | end, 88 | ets:delete(ets_user_online, UserId), 89 | game_debug:debug(info,"wwwwwww user terminate by user_id: ~p, user_pid: ~p, ws_pid: ~p wwwwwww ~n" 90 | , [UserId, UserPid, WsPid]), 91 | ok. 92 | 93 | code_change(_OldVsn, _State, _Extra) -> 94 | ok. 95 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | Copyright 2019, Naupio . 179 | 180 | Licensed under the Apache License, Version 2.0 (the "License"); 181 | you may not use this file except in compliance with the License. 182 | You may obtain a copy of the License at 183 | 184 | http://www.apache.org/licenses/LICENSE-2.0 185 | 186 | Unless required by applicable law or agreed to in writing, software 187 | distributed under the License is distributed on an "AS IS" BASIS, 188 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 189 | See the License for the specific language governing permissions and 190 | limitations under the License. 191 | 192 | -------------------------------------------------------------------------------- /apps/game_server/src/game_protobuf/maininterface_pb.erl: -------------------------------------------------------------------------------- 1 | %% -*- coding: utf-8 -*- 2 | %% @private 3 | %% Automatically generated, do not edit 4 | %% Generated by gpb_compile version 4.10.1 5 | -module(maininterface_pb). 6 | 7 | -export([encode_msg/1, encode_msg/2, encode_msg/3]). 8 | -export([decode_msg/2, decode_msg/3]). 9 | -export([merge_msgs/2, merge_msgs/3, merge_msgs/4]). 10 | -export([verify_msg/1, verify_msg/2, verify_msg/3]). 11 | -export([get_msg_defs/0]). 12 | -export([get_msg_names/0]). 13 | -export([get_group_names/0]). 14 | -export([get_msg_or_group_names/0]). 15 | -export([get_enum_names/0]). 16 | -export([find_msg_def/1, fetch_msg_def/1]). 17 | -export([find_enum_def/1, fetch_enum_def/1]). 18 | -export([enum_symbol_by_value/2, enum_value_by_symbol/2]). 19 | -export([get_service_names/0]). 20 | -export([get_service_def/1]). 21 | -export([get_rpc_names/1]). 22 | -export([find_rpc_def/2, fetch_rpc_def/2]). 23 | -export([fqbin_to_service_name/1]). 24 | -export([service_name_to_fqbin/1]). 25 | -export([fqbins_to_service_and_rpc_name/2]). 26 | -export([service_and_rpc_name_to_fqbins/2]). 27 | -export([fqbin_to_msg_name/1]). 28 | -export([msg_name_to_fqbin/1]). 29 | -export([fqbin_to_enum_name/1]). 30 | -export([enum_name_to_fqbin/1]). 31 | -export([get_package_name/0]). 32 | -export([uses_packages/0]). 33 | -export([source_basename/0]). 34 | -export([get_all_source_basenames/0]). 35 | -export([get_all_proto_names/0]). 36 | -export([get_msg_containment/1]). 37 | -export([get_pkg_containment/1]). 38 | -export([get_service_containment/1]). 39 | -export([get_rpc_containment/1]). 40 | -export([get_enum_containment/1]). 41 | -export([get_proto_by_msg_name_as_fqbin/1]). 42 | -export([get_proto_by_service_name_as_fqbin/1]). 43 | -export([get_proto_by_enum_name_as_fqbin/1]). 44 | -export([get_protos_by_pkg_name_as_fqbin/1]). 45 | -export([gpb_version_as_string/0, gpb_version_as_list/0]). 46 | 47 | -include("maininterface_pb.hrl"). 48 | -include("gpb.hrl"). 49 | 50 | %% enumerated types 51 | 52 | -export_type([]). 53 | 54 | %% message types 55 | -type userInfoReq() :: #userInfoReq{}. 56 | 57 | -type userInfoResp() :: #userInfoResp{}. 58 | 59 | -export_type(['userInfoReq'/0, 'userInfoResp'/0]). 60 | 61 | -spec encode_msg(#userInfoReq{} | #userInfoResp{}) -> binary(). 62 | encode_msg(Msg) when tuple_size(Msg) >= 1 -> 63 | encode_msg(Msg, element(1, Msg), []). 64 | 65 | -spec encode_msg(#userInfoReq{} | #userInfoResp{}, atom() | list()) -> binary(). 66 | encode_msg(Msg, MsgName) when is_atom(MsgName) -> 67 | encode_msg(Msg, MsgName, []); 68 | encode_msg(Msg, Opts) 69 | when tuple_size(Msg) >= 1, is_list(Opts) -> 70 | encode_msg(Msg, element(1, Msg), Opts). 71 | 72 | -spec encode_msg(#userInfoReq{} | #userInfoResp{}, atom(), list()) -> binary(). 73 | encode_msg(Msg, MsgName, Opts) -> 74 | case proplists:get_bool(verify, Opts) of 75 | true -> verify_msg(Msg, MsgName, Opts); 76 | false -> ok 77 | end, 78 | TrUserData = proplists:get_value(user_data, Opts), 79 | case MsgName of 80 | userInfoReq -> 81 | encode_msg_userInfoReq(id(Msg, TrUserData), TrUserData); 82 | userInfoResp -> 83 | encode_msg_userInfoResp(id(Msg, TrUserData), TrUserData) 84 | end. 85 | 86 | 87 | encode_msg_userInfoReq(Msg, TrUserData) -> 88 | encode_msg_userInfoReq(Msg, <<>>, TrUserData). 89 | 90 | 91 | encode_msg_userInfoReq(#userInfoReq{user_id = F1}, Bin, 92 | TrUserData) -> 93 | if F1 == undefined -> Bin; 94 | true -> 95 | begin 96 | TrF1 = id(F1, TrUserData), 97 | if TrF1 =:= 0 -> Bin; 98 | true -> e_varint(TrF1, <>, TrUserData) 99 | end 100 | end 101 | end. 102 | 103 | encode_msg_userInfoResp(Msg, TrUserData) -> 104 | encode_msg_userInfoResp(Msg, <<>>, TrUserData). 105 | 106 | 107 | encode_msg_userInfoResp(#userInfoResp{user_id = F1, 108 | user_name = F2, user_img_id = F3, 109 | user_rank = F4, 110 | user_account_experience = F5, 111 | user_gold = F6, user_diamond = F7}, 112 | Bin, TrUserData) -> 113 | B1 = if F1 == undefined -> Bin; 114 | true -> 115 | begin 116 | TrF1 = id(F1, TrUserData), 117 | if TrF1 =:= 0 -> Bin; 118 | true -> e_varint(TrF1, <>, TrUserData) 119 | end 120 | end 121 | end, 122 | B2 = if F2 == undefined -> B1; 123 | true -> 124 | begin 125 | TrF2 = id(F2, TrUserData), 126 | case is_empty_string(TrF2) of 127 | true -> B1; 128 | false -> 129 | e_type_string(TrF2, <>, TrUserData) 130 | end 131 | end 132 | end, 133 | B3 = if F3 == undefined -> B2; 134 | true -> 135 | begin 136 | TrF3 = id(F3, TrUserData), 137 | if TrF3 =:= 0 -> B2; 138 | true -> e_varint(TrF3, <>, TrUserData) 139 | end 140 | end 141 | end, 142 | B4 = if F4 == undefined -> B3; 143 | true -> 144 | begin 145 | TrF4 = id(F4, TrUserData), 146 | if TrF4 =:= 0 -> B3; 147 | true -> e_varint(TrF4, <>, TrUserData) 148 | end 149 | end 150 | end, 151 | B5 = if F5 == undefined -> B4; 152 | true -> 153 | begin 154 | TrF5 = id(F5, TrUserData), 155 | if TrF5 =:= 0 -> B4; 156 | true -> e_varint(TrF5, <>, TrUserData) 157 | end 158 | end 159 | end, 160 | B6 = if F6 == undefined -> B5; 161 | true -> 162 | begin 163 | TrF6 = id(F6, TrUserData), 164 | if TrF6 =:= 0 -> B5; 165 | true -> e_varint(TrF6, <>, TrUserData) 166 | end 167 | end 168 | end, 169 | if F7 == undefined -> B6; 170 | true -> 171 | begin 172 | TrF7 = id(F7, TrUserData), 173 | if TrF7 =:= 0 -> B6; 174 | true -> e_varint(TrF7, <>, TrUserData) 175 | end 176 | end 177 | end. 178 | 179 | -compile({nowarn_unused_function,e_type_sint/3}). 180 | e_type_sint(Value, Bin, _TrUserData) when Value >= 0 -> 181 | e_varint(Value * 2, Bin); 182 | e_type_sint(Value, Bin, _TrUserData) -> 183 | e_varint(Value * -2 - 1, Bin). 184 | 185 | -compile({nowarn_unused_function,e_type_int32/3}). 186 | e_type_int32(Value, Bin, _TrUserData) 187 | when 0 =< Value, Value =< 127 -> 188 | <>; 189 | e_type_int32(Value, Bin, _TrUserData) -> 190 | <> = <>, 191 | e_varint(N, Bin). 192 | 193 | -compile({nowarn_unused_function,e_type_int64/3}). 194 | e_type_int64(Value, Bin, _TrUserData) 195 | when 0 =< Value, Value =< 127 -> 196 | <>; 197 | e_type_int64(Value, Bin, _TrUserData) -> 198 | <> = <>, 199 | e_varint(N, Bin). 200 | 201 | -compile({nowarn_unused_function,e_type_bool/3}). 202 | e_type_bool(true, Bin, _TrUserData) -> 203 | <>; 204 | e_type_bool(false, Bin, _TrUserData) -> 205 | <>; 206 | e_type_bool(1, Bin, _TrUserData) -> <>; 207 | e_type_bool(0, Bin, _TrUserData) -> <>. 208 | 209 | -compile({nowarn_unused_function,e_type_string/3}). 210 | e_type_string(S, Bin, _TrUserData) -> 211 | Utf8 = unicode:characters_to_binary(S), 212 | Bin2 = e_varint(byte_size(Utf8), Bin), 213 | <>. 214 | 215 | -compile({nowarn_unused_function,e_type_bytes/3}). 216 | e_type_bytes(Bytes, Bin, _TrUserData) 217 | when is_binary(Bytes) -> 218 | Bin2 = e_varint(byte_size(Bytes), Bin), 219 | <>; 220 | e_type_bytes(Bytes, Bin, _TrUserData) 221 | when is_list(Bytes) -> 222 | BytesBin = iolist_to_binary(Bytes), 223 | Bin2 = e_varint(byte_size(BytesBin), Bin), 224 | <>. 225 | 226 | -compile({nowarn_unused_function,e_type_fixed32/3}). 227 | e_type_fixed32(Value, Bin, _TrUserData) -> 228 | <>. 229 | 230 | -compile({nowarn_unused_function,e_type_sfixed32/3}). 231 | e_type_sfixed32(Value, Bin, _TrUserData) -> 232 | <>. 233 | 234 | -compile({nowarn_unused_function,e_type_fixed64/3}). 235 | e_type_fixed64(Value, Bin, _TrUserData) -> 236 | <>. 237 | 238 | -compile({nowarn_unused_function,e_type_sfixed64/3}). 239 | e_type_sfixed64(Value, Bin, _TrUserData) -> 240 | <>. 241 | 242 | -compile({nowarn_unused_function,e_type_float/3}). 243 | e_type_float(V, Bin, _) when is_number(V) -> 244 | <>; 245 | e_type_float(infinity, Bin, _) -> 246 | <>; 247 | e_type_float('-infinity', Bin, _) -> 248 | <>; 249 | e_type_float(nan, Bin, _) -> 250 | <>. 251 | 252 | -compile({nowarn_unused_function,e_type_double/3}). 253 | e_type_double(V, Bin, _) when is_number(V) -> 254 | <>; 255 | e_type_double(infinity, Bin, _) -> 256 | <>; 257 | e_type_double('-infinity', Bin, _) -> 258 | <>; 259 | e_type_double(nan, Bin, _) -> 260 | <>. 261 | 262 | -compile({nowarn_unused_function,e_varint/3}). 263 | e_varint(N, Bin, _TrUserData) -> e_varint(N, Bin). 264 | 265 | -compile({nowarn_unused_function,e_varint/2}). 266 | e_varint(N, Bin) when N =< 127 -> <>; 267 | e_varint(N, Bin) -> 268 | Bin2 = <>, 269 | e_varint(N bsr 7, Bin2). 270 | 271 | is_empty_string("") -> true; 272 | is_empty_string(<<>>) -> true; 273 | is_empty_string(L) when is_list(L) -> 274 | not string_has_chars(L); 275 | is_empty_string(B) when is_binary(B) -> false. 276 | 277 | string_has_chars([C | _]) when is_integer(C) -> true; 278 | string_has_chars([H | T]) -> 279 | case string_has_chars(H) of 280 | true -> true; 281 | false -> string_has_chars(T) 282 | end; 283 | string_has_chars(B) 284 | when is_binary(B), byte_size(B) =/= 0 -> 285 | true; 286 | string_has_chars(C) when is_integer(C) -> true; 287 | string_has_chars(<<>>) -> false; 288 | string_has_chars([]) -> false. 289 | 290 | 291 | decode_msg(Bin, MsgName) when is_binary(Bin) -> 292 | decode_msg(Bin, MsgName, []). 293 | 294 | decode_msg(Bin, MsgName, Opts) when is_binary(Bin) -> 295 | TrUserData = proplists:get_value(user_data, Opts), 296 | decode_msg_1_catch(Bin, MsgName, TrUserData). 297 | 298 | -ifdef('OTP_RELEASE'). 299 | decode_msg_1_catch(Bin, MsgName, TrUserData) -> 300 | try decode_msg_2_doit(MsgName, Bin, TrUserData) 301 | catch Class:Reason:StackTrace -> error({gpb_error,{decoding_failure, {Bin, MsgName, {Class, Reason, StackTrace}}}}) 302 | end. 303 | -else. 304 | decode_msg_1_catch(Bin, MsgName, TrUserData) -> 305 | try decode_msg_2_doit(MsgName, Bin, TrUserData) 306 | catch Class:Reason -> 307 | StackTrace = erlang:get_stacktrace(), 308 | error({gpb_error,{decoding_failure, {Bin, MsgName, {Class, Reason, StackTrace}}}}) 309 | end. 310 | -endif. 311 | 312 | decode_msg_2_doit(userInfoReq, Bin, TrUserData) -> 313 | id(decode_msg_userInfoReq(Bin, TrUserData), TrUserData); 314 | decode_msg_2_doit(userInfoResp, Bin, TrUserData) -> 315 | id(decode_msg_userInfoResp(Bin, TrUserData), 316 | TrUserData). 317 | 318 | 319 | 320 | decode_msg_userInfoReq(Bin, TrUserData) -> 321 | dfp_read_field_def_userInfoReq(Bin, 322 | 0, 323 | 0, 324 | id(0, TrUserData), 325 | TrUserData). 326 | 327 | dfp_read_field_def_userInfoReq(<<8, Rest/binary>>, Z1, 328 | Z2, F@_1, TrUserData) -> 329 | d_field_userInfoReq_user_id(Rest, 330 | Z1, 331 | Z2, 332 | F@_1, 333 | TrUserData); 334 | dfp_read_field_def_userInfoReq(<<>>, 0, 0, F@_1, _) -> 335 | #userInfoReq{user_id = F@_1}; 336 | dfp_read_field_def_userInfoReq(Other, Z1, Z2, F@_1, 337 | TrUserData) -> 338 | dg_read_field_def_userInfoReq(Other, 339 | Z1, 340 | Z2, 341 | F@_1, 342 | TrUserData). 343 | 344 | dg_read_field_def_userInfoReq(<<1:1, X:7, Rest/binary>>, 345 | N, Acc, F@_1, TrUserData) 346 | when N < 32 - 7 -> 347 | dg_read_field_def_userInfoReq(Rest, 348 | N + 7, 349 | X bsl N + Acc, 350 | F@_1, 351 | TrUserData); 352 | dg_read_field_def_userInfoReq(<<0:1, X:7, Rest/binary>>, 353 | N, Acc, F@_1, TrUserData) -> 354 | Key = X bsl N + Acc, 355 | case Key of 356 | 8 -> 357 | d_field_userInfoReq_user_id(Rest, 358 | 0, 359 | 0, 360 | F@_1, 361 | TrUserData); 362 | _ -> 363 | case Key band 7 of 364 | 0 -> 365 | skip_varint_userInfoReq(Rest, 0, 0, F@_1, TrUserData); 366 | 1 -> skip_64_userInfoReq(Rest, 0, 0, F@_1, TrUserData); 367 | 2 -> 368 | skip_length_delimited_userInfoReq(Rest, 369 | 0, 370 | 0, 371 | F@_1, 372 | TrUserData); 373 | 3 -> 374 | skip_group_userInfoReq(Rest, 375 | Key bsr 3, 376 | 0, 377 | F@_1, 378 | TrUserData); 379 | 5 -> skip_32_userInfoReq(Rest, 0, 0, F@_1, TrUserData) 380 | end 381 | end; 382 | dg_read_field_def_userInfoReq(<<>>, 0, 0, F@_1, _) -> 383 | #userInfoReq{user_id = F@_1}. 384 | 385 | d_field_userInfoReq_user_id(<<1:1, X:7, Rest/binary>>, 386 | N, Acc, F@_1, TrUserData) 387 | when N < 57 -> 388 | d_field_userInfoReq_user_id(Rest, 389 | N + 7, 390 | X bsl N + Acc, 391 | F@_1, 392 | TrUserData); 393 | d_field_userInfoReq_user_id(<<0:1, X:7, Rest/binary>>, 394 | N, Acc, _, TrUserData) -> 395 | {NewFValue, RestF} = {id(X bsl N + Acc, TrUserData), 396 | Rest}, 397 | dfp_read_field_def_userInfoReq(RestF, 398 | 0, 399 | 0, 400 | NewFValue, 401 | TrUserData). 402 | 403 | skip_varint_userInfoReq(<<1:1, _:7, Rest/binary>>, Z1, 404 | Z2, F@_1, TrUserData) -> 405 | skip_varint_userInfoReq(Rest, Z1, Z2, F@_1, TrUserData); 406 | skip_varint_userInfoReq(<<0:1, _:7, Rest/binary>>, Z1, 407 | Z2, F@_1, TrUserData) -> 408 | dfp_read_field_def_userInfoReq(Rest, 409 | Z1, 410 | Z2, 411 | F@_1, 412 | TrUserData). 413 | 414 | skip_length_delimited_userInfoReq(<<1:1, X:7, 415 | Rest/binary>>, 416 | N, Acc, F@_1, TrUserData) 417 | when N < 57 -> 418 | skip_length_delimited_userInfoReq(Rest, 419 | N + 7, 420 | X bsl N + Acc, 421 | F@_1, 422 | TrUserData); 423 | skip_length_delimited_userInfoReq(<<0:1, X:7, 424 | Rest/binary>>, 425 | N, Acc, F@_1, TrUserData) -> 426 | Length = X bsl N + Acc, 427 | <<_:Length/binary, Rest2/binary>> = Rest, 428 | dfp_read_field_def_userInfoReq(Rest2, 429 | 0, 430 | 0, 431 | F@_1, 432 | TrUserData). 433 | 434 | skip_group_userInfoReq(Bin, FNum, Z2, F@_1, 435 | TrUserData) -> 436 | {_, Rest} = read_group(Bin, FNum), 437 | dfp_read_field_def_userInfoReq(Rest, 438 | 0, 439 | Z2, 440 | F@_1, 441 | TrUserData). 442 | 443 | skip_32_userInfoReq(<<_:32, Rest/binary>>, Z1, Z2, F@_1, 444 | TrUserData) -> 445 | dfp_read_field_def_userInfoReq(Rest, 446 | Z1, 447 | Z2, 448 | F@_1, 449 | TrUserData). 450 | 451 | skip_64_userInfoReq(<<_:64, Rest/binary>>, Z1, Z2, F@_1, 452 | TrUserData) -> 453 | dfp_read_field_def_userInfoReq(Rest, 454 | Z1, 455 | Z2, 456 | F@_1, 457 | TrUserData). 458 | 459 | decode_msg_userInfoResp(Bin, TrUserData) -> 460 | dfp_read_field_def_userInfoResp(Bin, 461 | 0, 462 | 0, 463 | id(0, TrUserData), 464 | id(<<>>, TrUserData), 465 | id(0, TrUserData), 466 | id(0, TrUserData), 467 | id(0, TrUserData), 468 | id(0, TrUserData), 469 | id(0, TrUserData), 470 | TrUserData). 471 | 472 | dfp_read_field_def_userInfoResp(<<8, Rest/binary>>, Z1, 473 | Z2, F@_1, F@_2, F@_3, F@_4, F@_5, F@_6, F@_7, 474 | TrUserData) -> 475 | d_field_userInfoResp_user_id(Rest, 476 | Z1, 477 | Z2, 478 | F@_1, 479 | F@_2, 480 | F@_3, 481 | F@_4, 482 | F@_5, 483 | F@_6, 484 | F@_7, 485 | TrUserData); 486 | dfp_read_field_def_userInfoResp(<<18, Rest/binary>>, Z1, 487 | Z2, F@_1, F@_2, F@_3, F@_4, F@_5, F@_6, F@_7, 488 | TrUserData) -> 489 | d_field_userInfoResp_user_name(Rest, 490 | Z1, 491 | Z2, 492 | F@_1, 493 | F@_2, 494 | F@_3, 495 | F@_4, 496 | F@_5, 497 | F@_6, 498 | F@_7, 499 | TrUserData); 500 | dfp_read_field_def_userInfoResp(<<24, Rest/binary>>, Z1, 501 | Z2, F@_1, F@_2, F@_3, F@_4, F@_5, F@_6, F@_7, 502 | TrUserData) -> 503 | d_field_userInfoResp_user_img_id(Rest, 504 | Z1, 505 | Z2, 506 | F@_1, 507 | F@_2, 508 | F@_3, 509 | F@_4, 510 | F@_5, 511 | F@_6, 512 | F@_7, 513 | TrUserData); 514 | dfp_read_field_def_userInfoResp(<<32, Rest/binary>>, Z1, 515 | Z2, F@_1, F@_2, F@_3, F@_4, F@_5, F@_6, F@_7, 516 | TrUserData) -> 517 | d_field_userInfoResp_user_rank(Rest, 518 | Z1, 519 | Z2, 520 | F@_1, 521 | F@_2, 522 | F@_3, 523 | F@_4, 524 | F@_5, 525 | F@_6, 526 | F@_7, 527 | TrUserData); 528 | dfp_read_field_def_userInfoResp(<<40, Rest/binary>>, Z1, 529 | Z2, F@_1, F@_2, F@_3, F@_4, F@_5, F@_6, F@_7, 530 | TrUserData) -> 531 | d_field_userInfoResp_user_account_experience(Rest, 532 | Z1, 533 | Z2, 534 | F@_1, 535 | F@_2, 536 | F@_3, 537 | F@_4, 538 | F@_5, 539 | F@_6, 540 | F@_7, 541 | TrUserData); 542 | dfp_read_field_def_userInfoResp(<<48, Rest/binary>>, Z1, 543 | Z2, F@_1, F@_2, F@_3, F@_4, F@_5, F@_6, F@_7, 544 | TrUserData) -> 545 | d_field_userInfoResp_user_gold(Rest, 546 | Z1, 547 | Z2, 548 | F@_1, 549 | F@_2, 550 | F@_3, 551 | F@_4, 552 | F@_5, 553 | F@_6, 554 | F@_7, 555 | TrUserData); 556 | dfp_read_field_def_userInfoResp(<<56, Rest/binary>>, Z1, 557 | Z2, F@_1, F@_2, F@_3, F@_4, F@_5, F@_6, F@_7, 558 | TrUserData) -> 559 | d_field_userInfoResp_user_diamond(Rest, 560 | Z1, 561 | Z2, 562 | F@_1, 563 | F@_2, 564 | F@_3, 565 | F@_4, 566 | F@_5, 567 | F@_6, 568 | F@_7, 569 | TrUserData); 570 | dfp_read_field_def_userInfoResp(<<>>, 0, 0, F@_1, F@_2, 571 | F@_3, F@_4, F@_5, F@_6, F@_7, _) -> 572 | #userInfoResp{user_id = F@_1, user_name = F@_2, 573 | user_img_id = F@_3, user_rank = F@_4, 574 | user_account_experience = F@_5, user_gold = F@_6, 575 | user_diamond = F@_7}; 576 | dfp_read_field_def_userInfoResp(Other, Z1, Z2, F@_1, 577 | F@_2, F@_3, F@_4, F@_5, F@_6, F@_7, 578 | TrUserData) -> 579 | dg_read_field_def_userInfoResp(Other, 580 | Z1, 581 | Z2, 582 | F@_1, 583 | F@_2, 584 | F@_3, 585 | F@_4, 586 | F@_5, 587 | F@_6, 588 | F@_7, 589 | TrUserData). 590 | 591 | dg_read_field_def_userInfoResp(<<1:1, X:7, 592 | Rest/binary>>, 593 | N, Acc, F@_1, F@_2, F@_3, F@_4, F@_5, F@_6, F@_7, 594 | TrUserData) 595 | when N < 32 - 7 -> 596 | dg_read_field_def_userInfoResp(Rest, 597 | N + 7, 598 | X bsl N + Acc, 599 | F@_1, 600 | F@_2, 601 | F@_3, 602 | F@_4, 603 | F@_5, 604 | F@_6, 605 | F@_7, 606 | TrUserData); 607 | dg_read_field_def_userInfoResp(<<0:1, X:7, 608 | Rest/binary>>, 609 | N, Acc, F@_1, F@_2, F@_3, F@_4, F@_5, F@_6, F@_7, 610 | TrUserData) -> 611 | Key = X bsl N + Acc, 612 | case Key of 613 | 8 -> 614 | d_field_userInfoResp_user_id(Rest, 615 | 0, 616 | 0, 617 | F@_1, 618 | F@_2, 619 | F@_3, 620 | F@_4, 621 | F@_5, 622 | F@_6, 623 | F@_7, 624 | TrUserData); 625 | 18 -> 626 | d_field_userInfoResp_user_name(Rest, 627 | 0, 628 | 0, 629 | F@_1, 630 | F@_2, 631 | F@_3, 632 | F@_4, 633 | F@_5, 634 | F@_6, 635 | F@_7, 636 | TrUserData); 637 | 24 -> 638 | d_field_userInfoResp_user_img_id(Rest, 639 | 0, 640 | 0, 641 | F@_1, 642 | F@_2, 643 | F@_3, 644 | F@_4, 645 | F@_5, 646 | F@_6, 647 | F@_7, 648 | TrUserData); 649 | 32 -> 650 | d_field_userInfoResp_user_rank(Rest, 651 | 0, 652 | 0, 653 | F@_1, 654 | F@_2, 655 | F@_3, 656 | F@_4, 657 | F@_5, 658 | F@_6, 659 | F@_7, 660 | TrUserData); 661 | 40 -> 662 | d_field_userInfoResp_user_account_experience(Rest, 663 | 0, 664 | 0, 665 | F@_1, 666 | F@_2, 667 | F@_3, 668 | F@_4, 669 | F@_5, 670 | F@_6, 671 | F@_7, 672 | TrUserData); 673 | 48 -> 674 | d_field_userInfoResp_user_gold(Rest, 675 | 0, 676 | 0, 677 | F@_1, 678 | F@_2, 679 | F@_3, 680 | F@_4, 681 | F@_5, 682 | F@_6, 683 | F@_7, 684 | TrUserData); 685 | 56 -> 686 | d_field_userInfoResp_user_diamond(Rest, 687 | 0, 688 | 0, 689 | F@_1, 690 | F@_2, 691 | F@_3, 692 | F@_4, 693 | F@_5, 694 | F@_6, 695 | F@_7, 696 | TrUserData); 697 | _ -> 698 | case Key band 7 of 699 | 0 -> 700 | skip_varint_userInfoResp(Rest, 701 | 0, 702 | 0, 703 | F@_1, 704 | F@_2, 705 | F@_3, 706 | F@_4, 707 | F@_5, 708 | F@_6, 709 | F@_7, 710 | TrUserData); 711 | 1 -> 712 | skip_64_userInfoResp(Rest, 713 | 0, 714 | 0, 715 | F@_1, 716 | F@_2, 717 | F@_3, 718 | F@_4, 719 | F@_5, 720 | F@_6, 721 | F@_7, 722 | TrUserData); 723 | 2 -> 724 | skip_length_delimited_userInfoResp(Rest, 725 | 0, 726 | 0, 727 | F@_1, 728 | F@_2, 729 | F@_3, 730 | F@_4, 731 | F@_5, 732 | F@_6, 733 | F@_7, 734 | TrUserData); 735 | 3 -> 736 | skip_group_userInfoResp(Rest, 737 | Key bsr 3, 738 | 0, 739 | F@_1, 740 | F@_2, 741 | F@_3, 742 | F@_4, 743 | F@_5, 744 | F@_6, 745 | F@_7, 746 | TrUserData); 747 | 5 -> 748 | skip_32_userInfoResp(Rest, 749 | 0, 750 | 0, 751 | F@_1, 752 | F@_2, 753 | F@_3, 754 | F@_4, 755 | F@_5, 756 | F@_6, 757 | F@_7, 758 | TrUserData) 759 | end 760 | end; 761 | dg_read_field_def_userInfoResp(<<>>, 0, 0, F@_1, F@_2, 762 | F@_3, F@_4, F@_5, F@_6, F@_7, _) -> 763 | #userInfoResp{user_id = F@_1, user_name = F@_2, 764 | user_img_id = F@_3, user_rank = F@_4, 765 | user_account_experience = F@_5, user_gold = F@_6, 766 | user_diamond = F@_7}. 767 | 768 | d_field_userInfoResp_user_id(<<1:1, X:7, Rest/binary>>, 769 | N, Acc, F@_1, F@_2, F@_3, F@_4, F@_5, F@_6, F@_7, 770 | TrUserData) 771 | when N < 57 -> 772 | d_field_userInfoResp_user_id(Rest, 773 | N + 7, 774 | X bsl N + Acc, 775 | F@_1, 776 | F@_2, 777 | F@_3, 778 | F@_4, 779 | F@_5, 780 | F@_6, 781 | F@_7, 782 | TrUserData); 783 | d_field_userInfoResp_user_id(<<0:1, X:7, Rest/binary>>, 784 | N, Acc, _, F@_2, F@_3, F@_4, F@_5, F@_6, F@_7, 785 | TrUserData) -> 786 | {NewFValue, RestF} = {id(X bsl N + Acc, TrUserData), 787 | Rest}, 788 | dfp_read_field_def_userInfoResp(RestF, 789 | 0, 790 | 0, 791 | NewFValue, 792 | F@_2, 793 | F@_3, 794 | F@_4, 795 | F@_5, 796 | F@_6, 797 | F@_7, 798 | TrUserData). 799 | 800 | d_field_userInfoResp_user_name(<<1:1, X:7, 801 | Rest/binary>>, 802 | N, Acc, F@_1, F@_2, F@_3, F@_4, F@_5, F@_6, F@_7, 803 | TrUserData) 804 | when N < 57 -> 805 | d_field_userInfoResp_user_name(Rest, 806 | N + 7, 807 | X bsl N + Acc, 808 | F@_1, 809 | F@_2, 810 | F@_3, 811 | F@_4, 812 | F@_5, 813 | F@_6, 814 | F@_7, 815 | TrUserData); 816 | d_field_userInfoResp_user_name(<<0:1, X:7, 817 | Rest/binary>>, 818 | N, Acc, F@_1, _, F@_3, F@_4, F@_5, F@_6, F@_7, 819 | TrUserData) -> 820 | {NewFValue, RestF} = begin 821 | Len = X bsl N + Acc, 822 | <> = Rest, 823 | {id(binary:copy(Bytes), TrUserData), Rest2} 824 | end, 825 | dfp_read_field_def_userInfoResp(RestF, 826 | 0, 827 | 0, 828 | F@_1, 829 | NewFValue, 830 | F@_3, 831 | F@_4, 832 | F@_5, 833 | F@_6, 834 | F@_7, 835 | TrUserData). 836 | 837 | d_field_userInfoResp_user_img_id(<<1:1, X:7, 838 | Rest/binary>>, 839 | N, Acc, F@_1, F@_2, F@_3, F@_4, F@_5, F@_6, 840 | F@_7, TrUserData) 841 | when N < 57 -> 842 | d_field_userInfoResp_user_img_id(Rest, 843 | N + 7, 844 | X bsl N + Acc, 845 | F@_1, 846 | F@_2, 847 | F@_3, 848 | F@_4, 849 | F@_5, 850 | F@_6, 851 | F@_7, 852 | TrUserData); 853 | d_field_userInfoResp_user_img_id(<<0:1, X:7, 854 | Rest/binary>>, 855 | N, Acc, F@_1, F@_2, _, F@_4, F@_5, F@_6, F@_7, 856 | TrUserData) -> 857 | {NewFValue, RestF} = {id(X bsl N + Acc, TrUserData), 858 | Rest}, 859 | dfp_read_field_def_userInfoResp(RestF, 860 | 0, 861 | 0, 862 | F@_1, 863 | F@_2, 864 | NewFValue, 865 | F@_4, 866 | F@_5, 867 | F@_6, 868 | F@_7, 869 | TrUserData). 870 | 871 | d_field_userInfoResp_user_rank(<<1:1, X:7, 872 | Rest/binary>>, 873 | N, Acc, F@_1, F@_2, F@_3, F@_4, F@_5, F@_6, F@_7, 874 | TrUserData) 875 | when N < 57 -> 876 | d_field_userInfoResp_user_rank(Rest, 877 | N + 7, 878 | X bsl N + Acc, 879 | F@_1, 880 | F@_2, 881 | F@_3, 882 | F@_4, 883 | F@_5, 884 | F@_6, 885 | F@_7, 886 | TrUserData); 887 | d_field_userInfoResp_user_rank(<<0:1, X:7, 888 | Rest/binary>>, 889 | N, Acc, F@_1, F@_2, F@_3, _, F@_5, F@_6, F@_7, 890 | TrUserData) -> 891 | {NewFValue, RestF} = {id(X bsl N + Acc, TrUserData), 892 | Rest}, 893 | dfp_read_field_def_userInfoResp(RestF, 894 | 0, 895 | 0, 896 | F@_1, 897 | F@_2, 898 | F@_3, 899 | NewFValue, 900 | F@_5, 901 | F@_6, 902 | F@_7, 903 | TrUserData). 904 | 905 | d_field_userInfoResp_user_account_experience(<<1:1, X:7, 906 | Rest/binary>>, 907 | N, Acc, F@_1, F@_2, F@_3, F@_4, 908 | F@_5, F@_6, F@_7, TrUserData) 909 | when N < 57 -> 910 | d_field_userInfoResp_user_account_experience(Rest, 911 | N + 7, 912 | X bsl N + Acc, 913 | F@_1, 914 | F@_2, 915 | F@_3, 916 | F@_4, 917 | F@_5, 918 | F@_6, 919 | F@_7, 920 | TrUserData); 921 | d_field_userInfoResp_user_account_experience(<<0:1, X:7, 922 | Rest/binary>>, 923 | N, Acc, F@_1, F@_2, F@_3, F@_4, _, 924 | F@_6, F@_7, TrUserData) -> 925 | {NewFValue, RestF} = {id(X bsl N + Acc, TrUserData), 926 | Rest}, 927 | dfp_read_field_def_userInfoResp(RestF, 928 | 0, 929 | 0, 930 | F@_1, 931 | F@_2, 932 | F@_3, 933 | F@_4, 934 | NewFValue, 935 | F@_6, 936 | F@_7, 937 | TrUserData). 938 | 939 | d_field_userInfoResp_user_gold(<<1:1, X:7, 940 | Rest/binary>>, 941 | N, Acc, F@_1, F@_2, F@_3, F@_4, F@_5, F@_6, F@_7, 942 | TrUserData) 943 | when N < 57 -> 944 | d_field_userInfoResp_user_gold(Rest, 945 | N + 7, 946 | X bsl N + Acc, 947 | F@_1, 948 | F@_2, 949 | F@_3, 950 | F@_4, 951 | F@_5, 952 | F@_6, 953 | F@_7, 954 | TrUserData); 955 | d_field_userInfoResp_user_gold(<<0:1, X:7, 956 | Rest/binary>>, 957 | N, Acc, F@_1, F@_2, F@_3, F@_4, F@_5, _, F@_7, 958 | TrUserData) -> 959 | {NewFValue, RestF} = {id(X bsl N + Acc, TrUserData), 960 | Rest}, 961 | dfp_read_field_def_userInfoResp(RestF, 962 | 0, 963 | 0, 964 | F@_1, 965 | F@_2, 966 | F@_3, 967 | F@_4, 968 | F@_5, 969 | NewFValue, 970 | F@_7, 971 | TrUserData). 972 | 973 | d_field_userInfoResp_user_diamond(<<1:1, X:7, 974 | Rest/binary>>, 975 | N, Acc, F@_1, F@_2, F@_3, F@_4, F@_5, F@_6, 976 | F@_7, TrUserData) 977 | when N < 57 -> 978 | d_field_userInfoResp_user_diamond(Rest, 979 | N + 7, 980 | X bsl N + Acc, 981 | F@_1, 982 | F@_2, 983 | F@_3, 984 | F@_4, 985 | F@_5, 986 | F@_6, 987 | F@_7, 988 | TrUserData); 989 | d_field_userInfoResp_user_diamond(<<0:1, X:7, 990 | Rest/binary>>, 991 | N, Acc, F@_1, F@_2, F@_3, F@_4, F@_5, F@_6, _, 992 | TrUserData) -> 993 | {NewFValue, RestF} = {id(X bsl N + Acc, TrUserData), 994 | Rest}, 995 | dfp_read_field_def_userInfoResp(RestF, 996 | 0, 997 | 0, 998 | F@_1, 999 | F@_2, 1000 | F@_3, 1001 | F@_4, 1002 | F@_5, 1003 | F@_6, 1004 | NewFValue, 1005 | TrUserData). 1006 | 1007 | skip_varint_userInfoResp(<<1:1, _:7, Rest/binary>>, Z1, 1008 | Z2, F@_1, F@_2, F@_3, F@_4, F@_5, F@_6, F@_7, 1009 | TrUserData) -> 1010 | skip_varint_userInfoResp(Rest, 1011 | Z1, 1012 | Z2, 1013 | F@_1, 1014 | F@_2, 1015 | F@_3, 1016 | F@_4, 1017 | F@_5, 1018 | F@_6, 1019 | F@_7, 1020 | TrUserData); 1021 | skip_varint_userInfoResp(<<0:1, _:7, Rest/binary>>, Z1, 1022 | Z2, F@_1, F@_2, F@_3, F@_4, F@_5, F@_6, F@_7, 1023 | TrUserData) -> 1024 | dfp_read_field_def_userInfoResp(Rest, 1025 | Z1, 1026 | Z2, 1027 | F@_1, 1028 | F@_2, 1029 | F@_3, 1030 | F@_4, 1031 | F@_5, 1032 | F@_6, 1033 | F@_7, 1034 | TrUserData). 1035 | 1036 | skip_length_delimited_userInfoResp(<<1:1, X:7, 1037 | Rest/binary>>, 1038 | N, Acc, F@_1, F@_2, F@_3, F@_4, F@_5, F@_6, 1039 | F@_7, TrUserData) 1040 | when N < 57 -> 1041 | skip_length_delimited_userInfoResp(Rest, 1042 | N + 7, 1043 | X bsl N + Acc, 1044 | F@_1, 1045 | F@_2, 1046 | F@_3, 1047 | F@_4, 1048 | F@_5, 1049 | F@_6, 1050 | F@_7, 1051 | TrUserData); 1052 | skip_length_delimited_userInfoResp(<<0:1, X:7, 1053 | Rest/binary>>, 1054 | N, Acc, F@_1, F@_2, F@_3, F@_4, F@_5, F@_6, 1055 | F@_7, TrUserData) -> 1056 | Length = X bsl N + Acc, 1057 | <<_:Length/binary, Rest2/binary>> = Rest, 1058 | dfp_read_field_def_userInfoResp(Rest2, 1059 | 0, 1060 | 0, 1061 | F@_1, 1062 | F@_2, 1063 | F@_3, 1064 | F@_4, 1065 | F@_5, 1066 | F@_6, 1067 | F@_7, 1068 | TrUserData). 1069 | 1070 | skip_group_userInfoResp(Bin, FNum, Z2, F@_1, F@_2, F@_3, 1071 | F@_4, F@_5, F@_6, F@_7, TrUserData) -> 1072 | {_, Rest} = read_group(Bin, FNum), 1073 | dfp_read_field_def_userInfoResp(Rest, 1074 | 0, 1075 | Z2, 1076 | F@_1, 1077 | F@_2, 1078 | F@_3, 1079 | F@_4, 1080 | F@_5, 1081 | F@_6, 1082 | F@_7, 1083 | TrUserData). 1084 | 1085 | skip_32_userInfoResp(<<_:32, Rest/binary>>, Z1, Z2, 1086 | F@_1, F@_2, F@_3, F@_4, F@_5, F@_6, F@_7, TrUserData) -> 1087 | dfp_read_field_def_userInfoResp(Rest, 1088 | Z1, 1089 | Z2, 1090 | F@_1, 1091 | F@_2, 1092 | F@_3, 1093 | F@_4, 1094 | F@_5, 1095 | F@_6, 1096 | F@_7, 1097 | TrUserData). 1098 | 1099 | skip_64_userInfoResp(<<_:64, Rest/binary>>, Z1, Z2, 1100 | F@_1, F@_2, F@_3, F@_4, F@_5, F@_6, F@_7, TrUserData) -> 1101 | dfp_read_field_def_userInfoResp(Rest, 1102 | Z1, 1103 | Z2, 1104 | F@_1, 1105 | F@_2, 1106 | F@_3, 1107 | F@_4, 1108 | F@_5, 1109 | F@_6, 1110 | F@_7, 1111 | TrUserData). 1112 | 1113 | read_group(Bin, FieldNum) -> 1114 | {NumBytes, EndTagLen} = read_gr_b(Bin, 0, 0, 0, 0, FieldNum), 1115 | <> = Bin, 1116 | {Group, Rest}. 1117 | 1118 | %% Like skipping over fields, but record the total length, 1119 | %% Each field is <(FieldNum bsl 3) bor FieldType> ++ 1120 | %% Record the length because varints may be non-optimally encoded. 1121 | %% 1122 | %% Groups can be nested, but assume the same FieldNum cannot be nested 1123 | %% because group field numbers are shared with the rest of the fields 1124 | %% numbers. Thus we can search just for an group-end with the same 1125 | %% field number. 1126 | %% 1127 | %% (The only time the same group field number could occur would 1128 | %% be in a nested sub message, but then it would be inside a 1129 | %% length-delimited entry, which we skip-read by length.) 1130 | read_gr_b(<<1:1, X:7, Tl/binary>>, N, Acc, NumBytes, TagLen, FieldNum) 1131 | when N < (32-7) -> 1132 | read_gr_b(Tl, N+7, X bsl N + Acc, NumBytes, TagLen+1, FieldNum); 1133 | read_gr_b(<<0:1, X:7, Tl/binary>>, N, Acc, NumBytes, TagLen, 1134 | FieldNum) -> 1135 | Key = X bsl N + Acc, 1136 | TagLen1 = TagLen + 1, 1137 | case {Key bsr 3, Key band 7} of 1138 | {FieldNum, 4} -> % 4 = group_end 1139 | {NumBytes, TagLen1}; 1140 | {_, 0} -> % 0 = varint 1141 | read_gr_vi(Tl, 0, NumBytes + TagLen1, FieldNum); 1142 | {_, 1} -> % 1 = bits64 1143 | <<_:64, Tl2/binary>> = Tl, 1144 | read_gr_b(Tl2, 0, 0, NumBytes + TagLen1 + 8, 0, FieldNum); 1145 | {_, 2} -> % 2 = length_delimited 1146 | read_gr_ld(Tl, 0, 0, NumBytes + TagLen1, FieldNum); 1147 | {_, 3} -> % 3 = group_start 1148 | read_gr_b(Tl, 0, 0, NumBytes + TagLen1, 0, FieldNum); 1149 | {_, 4} -> % 4 = group_end 1150 | read_gr_b(Tl, 0, 0, NumBytes + TagLen1, 0, FieldNum); 1151 | {_, 5} -> % 5 = bits32 1152 | <<_:32, Tl2/binary>> = Tl, 1153 | read_gr_b(Tl2, 0, 0, NumBytes + TagLen1 + 4, 0, FieldNum) 1154 | end. 1155 | 1156 | read_gr_vi(<<1:1, _:7, Tl/binary>>, N, NumBytes, FieldNum) 1157 | when N < (64-7) -> 1158 | read_gr_vi(Tl, N+7, NumBytes+1, FieldNum); 1159 | read_gr_vi(<<0:1, _:7, Tl/binary>>, _, NumBytes, FieldNum) -> 1160 | read_gr_b(Tl, 0, 0, NumBytes+1, 0, FieldNum). 1161 | 1162 | read_gr_ld(<<1:1, X:7, Tl/binary>>, N, Acc, NumBytes, FieldNum) 1163 | when N < (64-7) -> 1164 | read_gr_ld(Tl, N+7, X bsl N + Acc, NumBytes+1, FieldNum); 1165 | read_gr_ld(<<0:1, X:7, Tl/binary>>, N, Acc, NumBytes, FieldNum) -> 1166 | Len = X bsl N + Acc, 1167 | NumBytes1 = NumBytes + 1, 1168 | <<_:Len/binary, Tl2/binary>> = Tl, 1169 | read_gr_b(Tl2, 0, 0, NumBytes1 + Len, 0, FieldNum). 1170 | 1171 | merge_msgs(Prev, New) 1172 | when element(1, Prev) =:= element(1, New) -> 1173 | merge_msgs(Prev, New, element(1, Prev), []). 1174 | 1175 | merge_msgs(Prev, New, MsgName) when is_atom(MsgName) -> 1176 | merge_msgs(Prev, New, MsgName, []); 1177 | merge_msgs(Prev, New, Opts) 1178 | when element(1, Prev) =:= element(1, New), 1179 | is_list(Opts) -> 1180 | merge_msgs(Prev, New, element(1, Prev), Opts). 1181 | 1182 | merge_msgs(Prev, New, MsgName, Opts) -> 1183 | TrUserData = proplists:get_value(user_data, Opts), 1184 | case MsgName of 1185 | userInfoReq -> 1186 | merge_msg_userInfoReq(Prev, New, TrUserData); 1187 | userInfoResp -> 1188 | merge_msg_userInfoResp(Prev, New, TrUserData) 1189 | end. 1190 | 1191 | -compile({nowarn_unused_function,merge_msg_userInfoReq/3}). 1192 | merge_msg_userInfoReq(#userInfoReq{user_id = PFuser_id}, 1193 | #userInfoReq{user_id = NFuser_id}, _) -> 1194 | #userInfoReq{user_id = 1195 | if NFuser_id =:= undefined -> PFuser_id; 1196 | true -> NFuser_id 1197 | end}. 1198 | 1199 | -compile({nowarn_unused_function,merge_msg_userInfoResp/3}). 1200 | merge_msg_userInfoResp(#userInfoResp{user_id = 1201 | PFuser_id, 1202 | user_name = PFuser_name, 1203 | user_img_id = PFuser_img_id, 1204 | user_rank = PFuser_rank, 1205 | user_account_experience = 1206 | PFuser_account_experience, 1207 | user_gold = PFuser_gold, 1208 | user_diamond = PFuser_diamond}, 1209 | #userInfoResp{user_id = NFuser_id, 1210 | user_name = NFuser_name, 1211 | user_img_id = NFuser_img_id, 1212 | user_rank = NFuser_rank, 1213 | user_account_experience = 1214 | NFuser_account_experience, 1215 | user_gold = NFuser_gold, 1216 | user_diamond = NFuser_diamond}, 1217 | _) -> 1218 | #userInfoResp{user_id = 1219 | if NFuser_id =:= undefined -> PFuser_id; 1220 | true -> NFuser_id 1221 | end, 1222 | user_name = 1223 | if NFuser_name =:= undefined -> PFuser_name; 1224 | true -> NFuser_name 1225 | end, 1226 | user_img_id = 1227 | if NFuser_img_id =:= undefined -> PFuser_img_id; 1228 | true -> NFuser_img_id 1229 | end, 1230 | user_rank = 1231 | if NFuser_rank =:= undefined -> PFuser_rank; 1232 | true -> NFuser_rank 1233 | end, 1234 | user_account_experience = 1235 | if NFuser_account_experience =:= undefined -> 1236 | PFuser_account_experience; 1237 | true -> NFuser_account_experience 1238 | end, 1239 | user_gold = 1240 | if NFuser_gold =:= undefined -> PFuser_gold; 1241 | true -> NFuser_gold 1242 | end, 1243 | user_diamond = 1244 | if NFuser_diamond =:= undefined -> PFuser_diamond; 1245 | true -> NFuser_diamond 1246 | end}. 1247 | 1248 | 1249 | verify_msg(Msg) when tuple_size(Msg) >= 1 -> 1250 | verify_msg(Msg, element(1, Msg), []); 1251 | verify_msg(X) -> 1252 | mk_type_error(not_a_known_message, X, []). 1253 | 1254 | verify_msg(Msg, MsgName) when is_atom(MsgName) -> 1255 | verify_msg(Msg, MsgName, []); 1256 | verify_msg(Msg, Opts) when tuple_size(Msg) >= 1 -> 1257 | verify_msg(Msg, element(1, Msg), Opts); 1258 | verify_msg(X, _Opts) -> 1259 | mk_type_error(not_a_known_message, X, []). 1260 | 1261 | verify_msg(Msg, MsgName, Opts) -> 1262 | TrUserData = proplists:get_value(user_data, Opts), 1263 | case MsgName of 1264 | userInfoReq -> 1265 | v_msg_userInfoReq(Msg, [MsgName], TrUserData); 1266 | userInfoResp -> 1267 | v_msg_userInfoResp(Msg, [MsgName], TrUserData); 1268 | _ -> mk_type_error(not_a_known_message, Msg, []) 1269 | end. 1270 | 1271 | 1272 | -compile({nowarn_unused_function,v_msg_userInfoReq/3}). 1273 | -dialyzer({nowarn_function,v_msg_userInfoReq/3}). 1274 | v_msg_userInfoReq(#userInfoReq{user_id = F1}, Path, 1275 | TrUserData) -> 1276 | if F1 == undefined -> ok; 1277 | true -> v_type_uint64(F1, [user_id | Path], TrUserData) 1278 | end, 1279 | ok; 1280 | v_msg_userInfoReq(X, Path, _TrUserData) -> 1281 | mk_type_error({expected_msg, userInfoReq}, X, Path). 1282 | 1283 | -compile({nowarn_unused_function,v_msg_userInfoResp/3}). 1284 | -dialyzer({nowarn_function,v_msg_userInfoResp/3}). 1285 | v_msg_userInfoResp(#userInfoResp{user_id = F1, 1286 | user_name = F2, user_img_id = F3, 1287 | user_rank = F4, user_account_experience = F5, 1288 | user_gold = F6, user_diamond = F7}, 1289 | Path, TrUserData) -> 1290 | if F1 == undefined -> ok; 1291 | true -> v_type_uint64(F1, [user_id | Path], TrUserData) 1292 | end, 1293 | if F2 == undefined -> ok; 1294 | true -> 1295 | v_type_string(F2, [user_name | Path], TrUserData) 1296 | end, 1297 | if F3 == undefined -> ok; 1298 | true -> 1299 | v_type_uint32(F3, [user_img_id | Path], TrUserData) 1300 | end, 1301 | if F4 == undefined -> ok; 1302 | true -> 1303 | v_type_uint32(F4, [user_rank | Path], TrUserData) 1304 | end, 1305 | if F5 == undefined -> ok; 1306 | true -> 1307 | v_type_uint64(F5, 1308 | [user_account_experience | Path], 1309 | TrUserData) 1310 | end, 1311 | if F6 == undefined -> ok; 1312 | true -> 1313 | v_type_uint64(F6, [user_gold | Path], TrUserData) 1314 | end, 1315 | if F7 == undefined -> ok; 1316 | true -> 1317 | v_type_uint64(F7, [user_diamond | Path], TrUserData) 1318 | end, 1319 | ok; 1320 | v_msg_userInfoResp(X, Path, _TrUserData) -> 1321 | mk_type_error({expected_msg, userInfoResp}, X, Path). 1322 | 1323 | -compile({nowarn_unused_function,v_type_uint32/3}). 1324 | -dialyzer({nowarn_function,v_type_uint32/3}). 1325 | v_type_uint32(N, _Path, _TrUserData) 1326 | when 0 =< N, N =< 4294967295 -> 1327 | ok; 1328 | v_type_uint32(N, Path, _TrUserData) 1329 | when is_integer(N) -> 1330 | mk_type_error({value_out_of_range, 1331 | uint32, 1332 | unsigned, 1333 | 32}, 1334 | N, 1335 | Path); 1336 | v_type_uint32(X, Path, _TrUserData) -> 1337 | mk_type_error({bad_integer, uint32, unsigned, 32}, 1338 | X, 1339 | Path). 1340 | 1341 | -compile({nowarn_unused_function,v_type_uint64/3}). 1342 | -dialyzer({nowarn_function,v_type_uint64/3}). 1343 | v_type_uint64(N, _Path, _TrUserData) 1344 | when 0 =< N, N =< 18446744073709551615 -> 1345 | ok; 1346 | v_type_uint64(N, Path, _TrUserData) 1347 | when is_integer(N) -> 1348 | mk_type_error({value_out_of_range, 1349 | uint64, 1350 | unsigned, 1351 | 64}, 1352 | N, 1353 | Path); 1354 | v_type_uint64(X, Path, _TrUserData) -> 1355 | mk_type_error({bad_integer, uint64, unsigned, 64}, 1356 | X, 1357 | Path). 1358 | 1359 | -compile({nowarn_unused_function,v_type_string/3}). 1360 | -dialyzer({nowarn_function,v_type_string/3}). 1361 | v_type_string(S, Path, _TrUserData) 1362 | when is_list(S); is_binary(S) -> 1363 | try unicode:characters_to_binary(S) of 1364 | B when is_binary(B) -> ok; 1365 | {error, _, _} -> 1366 | mk_type_error(bad_unicode_string, S, Path) 1367 | catch 1368 | error:badarg -> 1369 | mk_type_error(bad_unicode_string, S, Path) 1370 | end; 1371 | v_type_string(X, Path, _TrUserData) -> 1372 | mk_type_error(bad_unicode_string, X, Path). 1373 | 1374 | -compile({nowarn_unused_function,mk_type_error/3}). 1375 | -spec mk_type_error(_, _, list()) -> no_return(). 1376 | mk_type_error(Error, ValueSeen, Path) -> 1377 | Path2 = prettify_path(Path), 1378 | erlang:error({gpb_type_error, 1379 | {Error, [{value, ValueSeen}, {path, Path2}]}}). 1380 | 1381 | 1382 | -compile({nowarn_unused_function,prettify_path/1}). 1383 | -dialyzer({nowarn_function,prettify_path/1}). 1384 | prettify_path([]) -> top_level; 1385 | prettify_path(PathR) -> 1386 | list_to_atom(lists:append(lists:join(".", 1387 | lists:map(fun atom_to_list/1, 1388 | lists:reverse(PathR))))). 1389 | 1390 | 1391 | -compile({nowarn_unused_function,id/2}). 1392 | -compile({inline,id/2}). 1393 | id(X, _TrUserData) -> X. 1394 | 1395 | -compile({nowarn_unused_function,v_ok/3}). 1396 | -compile({inline,v_ok/3}). 1397 | v_ok(_Value, _Path, _TrUserData) -> ok. 1398 | 1399 | -compile({nowarn_unused_function,m_overwrite/3}). 1400 | -compile({inline,m_overwrite/3}). 1401 | m_overwrite(_Prev, New, _TrUserData) -> New. 1402 | 1403 | -compile({nowarn_unused_function,cons/3}). 1404 | -compile({inline,cons/3}). 1405 | cons(Elem, Acc, _TrUserData) -> [Elem | Acc]. 1406 | 1407 | -compile({nowarn_unused_function,lists_reverse/2}). 1408 | -compile({inline,lists_reverse/2}). 1409 | 'lists_reverse'(L, _TrUserData) -> lists:reverse(L). 1410 | -compile({nowarn_unused_function,'erlang_++'/3}). 1411 | -compile({inline,'erlang_++'/3}). 1412 | 'erlang_++'(A, B, _TrUserData) -> A ++ B. 1413 | 1414 | 1415 | get_msg_defs() -> 1416 | [{{msg, userInfoReq}, 1417 | [#field{name = user_id, fnum = 1, rnum = 2, 1418 | type = uint64, occurrence = optional, opts = []}]}, 1419 | {{msg, userInfoResp}, 1420 | [#field{name = user_id, fnum = 1, rnum = 2, 1421 | type = uint64, occurrence = optional, opts = []}, 1422 | #field{name = user_name, fnum = 2, rnum = 3, 1423 | type = string, occurrence = optional, opts = []}, 1424 | #field{name = user_img_id, fnum = 3, rnum = 4, 1425 | type = uint32, occurrence = optional, opts = []}, 1426 | #field{name = user_rank, fnum = 4, rnum = 5, 1427 | type = uint32, occurrence = optional, opts = []}, 1428 | #field{name = user_account_experience, fnum = 5, 1429 | rnum = 6, type = uint64, occurrence = optional, 1430 | opts = []}, 1431 | #field{name = user_gold, fnum = 6, rnum = 7, 1432 | type = uint64, occurrence = optional, opts = []}, 1433 | #field{name = user_diamond, fnum = 7, rnum = 8, 1434 | type = uint64, occurrence = optional, opts = []}]}]. 1435 | 1436 | 1437 | get_msg_names() -> [userInfoReq, userInfoResp]. 1438 | 1439 | 1440 | get_group_names() -> []. 1441 | 1442 | 1443 | get_msg_or_group_names() -> [userInfoReq, userInfoResp]. 1444 | 1445 | 1446 | get_enum_names() -> []. 1447 | 1448 | 1449 | fetch_msg_def(MsgName) -> 1450 | case find_msg_def(MsgName) of 1451 | Fs when is_list(Fs) -> Fs; 1452 | error -> erlang:error({no_such_msg, MsgName}) 1453 | end. 1454 | 1455 | 1456 | -spec fetch_enum_def(_) -> no_return(). 1457 | fetch_enum_def(EnumName) -> 1458 | erlang:error({no_such_enum, EnumName}). 1459 | 1460 | 1461 | find_msg_def(userInfoReq) -> 1462 | [#field{name = user_id, fnum = 1, rnum = 2, 1463 | type = uint64, occurrence = optional, opts = []}]; 1464 | find_msg_def(userInfoResp) -> 1465 | [#field{name = user_id, fnum = 1, rnum = 2, 1466 | type = uint64, occurrence = optional, opts = []}, 1467 | #field{name = user_name, fnum = 2, rnum = 3, 1468 | type = string, occurrence = optional, opts = []}, 1469 | #field{name = user_img_id, fnum = 3, rnum = 4, 1470 | type = uint32, occurrence = optional, opts = []}, 1471 | #field{name = user_rank, fnum = 4, rnum = 5, 1472 | type = uint32, occurrence = optional, opts = []}, 1473 | #field{name = user_account_experience, fnum = 5, 1474 | rnum = 6, type = uint64, occurrence = optional, 1475 | opts = []}, 1476 | #field{name = user_gold, fnum = 6, rnum = 7, 1477 | type = uint64, occurrence = optional, opts = []}, 1478 | #field{name = user_diamond, fnum = 7, rnum = 8, 1479 | type = uint64, occurrence = optional, opts = []}]; 1480 | find_msg_def(_) -> error. 1481 | 1482 | 1483 | find_enum_def(_) -> error. 1484 | 1485 | 1486 | -spec enum_symbol_by_value(_, _) -> no_return(). 1487 | enum_symbol_by_value(E, V) -> 1488 | erlang:error({no_enum_defs, E, V}). 1489 | 1490 | 1491 | -spec enum_value_by_symbol(_, _) -> no_return(). 1492 | enum_value_by_symbol(E, V) -> 1493 | erlang:error({no_enum_defs, E, V}). 1494 | 1495 | 1496 | 1497 | get_service_names() -> []. 1498 | 1499 | 1500 | get_service_def(_) -> error. 1501 | 1502 | 1503 | get_rpc_names(_) -> error. 1504 | 1505 | 1506 | find_rpc_def(_, _) -> error. 1507 | 1508 | 1509 | 1510 | -spec fetch_rpc_def(_, _) -> no_return(). 1511 | fetch_rpc_def(ServiceName, RpcName) -> 1512 | erlang:error({no_such_rpc, ServiceName, RpcName}). 1513 | 1514 | 1515 | %% Convert a a fully qualified (ie with package name) service name 1516 | %% as a binary to a service name as an atom. 1517 | -spec fqbin_to_service_name(_) -> no_return(). 1518 | fqbin_to_service_name(X) -> 1519 | error({gpb_error, {badservice, X}}). 1520 | 1521 | 1522 | %% Convert a service name as an atom to a fully qualified 1523 | %% (ie with package name) name as a binary. 1524 | -spec service_name_to_fqbin(_) -> no_return(). 1525 | service_name_to_fqbin(X) -> 1526 | error({gpb_error, {badservice, X}}). 1527 | 1528 | 1529 | %% Convert a a fully qualified (ie with package name) service name 1530 | %% and an rpc name, both as binaries to a service name and an rpc 1531 | %% name, as atoms. 1532 | -spec fqbins_to_service_and_rpc_name(_, _) -> no_return(). 1533 | fqbins_to_service_and_rpc_name(S, R) -> 1534 | error({gpb_error, {badservice_or_rpc, {S, R}}}). 1535 | 1536 | 1537 | %% Convert a service name and an rpc name, both as atoms, 1538 | %% to a fully qualified (ie with package name) service name and 1539 | %% an rpc name as binaries. 1540 | -spec service_and_rpc_name_to_fqbins(_, _) -> no_return(). 1541 | service_and_rpc_name_to_fqbins(S, R) -> 1542 | error({gpb_error, {badservice_or_rpc, {S, R}}}). 1543 | 1544 | 1545 | fqbin_to_msg_name(<<"maininterface.proto.userInfoReq">>) -> userInfoReq; 1546 | fqbin_to_msg_name(<<"maininterface.proto.userInfoResp">>) -> userInfoResp; 1547 | fqbin_to_msg_name(E) -> error({gpb_error, {badmsg, E}}). 1548 | 1549 | 1550 | msg_name_to_fqbin(userInfoReq) -> <<"maininterface.proto.userInfoReq">>; 1551 | msg_name_to_fqbin(userInfoResp) -> <<"maininterface.proto.userInfoResp">>; 1552 | msg_name_to_fqbin(E) -> error({gpb_error, {badmsg, E}}). 1553 | 1554 | 1555 | -spec fqbin_to_enum_name(_) -> no_return(). 1556 | fqbin_to_enum_name(E) -> 1557 | error({gpb_error, {badenum, E}}). 1558 | 1559 | 1560 | -spec enum_name_to_fqbin(_) -> no_return(). 1561 | enum_name_to_fqbin(E) -> 1562 | error({gpb_error, {badenum, E}}). 1563 | 1564 | 1565 | get_package_name() -> 'maininterface.proto'. 1566 | 1567 | 1568 | %% Whether or not the message names 1569 | %% are prepended with package name or not. 1570 | uses_packages() -> false. 1571 | 1572 | 1573 | source_basename() -> "maininterface.proto". 1574 | 1575 | 1576 | %% Retrieve all proto file names, also imported ones. 1577 | %% The order is top-down. The first element is always the main 1578 | %% source file. The files are returned with extension, 1579 | %% see get_all_proto_names/0 for a version that returns 1580 | %% the basenames sans extension 1581 | get_all_source_basenames() -> ["maininterface.proto"]. 1582 | 1583 | 1584 | %% Retrieve all proto file names, also imported ones. 1585 | %% The order is top-down. The first element is always the main 1586 | %% source file. The files are returned sans .proto extension, 1587 | %% to make it easier to use them with the various get_xyz_containment 1588 | %% functions. 1589 | get_all_proto_names() -> ["maininterface"]. 1590 | 1591 | 1592 | get_msg_containment("maininterface") -> 1593 | [userInfoReq, userInfoResp]; 1594 | get_msg_containment(P) -> 1595 | error({gpb_error, {badproto, P}}). 1596 | 1597 | 1598 | get_pkg_containment("maininterface") -> undefined; 1599 | get_pkg_containment(P) -> 1600 | error({gpb_error, {badproto, P}}). 1601 | 1602 | 1603 | get_service_containment("maininterface") -> []; 1604 | get_service_containment(P) -> 1605 | error({gpb_error, {badproto, P}}). 1606 | 1607 | 1608 | get_rpc_containment("maininterface") -> []; 1609 | get_rpc_containment(P) -> 1610 | error({gpb_error, {badproto, P}}). 1611 | 1612 | 1613 | get_enum_containment("maininterface") -> []; 1614 | get_enum_containment(P) -> 1615 | error({gpb_error, {badproto, P}}). 1616 | 1617 | 1618 | get_proto_by_msg_name_as_fqbin(<<"maininterface.proto.userInfoResp">>) -> 1619 | "maininterface"; 1620 | get_proto_by_msg_name_as_fqbin(<<"maininterface.proto.userInfoReq">>) -> 1621 | "maininterface"; 1622 | get_proto_by_msg_name_as_fqbin(E) -> 1623 | error({gpb_error, {badmsg, E}}). 1624 | 1625 | 1626 | -spec get_proto_by_service_name_as_fqbin(_) -> no_return(). 1627 | get_proto_by_service_name_as_fqbin(E) -> 1628 | error({gpb_error, {badservice, E}}). 1629 | 1630 | 1631 | -spec get_proto_by_enum_name_as_fqbin(_) -> no_return(). 1632 | get_proto_by_enum_name_as_fqbin(E) -> 1633 | error({gpb_error, {badenum, E}}). 1634 | 1635 | 1636 | -spec get_protos_by_pkg_name_as_fqbin(_) -> no_return(). 1637 | get_protos_by_pkg_name_as_fqbin(E) -> 1638 | error({gpb_error, {badpkg, E}}). 1639 | 1640 | 1641 | 1642 | gpb_version_as_string() -> 1643 | "4.10.1". 1644 | 1645 | gpb_version_as_list() -> 1646 | [4,10,1]. 1647 | -------------------------------------------------------------------------------- /apps/game_server/src/game_protobuf/common_pb.erl: -------------------------------------------------------------------------------- 1 | %% -*- coding: utf-8 -*- 2 | %% @private 3 | %% Automatically generated, do not edit 4 | %% Generated by gpb_compile version 4.10.1 5 | -module(common_pb). 6 | 7 | -export([encode_msg/1, encode_msg/2, encode_msg/3]). 8 | -export([decode_msg/2, decode_msg/3]). 9 | -export([merge_msgs/2, merge_msgs/3, merge_msgs/4]). 10 | -export([verify_msg/1, verify_msg/2, verify_msg/3]). 11 | -export([get_msg_defs/0]). 12 | -export([get_msg_names/0]). 13 | -export([get_group_names/0]). 14 | -export([get_msg_or_group_names/0]). 15 | -export([get_enum_names/0]). 16 | -export([find_msg_def/1, fetch_msg_def/1]). 17 | -export([find_enum_def/1, fetch_enum_def/1]). 18 | -export([enum_symbol_by_value/2, enum_value_by_symbol/2]). 19 | -export([enum_symbol_by_value_LoginStatus/1, enum_value_by_symbol_LoginStatus/1]). 20 | -export([get_service_names/0]). 21 | -export([get_service_def/1]). 22 | -export([get_rpc_names/1]). 23 | -export([find_rpc_def/2, fetch_rpc_def/2]). 24 | -export([fqbin_to_service_name/1]). 25 | -export([service_name_to_fqbin/1]). 26 | -export([fqbins_to_service_and_rpc_name/2]). 27 | -export([service_and_rpc_name_to_fqbins/2]). 28 | -export([fqbin_to_msg_name/1]). 29 | -export([msg_name_to_fqbin/1]). 30 | -export([fqbin_to_enum_name/1]). 31 | -export([enum_name_to_fqbin/1]). 32 | -export([get_package_name/0]). 33 | -export([uses_packages/0]). 34 | -export([source_basename/0]). 35 | -export([get_all_source_basenames/0]). 36 | -export([get_all_proto_names/0]). 37 | -export([get_msg_containment/1]). 38 | -export([get_pkg_containment/1]). 39 | -export([get_service_containment/1]). 40 | -export([get_rpc_containment/1]). 41 | -export([get_enum_containment/1]). 42 | -export([get_proto_by_msg_name_as_fqbin/1]). 43 | -export([get_proto_by_service_name_as_fqbin/1]). 44 | -export([get_proto_by_enum_name_as_fqbin/1]). 45 | -export([get_protos_by_pkg_name_as_fqbin/1]). 46 | -export([gpb_version_as_string/0, gpb_version_as_list/0]). 47 | 48 | -include("common_pb.hrl"). 49 | -include("gpb.hrl"). 50 | 51 | %% enumerated types 52 | -type 'LoginStatus'() :: 'FAILED' | 'SUCCEEDED' | 'NOTFOUND'. 53 | -export_type(['LoginStatus'/0]). 54 | 55 | %% message types 56 | -type loginReq() :: #loginReq{}. 57 | 58 | -type loginResp() :: #loginResp{}. 59 | 60 | -type helloReq() :: #helloReq{}. 61 | 62 | -type worldResp() :: #worldResp{}. 63 | 64 | -type heartbeatReq() :: #heartbeatReq{}. 65 | 66 | -type heartbeatResp() :: #heartbeatResp{}. 67 | 68 | -export_type(['loginReq'/0, 'loginResp'/0, 'helloReq'/0, 'worldResp'/0, 'heartbeatReq'/0, 'heartbeatResp'/0]). 69 | 70 | -spec encode_msg(#loginReq{} | #loginResp{} | #helloReq{} | #worldResp{} | #heartbeatReq{} | #heartbeatResp{}) -> binary(). 71 | encode_msg(Msg) when tuple_size(Msg) >= 1 -> 72 | encode_msg(Msg, element(1, Msg), []). 73 | 74 | -spec encode_msg(#loginReq{} | #loginResp{} | #helloReq{} | #worldResp{} | #heartbeatReq{} | #heartbeatResp{}, atom() | list()) -> binary(). 75 | encode_msg(Msg, MsgName) when is_atom(MsgName) -> 76 | encode_msg(Msg, MsgName, []); 77 | encode_msg(Msg, Opts) 78 | when tuple_size(Msg) >= 1, is_list(Opts) -> 79 | encode_msg(Msg, element(1, Msg), Opts). 80 | 81 | -spec encode_msg(#loginReq{} | #loginResp{} | #helloReq{} | #worldResp{} | #heartbeatReq{} | #heartbeatResp{}, atom(), list()) -> binary(). 82 | encode_msg(Msg, MsgName, Opts) -> 83 | case proplists:get_bool(verify, Opts) of 84 | true -> verify_msg(Msg, MsgName, Opts); 85 | false -> ok 86 | end, 87 | TrUserData = proplists:get_value(user_data, Opts), 88 | case MsgName of 89 | loginReq -> 90 | encode_msg_loginReq(id(Msg, TrUserData), TrUserData); 91 | loginResp -> 92 | encode_msg_loginResp(id(Msg, TrUserData), TrUserData); 93 | helloReq -> 94 | encode_msg_helloReq(id(Msg, TrUserData), TrUserData); 95 | worldResp -> 96 | encode_msg_worldResp(id(Msg, TrUserData), TrUserData); 97 | heartbeatReq -> 98 | encode_msg_heartbeatReq(id(Msg, TrUserData), 99 | TrUserData); 100 | heartbeatResp -> 101 | encode_msg_heartbeatResp(id(Msg, TrUserData), 102 | TrUserData) 103 | end. 104 | 105 | 106 | encode_msg_loginReq(Msg, TrUserData) -> 107 | encode_msg_loginReq(Msg, <<>>, TrUserData). 108 | 109 | 110 | encode_msg_loginReq(#loginReq{cookie = F1}, Bin, 111 | TrUserData) -> 112 | if F1 == undefined -> Bin; 113 | true -> 114 | begin 115 | TrF1 = id(F1, TrUserData), 116 | case is_empty_string(TrF1) of 117 | true -> Bin; 118 | false -> 119 | e_type_string(TrF1, <>, TrUserData) 120 | end 121 | end 122 | end. 123 | 124 | encode_msg_loginResp(Msg, TrUserData) -> 125 | encode_msg_loginResp(Msg, <<>>, TrUserData). 126 | 127 | 128 | encode_msg_loginResp(#loginResp{result = F1, 129 | user_id = F2}, 130 | Bin, TrUserData) -> 131 | B1 = if F1 == undefined -> Bin; 132 | true -> 133 | begin 134 | TrF1 = id(F1, TrUserData), 135 | if TrF1 =:= 'FAILED'; TrF1 =:= 0 -> Bin; 136 | true -> 137 | e_enum_LoginStatus(TrF1, 138 | <>, 139 | TrUserData) 140 | end 141 | end 142 | end, 143 | if F2 == undefined -> B1; 144 | true -> 145 | begin 146 | TrF2 = id(F2, TrUserData), 147 | if TrF2 =:= 0 -> B1; 148 | true -> e_varint(TrF2, <>, TrUserData) 149 | end 150 | end 151 | end. 152 | 153 | encode_msg_helloReq(Msg, TrUserData) -> 154 | encode_msg_helloReq(Msg, <<>>, TrUserData). 155 | 156 | 157 | encode_msg_helloReq(#helloReq{msg = F1}, Bin, 158 | TrUserData) -> 159 | if F1 == undefined -> Bin; 160 | true -> 161 | begin 162 | TrF1 = id(F1, TrUserData), 163 | case is_empty_string(TrF1) of 164 | true -> Bin; 165 | false -> 166 | e_type_string(TrF1, <>, TrUserData) 167 | end 168 | end 169 | end. 170 | 171 | encode_msg_worldResp(Msg, TrUserData) -> 172 | encode_msg_worldResp(Msg, <<>>, TrUserData). 173 | 174 | 175 | encode_msg_worldResp(#worldResp{msg = F1}, Bin, 176 | TrUserData) -> 177 | if F1 == undefined -> Bin; 178 | true -> 179 | begin 180 | TrF1 = id(F1, TrUserData), 181 | case is_empty_string(TrF1) of 182 | true -> Bin; 183 | false -> 184 | e_type_string(TrF1, <>, TrUserData) 185 | end 186 | end 187 | end. 188 | 189 | encode_msg_heartbeatReq(_Msg, _TrUserData) -> <<>>. 190 | 191 | encode_msg_heartbeatResp(Msg, TrUserData) -> 192 | encode_msg_heartbeatResp(Msg, <<>>, TrUserData). 193 | 194 | 195 | encode_msg_heartbeatResp(#heartbeatResp{unixtime = F1}, 196 | Bin, TrUserData) -> 197 | if F1 == undefined -> Bin; 198 | true -> 199 | begin 200 | TrF1 = id(F1, TrUserData), 201 | if TrF1 =:= 0 -> Bin; 202 | true -> e_varint(TrF1, <>, TrUserData) 203 | end 204 | end 205 | end. 206 | 207 | e_enum_LoginStatus('FAILED', Bin, _TrUserData) -> 208 | <>; 209 | e_enum_LoginStatus('SUCCEEDED', Bin, _TrUserData) -> 210 | <>; 211 | e_enum_LoginStatus('NOTFOUND', Bin, _TrUserData) -> 212 | <>; 213 | e_enum_LoginStatus(V, Bin, _TrUserData) -> 214 | e_varint(V, Bin). 215 | 216 | -compile({nowarn_unused_function,e_type_sint/3}). 217 | e_type_sint(Value, Bin, _TrUserData) when Value >= 0 -> 218 | e_varint(Value * 2, Bin); 219 | e_type_sint(Value, Bin, _TrUserData) -> 220 | e_varint(Value * -2 - 1, Bin). 221 | 222 | -compile({nowarn_unused_function,e_type_int32/3}). 223 | e_type_int32(Value, Bin, _TrUserData) 224 | when 0 =< Value, Value =< 127 -> 225 | <>; 226 | e_type_int32(Value, Bin, _TrUserData) -> 227 | <> = <>, 228 | e_varint(N, Bin). 229 | 230 | -compile({nowarn_unused_function,e_type_int64/3}). 231 | e_type_int64(Value, Bin, _TrUserData) 232 | when 0 =< Value, Value =< 127 -> 233 | <>; 234 | e_type_int64(Value, Bin, _TrUserData) -> 235 | <> = <>, 236 | e_varint(N, Bin). 237 | 238 | -compile({nowarn_unused_function,e_type_bool/3}). 239 | e_type_bool(true, Bin, _TrUserData) -> 240 | <>; 241 | e_type_bool(false, Bin, _TrUserData) -> 242 | <>; 243 | e_type_bool(1, Bin, _TrUserData) -> <>; 244 | e_type_bool(0, Bin, _TrUserData) -> <>. 245 | 246 | -compile({nowarn_unused_function,e_type_string/3}). 247 | e_type_string(S, Bin, _TrUserData) -> 248 | Utf8 = unicode:characters_to_binary(S), 249 | Bin2 = e_varint(byte_size(Utf8), Bin), 250 | <>. 251 | 252 | -compile({nowarn_unused_function,e_type_bytes/3}). 253 | e_type_bytes(Bytes, Bin, _TrUserData) 254 | when is_binary(Bytes) -> 255 | Bin2 = e_varint(byte_size(Bytes), Bin), 256 | <>; 257 | e_type_bytes(Bytes, Bin, _TrUserData) 258 | when is_list(Bytes) -> 259 | BytesBin = iolist_to_binary(Bytes), 260 | Bin2 = e_varint(byte_size(BytesBin), Bin), 261 | <>. 262 | 263 | -compile({nowarn_unused_function,e_type_fixed32/3}). 264 | e_type_fixed32(Value, Bin, _TrUserData) -> 265 | <>. 266 | 267 | -compile({nowarn_unused_function,e_type_sfixed32/3}). 268 | e_type_sfixed32(Value, Bin, _TrUserData) -> 269 | <>. 270 | 271 | -compile({nowarn_unused_function,e_type_fixed64/3}). 272 | e_type_fixed64(Value, Bin, _TrUserData) -> 273 | <>. 274 | 275 | -compile({nowarn_unused_function,e_type_sfixed64/3}). 276 | e_type_sfixed64(Value, Bin, _TrUserData) -> 277 | <>. 278 | 279 | -compile({nowarn_unused_function,e_type_float/3}). 280 | e_type_float(V, Bin, _) when is_number(V) -> 281 | <>; 282 | e_type_float(infinity, Bin, _) -> 283 | <>; 284 | e_type_float('-infinity', Bin, _) -> 285 | <>; 286 | e_type_float(nan, Bin, _) -> 287 | <>. 288 | 289 | -compile({nowarn_unused_function,e_type_double/3}). 290 | e_type_double(V, Bin, _) when is_number(V) -> 291 | <>; 292 | e_type_double(infinity, Bin, _) -> 293 | <>; 294 | e_type_double('-infinity', Bin, _) -> 295 | <>; 296 | e_type_double(nan, Bin, _) -> 297 | <>. 298 | 299 | -compile({nowarn_unused_function,e_varint/3}). 300 | e_varint(N, Bin, _TrUserData) -> e_varint(N, Bin). 301 | 302 | -compile({nowarn_unused_function,e_varint/2}). 303 | e_varint(N, Bin) when N =< 127 -> <>; 304 | e_varint(N, Bin) -> 305 | Bin2 = <>, 306 | e_varint(N bsr 7, Bin2). 307 | 308 | is_empty_string("") -> true; 309 | is_empty_string(<<>>) -> true; 310 | is_empty_string(L) when is_list(L) -> 311 | not string_has_chars(L); 312 | is_empty_string(B) when is_binary(B) -> false. 313 | 314 | string_has_chars([C | _]) when is_integer(C) -> true; 315 | string_has_chars([H | T]) -> 316 | case string_has_chars(H) of 317 | true -> true; 318 | false -> string_has_chars(T) 319 | end; 320 | string_has_chars(B) 321 | when is_binary(B), byte_size(B) =/= 0 -> 322 | true; 323 | string_has_chars(C) when is_integer(C) -> true; 324 | string_has_chars(<<>>) -> false; 325 | string_has_chars([]) -> false. 326 | 327 | 328 | decode_msg(Bin, MsgName) when is_binary(Bin) -> 329 | decode_msg(Bin, MsgName, []). 330 | 331 | decode_msg(Bin, MsgName, Opts) when is_binary(Bin) -> 332 | TrUserData = proplists:get_value(user_data, Opts), 333 | decode_msg_1_catch(Bin, MsgName, TrUserData). 334 | 335 | -ifdef('OTP_RELEASE'). 336 | decode_msg_1_catch(Bin, MsgName, TrUserData) -> 337 | try decode_msg_2_doit(MsgName, Bin, TrUserData) 338 | catch Class:Reason:StackTrace -> error({gpb_error,{decoding_failure, {Bin, MsgName, {Class, Reason, StackTrace}}}}) 339 | end. 340 | -else. 341 | decode_msg_1_catch(Bin, MsgName, TrUserData) -> 342 | try decode_msg_2_doit(MsgName, Bin, TrUserData) 343 | catch Class:Reason -> 344 | StackTrace = erlang:get_stacktrace(), 345 | error({gpb_error,{decoding_failure, {Bin, MsgName, {Class, Reason, StackTrace}}}}) 346 | end. 347 | -endif. 348 | 349 | decode_msg_2_doit(loginReq, Bin, TrUserData) -> 350 | id(decode_msg_loginReq(Bin, TrUserData), TrUserData); 351 | decode_msg_2_doit(loginResp, Bin, TrUserData) -> 352 | id(decode_msg_loginResp(Bin, TrUserData), TrUserData); 353 | decode_msg_2_doit(helloReq, Bin, TrUserData) -> 354 | id(decode_msg_helloReq(Bin, TrUserData), TrUserData); 355 | decode_msg_2_doit(worldResp, Bin, TrUserData) -> 356 | id(decode_msg_worldResp(Bin, TrUserData), TrUserData); 357 | decode_msg_2_doit(heartbeatReq, Bin, TrUserData) -> 358 | id(decode_msg_heartbeatReq(Bin, TrUserData), 359 | TrUserData); 360 | decode_msg_2_doit(heartbeatResp, Bin, TrUserData) -> 361 | id(decode_msg_heartbeatResp(Bin, TrUserData), 362 | TrUserData). 363 | 364 | 365 | 366 | decode_msg_loginReq(Bin, TrUserData) -> 367 | dfp_read_field_def_loginReq(Bin, 368 | 0, 369 | 0, 370 | id(<<>>, TrUserData), 371 | TrUserData). 372 | 373 | dfp_read_field_def_loginReq(<<10, Rest/binary>>, Z1, Z2, 374 | F@_1, TrUserData) -> 375 | d_field_loginReq_cookie(Rest, Z1, Z2, F@_1, TrUserData); 376 | dfp_read_field_def_loginReq(<<>>, 0, 0, F@_1, _) -> 377 | #loginReq{cookie = F@_1}; 378 | dfp_read_field_def_loginReq(Other, Z1, Z2, F@_1, 379 | TrUserData) -> 380 | dg_read_field_def_loginReq(Other, 381 | Z1, 382 | Z2, 383 | F@_1, 384 | TrUserData). 385 | 386 | dg_read_field_def_loginReq(<<1:1, X:7, Rest/binary>>, N, 387 | Acc, F@_1, TrUserData) 388 | when N < 32 - 7 -> 389 | dg_read_field_def_loginReq(Rest, 390 | N + 7, 391 | X bsl N + Acc, 392 | F@_1, 393 | TrUserData); 394 | dg_read_field_def_loginReq(<<0:1, X:7, Rest/binary>>, N, 395 | Acc, F@_1, TrUserData) -> 396 | Key = X bsl N + Acc, 397 | case Key of 398 | 10 -> 399 | d_field_loginReq_cookie(Rest, 0, 0, F@_1, TrUserData); 400 | _ -> 401 | case Key band 7 of 402 | 0 -> skip_varint_loginReq(Rest, 0, 0, F@_1, TrUserData); 403 | 1 -> skip_64_loginReq(Rest, 0, 0, F@_1, TrUserData); 404 | 2 -> 405 | skip_length_delimited_loginReq(Rest, 406 | 0, 407 | 0, 408 | F@_1, 409 | TrUserData); 410 | 3 -> 411 | skip_group_loginReq(Rest, 412 | Key bsr 3, 413 | 0, 414 | F@_1, 415 | TrUserData); 416 | 5 -> skip_32_loginReq(Rest, 0, 0, F@_1, TrUserData) 417 | end 418 | end; 419 | dg_read_field_def_loginReq(<<>>, 0, 0, F@_1, _) -> 420 | #loginReq{cookie = F@_1}. 421 | 422 | d_field_loginReq_cookie(<<1:1, X:7, Rest/binary>>, N, 423 | Acc, F@_1, TrUserData) 424 | when N < 57 -> 425 | d_field_loginReq_cookie(Rest, 426 | N + 7, 427 | X bsl N + Acc, 428 | F@_1, 429 | TrUserData); 430 | d_field_loginReq_cookie(<<0:1, X:7, Rest/binary>>, N, 431 | Acc, _, TrUserData) -> 432 | {NewFValue, RestF} = begin 433 | Len = X bsl N + Acc, 434 | <> = Rest, 435 | {id(binary:copy(Bytes), TrUserData), Rest2} 436 | end, 437 | dfp_read_field_def_loginReq(RestF, 438 | 0, 439 | 0, 440 | NewFValue, 441 | TrUserData). 442 | 443 | skip_varint_loginReq(<<1:1, _:7, Rest/binary>>, Z1, Z2, 444 | F@_1, TrUserData) -> 445 | skip_varint_loginReq(Rest, Z1, Z2, F@_1, TrUserData); 446 | skip_varint_loginReq(<<0:1, _:7, Rest/binary>>, Z1, Z2, 447 | F@_1, TrUserData) -> 448 | dfp_read_field_def_loginReq(Rest, 449 | Z1, 450 | Z2, 451 | F@_1, 452 | TrUserData). 453 | 454 | skip_length_delimited_loginReq(<<1:1, X:7, 455 | Rest/binary>>, 456 | N, Acc, F@_1, TrUserData) 457 | when N < 57 -> 458 | skip_length_delimited_loginReq(Rest, 459 | N + 7, 460 | X bsl N + Acc, 461 | F@_1, 462 | TrUserData); 463 | skip_length_delimited_loginReq(<<0:1, X:7, 464 | Rest/binary>>, 465 | N, Acc, F@_1, TrUserData) -> 466 | Length = X bsl N + Acc, 467 | <<_:Length/binary, Rest2/binary>> = Rest, 468 | dfp_read_field_def_loginReq(Rest2, 469 | 0, 470 | 0, 471 | F@_1, 472 | TrUserData). 473 | 474 | skip_group_loginReq(Bin, FNum, Z2, F@_1, TrUserData) -> 475 | {_, Rest} = read_group(Bin, FNum), 476 | dfp_read_field_def_loginReq(Rest, 477 | 0, 478 | Z2, 479 | F@_1, 480 | TrUserData). 481 | 482 | skip_32_loginReq(<<_:32, Rest/binary>>, Z1, Z2, F@_1, 483 | TrUserData) -> 484 | dfp_read_field_def_loginReq(Rest, 485 | Z1, 486 | Z2, 487 | F@_1, 488 | TrUserData). 489 | 490 | skip_64_loginReq(<<_:64, Rest/binary>>, Z1, Z2, F@_1, 491 | TrUserData) -> 492 | dfp_read_field_def_loginReq(Rest, 493 | Z1, 494 | Z2, 495 | F@_1, 496 | TrUserData). 497 | 498 | decode_msg_loginResp(Bin, TrUserData) -> 499 | dfp_read_field_def_loginResp(Bin, 500 | 0, 501 | 0, 502 | id('FAILED', TrUserData), 503 | id(0, TrUserData), 504 | TrUserData). 505 | 506 | dfp_read_field_def_loginResp(<<8, Rest/binary>>, Z1, Z2, 507 | F@_1, F@_2, TrUserData) -> 508 | d_field_loginResp_result(Rest, 509 | Z1, 510 | Z2, 511 | F@_1, 512 | F@_2, 513 | TrUserData); 514 | dfp_read_field_def_loginResp(<<16, Rest/binary>>, Z1, 515 | Z2, F@_1, F@_2, TrUserData) -> 516 | d_field_loginResp_user_id(Rest, 517 | Z1, 518 | Z2, 519 | F@_1, 520 | F@_2, 521 | TrUserData); 522 | dfp_read_field_def_loginResp(<<>>, 0, 0, F@_1, F@_2, 523 | _) -> 524 | #loginResp{result = F@_1, user_id = F@_2}; 525 | dfp_read_field_def_loginResp(Other, Z1, Z2, F@_1, F@_2, 526 | TrUserData) -> 527 | dg_read_field_def_loginResp(Other, 528 | Z1, 529 | Z2, 530 | F@_1, 531 | F@_2, 532 | TrUserData). 533 | 534 | dg_read_field_def_loginResp(<<1:1, X:7, Rest/binary>>, 535 | N, Acc, F@_1, F@_2, TrUserData) 536 | when N < 32 - 7 -> 537 | dg_read_field_def_loginResp(Rest, 538 | N + 7, 539 | X bsl N + Acc, 540 | F@_1, 541 | F@_2, 542 | TrUserData); 543 | dg_read_field_def_loginResp(<<0:1, X:7, Rest/binary>>, 544 | N, Acc, F@_1, F@_2, TrUserData) -> 545 | Key = X bsl N + Acc, 546 | case Key of 547 | 8 -> 548 | d_field_loginResp_result(Rest, 549 | 0, 550 | 0, 551 | F@_1, 552 | F@_2, 553 | TrUserData); 554 | 16 -> 555 | d_field_loginResp_user_id(Rest, 556 | 0, 557 | 0, 558 | F@_1, 559 | F@_2, 560 | TrUserData); 561 | _ -> 562 | case Key band 7 of 563 | 0 -> 564 | skip_varint_loginResp(Rest, 565 | 0, 566 | 0, 567 | F@_1, 568 | F@_2, 569 | TrUserData); 570 | 1 -> 571 | skip_64_loginResp(Rest, 0, 0, F@_1, F@_2, TrUserData); 572 | 2 -> 573 | skip_length_delimited_loginResp(Rest, 574 | 0, 575 | 0, 576 | F@_1, 577 | F@_2, 578 | TrUserData); 579 | 3 -> 580 | skip_group_loginResp(Rest, 581 | Key bsr 3, 582 | 0, 583 | F@_1, 584 | F@_2, 585 | TrUserData); 586 | 5 -> 587 | skip_32_loginResp(Rest, 0, 0, F@_1, F@_2, TrUserData) 588 | end 589 | end; 590 | dg_read_field_def_loginResp(<<>>, 0, 0, F@_1, F@_2, 591 | _) -> 592 | #loginResp{result = F@_1, user_id = F@_2}. 593 | 594 | d_field_loginResp_result(<<1:1, X:7, Rest/binary>>, N, 595 | Acc, F@_1, F@_2, TrUserData) 596 | when N < 57 -> 597 | d_field_loginResp_result(Rest, 598 | N + 7, 599 | X bsl N + Acc, 600 | F@_1, 601 | F@_2, 602 | TrUserData); 603 | d_field_loginResp_result(<<0:1, X:7, Rest/binary>>, N, 604 | Acc, _, F@_2, TrUserData) -> 605 | {NewFValue, RestF} = {id(d_enum_LoginStatus(begin 606 | <> = 607 | <<(X bsl N + 608 | Acc):32/unsigned-native>>, 609 | id(Res, TrUserData) 610 | end), 611 | TrUserData), 612 | Rest}, 613 | dfp_read_field_def_loginResp(RestF, 614 | 0, 615 | 0, 616 | NewFValue, 617 | F@_2, 618 | TrUserData). 619 | 620 | d_field_loginResp_user_id(<<1:1, X:7, Rest/binary>>, N, 621 | Acc, F@_1, F@_2, TrUserData) 622 | when N < 57 -> 623 | d_field_loginResp_user_id(Rest, 624 | N + 7, 625 | X bsl N + Acc, 626 | F@_1, 627 | F@_2, 628 | TrUserData); 629 | d_field_loginResp_user_id(<<0:1, X:7, Rest/binary>>, N, 630 | Acc, F@_1, _, TrUserData) -> 631 | {NewFValue, RestF} = {id(X bsl N + Acc, TrUserData), 632 | Rest}, 633 | dfp_read_field_def_loginResp(RestF, 634 | 0, 635 | 0, 636 | F@_1, 637 | NewFValue, 638 | TrUserData). 639 | 640 | skip_varint_loginResp(<<1:1, _:7, Rest/binary>>, Z1, Z2, 641 | F@_1, F@_2, TrUserData) -> 642 | skip_varint_loginResp(Rest, 643 | Z1, 644 | Z2, 645 | F@_1, 646 | F@_2, 647 | TrUserData); 648 | skip_varint_loginResp(<<0:1, _:7, Rest/binary>>, Z1, Z2, 649 | F@_1, F@_2, TrUserData) -> 650 | dfp_read_field_def_loginResp(Rest, 651 | Z1, 652 | Z2, 653 | F@_1, 654 | F@_2, 655 | TrUserData). 656 | 657 | skip_length_delimited_loginResp(<<1:1, X:7, 658 | Rest/binary>>, 659 | N, Acc, F@_1, F@_2, TrUserData) 660 | when N < 57 -> 661 | skip_length_delimited_loginResp(Rest, 662 | N + 7, 663 | X bsl N + Acc, 664 | F@_1, 665 | F@_2, 666 | TrUserData); 667 | skip_length_delimited_loginResp(<<0:1, X:7, 668 | Rest/binary>>, 669 | N, Acc, F@_1, F@_2, TrUserData) -> 670 | Length = X bsl N + Acc, 671 | <<_:Length/binary, Rest2/binary>> = Rest, 672 | dfp_read_field_def_loginResp(Rest2, 673 | 0, 674 | 0, 675 | F@_1, 676 | F@_2, 677 | TrUserData). 678 | 679 | skip_group_loginResp(Bin, FNum, Z2, F@_1, F@_2, 680 | TrUserData) -> 681 | {_, Rest} = read_group(Bin, FNum), 682 | dfp_read_field_def_loginResp(Rest, 683 | 0, 684 | Z2, 685 | F@_1, 686 | F@_2, 687 | TrUserData). 688 | 689 | skip_32_loginResp(<<_:32, Rest/binary>>, Z1, Z2, F@_1, 690 | F@_2, TrUserData) -> 691 | dfp_read_field_def_loginResp(Rest, 692 | Z1, 693 | Z2, 694 | F@_1, 695 | F@_2, 696 | TrUserData). 697 | 698 | skip_64_loginResp(<<_:64, Rest/binary>>, Z1, Z2, F@_1, 699 | F@_2, TrUserData) -> 700 | dfp_read_field_def_loginResp(Rest, 701 | Z1, 702 | Z2, 703 | F@_1, 704 | F@_2, 705 | TrUserData). 706 | 707 | decode_msg_helloReq(Bin, TrUserData) -> 708 | dfp_read_field_def_helloReq(Bin, 709 | 0, 710 | 0, 711 | id(<<>>, TrUserData), 712 | TrUserData). 713 | 714 | dfp_read_field_def_helloReq(<<10, Rest/binary>>, Z1, Z2, 715 | F@_1, TrUserData) -> 716 | d_field_helloReq_msg(Rest, Z1, Z2, F@_1, TrUserData); 717 | dfp_read_field_def_helloReq(<<>>, 0, 0, F@_1, _) -> 718 | #helloReq{msg = F@_1}; 719 | dfp_read_field_def_helloReq(Other, Z1, Z2, F@_1, 720 | TrUserData) -> 721 | dg_read_field_def_helloReq(Other, 722 | Z1, 723 | Z2, 724 | F@_1, 725 | TrUserData). 726 | 727 | dg_read_field_def_helloReq(<<1:1, X:7, Rest/binary>>, N, 728 | Acc, F@_1, TrUserData) 729 | when N < 32 - 7 -> 730 | dg_read_field_def_helloReq(Rest, 731 | N + 7, 732 | X bsl N + Acc, 733 | F@_1, 734 | TrUserData); 735 | dg_read_field_def_helloReq(<<0:1, X:7, Rest/binary>>, N, 736 | Acc, F@_1, TrUserData) -> 737 | Key = X bsl N + Acc, 738 | case Key of 739 | 10 -> 740 | d_field_helloReq_msg(Rest, 0, 0, F@_1, TrUserData); 741 | _ -> 742 | case Key band 7 of 743 | 0 -> skip_varint_helloReq(Rest, 0, 0, F@_1, TrUserData); 744 | 1 -> skip_64_helloReq(Rest, 0, 0, F@_1, TrUserData); 745 | 2 -> 746 | skip_length_delimited_helloReq(Rest, 747 | 0, 748 | 0, 749 | F@_1, 750 | TrUserData); 751 | 3 -> 752 | skip_group_helloReq(Rest, 753 | Key bsr 3, 754 | 0, 755 | F@_1, 756 | TrUserData); 757 | 5 -> skip_32_helloReq(Rest, 0, 0, F@_1, TrUserData) 758 | end 759 | end; 760 | dg_read_field_def_helloReq(<<>>, 0, 0, F@_1, _) -> 761 | #helloReq{msg = F@_1}. 762 | 763 | d_field_helloReq_msg(<<1:1, X:7, Rest/binary>>, N, Acc, 764 | F@_1, TrUserData) 765 | when N < 57 -> 766 | d_field_helloReq_msg(Rest, 767 | N + 7, 768 | X bsl N + Acc, 769 | F@_1, 770 | TrUserData); 771 | d_field_helloReq_msg(<<0:1, X:7, Rest/binary>>, N, Acc, 772 | _, TrUserData) -> 773 | {NewFValue, RestF} = begin 774 | Len = X bsl N + Acc, 775 | <> = Rest, 776 | {id(binary:copy(Bytes), TrUserData), Rest2} 777 | end, 778 | dfp_read_field_def_helloReq(RestF, 779 | 0, 780 | 0, 781 | NewFValue, 782 | TrUserData). 783 | 784 | skip_varint_helloReq(<<1:1, _:7, Rest/binary>>, Z1, Z2, 785 | F@_1, TrUserData) -> 786 | skip_varint_helloReq(Rest, Z1, Z2, F@_1, TrUserData); 787 | skip_varint_helloReq(<<0:1, _:7, Rest/binary>>, Z1, Z2, 788 | F@_1, TrUserData) -> 789 | dfp_read_field_def_helloReq(Rest, 790 | Z1, 791 | Z2, 792 | F@_1, 793 | TrUserData). 794 | 795 | skip_length_delimited_helloReq(<<1:1, X:7, 796 | Rest/binary>>, 797 | N, Acc, F@_1, TrUserData) 798 | when N < 57 -> 799 | skip_length_delimited_helloReq(Rest, 800 | N + 7, 801 | X bsl N + Acc, 802 | F@_1, 803 | TrUserData); 804 | skip_length_delimited_helloReq(<<0:1, X:7, 805 | Rest/binary>>, 806 | N, Acc, F@_1, TrUserData) -> 807 | Length = X bsl N + Acc, 808 | <<_:Length/binary, Rest2/binary>> = Rest, 809 | dfp_read_field_def_helloReq(Rest2, 810 | 0, 811 | 0, 812 | F@_1, 813 | TrUserData). 814 | 815 | skip_group_helloReq(Bin, FNum, Z2, F@_1, TrUserData) -> 816 | {_, Rest} = read_group(Bin, FNum), 817 | dfp_read_field_def_helloReq(Rest, 818 | 0, 819 | Z2, 820 | F@_1, 821 | TrUserData). 822 | 823 | skip_32_helloReq(<<_:32, Rest/binary>>, Z1, Z2, F@_1, 824 | TrUserData) -> 825 | dfp_read_field_def_helloReq(Rest, 826 | Z1, 827 | Z2, 828 | F@_1, 829 | TrUserData). 830 | 831 | skip_64_helloReq(<<_:64, Rest/binary>>, Z1, Z2, F@_1, 832 | TrUserData) -> 833 | dfp_read_field_def_helloReq(Rest, 834 | Z1, 835 | Z2, 836 | F@_1, 837 | TrUserData). 838 | 839 | decode_msg_worldResp(Bin, TrUserData) -> 840 | dfp_read_field_def_worldResp(Bin, 841 | 0, 842 | 0, 843 | id(<<>>, TrUserData), 844 | TrUserData). 845 | 846 | dfp_read_field_def_worldResp(<<10, Rest/binary>>, Z1, 847 | Z2, F@_1, TrUserData) -> 848 | d_field_worldResp_msg(Rest, Z1, Z2, F@_1, TrUserData); 849 | dfp_read_field_def_worldResp(<<>>, 0, 0, F@_1, _) -> 850 | #worldResp{msg = F@_1}; 851 | dfp_read_field_def_worldResp(Other, Z1, Z2, F@_1, 852 | TrUserData) -> 853 | dg_read_field_def_worldResp(Other, 854 | Z1, 855 | Z2, 856 | F@_1, 857 | TrUserData). 858 | 859 | dg_read_field_def_worldResp(<<1:1, X:7, Rest/binary>>, 860 | N, Acc, F@_1, TrUserData) 861 | when N < 32 - 7 -> 862 | dg_read_field_def_worldResp(Rest, 863 | N + 7, 864 | X bsl N + Acc, 865 | F@_1, 866 | TrUserData); 867 | dg_read_field_def_worldResp(<<0:1, X:7, Rest/binary>>, 868 | N, Acc, F@_1, TrUserData) -> 869 | Key = X bsl N + Acc, 870 | case Key of 871 | 10 -> 872 | d_field_worldResp_msg(Rest, 0, 0, F@_1, TrUserData); 873 | _ -> 874 | case Key band 7 of 875 | 0 -> 876 | skip_varint_worldResp(Rest, 0, 0, F@_1, TrUserData); 877 | 1 -> skip_64_worldResp(Rest, 0, 0, F@_1, TrUserData); 878 | 2 -> 879 | skip_length_delimited_worldResp(Rest, 880 | 0, 881 | 0, 882 | F@_1, 883 | TrUserData); 884 | 3 -> 885 | skip_group_worldResp(Rest, 886 | Key bsr 3, 887 | 0, 888 | F@_1, 889 | TrUserData); 890 | 5 -> skip_32_worldResp(Rest, 0, 0, F@_1, TrUserData) 891 | end 892 | end; 893 | dg_read_field_def_worldResp(<<>>, 0, 0, F@_1, _) -> 894 | #worldResp{msg = F@_1}. 895 | 896 | d_field_worldResp_msg(<<1:1, X:7, Rest/binary>>, N, Acc, 897 | F@_1, TrUserData) 898 | when N < 57 -> 899 | d_field_worldResp_msg(Rest, 900 | N + 7, 901 | X bsl N + Acc, 902 | F@_1, 903 | TrUserData); 904 | d_field_worldResp_msg(<<0:1, X:7, Rest/binary>>, N, Acc, 905 | _, TrUserData) -> 906 | {NewFValue, RestF} = begin 907 | Len = X bsl N + Acc, 908 | <> = Rest, 909 | {id(binary:copy(Bytes), TrUserData), Rest2} 910 | end, 911 | dfp_read_field_def_worldResp(RestF, 912 | 0, 913 | 0, 914 | NewFValue, 915 | TrUserData). 916 | 917 | skip_varint_worldResp(<<1:1, _:7, Rest/binary>>, Z1, Z2, 918 | F@_1, TrUserData) -> 919 | skip_varint_worldResp(Rest, Z1, Z2, F@_1, TrUserData); 920 | skip_varint_worldResp(<<0:1, _:7, Rest/binary>>, Z1, Z2, 921 | F@_1, TrUserData) -> 922 | dfp_read_field_def_worldResp(Rest, 923 | Z1, 924 | Z2, 925 | F@_1, 926 | TrUserData). 927 | 928 | skip_length_delimited_worldResp(<<1:1, X:7, 929 | Rest/binary>>, 930 | N, Acc, F@_1, TrUserData) 931 | when N < 57 -> 932 | skip_length_delimited_worldResp(Rest, 933 | N + 7, 934 | X bsl N + Acc, 935 | F@_1, 936 | TrUserData); 937 | skip_length_delimited_worldResp(<<0:1, X:7, 938 | Rest/binary>>, 939 | N, Acc, F@_1, TrUserData) -> 940 | Length = X bsl N + Acc, 941 | <<_:Length/binary, Rest2/binary>> = Rest, 942 | dfp_read_field_def_worldResp(Rest2, 943 | 0, 944 | 0, 945 | F@_1, 946 | TrUserData). 947 | 948 | skip_group_worldResp(Bin, FNum, Z2, F@_1, TrUserData) -> 949 | {_, Rest} = read_group(Bin, FNum), 950 | dfp_read_field_def_worldResp(Rest, 951 | 0, 952 | Z2, 953 | F@_1, 954 | TrUserData). 955 | 956 | skip_32_worldResp(<<_:32, Rest/binary>>, Z1, Z2, F@_1, 957 | TrUserData) -> 958 | dfp_read_field_def_worldResp(Rest, 959 | Z1, 960 | Z2, 961 | F@_1, 962 | TrUserData). 963 | 964 | skip_64_worldResp(<<_:64, Rest/binary>>, Z1, Z2, F@_1, 965 | TrUserData) -> 966 | dfp_read_field_def_worldResp(Rest, 967 | Z1, 968 | Z2, 969 | F@_1, 970 | TrUserData). 971 | 972 | decode_msg_heartbeatReq(Bin, TrUserData) -> 973 | dfp_read_field_def_heartbeatReq(Bin, 0, 0, TrUserData). 974 | 975 | dfp_read_field_def_heartbeatReq(<<>>, 0, 0, _) -> 976 | #heartbeatReq{}; 977 | dfp_read_field_def_heartbeatReq(Other, Z1, Z2, 978 | TrUserData) -> 979 | dg_read_field_def_heartbeatReq(Other, 980 | Z1, 981 | Z2, 982 | TrUserData). 983 | 984 | dg_read_field_def_heartbeatReq(<<1:1, X:7, 985 | Rest/binary>>, 986 | N, Acc, TrUserData) 987 | when N < 32 - 7 -> 988 | dg_read_field_def_heartbeatReq(Rest, 989 | N + 7, 990 | X bsl N + Acc, 991 | TrUserData); 992 | dg_read_field_def_heartbeatReq(<<0:1, X:7, 993 | Rest/binary>>, 994 | N, Acc, TrUserData) -> 995 | Key = X bsl N + Acc, 996 | case Key band 7 of 997 | 0 -> skip_varint_heartbeatReq(Rest, 0, 0, TrUserData); 998 | 1 -> skip_64_heartbeatReq(Rest, 0, 0, TrUserData); 999 | 2 -> 1000 | skip_length_delimited_heartbeatReq(Rest, 1001 | 0, 1002 | 0, 1003 | TrUserData); 1004 | 3 -> 1005 | skip_group_heartbeatReq(Rest, Key bsr 3, 0, TrUserData); 1006 | 5 -> skip_32_heartbeatReq(Rest, 0, 0, TrUserData) 1007 | end; 1008 | dg_read_field_def_heartbeatReq(<<>>, 0, 0, _) -> 1009 | #heartbeatReq{}. 1010 | 1011 | skip_varint_heartbeatReq(<<1:1, _:7, Rest/binary>>, Z1, 1012 | Z2, TrUserData) -> 1013 | skip_varint_heartbeatReq(Rest, Z1, Z2, TrUserData); 1014 | skip_varint_heartbeatReq(<<0:1, _:7, Rest/binary>>, Z1, 1015 | Z2, TrUserData) -> 1016 | dfp_read_field_def_heartbeatReq(Rest, 1017 | Z1, 1018 | Z2, 1019 | TrUserData). 1020 | 1021 | skip_length_delimited_heartbeatReq(<<1:1, X:7, 1022 | Rest/binary>>, 1023 | N, Acc, TrUserData) 1024 | when N < 57 -> 1025 | skip_length_delimited_heartbeatReq(Rest, 1026 | N + 7, 1027 | X bsl N + Acc, 1028 | TrUserData); 1029 | skip_length_delimited_heartbeatReq(<<0:1, X:7, 1030 | Rest/binary>>, 1031 | N, Acc, TrUserData) -> 1032 | Length = X bsl N + Acc, 1033 | <<_:Length/binary, Rest2/binary>> = Rest, 1034 | dfp_read_field_def_heartbeatReq(Rest2, 1035 | 0, 1036 | 0, 1037 | TrUserData). 1038 | 1039 | skip_group_heartbeatReq(Bin, FNum, Z2, TrUserData) -> 1040 | {_, Rest} = read_group(Bin, FNum), 1041 | dfp_read_field_def_heartbeatReq(Rest, 1042 | 0, 1043 | Z2, 1044 | TrUserData). 1045 | 1046 | skip_32_heartbeatReq(<<_:32, Rest/binary>>, Z1, Z2, 1047 | TrUserData) -> 1048 | dfp_read_field_def_heartbeatReq(Rest, 1049 | Z1, 1050 | Z2, 1051 | TrUserData). 1052 | 1053 | skip_64_heartbeatReq(<<_:64, Rest/binary>>, Z1, Z2, 1054 | TrUserData) -> 1055 | dfp_read_field_def_heartbeatReq(Rest, 1056 | Z1, 1057 | Z2, 1058 | TrUserData). 1059 | 1060 | decode_msg_heartbeatResp(Bin, TrUserData) -> 1061 | dfp_read_field_def_heartbeatResp(Bin, 1062 | 0, 1063 | 0, 1064 | id(0, TrUserData), 1065 | TrUserData). 1066 | 1067 | dfp_read_field_def_heartbeatResp(<<8, Rest/binary>>, Z1, 1068 | Z2, F@_1, TrUserData) -> 1069 | d_field_heartbeatResp_unixtime(Rest, 1070 | Z1, 1071 | Z2, 1072 | F@_1, 1073 | TrUserData); 1074 | dfp_read_field_def_heartbeatResp(<<>>, 0, 0, F@_1, _) -> 1075 | #heartbeatResp{unixtime = F@_1}; 1076 | dfp_read_field_def_heartbeatResp(Other, Z1, Z2, F@_1, 1077 | TrUserData) -> 1078 | dg_read_field_def_heartbeatResp(Other, 1079 | Z1, 1080 | Z2, 1081 | F@_1, 1082 | TrUserData). 1083 | 1084 | dg_read_field_def_heartbeatResp(<<1:1, X:7, 1085 | Rest/binary>>, 1086 | N, Acc, F@_1, TrUserData) 1087 | when N < 32 - 7 -> 1088 | dg_read_field_def_heartbeatResp(Rest, 1089 | N + 7, 1090 | X bsl N + Acc, 1091 | F@_1, 1092 | TrUserData); 1093 | dg_read_field_def_heartbeatResp(<<0:1, X:7, 1094 | Rest/binary>>, 1095 | N, Acc, F@_1, TrUserData) -> 1096 | Key = X bsl N + Acc, 1097 | case Key of 1098 | 8 -> 1099 | d_field_heartbeatResp_unixtime(Rest, 1100 | 0, 1101 | 0, 1102 | F@_1, 1103 | TrUserData); 1104 | _ -> 1105 | case Key band 7 of 1106 | 0 -> 1107 | skip_varint_heartbeatResp(Rest, 0, 0, F@_1, TrUserData); 1108 | 1 -> 1109 | skip_64_heartbeatResp(Rest, 0, 0, F@_1, TrUserData); 1110 | 2 -> 1111 | skip_length_delimited_heartbeatResp(Rest, 1112 | 0, 1113 | 0, 1114 | F@_1, 1115 | TrUserData); 1116 | 3 -> 1117 | skip_group_heartbeatResp(Rest, 1118 | Key bsr 3, 1119 | 0, 1120 | F@_1, 1121 | TrUserData); 1122 | 5 -> skip_32_heartbeatResp(Rest, 0, 0, F@_1, TrUserData) 1123 | end 1124 | end; 1125 | dg_read_field_def_heartbeatResp(<<>>, 0, 0, F@_1, _) -> 1126 | #heartbeatResp{unixtime = F@_1}. 1127 | 1128 | d_field_heartbeatResp_unixtime(<<1:1, X:7, 1129 | Rest/binary>>, 1130 | N, Acc, F@_1, TrUserData) 1131 | when N < 57 -> 1132 | d_field_heartbeatResp_unixtime(Rest, 1133 | N + 7, 1134 | X bsl N + Acc, 1135 | F@_1, 1136 | TrUserData); 1137 | d_field_heartbeatResp_unixtime(<<0:1, X:7, 1138 | Rest/binary>>, 1139 | N, Acc, _, TrUserData) -> 1140 | {NewFValue, RestF} = {id(X bsl N + Acc, TrUserData), 1141 | Rest}, 1142 | dfp_read_field_def_heartbeatResp(RestF, 1143 | 0, 1144 | 0, 1145 | NewFValue, 1146 | TrUserData). 1147 | 1148 | skip_varint_heartbeatResp(<<1:1, _:7, Rest/binary>>, Z1, 1149 | Z2, F@_1, TrUserData) -> 1150 | skip_varint_heartbeatResp(Rest, 1151 | Z1, 1152 | Z2, 1153 | F@_1, 1154 | TrUserData); 1155 | skip_varint_heartbeatResp(<<0:1, _:7, Rest/binary>>, Z1, 1156 | Z2, F@_1, TrUserData) -> 1157 | dfp_read_field_def_heartbeatResp(Rest, 1158 | Z1, 1159 | Z2, 1160 | F@_1, 1161 | TrUserData). 1162 | 1163 | skip_length_delimited_heartbeatResp(<<1:1, X:7, 1164 | Rest/binary>>, 1165 | N, Acc, F@_1, TrUserData) 1166 | when N < 57 -> 1167 | skip_length_delimited_heartbeatResp(Rest, 1168 | N + 7, 1169 | X bsl N + Acc, 1170 | F@_1, 1171 | TrUserData); 1172 | skip_length_delimited_heartbeatResp(<<0:1, X:7, 1173 | Rest/binary>>, 1174 | N, Acc, F@_1, TrUserData) -> 1175 | Length = X bsl N + Acc, 1176 | <<_:Length/binary, Rest2/binary>> = Rest, 1177 | dfp_read_field_def_heartbeatResp(Rest2, 1178 | 0, 1179 | 0, 1180 | F@_1, 1181 | TrUserData). 1182 | 1183 | skip_group_heartbeatResp(Bin, FNum, Z2, F@_1, 1184 | TrUserData) -> 1185 | {_, Rest} = read_group(Bin, FNum), 1186 | dfp_read_field_def_heartbeatResp(Rest, 1187 | 0, 1188 | Z2, 1189 | F@_1, 1190 | TrUserData). 1191 | 1192 | skip_32_heartbeatResp(<<_:32, Rest/binary>>, Z1, Z2, 1193 | F@_1, TrUserData) -> 1194 | dfp_read_field_def_heartbeatResp(Rest, 1195 | Z1, 1196 | Z2, 1197 | F@_1, 1198 | TrUserData). 1199 | 1200 | skip_64_heartbeatResp(<<_:64, Rest/binary>>, Z1, Z2, 1201 | F@_1, TrUserData) -> 1202 | dfp_read_field_def_heartbeatResp(Rest, 1203 | Z1, 1204 | Z2, 1205 | F@_1, 1206 | TrUserData). 1207 | 1208 | d_enum_LoginStatus(0) -> 'FAILED'; 1209 | d_enum_LoginStatus(1) -> 'SUCCEEDED'; 1210 | d_enum_LoginStatus(2) -> 'NOTFOUND'; 1211 | d_enum_LoginStatus(V) -> V. 1212 | 1213 | read_group(Bin, FieldNum) -> 1214 | {NumBytes, EndTagLen} = read_gr_b(Bin, 0, 0, 0, 0, FieldNum), 1215 | <> = Bin, 1216 | {Group, Rest}. 1217 | 1218 | %% Like skipping over fields, but record the total length, 1219 | %% Each field is <(FieldNum bsl 3) bor FieldType> ++ 1220 | %% Record the length because varints may be non-optimally encoded. 1221 | %% 1222 | %% Groups can be nested, but assume the same FieldNum cannot be nested 1223 | %% because group field numbers are shared with the rest of the fields 1224 | %% numbers. Thus we can search just for an group-end with the same 1225 | %% field number. 1226 | %% 1227 | %% (The only time the same group field number could occur would 1228 | %% be in a nested sub message, but then it would be inside a 1229 | %% length-delimited entry, which we skip-read by length.) 1230 | read_gr_b(<<1:1, X:7, Tl/binary>>, N, Acc, NumBytes, TagLen, FieldNum) 1231 | when N < (32-7) -> 1232 | read_gr_b(Tl, N+7, X bsl N + Acc, NumBytes, TagLen+1, FieldNum); 1233 | read_gr_b(<<0:1, X:7, Tl/binary>>, N, Acc, NumBytes, TagLen, 1234 | FieldNum) -> 1235 | Key = X bsl N + Acc, 1236 | TagLen1 = TagLen + 1, 1237 | case {Key bsr 3, Key band 7} of 1238 | {FieldNum, 4} -> % 4 = group_end 1239 | {NumBytes, TagLen1}; 1240 | {_, 0} -> % 0 = varint 1241 | read_gr_vi(Tl, 0, NumBytes + TagLen1, FieldNum); 1242 | {_, 1} -> % 1 = bits64 1243 | <<_:64, Tl2/binary>> = Tl, 1244 | read_gr_b(Tl2, 0, 0, NumBytes + TagLen1 + 8, 0, FieldNum); 1245 | {_, 2} -> % 2 = length_delimited 1246 | read_gr_ld(Tl, 0, 0, NumBytes + TagLen1, FieldNum); 1247 | {_, 3} -> % 3 = group_start 1248 | read_gr_b(Tl, 0, 0, NumBytes + TagLen1, 0, FieldNum); 1249 | {_, 4} -> % 4 = group_end 1250 | read_gr_b(Tl, 0, 0, NumBytes + TagLen1, 0, FieldNum); 1251 | {_, 5} -> % 5 = bits32 1252 | <<_:32, Tl2/binary>> = Tl, 1253 | read_gr_b(Tl2, 0, 0, NumBytes + TagLen1 + 4, 0, FieldNum) 1254 | end. 1255 | 1256 | read_gr_vi(<<1:1, _:7, Tl/binary>>, N, NumBytes, FieldNum) 1257 | when N < (64-7) -> 1258 | read_gr_vi(Tl, N+7, NumBytes+1, FieldNum); 1259 | read_gr_vi(<<0:1, _:7, Tl/binary>>, _, NumBytes, FieldNum) -> 1260 | read_gr_b(Tl, 0, 0, NumBytes+1, 0, FieldNum). 1261 | 1262 | read_gr_ld(<<1:1, X:7, Tl/binary>>, N, Acc, NumBytes, FieldNum) 1263 | when N < (64-7) -> 1264 | read_gr_ld(Tl, N+7, X bsl N + Acc, NumBytes+1, FieldNum); 1265 | read_gr_ld(<<0:1, X:7, Tl/binary>>, N, Acc, NumBytes, FieldNum) -> 1266 | Len = X bsl N + Acc, 1267 | NumBytes1 = NumBytes + 1, 1268 | <<_:Len/binary, Tl2/binary>> = Tl, 1269 | read_gr_b(Tl2, 0, 0, NumBytes1 + Len, 0, FieldNum). 1270 | 1271 | merge_msgs(Prev, New) 1272 | when element(1, Prev) =:= element(1, New) -> 1273 | merge_msgs(Prev, New, element(1, Prev), []). 1274 | 1275 | merge_msgs(Prev, New, MsgName) when is_atom(MsgName) -> 1276 | merge_msgs(Prev, New, MsgName, []); 1277 | merge_msgs(Prev, New, Opts) 1278 | when element(1, Prev) =:= element(1, New), 1279 | is_list(Opts) -> 1280 | merge_msgs(Prev, New, element(1, Prev), Opts). 1281 | 1282 | merge_msgs(Prev, New, MsgName, Opts) -> 1283 | TrUserData = proplists:get_value(user_data, Opts), 1284 | case MsgName of 1285 | loginReq -> merge_msg_loginReq(Prev, New, TrUserData); 1286 | loginResp -> merge_msg_loginResp(Prev, New, TrUserData); 1287 | helloReq -> merge_msg_helloReq(Prev, New, TrUserData); 1288 | worldResp -> merge_msg_worldResp(Prev, New, TrUserData); 1289 | heartbeatReq -> 1290 | merge_msg_heartbeatReq(Prev, New, TrUserData); 1291 | heartbeatResp -> 1292 | merge_msg_heartbeatResp(Prev, New, TrUserData) 1293 | end. 1294 | 1295 | -compile({nowarn_unused_function,merge_msg_loginReq/3}). 1296 | merge_msg_loginReq(#loginReq{cookie = PFcookie}, 1297 | #loginReq{cookie = NFcookie}, _) -> 1298 | #loginReq{cookie = 1299 | if NFcookie =:= undefined -> PFcookie; 1300 | true -> NFcookie 1301 | end}. 1302 | 1303 | -compile({nowarn_unused_function,merge_msg_loginResp/3}). 1304 | merge_msg_loginResp(#loginResp{result = PFresult, 1305 | user_id = PFuser_id}, 1306 | #loginResp{result = NFresult, user_id = NFuser_id}, 1307 | _) -> 1308 | #loginResp{result = 1309 | if NFresult =:= undefined -> PFresult; 1310 | true -> NFresult 1311 | end, 1312 | user_id = 1313 | if NFuser_id =:= undefined -> PFuser_id; 1314 | true -> NFuser_id 1315 | end}. 1316 | 1317 | -compile({nowarn_unused_function,merge_msg_helloReq/3}). 1318 | merge_msg_helloReq(#helloReq{msg = PFmsg}, 1319 | #helloReq{msg = NFmsg}, _) -> 1320 | #helloReq{msg = 1321 | if NFmsg =:= undefined -> PFmsg; 1322 | true -> NFmsg 1323 | end}. 1324 | 1325 | -compile({nowarn_unused_function,merge_msg_worldResp/3}). 1326 | merge_msg_worldResp(#worldResp{msg = PFmsg}, 1327 | #worldResp{msg = NFmsg}, _) -> 1328 | #worldResp{msg = 1329 | if NFmsg =:= undefined -> PFmsg; 1330 | true -> NFmsg 1331 | end}. 1332 | 1333 | -compile({nowarn_unused_function,merge_msg_heartbeatReq/3}). 1334 | merge_msg_heartbeatReq(_Prev, New, _TrUserData) -> New. 1335 | 1336 | -compile({nowarn_unused_function,merge_msg_heartbeatResp/3}). 1337 | merge_msg_heartbeatResp(#heartbeatResp{unixtime = 1338 | PFunixtime}, 1339 | #heartbeatResp{unixtime = NFunixtime}, _) -> 1340 | #heartbeatResp{unixtime = 1341 | if NFunixtime =:= undefined -> PFunixtime; 1342 | true -> NFunixtime 1343 | end}. 1344 | 1345 | 1346 | verify_msg(Msg) when tuple_size(Msg) >= 1 -> 1347 | verify_msg(Msg, element(1, Msg), []); 1348 | verify_msg(X) -> 1349 | mk_type_error(not_a_known_message, X, []). 1350 | 1351 | verify_msg(Msg, MsgName) when is_atom(MsgName) -> 1352 | verify_msg(Msg, MsgName, []); 1353 | verify_msg(Msg, Opts) when tuple_size(Msg) >= 1 -> 1354 | verify_msg(Msg, element(1, Msg), Opts); 1355 | verify_msg(X, _Opts) -> 1356 | mk_type_error(not_a_known_message, X, []). 1357 | 1358 | verify_msg(Msg, MsgName, Opts) -> 1359 | TrUserData = proplists:get_value(user_data, Opts), 1360 | case MsgName of 1361 | loginReq -> v_msg_loginReq(Msg, [MsgName], TrUserData); 1362 | loginResp -> 1363 | v_msg_loginResp(Msg, [MsgName], TrUserData); 1364 | helloReq -> v_msg_helloReq(Msg, [MsgName], TrUserData); 1365 | worldResp -> 1366 | v_msg_worldResp(Msg, [MsgName], TrUserData); 1367 | heartbeatReq -> 1368 | v_msg_heartbeatReq(Msg, [MsgName], TrUserData); 1369 | heartbeatResp -> 1370 | v_msg_heartbeatResp(Msg, [MsgName], TrUserData); 1371 | _ -> mk_type_error(not_a_known_message, Msg, []) 1372 | end. 1373 | 1374 | 1375 | -compile({nowarn_unused_function,v_msg_loginReq/3}). 1376 | -dialyzer({nowarn_function,v_msg_loginReq/3}). 1377 | v_msg_loginReq(#loginReq{cookie = F1}, Path, 1378 | TrUserData) -> 1379 | if F1 == undefined -> ok; 1380 | true -> v_type_string(F1, [cookie | Path], TrUserData) 1381 | end, 1382 | ok; 1383 | v_msg_loginReq(X, Path, _TrUserData) -> 1384 | mk_type_error({expected_msg, loginReq}, X, Path). 1385 | 1386 | -compile({nowarn_unused_function,v_msg_loginResp/3}). 1387 | -dialyzer({nowarn_function,v_msg_loginResp/3}). 1388 | v_msg_loginResp(#loginResp{result = F1, user_id = F2}, 1389 | Path, TrUserData) -> 1390 | if F1 == undefined -> ok; 1391 | true -> 1392 | v_enum_LoginStatus(F1, [result | Path], TrUserData) 1393 | end, 1394 | if F2 == undefined -> ok; 1395 | true -> v_type_uint64(F2, [user_id | Path], TrUserData) 1396 | end, 1397 | ok; 1398 | v_msg_loginResp(X, Path, _TrUserData) -> 1399 | mk_type_error({expected_msg, loginResp}, X, Path). 1400 | 1401 | -compile({nowarn_unused_function,v_msg_helloReq/3}). 1402 | -dialyzer({nowarn_function,v_msg_helloReq/3}). 1403 | v_msg_helloReq(#helloReq{msg = F1}, Path, TrUserData) -> 1404 | if F1 == undefined -> ok; 1405 | true -> v_type_string(F1, [msg | Path], TrUserData) 1406 | end, 1407 | ok; 1408 | v_msg_helloReq(X, Path, _TrUserData) -> 1409 | mk_type_error({expected_msg, helloReq}, X, Path). 1410 | 1411 | -compile({nowarn_unused_function,v_msg_worldResp/3}). 1412 | -dialyzer({nowarn_function,v_msg_worldResp/3}). 1413 | v_msg_worldResp(#worldResp{msg = F1}, Path, 1414 | TrUserData) -> 1415 | if F1 == undefined -> ok; 1416 | true -> v_type_string(F1, [msg | Path], TrUserData) 1417 | end, 1418 | ok; 1419 | v_msg_worldResp(X, Path, _TrUserData) -> 1420 | mk_type_error({expected_msg, worldResp}, X, Path). 1421 | 1422 | -compile({nowarn_unused_function,v_msg_heartbeatReq/3}). 1423 | -dialyzer({nowarn_function,v_msg_heartbeatReq/3}). 1424 | v_msg_heartbeatReq(#heartbeatReq{}, _Path, _) -> ok; 1425 | v_msg_heartbeatReq(X, Path, _TrUserData) -> 1426 | mk_type_error({expected_msg, heartbeatReq}, X, Path). 1427 | 1428 | -compile({nowarn_unused_function,v_msg_heartbeatResp/3}). 1429 | -dialyzer({nowarn_function,v_msg_heartbeatResp/3}). 1430 | v_msg_heartbeatResp(#heartbeatResp{unixtime = F1}, Path, 1431 | TrUserData) -> 1432 | if F1 == undefined -> ok; 1433 | true -> v_type_uint64(F1, [unixtime | Path], TrUserData) 1434 | end, 1435 | ok; 1436 | v_msg_heartbeatResp(X, Path, _TrUserData) -> 1437 | mk_type_error({expected_msg, heartbeatResp}, X, Path). 1438 | 1439 | -compile({nowarn_unused_function,v_enum_LoginStatus/3}). 1440 | -dialyzer({nowarn_function,v_enum_LoginStatus/3}). 1441 | v_enum_LoginStatus('FAILED', _Path, _TrUserData) -> ok; 1442 | v_enum_LoginStatus('SUCCEEDED', _Path, _TrUserData) -> 1443 | ok; 1444 | v_enum_LoginStatus('NOTFOUND', _Path, _TrUserData) -> 1445 | ok; 1446 | v_enum_LoginStatus(V, Path, TrUserData) 1447 | when is_integer(V) -> 1448 | v_type_sint32(V, Path, TrUserData); 1449 | v_enum_LoginStatus(X, Path, _TrUserData) -> 1450 | mk_type_error({invalid_enum, 'LoginStatus'}, X, Path). 1451 | 1452 | -compile({nowarn_unused_function,v_type_sint32/3}). 1453 | -dialyzer({nowarn_function,v_type_sint32/3}). 1454 | v_type_sint32(N, _Path, _TrUserData) 1455 | when -2147483648 =< N, N =< 2147483647 -> 1456 | ok; 1457 | v_type_sint32(N, Path, _TrUserData) 1458 | when is_integer(N) -> 1459 | mk_type_error({value_out_of_range, sint32, signed, 32}, 1460 | N, 1461 | Path); 1462 | v_type_sint32(X, Path, _TrUserData) -> 1463 | mk_type_error({bad_integer, sint32, signed, 32}, 1464 | X, 1465 | Path). 1466 | 1467 | -compile({nowarn_unused_function,v_type_uint64/3}). 1468 | -dialyzer({nowarn_function,v_type_uint64/3}). 1469 | v_type_uint64(N, _Path, _TrUserData) 1470 | when 0 =< N, N =< 18446744073709551615 -> 1471 | ok; 1472 | v_type_uint64(N, Path, _TrUserData) 1473 | when is_integer(N) -> 1474 | mk_type_error({value_out_of_range, 1475 | uint64, 1476 | unsigned, 1477 | 64}, 1478 | N, 1479 | Path); 1480 | v_type_uint64(X, Path, _TrUserData) -> 1481 | mk_type_error({bad_integer, uint64, unsigned, 64}, 1482 | X, 1483 | Path). 1484 | 1485 | -compile({nowarn_unused_function,v_type_string/3}). 1486 | -dialyzer({nowarn_function,v_type_string/3}). 1487 | v_type_string(S, Path, _TrUserData) 1488 | when is_list(S); is_binary(S) -> 1489 | try unicode:characters_to_binary(S) of 1490 | B when is_binary(B) -> ok; 1491 | {error, _, _} -> 1492 | mk_type_error(bad_unicode_string, S, Path) 1493 | catch 1494 | error:badarg -> 1495 | mk_type_error(bad_unicode_string, S, Path) 1496 | end; 1497 | v_type_string(X, Path, _TrUserData) -> 1498 | mk_type_error(bad_unicode_string, X, Path). 1499 | 1500 | -compile({nowarn_unused_function,mk_type_error/3}). 1501 | -spec mk_type_error(_, _, list()) -> no_return(). 1502 | mk_type_error(Error, ValueSeen, Path) -> 1503 | Path2 = prettify_path(Path), 1504 | erlang:error({gpb_type_error, 1505 | {Error, [{value, ValueSeen}, {path, Path2}]}}). 1506 | 1507 | 1508 | -compile({nowarn_unused_function,prettify_path/1}). 1509 | -dialyzer({nowarn_function,prettify_path/1}). 1510 | prettify_path([]) -> top_level; 1511 | prettify_path(PathR) -> 1512 | list_to_atom(lists:append(lists:join(".", 1513 | lists:map(fun atom_to_list/1, 1514 | lists:reverse(PathR))))). 1515 | 1516 | 1517 | -compile({nowarn_unused_function,id/2}). 1518 | -compile({inline,id/2}). 1519 | id(X, _TrUserData) -> X. 1520 | 1521 | -compile({nowarn_unused_function,v_ok/3}). 1522 | -compile({inline,v_ok/3}). 1523 | v_ok(_Value, _Path, _TrUserData) -> ok. 1524 | 1525 | -compile({nowarn_unused_function,m_overwrite/3}). 1526 | -compile({inline,m_overwrite/3}). 1527 | m_overwrite(_Prev, New, _TrUserData) -> New. 1528 | 1529 | -compile({nowarn_unused_function,cons/3}). 1530 | -compile({inline,cons/3}). 1531 | cons(Elem, Acc, _TrUserData) -> [Elem | Acc]. 1532 | 1533 | -compile({nowarn_unused_function,lists_reverse/2}). 1534 | -compile({inline,lists_reverse/2}). 1535 | 'lists_reverse'(L, _TrUserData) -> lists:reverse(L). 1536 | -compile({nowarn_unused_function,'erlang_++'/3}). 1537 | -compile({inline,'erlang_++'/3}). 1538 | 'erlang_++'(A, B, _TrUserData) -> A ++ B. 1539 | 1540 | 1541 | get_msg_defs() -> 1542 | [{{enum, 'LoginStatus'}, 1543 | [{'FAILED', 0}, {'SUCCEEDED', 1}, {'NOTFOUND', 2}]}, 1544 | {{msg, loginReq}, 1545 | [#field{name = cookie, fnum = 1, rnum = 2, 1546 | type = string, occurrence = optional, opts = []}]}, 1547 | {{msg, loginResp}, 1548 | [#field{name = result, fnum = 1, rnum = 2, 1549 | type = {enum, 'LoginStatus'}, occurrence = optional, 1550 | opts = []}, 1551 | #field{name = user_id, fnum = 2, rnum = 3, 1552 | type = uint64, occurrence = optional, opts = []}]}, 1553 | {{msg, helloReq}, 1554 | [#field{name = msg, fnum = 1, rnum = 2, type = string, 1555 | occurrence = optional, opts = []}]}, 1556 | {{msg, worldResp}, 1557 | [#field{name = msg, fnum = 1, rnum = 2, type = string, 1558 | occurrence = optional, opts = []}]}, 1559 | {{msg, heartbeatReq}, []}, 1560 | {{msg, heartbeatResp}, 1561 | [#field{name = unixtime, fnum = 1, rnum = 2, 1562 | type = uint64, occurrence = optional, opts = []}]}]. 1563 | 1564 | 1565 | get_msg_names() -> 1566 | [loginReq, 1567 | loginResp, 1568 | helloReq, 1569 | worldResp, 1570 | heartbeatReq, 1571 | heartbeatResp]. 1572 | 1573 | 1574 | get_group_names() -> []. 1575 | 1576 | 1577 | get_msg_or_group_names() -> 1578 | [loginReq, 1579 | loginResp, 1580 | helloReq, 1581 | worldResp, 1582 | heartbeatReq, 1583 | heartbeatResp]. 1584 | 1585 | 1586 | get_enum_names() -> ['LoginStatus']. 1587 | 1588 | 1589 | fetch_msg_def(MsgName) -> 1590 | case find_msg_def(MsgName) of 1591 | Fs when is_list(Fs) -> Fs; 1592 | error -> erlang:error({no_such_msg, MsgName}) 1593 | end. 1594 | 1595 | 1596 | fetch_enum_def(EnumName) -> 1597 | case find_enum_def(EnumName) of 1598 | Es when is_list(Es) -> Es; 1599 | error -> erlang:error({no_such_enum, EnumName}) 1600 | end. 1601 | 1602 | 1603 | find_msg_def(loginReq) -> 1604 | [#field{name = cookie, fnum = 1, rnum = 2, 1605 | type = string, occurrence = optional, opts = []}]; 1606 | find_msg_def(loginResp) -> 1607 | [#field{name = result, fnum = 1, rnum = 2, 1608 | type = {enum, 'LoginStatus'}, occurrence = optional, 1609 | opts = []}, 1610 | #field{name = user_id, fnum = 2, rnum = 3, 1611 | type = uint64, occurrence = optional, opts = []}]; 1612 | find_msg_def(helloReq) -> 1613 | [#field{name = msg, fnum = 1, rnum = 2, type = string, 1614 | occurrence = optional, opts = []}]; 1615 | find_msg_def(worldResp) -> 1616 | [#field{name = msg, fnum = 1, rnum = 2, type = string, 1617 | occurrence = optional, opts = []}]; 1618 | find_msg_def(heartbeatReq) -> []; 1619 | find_msg_def(heartbeatResp) -> 1620 | [#field{name = unixtime, fnum = 1, rnum = 2, 1621 | type = uint64, occurrence = optional, opts = []}]; 1622 | find_msg_def(_) -> error. 1623 | 1624 | 1625 | find_enum_def('LoginStatus') -> 1626 | [{'FAILED', 0}, {'SUCCEEDED', 1}, {'NOTFOUND', 2}]; 1627 | find_enum_def(_) -> error. 1628 | 1629 | 1630 | enum_symbol_by_value('LoginStatus', Value) -> 1631 | enum_symbol_by_value_LoginStatus(Value). 1632 | 1633 | 1634 | enum_value_by_symbol('LoginStatus', Sym) -> 1635 | enum_value_by_symbol_LoginStatus(Sym). 1636 | 1637 | 1638 | enum_symbol_by_value_LoginStatus(0) -> 'FAILED'; 1639 | enum_symbol_by_value_LoginStatus(1) -> 'SUCCEEDED'; 1640 | enum_symbol_by_value_LoginStatus(2) -> 'NOTFOUND'. 1641 | 1642 | 1643 | enum_value_by_symbol_LoginStatus('FAILED') -> 0; 1644 | enum_value_by_symbol_LoginStatus('SUCCEEDED') -> 1; 1645 | enum_value_by_symbol_LoginStatus('NOTFOUND') -> 2. 1646 | 1647 | 1648 | get_service_names() -> []. 1649 | 1650 | 1651 | get_service_def(_) -> error. 1652 | 1653 | 1654 | get_rpc_names(_) -> error. 1655 | 1656 | 1657 | find_rpc_def(_, _) -> error. 1658 | 1659 | 1660 | 1661 | -spec fetch_rpc_def(_, _) -> no_return(). 1662 | fetch_rpc_def(ServiceName, RpcName) -> 1663 | erlang:error({no_such_rpc, ServiceName, RpcName}). 1664 | 1665 | 1666 | %% Convert a a fully qualified (ie with package name) service name 1667 | %% as a binary to a service name as an atom. 1668 | -spec fqbin_to_service_name(_) -> no_return(). 1669 | fqbin_to_service_name(X) -> 1670 | error({gpb_error, {badservice, X}}). 1671 | 1672 | 1673 | %% Convert a service name as an atom to a fully qualified 1674 | %% (ie with package name) name as a binary. 1675 | -spec service_name_to_fqbin(_) -> no_return(). 1676 | service_name_to_fqbin(X) -> 1677 | error({gpb_error, {badservice, X}}). 1678 | 1679 | 1680 | %% Convert a a fully qualified (ie with package name) service name 1681 | %% and an rpc name, both as binaries to a service name and an rpc 1682 | %% name, as atoms. 1683 | -spec fqbins_to_service_and_rpc_name(_, _) -> no_return(). 1684 | fqbins_to_service_and_rpc_name(S, R) -> 1685 | error({gpb_error, {badservice_or_rpc, {S, R}}}). 1686 | 1687 | 1688 | %% Convert a service name and an rpc name, both as atoms, 1689 | %% to a fully qualified (ie with package name) service name and 1690 | %% an rpc name as binaries. 1691 | -spec service_and_rpc_name_to_fqbins(_, _) -> no_return(). 1692 | service_and_rpc_name_to_fqbins(S, R) -> 1693 | error({gpb_error, {badservice_or_rpc, {S, R}}}). 1694 | 1695 | 1696 | fqbin_to_msg_name(<<"common.proto.loginReq">>) -> loginReq; 1697 | fqbin_to_msg_name(<<"common.proto.loginResp">>) -> loginResp; 1698 | fqbin_to_msg_name(<<"common.proto.helloReq">>) -> helloReq; 1699 | fqbin_to_msg_name(<<"common.proto.worldResp">>) -> worldResp; 1700 | fqbin_to_msg_name(<<"common.proto.heartbeatReq">>) -> heartbeatReq; 1701 | fqbin_to_msg_name(<<"common.proto.heartbeatResp">>) -> heartbeatResp; 1702 | fqbin_to_msg_name(E) -> error({gpb_error, {badmsg, E}}). 1703 | 1704 | 1705 | msg_name_to_fqbin(loginReq) -> <<"common.proto.loginReq">>; 1706 | msg_name_to_fqbin(loginResp) -> <<"common.proto.loginResp">>; 1707 | msg_name_to_fqbin(helloReq) -> <<"common.proto.helloReq">>; 1708 | msg_name_to_fqbin(worldResp) -> <<"common.proto.worldResp">>; 1709 | msg_name_to_fqbin(heartbeatReq) -> <<"common.proto.heartbeatReq">>; 1710 | msg_name_to_fqbin(heartbeatResp) -> <<"common.proto.heartbeatResp">>; 1711 | msg_name_to_fqbin(E) -> error({gpb_error, {badmsg, E}}). 1712 | 1713 | 1714 | fqbin_to_enum_name(<<"common.proto.LoginStatus">>) -> 'LoginStatus'; 1715 | fqbin_to_enum_name(E) -> 1716 | error({gpb_error, {badenum, E}}). 1717 | 1718 | 1719 | enum_name_to_fqbin('LoginStatus') -> <<"common.proto.LoginStatus">>; 1720 | enum_name_to_fqbin(E) -> 1721 | error({gpb_error, {badenum, E}}). 1722 | 1723 | 1724 | get_package_name() -> 'common.proto'. 1725 | 1726 | 1727 | %% Whether or not the message names 1728 | %% are prepended with package name or not. 1729 | uses_packages() -> false. 1730 | 1731 | 1732 | source_basename() -> "common.proto". 1733 | 1734 | 1735 | %% Retrieve all proto file names, also imported ones. 1736 | %% The order is top-down. The first element is always the main 1737 | %% source file. The files are returned with extension, 1738 | %% see get_all_proto_names/0 for a version that returns 1739 | %% the basenames sans extension 1740 | get_all_source_basenames() -> ["common.proto"]. 1741 | 1742 | 1743 | %% Retrieve all proto file names, also imported ones. 1744 | %% The order is top-down. The first element is always the main 1745 | %% source file. The files are returned sans .proto extension, 1746 | %% to make it easier to use them with the various get_xyz_containment 1747 | %% functions. 1748 | get_all_proto_names() -> ["common"]. 1749 | 1750 | 1751 | get_msg_containment("common") -> 1752 | [heartbeatReq, 1753 | heartbeatResp, 1754 | helloReq, 1755 | loginReq, 1756 | loginResp, 1757 | worldResp]; 1758 | get_msg_containment(P) -> 1759 | error({gpb_error, {badproto, P}}). 1760 | 1761 | 1762 | get_pkg_containment("common") -> undefined; 1763 | get_pkg_containment(P) -> 1764 | error({gpb_error, {badproto, P}}). 1765 | 1766 | 1767 | get_service_containment("common") -> []; 1768 | get_service_containment(P) -> 1769 | error({gpb_error, {badproto, P}}). 1770 | 1771 | 1772 | get_rpc_containment("common") -> []; 1773 | get_rpc_containment(P) -> 1774 | error({gpb_error, {badproto, P}}). 1775 | 1776 | 1777 | get_enum_containment("common") -> ['LoginStatus']; 1778 | get_enum_containment(P) -> 1779 | error({gpb_error, {badproto, P}}). 1780 | 1781 | 1782 | get_proto_by_msg_name_as_fqbin(<<"common.proto.worldResp">>) -> "common"; 1783 | get_proto_by_msg_name_as_fqbin(<<"common.proto.loginResp">>) -> "common"; 1784 | get_proto_by_msg_name_as_fqbin(<<"common.proto.heartbeatResp">>) -> "common"; 1785 | get_proto_by_msg_name_as_fqbin(<<"common.proto.loginReq">>) -> "common"; 1786 | get_proto_by_msg_name_as_fqbin(<<"common.proto.helloReq">>) -> "common"; 1787 | get_proto_by_msg_name_as_fqbin(<<"common.proto.heartbeatReq">>) -> "common"; 1788 | get_proto_by_msg_name_as_fqbin(E) -> 1789 | error({gpb_error, {badmsg, E}}). 1790 | 1791 | 1792 | -spec get_proto_by_service_name_as_fqbin(_) -> no_return(). 1793 | get_proto_by_service_name_as_fqbin(E) -> 1794 | error({gpb_error, {badservice, E}}). 1795 | 1796 | 1797 | get_proto_by_enum_name_as_fqbin(<<"common.proto.LoginStatus">>) -> "common"; 1798 | get_proto_by_enum_name_as_fqbin(E) -> 1799 | error({gpb_error, {badenum, E}}). 1800 | 1801 | 1802 | -spec get_protos_by_pkg_name_as_fqbin(_) -> no_return(). 1803 | get_protos_by_pkg_name_as_fqbin(E) -> 1804 | error({gpb_error, {badpkg, E}}). 1805 | 1806 | 1807 | 1808 | gpb_version_as_string() -> 1809 | "4.10.1". 1810 | 1811 | gpb_version_as_list() -> 1812 | [4,10,1]. 1813 | --------------------------------------------------------------------------------