1 module hunt.imf.ConnectionManager; 2 3 import hunt.collection.HashMap; 4 import hunt.imf.ConnectBase; 5 import hunt.imf.protocol.protobuf.ProtobufTcpConnection; 6 import hunt.imf.protocol.http.HttpConnection; 7 import hunt.imf.protocol.websocket.WsConnection; 8 import hunt.imf.protocol.protobuf.TcpConnectionEventHandler; 9 import hunt.imf.protocol.http.HttpConnectionEventHandler; 10 import hunt.imf.protocol.websocket.WsConnectionEventHandler; 11 import hunt.http.codec.websocket.stream.WebSocketConnection; 12 import hunt.net; 13 import hunt.logging; 14 import std.conv : to; 15 16 class ConnectionManager(T) { 17 18 alias CloseCallBack = void delegate(ConnectBase connection); 19 20 private { 21 HashMap!(T,ConnectBase) _mapConns; 22 string _protocolName; 23 CloseCallBack _onClosed = null; 24 } 25 26 this () 27 { 28 _mapConns = new HashMap!(T,ConnectBase); 29 } 30 31 32 void onConnection ( ConnectBase connection) 33 { 34 synchronized(this) 35 { 36 trace("----------------put--%s",connection.getProtocol()); 37 _mapConns.put(connection.getConnection().getId().to!T,connection); 38 } 39 } 40 41 void onClosed(ConnectBase connection) 42 { 43 if (_onClosed !is null) 44 { 45 _onClosed(connection); 46 } 47 synchronized(this){ 48 trace("----------------del--%s",connection.getProtocol()); 49 _mapConns.remove(connection.getConnection().getId().to!T); 50 } 51 } 52 53 ConnectBase getConnection(T connId) 54 { 55 synchronized(this) 56 { 57 return _mapConns.get(connId); 58 } 59 } 60 61 void putConnection(T connId ,ConnectBase conn) 62 { 63 synchronized(this) 64 { 65 _mapConns.put(connId,conn); 66 } 67 } 68 69 void removeConnection(T connId) 70 { 71 synchronized(this) 72 { 73 _mapConns.remove(connId); 74 } 75 } 76 77 bool isExist(T connId) 78 { 79 HashMap!(T,ConnectBase) temp = null; 80 synchronized(this) 81 { 82 temp = _mapConns; 83 } 84 return temp.containsKey(connId); 85 } 86 87 void setProtocolName(string name) 88 { 89 _protocolName = name; 90 } 91 92 string getProtocolName() 93 { 94 return _protocolName; 95 } 96 97 void setCloseHandler (CloseCallBack callback) 98 { 99 _onClosed = callback; 100 } 101 102 }