comparison src/goodjava/rpc/RpcCon.java @ 1402:27efb1fcbcb5

move luan.lib to goodjava
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 17 Sep 2019 01:35:01 -0400
parents src/luan/lib/rpc/RpcCon.java@e8fc6712b468
children 59fd2e8b1b9d
comparison
equal deleted inserted replaced
1401:ef1620aa99cb 1402:27efb1fcbcb5
1 package goodjava.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 goodjava.parser.ParseException;
11 import goodjava.json.JsonParser;
12 import goodjava.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 boolean readSome = false;
22
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 }
34 }
35
36 public void close()
37 throws RpcError
38 {
39 try {
40 socket.close();
41 } catch(IOException e) {
42 throw new RpcError(e);
43 }
44 }
45
46 public boolean isClosed() {
47 return socket.isClosed();
48 }
49
50 void write(InputStream in,long lenIn,List list)
51 throws RpcError
52 {
53 if( in != null )
54 list.add(0,lenIn);
55 String json = JsonToString.toString(list);
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);
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 }
78 }
79 } catch(IOException e) {
80 close();
81 throw new RpcError(e);
82 }
83 }
84
85 List readJson()
86 throws RpcError
87 {
88 try {
89 if( inBinary != null ) {
90 inBinary.close();
91 inBinary = null;
92 lenBinary = -1;
93 }
94 readSome = false;
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);
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();
127 readSome = true;
128 total += n;
129 }
130 }
131
132 }