45714
|
1 |
/*
|
|
2 |
* Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.tools.jdi;
|
|
27 |
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.io.InputStream;
|
|
30 |
import java.io.OutputStream;
|
|
31 |
import java.net.Socket;
|
|
32 |
|
|
33 |
import com.sun.jdi.connect.spi.ClosedConnectionException;
|
|
34 |
import com.sun.jdi.connect.spi.Connection;
|
|
35 |
|
|
36 |
/*
|
|
37 |
* The Connection returned by attach and accept is one of these
|
|
38 |
*/
|
|
39 |
class SocketConnection extends Connection {
|
|
40 |
private Socket socket;
|
|
41 |
private boolean closed = false;
|
|
42 |
private OutputStream socketOutput;
|
|
43 |
private InputStream socketInput;
|
|
44 |
private Object receiveLock = new Object();
|
|
45 |
private Object sendLock = new Object();
|
|
46 |
private Object closeLock = new Object();
|
|
47 |
|
|
48 |
SocketConnection(Socket socket) throws IOException {
|
|
49 |
this.socket = socket;
|
|
50 |
socket.setTcpNoDelay(true);
|
|
51 |
socketInput = socket.getInputStream();
|
|
52 |
socketOutput = socket.getOutputStream();
|
|
53 |
}
|
|
54 |
|
|
55 |
public void close() throws IOException {
|
|
56 |
synchronized (closeLock) {
|
|
57 |
if (closed) {
|
|
58 |
return;
|
|
59 |
}
|
|
60 |
socketOutput.close();
|
|
61 |
socketInput.close();
|
|
62 |
socket.close();
|
|
63 |
closed = true;
|
|
64 |
}
|
|
65 |
}
|
|
66 |
|
|
67 |
public boolean isOpen() {
|
|
68 |
synchronized (closeLock) {
|
|
69 |
return !closed;
|
|
70 |
}
|
|
71 |
}
|
|
72 |
|
|
73 |
public byte[] readPacket() throws IOException {
|
|
74 |
if (!isOpen()) {
|
|
75 |
throw new ClosedConnectionException("connection is closed");
|
|
76 |
}
|
|
77 |
synchronized (receiveLock) {
|
|
78 |
int b1,b2,b3,b4;
|
|
79 |
|
|
80 |
// length
|
|
81 |
try {
|
|
82 |
b1 = socketInput.read();
|
|
83 |
b2 = socketInput.read();
|
|
84 |
b3 = socketInput.read();
|
|
85 |
b4 = socketInput.read();
|
|
86 |
} catch (IOException ioe) {
|
|
87 |
if (!isOpen()) {
|
|
88 |
throw new ClosedConnectionException("connection is closed");
|
|
89 |
} else {
|
|
90 |
throw ioe;
|
|
91 |
}
|
|
92 |
}
|
|
93 |
|
|
94 |
// EOF
|
|
95 |
if (b1<0) {
|
|
96 |
return new byte[0];
|
|
97 |
}
|
|
98 |
|
|
99 |
if (b2<0 || b3<0 || b4<0) {
|
|
100 |
throw new IOException("protocol error - premature EOF");
|
|
101 |
}
|
|
102 |
|
|
103 |
int len = ((b1 << 24) | (b2 << 16) | (b3 << 8) | (b4 << 0));
|
|
104 |
|
|
105 |
if (len < 0) {
|
|
106 |
throw new IOException("protocol error - invalid length");
|
|
107 |
}
|
|
108 |
|
|
109 |
byte b[] = new byte[len];
|
|
110 |
b[0] = (byte)b1;
|
|
111 |
b[1] = (byte)b2;
|
|
112 |
b[2] = (byte)b3;
|
|
113 |
b[3] = (byte)b4;
|
|
114 |
|
|
115 |
int off = 4;
|
|
116 |
len -= off;
|
|
117 |
|
|
118 |
while (len > 0) {
|
|
119 |
int count;
|
|
120 |
try {
|
|
121 |
count = socketInput.read(b, off, len);
|
|
122 |
} catch (IOException ioe) {
|
|
123 |
if (!isOpen()) {
|
|
124 |
throw new ClosedConnectionException("connection is closed");
|
|
125 |
} else {
|
|
126 |
throw ioe;
|
|
127 |
}
|
|
128 |
}
|
|
129 |
if (count < 0) {
|
|
130 |
throw new IOException("protocol error - premature EOF");
|
|
131 |
}
|
|
132 |
len -= count;
|
|
133 |
off += count;
|
|
134 |
}
|
|
135 |
|
|
136 |
return b;
|
|
137 |
}
|
|
138 |
}
|
|
139 |
|
|
140 |
public void writePacket(byte b[]) throws IOException {
|
|
141 |
if (!isOpen()) {
|
|
142 |
throw new ClosedConnectionException("connection is closed");
|
|
143 |
}
|
|
144 |
|
|
145 |
/*
|
|
146 |
* Check the packet size
|
|
147 |
*/
|
|
148 |
if (b.length < 11) {
|
|
149 |
throw new IllegalArgumentException("packet is insufficient size");
|
|
150 |
}
|
|
151 |
int b0 = b[0] & 0xff;
|
|
152 |
int b1 = b[1] & 0xff;
|
|
153 |
int b2 = b[2] & 0xff;
|
|
154 |
int b3 = b[3] & 0xff;
|
|
155 |
int len = ((b0 << 24) | (b1 << 16) | (b2 << 8) | (b3 << 0));
|
|
156 |
if (len < 11) {
|
|
157 |
throw new IllegalArgumentException("packet is insufficient size");
|
|
158 |
}
|
|
159 |
|
|
160 |
/*
|
|
161 |
* Check that the byte array contains the complete packet
|
|
162 |
*/
|
|
163 |
if (len > b.length) {
|
|
164 |
throw new IllegalArgumentException("length mis-match");
|
|
165 |
}
|
|
166 |
|
|
167 |
synchronized (sendLock) {
|
|
168 |
try {
|
|
169 |
/*
|
|
170 |
* Send the packet (ignoring any bytes that follow
|
|
171 |
* the packet in the byte array).
|
|
172 |
*/
|
|
173 |
socketOutput.write(b, 0, len);
|
|
174 |
} catch (IOException ioe) {
|
|
175 |
if (!isOpen()) {
|
|
176 |
throw new ClosedConnectionException("connection is closed");
|
|
177 |
} else {
|
|
178 |
throw ioe;
|
|
179 |
}
|
|
180 |
}
|
|
181 |
}
|
|
182 |
}
|
|
183 |
}
|