源战役
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 line
4.9 KiB

  1. %% ---------------------------------------------------------------------------
  2. %% @doc gen_server模板
  3. %% @author hek
  4. %% @since 2016-11-14
  5. %% @deprecated
  6. %% ---------------------------------------------------------------------------
  7. -define(DEBUG_MODULE, none).
  8. -define(DEBUG_REQUEST, none).
  9. -define(LOCAL_DEBUG(Format, Args),
  10. case ?MODULE of
  11. ?DEBUG_MODULE -> ?PRINT(Format, Args);
  12. _ -> skip
  13. end
  14. ).
  15. -define(LOCAL_DEBUG(Request, Format, Args),
  16. case {?MODULE, Request} of
  17. {?DEBUG_MODULE, DEBUG_REQUEST} -> ?PRINT(Format, Args);
  18. _ -> skip
  19. end
  20. ).
  21. %% --------------------------------------------------------------------------
  22. %% Function: init/1
  23. %% Description: Initiates the server
  24. %% Returns: {ok, State} |
  25. %% {ok, State, Timeout} |
  26. %% ignore |
  27. %% {stop, Reason}
  28. %% --------------------------------------------------------------------------
  29. -define(DO_INIT(Args),
  30. case catch do_init(Args) of
  31. {'EXIT', Reason} ->
  32. ?ERR("init error:~n~p~n", [Reason]),
  33. {stop, Reason};
  34. {ok, State} ->
  35. {ok, State};
  36. Other ->
  37. {stop, Other}
  38. end
  39. ).
  40. %% --------------------------------------------------------------------------
  41. %% Function: handle_call/3
  42. %% Description: Handling call messages
  43. %% Returns: {reply, Reply, State} |
  44. %% {reply, Reply, State, Timeout} |
  45. %% {noreply, State} |
  46. %% {noreply, State, Timeout} |
  47. %% {stop, Reason, Reply, State} | (terminate/2 is called)
  48. %% {stop, Reason, State} (terminate/2 is called)
  49. %% --------------------------------------------------------------------------
  50. -define(DO_HANDLE_CALL(Request, From, State),
  51. case catch do_handle_call(Request, From, State) of
  52. {'EXIT', Reason} ->
  53. ?ERR("handle_call error:~n~p~n", [Reason]),
  54. {reply, error, State};
  55. {reply, Reply, NewState} ->
  56. {reply, Reply, NewState};
  57. _ ->
  58. {reply, true, State}
  59. end
  60. ).
  61. %% --------------------------------------------------------------------------
  62. %% Function: handle_cast/2
  63. %% Description: Handling cast messages
  64. %% Returns: {noreply, State} |
  65. %% {noreply, State, Timeout} |
  66. %% {stop, Reason, State} (terminate/2 is called)
  67. %% --------------------------------------------------------------------------
  68. -define(DO_HANDLE_CAST(Msg, State),
  69. case catch do_handle_cast(Msg, State) of
  70. {'EXIT', Reason} ->
  71. ?ERR("handle_cast error:~n~p~n", [Reason]),
  72. {noreply, State};
  73. {noreply, NewState} ->
  74. {noreply, NewState};
  75. {stop, normal, NewState} ->
  76. {stop, normal, NewState};
  77. _ ->
  78. {noreply, State}
  79. end
  80. ).
  81. %% --------------------------------------------------------------------------
  82. %% Function: handle_info/2
  83. %% Description: Handling all non call/cast messages
  84. %% Returns: {noreply, State} |
  85. %% {noreply, State, Timeout} |
  86. %% {stop, Reason, State} (terminate/2 is called)
  87. %% --------------------------------------------------------------------------
  88. -define(DO_HANDLE_INFO(Info, State),
  89. case catch do_handle_info(Info, State) of
  90. {'EXIT', Reason} ->
  91. ?ERR("handle_info error:~n~p~n", [Reason]),
  92. {noreply, State};
  93. {noreply, NewState} ->
  94. {noreply, NewState};
  95. _ ->
  96. {noreply, State}
  97. end
  98. ).
  99. %% --------------------------------------------------------------------------
  100. %% Function: terminate/2
  101. %% Description: Shutdown the server
  102. %% Returns: any (ignored by gen_server)
  103. %% --------------------------------------------------------------------------
  104. -define(DO_TERMINATE(Reason, State),
  105. case catch do_terminate(Reason, State) of
  106. ok -> ok;
  107. {'EXIT', Reason} ->
  108. ?ERR("terminate error:~n~p~n", [Reason]),
  109. {noreply, State};
  110. {noreply, NewState} ->
  111. {noreply, NewState};
  112. _ ->
  113. {noreply, State}
  114. end
  115. ).
  116. %% --------------------------------------------------------------------------
  117. %% Func: code_change/3
  118. %% Purpose: Convert process state when code is changed
  119. %% Returns: {ok, NewState}
  120. %% --------------------------------------------------------------------------
  121. -define(DO_CODE_CHANGE(OldVsn, State, Extra),
  122. case catch do_code_change(OldVsn, State, Extra) of
  123. {ok, NewState} -> {ok, NewState};
  124. {'EXIT', Reason} ->
  125. ?ERR("code_change error:~n~p~n", [Reason]),
  126. {ok, State};
  127. {noreply, NewState} ->
  128. {ok, NewState};
  129. _ ->
  130. {ok, State}
  131. end
  132. ).