1 2 3 4 module kissrpc.RpcManager; 5 6 import kissrpc.RpcServer; 7 import kiss.aio.AsynchronousChannelThreadGroup; 8 import kissrpc.Logs; 9 import kissrpc.RpcServerListener; 10 11 import std.experimental.logger.core; 12 13 14 class RpcManager { 15 16 public: 17 this() { 18 19 } 20 21 static @property getInstance() { 22 if (_instance is null) { 23 _instance = new RpcManager(); 24 } 25 return _instance; 26 } 27 28 //T是RpcServerListener类或者子类 A是server端RPC类 29 void startService(T, A...)(string ip, ushort port, int threadNum) { 30 if(isServerStart) 31 { 32 warningf("rpc service has already start !!!"); 33 return; 34 } 35 isServerStart = true; 36 _serverGroup = AsynchronousChannelThreadGroup.open(5,threadNum); 37 for(int i = 0; i < threadNum; i++) { 38 RpcServer service = new RpcServer(ip, port, _serverGroup,new T); 39 foreach(t;A) { 40 auto rpcClass = new t(service); 41 } 42 } 43 _serverGroup.start(); 44 } 45 46 void stopService() { 47 isServerStart = false; 48 _serverGroup.stop(); 49 } 50 //T是RpcClientListener类 或者子类 51 void connectService(T)(string ip, ushort port, int threadNum) { 52 if(isClientStart) 53 { 54 warningf("rpc service has already start !!!"); 55 return; 56 } 57 isClientStart = true; 58 _ClientGroup = AsynchronousChannelThreadGroup.open(5,threadNum); 59 for(int i = 0; i < threadNum; i++) { 60 T client = new T(ip, port, _ClientGroup.getWorkSelector()); 61 } 62 _ClientGroup.start(); 63 } 64 void stopClient() { 65 isClientStart = false; 66 _ClientGroup.stop(); 67 } 68 69 private : 70 __gshared static RpcManager _instance; 71 AsynchronousChannelThreadGroup _serverGroup; 72 AsynchronousChannelThreadGroup _ClientGroup; 73 74 bool isServerStart; 75 bool isClientStart; 76 }