1 module hunt.imf.protocol.protobuf.ProtobufProtocol;
2 
3 import hunt.imf.protocol.Protocol;
4 import hunt.net.codec.Codec;
5 import hunt.imf.ConnectionEventBaseHandler;
6 
7 import hunt.imf.protocol.protobuf.TcpConnectionEventHandler;
8 import hunt.imf.protocol.protobuf.ProtobufCodec;
9 import hunt.imf.GatewayApplication;
10 import hunt.net.NetServerOptions;
11 import hunt.imf.ConnectionManager;
12 import hunt.imf.ConnectBase;
13 import hunt.net;
14 
15 class ProtobufProtocol : Protocol {
16 
17     alias CloseCallBack = void delegate(ConnectBase connection);
18 
19     private {
20         string _host;
21         ushort _port;
22         enum string _name = typeof(this).stringof;
23         ConnectionEventBaseHandler _handler;
24         NetServerOptions _options = null;
25         Codec _codec;
26     }
27 
28     this(string host , ushort port)
29     {
30         _host = host;
31         _port = port;
32         _handler = new TcpConnectionEventHandler(_name);
33         _codec = new ProtobufCodec();
34     }
35 
36     override void registerHandler()
37     {
38         GatewayApplication.instance().registerConnectionManager(_name);
39         ConnectionManager!int manager = GatewayApplication.instance().getConnectionManager(_name);
40         _handler.setOnConnection(&manager.onConnection);
41         _handler.setOnClosed(&manager.onClosed);
42     }
43 
44     void setDisConnectHandler (CloseCallBack handler)
45     {
46         GatewayApplication.instance().registerConnectionManager(_name);
47         ConnectionManager!int manager = GatewayApplication.instance().getConnectionManager(_name);
48         if (manager !is null )
49         {
50             manager.setCloseHandler(handler);
51         }
52     }
53 
54     void setCodec(Codec codec) {
55         _codec = codec;
56     }
57 
58     void setConnectionEventHandler(ConnectionEventBaseHandler handler)
59     {
60         _handler = handler;
61     }
62 
63     override NetServerOptions getOptions()
64     {
65         return _options;
66     }
67 
68     override string getName() {return _name;}
69 
70     override ushort getPort() {return _port;}
71 
72     override ConnectionEventHandler getHandler() {return _handler;}
73 
74     override Codec getCodec() {return _codec;}
75 
76     override string getHost() {return _host;}
77 }