1118
|
1 package luan.lib.rpc;
|
|
2
|
|
3 import java.io.InputStream;
|
|
4 import java.io.OutputStream;
|
|
5 import java.io.IOException;
|
|
6 import java.io.EOFException;
|
|
7 import java.net.Socket;
|
|
8 import java.nio.charset.StandardCharsets;
|
|
9 import java.util.List;
|
|
10 import luan.lib.parser.ParseException;
|
|
11 import luan.lib.json.JsonParser;
|
|
12 import luan.lib.json.JsonToString;
|
|
13
|
|
14
|
|
15 public class RpcCon {
|
|
16 final Socket socket;
|
|
17 final InputStream in;
|
|
18 final OutputStream out;
|
|
19 InputStream inBinary = null;
|
|
20 long lenBinary = -1;
|
|
21
|
1119
|
22 RpcCon(Socket socket)
|
|
23 throws RpcError
|
|
24 {
|
|
25 try {
|
|
26 this.socket = socket;
|
|
27 this.in = socket.getInputStream();
|
|
28 this.out = socket.getOutputStream();
|
|
29 } catch(IOException e) {
|
|
30 close();
|
|
31 throw new RpcError(e);
|
|
32 }
|
1118
|
33 }
|
|
34
|
|
35 public void close()
|
1119
|
36 throws RpcError
|
1118
|
37 {
|
1119
|
38 try {
|
|
39 socket.close();
|
|
40 } catch(IOException e) {
|
|
41 throw new RpcError(e);
|
|
42 }
|
1118
|
43 }
|
|
44
|
|
45 public boolean isClosed() {
|
|
46 return socket.isClosed();
|
|
47 }
|
|
48
|
|
49 void write(InputStream in,long lenIn,List list)
|
1119
|
50 throws RpcError
|
1118
|
51 {
|
|
52 if( in != null )
|
|
53 list.add(0,lenIn);
|
|
54 String json = JsonToString.toString(list);
|
|
55 byte[] aJson = json.getBytes(StandardCharsets.UTF_8);
|
|
56 int len = aJson.length;
|
|
57 byte[] a = new byte[4+len];
|
|
58 a[0] = (byte)(len >>> 24);
|
|
59 a[1] = (byte)(len >>> 16);
|
|
60 a[2] = (byte)(len >>> 8);
|
|
61 a[3] = (byte)(len >>> 0);
|
|
62 System.arraycopy(aJson,0,a,4,len);
|
1119
|
63 try {
|
|
64 out.write(a);
|
|
65 if( in != null ) {
|
|
66 a = new byte[8192];
|
|
67 long total = 0;
|
|
68 int n;
|
|
69 while( (n=in.read(a)) != -1 ) {
|
|
70 out.write(a,0,n);
|
|
71 total += n;
|
|
72 }
|
|
73 if( total != lenIn ) {
|
|
74 close();
|
|
75 throw new RpcError("InputStream wrong length "+total+" when should be "+lenIn);
|
|
76 }
|
1118
|
77 }
|
1119
|
78 } catch(IOException e) {
|
|
79 close();
|
|
80 throw new RpcError(e);
|
1118
|
81 }
|
|
82 }
|
|
83
|
|
84 List readJson()
|
1119
|
85 throws RpcError
|
1118
|
86 {
|
1119
|
87 try {
|
|
88 if( inBinary != null ) {
|
|
89 inBinary.close();
|
|
90 inBinary = null;
|
|
91 lenBinary = -1;
|
|
92 }
|
|
93 byte[] a = new byte[4];
|
|
94 readAll(a);
|
|
95 int len = 0;
|
|
96 for( byte b : a ) {
|
|
97 len <<= 8;
|
|
98 len |= b&0xFF;
|
|
99 }
|
|
100 a = new byte[len];
|
|
101 readAll(a);
|
|
102 String json = new String(a,StandardCharsets.UTF_8);
|
|
103 List list = (List)JsonParser.parse(json);
|
|
104 if( list.get(0) instanceof Long ) {
|
|
105 lenBinary = (Long)list.remove(0);
|
|
106 inBinary = new FixedLengthInputStream(in,lenBinary);
|
|
107 }
|
|
108 return list;
|
|
109 } catch(IOException e) {
|
|
110 close();
|
|
111 throw new RpcError(e);
|
|
112 } catch(ParseException e) {
|
|
113 close();
|
|
114 throw new RpcError(e);
|
1118
|
115 }
|
|
116 }
|
|
117
|
|
118 private void readAll(final byte[] a) throws IOException {
|
|
119 int total = 0;
|
|
120 int n;
|
|
121 while( total < a.length ){
|
|
122 n = in.read( a, total, a.length-total );
|
|
123 if( n == -1 )
|
|
124 throw new EOFException();
|
|
125 total += n;
|
|
126 }
|
|
127 }
|
|
128
|
|
129 }
|