1 module hunt.imf.protocol.protobuf.TcpConnectionEventHandler;
2 
3 import hunt.net;
4 import hunt.imf.protocol.protobuf.ProtobufTcpConnection;
5 import hunt.imf.ConnectionEventBaseHandler;
6 import hunt.imf.ConnectBase;
7 import hunt.imf.MessageBuffer;
8 import hunt.String;
9 import std.stdio;
10 
11 class TcpConnectionEventHandler : ConnectionEventBaseHandler
12 {
13     //alias ConnCallBack = void delegate(ConnectBase connection);
14     //alias MsgCallBack = void delegate(Connection connection ,Object message);
15 
16     this(string attribute){
17         _attribute = attribute;
18     }
19 
20     override
21     void connectionOpened(Connection connection)
22     {
23         if (_onConnection !is null)
24         {
25             connection.setAttribute(SESSION.PROTOCOL,new String(_attribute));
26             ProtobufTcpConnection conn = new ProtobufTcpConnection(connection);
27             _onConnection(conn);
28         }
29     }
30 
31     override
32     void connectionClosed(Connection connection)
33     {
34         connection.setState(ConnectionState.Closed);
35         if (_onClosed !is null )
36         {
37             ProtobufTcpConnection conn = new ProtobufTcpConnection(connection);
38             _onClosed(conn);
39         }
40     }
41 
42     override
43     void messageReceived(Connection connection, Object message)
44     {
45         MessageBuffer msg = cast(MessageBuffer)message;
46         ConnectBase.dispatchMessage(new ProtobufTcpConnection(connection),msg);
47     }
48 
49     override
50     void exceptionCaught(Connection connection, Throwable t)
51     {
52 
53     }
54 
55     override
56     void failedOpeningConnection(int connectionId, Throwable t) { }
57 
58     override
59     void failedAcceptingConnection(int connectionId, Throwable t) { }
60 
61     override
62     void setOnConnection(ConnCallBack callback)
63     {
64         _onConnection = callback;
65     }
66 
67     override
68     void setOnClosed(ConnCallBack callback)
69     {
70         _onClosed = callback;
71     }
72 
73     override
74     void setOnMessage(MsgCallBack callback)
75     {
76         _onMessage = callback;
77     }
78 
79 private
80 {
81     string _attribute = null;
82     ConnCallBack _onConnection = null;
83     ConnCallBack _onClosed = null;
84     MsgCallBack _onMessage = null;
85 }
86 
87 }
88