Mercurial Hosting > luan
annotate src/goodjava/rpc/RpcCon.java @ 1493:471ef3e6a84e
more io
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 03 May 2020 00:12:15 -0600 |
parents | aaac1d29edea |
children | 91c167099462 |
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; | |
1492 | 4 import java.io.BufferedOutputStream; |
1118 | 5 import java.io.IOException; |
6 import java.net.Socket; | |
7 import java.util.List; | |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1120
diff
changeset
|
8 import goodjava.parser.ParseException; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1120
diff
changeset
|
9 import goodjava.json.JsonParser; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1120
diff
changeset
|
10 import goodjava.json.JsonToString; |
1492 | 11 import goodjava.io.BufferedInputStream; |
12 import goodjava.io.DataInputStream; | |
13 import goodjava.io.DataOutputStream; | |
1493 | 14 import goodjava.io.IoUtils; |
1118 | 15 |
16 | |
17 public class RpcCon { | |
1492 | 18 private final Socket socket; |
19 private final DataInputStream in; | |
20 private final DataOutputStream out; | |
1118 | 21 InputStream inBinary = null; |
22 long lenBinary = -1; | |
23 | |
1119 | 24 RpcCon(Socket socket) |
25 throws RpcError | |
26 { | |
27 try { | |
28 this.socket = socket; | |
1492 | 29 this.in = new DataInputStream(new BufferedInputStream(socket.getInputStream())); |
30 this.out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); | |
1119 | 31 } catch(IOException e) { |
32 close(); | |
33 throw new RpcError(e); | |
34 } | |
1118 | 35 } |
36 | |
37 public void close() | |
1119 | 38 throws RpcError |
1118 | 39 { |
1119 | 40 try { |
41 socket.close(); | |
42 } catch(IOException e) { | |
43 throw new RpcError(e); | |
44 } | |
1118 | 45 } |
46 | |
47 public boolean isClosed() { | |
48 return socket.isClosed(); | |
49 } | |
50 | |
51 void write(InputStream in,long lenIn,List list) | |
1119 | 52 throws RpcError |
1118 | 53 { |
54 if( in != null ) | |
55 list.add(0,lenIn); | |
1419
59fd2e8b1b9d
stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
56 String json = new JsonToString().toString(list); |
1119 | 57 try { |
1492 | 58 out.writeString(json); |
1119 | 59 if( in != null ) { |
1493 | 60 long total = IoUtils.copyAll(in,out); |
1119 | 61 if( total != lenIn ) { |
62 close(); | |
63 throw new RpcError("InputStream wrong length "+total+" when should be "+lenIn); | |
64 } | |
1118 | 65 } |
1492 | 66 out.flush(); |
1119 | 67 } catch(IOException e) { |
68 close(); | |
69 throw new RpcError(e); | |
1118 | 70 } |
71 } | |
72 | |
73 List readJson() | |
1119 | 74 throws RpcError |
1118 | 75 { |
1119 | 76 try { |
77 if( inBinary != null ) { | |
78 inBinary.close(); | |
79 inBinary = null; | |
80 lenBinary = -1; | |
81 } | |
1492 | 82 String json = in.readString(); |
1119 | 83 List list = (List)JsonParser.parse(json); |
84 if( list.get(0) instanceof Long ) { | |
85 lenBinary = (Long)list.remove(0); | |
86 inBinary = new FixedLengthInputStream(in,lenBinary); | |
87 } | |
88 return list; | |
89 } catch(IOException e) { | |
90 close(); | |
91 throw new RpcError(e); | |
92 } catch(ParseException e) { | |
93 close(); | |
94 throw new RpcError(e); | |
1118 | 95 } |
96 } | |
97 | |
98 } |