-
Notifications
You must be signed in to change notification settings - Fork 1
/
PxpRpc.java
124 lines (113 loc) · 3.31 KB
/
PxpRpc.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package pxprpc.test;
import java.io.Closeable;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.*;
import pxprpc.backend.TCPBackend;
import pxprpc.base.Serializer2;
import pxprpc.base.ServerContext;
import pxprpc.base.Utils;
import pxprpc.extend.*;
public class PxpRpc {
//Just for test, will ignore session check.
//Rpc server handler demo.
public static class Handler1 {
public Object get1234(){
return "1234";
}
public void printString(Object s){
System.out.println(s);
}
@MethodTypeDecl("cilfdb->il")
public Object[] testPrintArg(AsyncReturn<Object[]> r,boolean a,int b,long c,float d,double e,ByteBuffer f){
System.out.println(""+a+","+b+","+c+","+d+","+e+","+new String(Utils.toBytes(f), ServerContext.charset));
r.resolve(new Object[]{100,1234567890l});
return null;
}
public void testUnser(ByteBuffer buf){
Serializer2 ser = new Serializer2().prepareUnserializing(buf);
System.out.println(","+ser.getInt()+","+ser.getLong()+","+ser.getFloat()+","+ser.getDouble()+","+ser.getString()+","+ser.getString());
}
public void testTableUnser(ByteBuffer buf){
List<Map<String, Object>> maparr = new TableSerializer().load(buf).toMapArray();
for(Map<String, Object> e:maparr){
for(Map.Entry<String, Object> kv:e.entrySet()){
System.out.print(kv.getKey()+":");
System.out.print(kv.getValue());
System.out.print(" ");
}
System.out.println();
}
}
public Object testNone(AsyncReturn<Object> r,Object para){
System.out.print("expect null:");
System.out.println(para);
r.resolve(null);
return new Object();
}
public TickEvent onTick() {
TickEvent te = new TickEvent();
te.start();
return te;
}
public static Timer tm=new Timer();
//as Parameter class is support since 1.8, We have to use return type to identify the typedecl even for async callable.
public String wait1Sec(final AsyncReturn<Object> asyncRet) {
tm.schedule(new TimerTask() {
@Override
public void run() {
asyncRet.resolve("tick");
}
}, 1000);
return null;
}
public void raiseError1() throws IOException {
throw new IOException("dummy exception");
}
public Closeable autoCloseable(){
return new Closeable(){
@Override
public void close() throws IOException {
System.out.println("auto closeable closed");
}
};
}
}
public static class TickEvent extends EventDispatcher {
public TickEvent() {
setEventType(String.class);
}
public void start() {
Handler1.tm.schedule(new TimerTask() {
@Override
public void run() {
TickEvent.this.fireEvent("tick");
}
}, 1000,1000);
}
}
public static void main(String[] args) {
final TCPBackend pxptcp = new TCPBackend();
int listenPort=1064;
DefaultFuncMap.registered.put("test1", new Handler1());
pxptcp.bindAddr=new InetSocketAddress(listenPort);
Thread th=new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("ready...");
pxptcp.listenAndServe();
} catch (IOException e) {
e.printStackTrace();
if(!e.getMessage().contains("Interrupted function call")) {
e.printStackTrace();
}
}
System.out.println("server stoped");
}
});
th.start();
}
}