gen_server模板

gen_server代码模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
%% gen_server代码模板
-module(new_file).

-behaviour(gen_server).
% --------------------------------------------------------------------
% Include files
% --------------------------------------------------------------------

% --------------------------------------------------------------------
% External exports
-export([]).

% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

-record(state, {}).



% --------------------------------------------------------------------
% Function: init/1
% Description: Initiates the server
% Returns: {ok, State} |
% {ok, State, Timeout} |
% ignore |
% {stop, Reason}
% --------------------------------------------------------------------
init([]) ->
{ok, #state{}}.

% --------------------------------------------------------------------
% Function: handle_call/3
% Description: Handling call messages
% Returns: {reply, Reply, State} |
% {reply, Reply, State, Timeout} |
% {noreply, State} |
% {noreply, State, Timeout} |
% {stop, Reason, Reply, State} | (terminate/2 is called)
% {stop, Reason, State} (terminate/2 is called)
% --------------------------------------------------------------------
handle_call(Request, From, State) ->
Reply = ok,
{reply, Reply, State}.

% --------------------------------------------------------------------
% Function: handle_cast/2
% Description: Handling cast messages
% Returns: {noreply, State} |
% {noreply, State, Timeout} |
% {stop, Reason, State} (terminate/2 is called)
% --------------------------------------------------------------------
handle_cast(Msg, State) ->
{noreply, State}.

% --------------------------------------------------------------------
% Function: handle_info/2
% Description: Handling all non call/cast messages
% Returns: {noreply, State} |
% {noreply, State, Timeout} |
% {stop, Reason, State} (terminate/2 is called)
% --------------------------------------------------------------------
handle_info(Info, State) ->
{noreply, State}.

% --------------------------------------------------------------------
% Function: terminate/2
% Description: Shutdown the server
% Returns: any (ignored by gen_server)
% --------------------------------------------------------------------
terminate(Reason, State) ->
ok.

% --------------------------------------------------------------------
% Func: code_change/3
% Purpose: Convert process state when code is changed
% Returns: {ok, NewState}
% --------------------------------------------------------------------
code_change(OldVsn, State, Extra) ->
{ok, State}.