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