1 module hunt.imf.protocol.websocket.WsProtocol;
2 
3 import hunt.imf.protocol.Protocol;
4 import hunt.net.codec.Codec;
5 import hunt.imf.ConnectionEventBaseHandler;
6 import hunt.http.server.HttpServerOptions;
7 import hunt.imf.protocol.websocket.WsConnectionEventHandler;
8 import hunt.http.HttpOptions;
9 import hunt.http.server.ServerHttpHandler;
10 import hunt.http.codec.http.stream;
11 import hunt.http.server.Http2ServerRequestHandler;
12 import hunt.http.server.ServerSessionListener;
13 import hunt.http.codec.http.model.MetaData;
14 import hunt.net.NetServerOptions;
15 import hunt.http.server.WebSocketHandler;
16 import hunt.http.server.HttpServerHandler;
17 import hunt.net.Connection;
18 import hunt.imf.ConnectionManager;
19 import hunt.imf.protocol.websocket.WsConnectionEventHandler;
20 import hunt.imf.protocol.websocket.WsCodec;
21 import hunt.imf.GatewayApplication;
22 import hunt.logging;
23 import hunt.imf.ConnectBase;
24 
25 class WsProtocol : Protocol{
26 
27     alias CloseCallBack = void delegate(ConnectBase connection);
28 
29     private {
30         string _host;
31         ushort _port;
32         enum string _name = typeof(this).stringof;
33         ConnectionEventHandler _handler = null;
34         NetServerOptions _options = null;
35         ServerHttpHandler _serverHandler = null;
36         ServerSessionListener _listener = null;
37         WsConnectionEventHandler _eventHandler = null;
38         HttpServerOptions _httpOptions = null;
39         Codec _codec;
40     }
41 
42     this(string host , ushort port)
43     {
44         _host = host;
45         _port = port;
46         _codec = new WsCodec();
47 
48         HttpServerOptions config = new HttpServerOptions();
49         _options = config.getTcpConfiguration();
50         if(_options is null ) {
51             _options = new NetServerOptions();
52             config.setTcpConfiguration(_options);
53         }
54         _httpOptions = config;
55         _serverHandler = new class ServerHttpHandlerAdapter {
56             override
57             bool messageComplete(HttpRequest request, HttpResponse response,
58             HttpOutputStream output,
59             HttpConnection connection) {
60                 return true;
61             }
62         };
63         _listener = new Http2ServerRequestHandler(_serverHandler);
64         _eventHandler = new WsConnectionEventHandler(_name);
65     }
66 
67     override void registerHandler()
68     {
69         GatewayApplication.instance().registerConnectionManager(_name);
70         ConnectionManager!int manager = GatewayApplication.instance().getConnectionManager(_name);
71         _eventHandler.setOnConnection(&manager.onConnection);
72         _eventHandler.setOnClosed(&manager.onClosed);
73     }
74 
75     void setDisConnectHandler (CloseCallBack handler)
76     {
77         GatewayApplication.instance().registerConnectionManager(_name);
78         ConnectionManager!int manager = GatewayApplication.instance().getConnectionManager(_name);
79         if (manager !is null )
80         {
81             manager.setCloseHandler(handler);
82         }
83     }
84 
85     override NetServerOptions getOptions()
86     {
87         return _options;
88     }
89 
90     override string getName() {return _name;}
91 
92     override ushort getPort() {return _port;}
93 
94     override ConnectionEventHandler getHandler()
95     {
96         if (_handler is null)
97         {
98             _handler = new HttpServerHandler(_httpOptions, _listener,_serverHandler, _eventHandler);
99         }
100         return _handler;
101     }
102 
103     override Codec getCodec() {return _codec;}
104 
105     override string getHost() {return _host;}
106 }