Mercurial Hosting > luan
comparison src/luan/lib/rpc/RpcCon.java @ 1119:87c674f3f6b7
add RpcError
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 07 Aug 2017 12:35:45 -0600 |
parents | e4710ddfd287 |
children | e8fc6712b468 |
comparison
equal
deleted
inserted
replaced
1118:e4710ddfd287 | 1119:87c674f3f6b7 |
---|---|
17 final InputStream in; | 17 final InputStream in; |
18 final OutputStream out; | 18 final OutputStream out; |
19 InputStream inBinary = null; | 19 InputStream inBinary = null; |
20 long lenBinary = -1; | 20 long lenBinary = -1; |
21 | 21 |
22 RpcCon(Socket socket) throws IOException { | 22 RpcCon(Socket socket) |
23 this.socket = socket; | 23 throws RpcError |
24 this.in = socket.getInputStream(); | 24 { |
25 this.out = socket.getOutputStream(); | 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 } | |
26 } | 33 } |
27 | 34 |
28 public void close() | 35 public void close() |
29 throws IOException | 36 throws RpcError |
30 { | 37 { |
31 socket.close(); | 38 try { |
39 socket.close(); | |
40 } catch(IOException e) { | |
41 throw new RpcError(e); | |
42 } | |
32 } | 43 } |
33 | 44 |
34 public boolean isClosed() { | 45 public boolean isClosed() { |
35 return socket.isClosed(); | 46 return socket.isClosed(); |
36 } | 47 } |
37 | 48 |
38 void write(InputStream in,long lenIn,List list) | 49 void write(InputStream in,long lenIn,List list) |
39 throws IOException | 50 throws RpcError |
40 { | 51 { |
41 if( in != null ) | 52 if( in != null ) |
42 list.add(0,lenIn); | 53 list.add(0,lenIn); |
43 String json = JsonToString.toString(list); | 54 String json = JsonToString.toString(list); |
44 byte[] aJson = json.getBytes(StandardCharsets.UTF_8); | 55 byte[] aJson = json.getBytes(StandardCharsets.UTF_8); |
47 a[0] = (byte)(len >>> 24); | 58 a[0] = (byte)(len >>> 24); |
48 a[1] = (byte)(len >>> 16); | 59 a[1] = (byte)(len >>> 16); |
49 a[2] = (byte)(len >>> 8); | 60 a[2] = (byte)(len >>> 8); |
50 a[3] = (byte)(len >>> 0); | 61 a[3] = (byte)(len >>> 0); |
51 System.arraycopy(aJson,0,a,4,len); | 62 System.arraycopy(aJson,0,a,4,len); |
52 out.write(a); | 63 try { |
53 if( in != null ) { | 64 out.write(a); |
54 a = new byte[8192]; | 65 if( in != null ) { |
55 long total = 0; | 66 a = new byte[8192]; |
56 int n; | 67 long total = 0; |
57 while( (n=in.read(a)) != -1 ) { | 68 int n; |
58 out.write(a,0,n); | 69 while( (n=in.read(a)) != -1 ) { |
59 total += n; | 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 } | |
60 } | 77 } |
61 if( total != lenIn ) | 78 } catch(IOException e) { |
62 throw new IOException("InputStream wrong length "+total+" when should be "+lenIn); | 79 close(); |
80 throw new RpcError(e); | |
63 } | 81 } |
64 } | 82 } |
65 | 83 |
66 List readJson() | 84 List readJson() |
67 throws IOException | 85 throws RpcError |
68 { | 86 { |
69 if( inBinary != null ) { | 87 try { |
70 inBinary.close(); | 88 if( inBinary != null ) { |
71 inBinary = null; | 89 inBinary.close(); |
72 lenBinary = -1; | 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); | |
73 } | 115 } |
74 byte[] a = new byte[4]; | |
75 readAll(a); | |
76 int len = 0; | |
77 for( byte b : a ) { | |
78 len <<= 8; | |
79 len |= b&0xFF; | |
80 } | |
81 a = new byte[len]; | |
82 readAll(a); | |
83 String json = new String(a,StandardCharsets.UTF_8); | |
84 List list; | |
85 try { | |
86 list = (List)JsonParser.parse(json); | |
87 } catch(ParseException e) { | |
88 throw new IOException(e); | |
89 } | |
90 if( list.get(0) instanceof Long ) { | |
91 lenBinary = (Long)list.remove(0); | |
92 inBinary = new FixedLengthInputStream(in,lenBinary); | |
93 } | |
94 return list; | |
95 } | 116 } |
96 | 117 |
97 private void readAll(final byte[] a) throws IOException { | 118 private void readAll(final byte[] a) throws IOException { |
98 int total = 0; | 119 int total = 0; |
99 int n; | 120 int n; |