gen_server通用模板

 1 %%%-------------------------------------------------------------------
 2 %%% File    : gen_server_template.full
 3 %%% Author  : my name <yourname@localhost.localdomain>
 4 %%% Description : 
 5 %%%
 6 %%% Created :  2 Mar 2007 by my name <yourname@localhost.localdomain>
 7 %%%-------------------------------------------------------------------
 8 -module(template).
 9 
10 -behaviour(gen_server).
11 
12 %% API
13 -export([start_link/0]).
14 
15 %% gen_server callbacks
16 -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
17      terminate/2, code_change/3]).
18 
19 -record(state, {}).
20 
21 %%====================================================================
22 %% API
23 %%====================================================================
24 %%--------------------------------------------------------------------
25 %% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
26 %% Description: Starts the server
27 %%--------------------------------------------------------------------
28 start_link() ->
29     gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
30 
31 %%====================================================================
32 %% gen_server callbacks
33 %%====================================================================
34 
35 %%--------------------------------------------------------------------
36 %% Function: init(Args) -> {ok, State} |
37 %%                         {ok, State, Timeout} |
38 %%                         ignore               |
39 %%                         {stop, Reason}
40 %% Description: Initiates the server
41 %%--------------------------------------------------------------------
42 init([]) ->
43     {ok, #state{}}.
44 
45 %%--------------------------------------------------------------------
46 %% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
47 %%                                      {reply, Reply, State, Timeout} |
48 %%                                      {noreply, State} |
49 %%                                      {noreply, State, Timeout} |
50 %%                                      {stop, Reason, Reply, State} |
51 %%                                      {stop, Reason, State}
52 %% Description: Handling call messages
53 %%--------------------------------------------------------------------
54 handle_call(_Request, _From, State) ->
55     Reply = ok,
56     {reply, Reply, State}.
57 
58 %%--------------------------------------------------------------------
59 %% Function: handle_cast(Msg, State) -> {noreply, State} |
60 %%                                      {noreply, State, Timeout} |
61 %%                                      {stop, Reason, State}
62 %% Description: Handling cast messages
63 %%--------------------------------------------------------------------
64 handle_cast(_Msg, State) ->
65     {noreply, State}.
66 
67 %%--------------------------------------------------------------------
68 %% Function: handle_info(Info, State) -> {noreply, State} |
69 %%                                       {noreply, State, Timeout} |
70 %%                                       {stop, Reason, State}
71 %% Description: Handling all non call/cast messages
72 %%--------------------------------------------------------------------
73 handle_info(_Info, State) ->
74     {noreply, State}.
75 
76 %%--------------------------------------------------------------------
77 %% Function: terminate(Reason, State) -> void()
78 %% Description: This function is called by a gen_server when it is about to
79 %% terminate. It should be the opposite of Module:init/1 and do any necessary
80 %% cleaning up. When it returns, the gen_server terminates with Reason.
81 %% The return value is ignored.
82 %%--------------------------------------------------------------------
83 terminate(_Reason, _State) ->
84     ok.
85 
86 %%--------------------------------------------------------------------
87 %% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
88 %% Description: Convert process state when code is changed
89 %%--------------------------------------------------------------------
90 code_change(_OldVsn, State, _Extra) ->
91     {ok, State}.
92 
93 %%--------------------------------------------------------------------
94 %%% Internal functions
95 %%--------------------------------------------------------------------

 

转载于:https://www.cnblogs.com/study-development/archive/2013/03/26/2982139.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值