1 module kissrpc.RpcClientImpl;
2 
3 import kissrpc.RpcClient;
4 import kissrpc.RpcRequest;
5 import kissrpc.RpcResponse;
6 import kissrpc.Unit;
7 import kissrpc.Logs;
8 
9 import std.stdio;
10 
11 class RpcClientImpl(T)
12 {
13 	this(RpcClient refClient)
14 	{
15 		client = refClient;
16 		foreach(i, func; __traits(derivedMembers, T))
17 		{
18 			foreach(t ;__traits(getVirtualMethods, T, func))
19 			{
20 				deWritefln("rpc client impl class:%s, member func:%s",typeid(T).toString(), typeid(typeof(t)));
21 				client.bind(typeid(T).toString(), func);
22 			}
23 		}
24 	}
25 
26 	void asyncCall(RpcRequest req, ReponsCallback callback, RPC_PACKAGE_PROTOCOL protocol = RPC_PACKAGE_PROTOCOL.TPP_FLAT_BUF, const size_t funcId = 0)
27 	{
28 		deWritefln("rpc client imlp async call:%s, %s", funcId, RpcBindFunctionMap[funcId]);
29 
30 		req.bindFunc(funcId);
31 		client.requestRemoteCall(req, protocol);
32 		client.bindCallback(funcId, callback);
33 	}
34 
35 
36 	RpcResponse syncCall(RpcRequest req, RPC_PACKAGE_PROTOCOL protocol = RPC_PACKAGE_PROTOCOL.TPP_FLAT_BUF, const size_t funcId = 0)
37 	{
38 			deWritefln("rpc client imlp sync call:%s, %s", funcId, RpcBindFunctionMap[funcId]);
39 			
40 			req.bindFunc(funcId);
41 			req.setNonblock(false);
42 			
43 			RpcResponse retResp;
44 					
45 			void callback(RpcResponse resp)
46 			{
47 				retResp = resp;
48 				req.semaphoreRelease();
49 			}
50 
51 			client.bindCallback(funcId,  &callback);
52 			client.requestRemoteCall(req, protocol);
53 
54 			req.semaphoreWait();
55 
56 			return retResp;
57 	}
58 
59 
60 private:
61 	RpcClient client;
62 }
63