Libraryless. Click here for Pure Java version (3596L/24K).
1 | static DialogIO talkTo(int port) {
|
2 | return talkTo("localhost", port);
|
3 | } |
4 | |
5 | static int talkTo_defaultTimeout = 10000; // This is the CONNECT timeout |
6 | static int talkTo_timeoutForReads = 0; // Timeout waiting for answers (0 = no timeout) |
7 | |
8 | static ThreadLocal<Map<S, DialogIO>> talkTo_byThread = new ThreadLocal; |
9 | |
10 | static DialogIO talkTo(S ip, int port) ctex {
|
11 | S full = ip + ":" + port; |
12 | Map<S, DialogIO> map = talkTo_byThread.get(); |
13 | if (map != null && map.containsKey(full)) ret map.get(full); |
14 | |
15 | if (isLocalhost(ip) && port == vmPort()) ret talkToThisVM(); |
16 | |
17 | ret new talkTo_IO(ip, port); |
18 | } |
19 | |
20 | sclass talkTo_IO extends DialogIO {
|
21 | S ip; |
22 | int port; |
23 | Socket s; |
24 | Writer w; |
25 | BufferedReader in; |
26 | |
27 | *(S *ip, int *port) ctex {
|
28 | s = new Socket; |
29 | try {
|
30 | if (talkTo_timeoutForReads != 0) |
31 | s.setSoTimeout(talkTo_timeoutForReads); |
32 | s.connect(new InetSocketAddress(ip, port), talkTo_defaultTimeout); |
33 | } catch (Throwable e) {
|
34 | fail("Tried talking to " + ip + ":" + port, e);
|
35 | } |
36 | |
37 | w = new OutputStreamWriter(s.getOutputStream(), "UTF-8"); |
38 | in = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8")); |
39 | } |
40 | |
41 | boolean isLocalConnection() {
|
42 | return s.getInetAddress().isLoopbackAddress(); |
43 | } |
44 | |
45 | boolean isStillConnected() {
|
46 | return !(eos || s.isClosed()); |
47 | } |
48 | |
49 | void sendLine(String line) ctex {
|
50 | lock lock; |
51 | w.write(line + "\n"); |
52 | w.flush(); |
53 | } |
54 | |
55 | S readLineImpl() ctex {
|
56 | ret in.readLine(); |
57 | } |
58 | |
59 | public void close() {
|
60 | try {
|
61 | if (!noClose) s.close(); |
62 | } catch (IOException e) {
|
63 | // whatever |
64 | } |
65 | } |
66 | |
67 | Socket getSocket() {
|
68 | return s; |
69 | } |
70 | } |
Began life as a copy of #1000937
download show line numbers debug dex old transpilations
Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1001076 |
| Snippet name: | talkTo |
| Eternal ID of this version: | #1001076/11 |
| Text MD5: | 46dd88aa4e2273eb2ee46fe8402736f0 |
| Transpilation MD5: | e0f505a630cc5e694510e61f3a8e8234 |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2019-07-13 21:36:57 |
| Source code size: | 1714 bytes / 70 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 1273 / 6308 |
| Version history: | 10 change(s) |
| Referenced in: | [show references] |