Mercurial Hosting > luan
annotate src/goodjava/rpc/RpcCon.java @ 1447:851b9a48cc44
Luan.parse
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 18 Feb 2020 14:54:35 -0700 |
parents | 59fd2e8b1b9d |
children | aaac1d29edea |
rev | line source |
---|---|
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1120
diff
changeset
|
1 package goodjava.rpc; |
1118 | 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; | |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1120
diff
changeset
|
10 import goodjava.parser.ParseException; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1120
diff
changeset
|
11 import goodjava.json.JsonParser; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1120
diff
changeset
|
12 import goodjava.json.JsonToString; |
1118 | 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; | |
1120
e8fc6712b468
luan Rpc uses luan.lib.rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
1119
diff
changeset
|
21 boolean readSome = false; |
1118 | 22 |
1119 | 23 RpcCon(Socket socket) |
24 throws RpcError | |
25 { | |
26 try { | |
27 this.socket = socket; | |
28 this.in = socket.getInputStream(); | |
29 this.out = socket.getOutputStream(); | |
30 } catch(IOException e) { | |
31 close(); | |
32 throw new RpcError(e); | |
33 } | |
1118 | 34 } |
35 | |
36 public void close() | |
1119 | 37 throws RpcError |
1118 | 38 { |
1119 | 39 try { |
40 socket.close(); | |
41 } catch(IOException e) { | |
42 throw new RpcError(e); | |
43 } | |
1118 | 44 } |
45 | |
46 public boolean isClosed() { | |
47 return socket.isClosed(); | |
48 } | |
49 | |
50 void write(InputStream in,long lenIn,List list) | |
1119 | 51 throws RpcError |
1118 | 52 { |
53 if( in != null ) | |
54 list.add(0,lenIn); | |
1419
59fd2e8b1b9d
stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
55 String json = new JsonToString().toString(list); |
1118 | 56 byte[] aJson = json.getBytes(StandardCharsets.UTF_8); |
57 int len = aJson.length; | |
58 byte[] a = new byte[4+len]; | |
59 a[0] = (byte)(len >>> 24); | |
60 a[1] = (byte)(len >>> 16); | |
61 a[2] = (byte)(len >>> 8); | |
62 a[3] = (byte)(len >>> 0); | |
63 System.arraycopy(aJson,0,a,4,len); | |
1119 | 64 try { |
65 out.write(a); | |
66 if( in != null ) { | |
67 a = new byte[8192]; | |
68 long total = 0; | |
69 int n; | |
70 while( (n=in.read(a)) != -1 ) { | |
71 out.write(a,0,n); | |
72 total += n; | |
73 } | |
74 if( total != lenIn ) { | |
75 close(); | |
76 throw new RpcError("InputStream wrong length "+total+" when should be "+lenIn); | |
77 } | |
1118 | 78 } |
1119 | 79 } catch(IOException e) { |
80 close(); | |
81 throw new RpcError(e); | |
1118 | 82 } |
83 } | |
84 | |
85 List readJson() | |
1119 | 86 throws RpcError |
1118 | 87 { |
1119 | 88 try { |
89 if( inBinary != null ) { | |
90 inBinary.close(); | |
91 inBinary = null; | |
92 lenBinary = -1; | |
93 } | |
1120
e8fc6712b468
luan Rpc uses luan.lib.rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
1119
diff
changeset
|
94 readSome = false; |
1119 | 95 byte[] a = new byte[4]; |
96 readAll(a); | |
97 int len = 0; | |
98 for( byte b : a ) { | |
99 len <<= 8; | |
100 len |= b&0xFF; | |
101 } | |
102 a = new byte[len]; | |
103 readAll(a); | |
104 String json = new String(a,StandardCharsets.UTF_8); | |
105 List list = (List)JsonParser.parse(json); | |
106 if( list.get(0) instanceof Long ) { | |
107 lenBinary = (Long)list.remove(0); | |
108 inBinary = new FixedLengthInputStream(in,lenBinary); | |
109 } | |
110 return list; | |
111 } catch(IOException e) { | |
112 close(); | |
113 throw new RpcError(e); | |
114 } catch(ParseException e) { | |
115 close(); | |
116 throw new RpcError(e); | |
1118 | 117 } |
118 } | |
119 | |
120 private void readAll(final byte[] a) throws IOException { | |
121 int total = 0; | |
122 int n; | |
123 while( total < a.length ){ | |
124 n = in.read( a, total, a.length-total ); | |
125 if( n == -1 ) | |
126 throw new EOFException(); | |
1120
e8fc6712b468
luan Rpc uses luan.lib.rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
1119
diff
changeset
|
127 readSome = true; |
1118 | 128 total += n; |
129 } | |
130 } | |
131 | |
132 } |