2
|
1 |
/*
|
45564
|
2 |
* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
|
2
|
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
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.jdi.connect.spi;
|
|
27 |
|
|
28 |
import java.io.IOException;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* A connection between a debugger and a target VM which it debugs.
|
|
32 |
*
|
|
33 |
* <p> A Connection represents a bi-directional communication channel
|
|
34 |
* between a debugger and a target VM. A Connection is created when
|
45564
|
35 |
* {@link TransportService TransportService} establishes a connection
|
|
36 |
* and successfully handshakes with a target VM. A TransportService
|
|
37 |
* implementation provides a reliable JDWP packet transportation service
|
|
38 |
* and consequently a Connection provides a reliable flow of JDWP packets
|
|
39 |
* between the debugger and the target VM. A Connection is stream oriented,
|
|
40 |
* that is, the JDWP packets written to a connection are read by the target VM
|
|
41 |
* in the order in which they were written. Similarly packets written
|
2
|
42 |
* to a Connection by the target VM are read by the debugger in the
|
|
43 |
* order in which they were written.
|
|
44 |
*
|
|
45 |
* <p> A connection is either open or closed. It is open upon creation,
|
|
46 |
* and remains open until it is closed. Once closed, it remains closed,
|
|
47 |
* and any attempt to invoke an I/O operation upon it will cause a
|
|
48 |
* {@link ClosedConnectionException} to be thrown. A connection can
|
|
49 |
* be tested by invoking the {@link #isOpen isOpen} method.
|
|
50 |
*
|
|
51 |
* <p> A Connection is safe for access by multiple concurrent threads,
|
|
52 |
* although at most one thread may be reading and at most one thread may
|
30037
|
53 |
* be writing at any given time.
|
2
|
54 |
*
|
|
55 |
* @since 1.5
|
|
56 |
*/
|
|
57 |
public abstract class Connection {
|
|
58 |
|
|
59 |
/**
|
|
60 |
* Reads a packet from the target VM.
|
|
61 |
*
|
|
62 |
* <p> Attempts to read a JDWP packet from the target VM.
|
|
63 |
* A read operation may block indefinitely and only returns
|
|
64 |
* when it reads all bytes of a packet, or in the case of a
|
|
65 |
* transport service that is based on a stream-oriented
|
|
66 |
* communication protocol, the end of stream is encountered.
|
|
67 |
*
|
|
68 |
* <p> Reading a packet does not do any integrity checking on
|
|
69 |
* the packet aside from a check that the length of the packet
|
30037
|
70 |
* (as indicated by the value of the {@code length} field, the
|
2
|
71 |
* first four bytes of the packet) is 11 or more bytes.
|
30037
|
72 |
* If the value of the {@code length} value is less then 11
|
|
73 |
* then an {@code IOException} is thrown.
|
2
|
74 |
*
|
|
75 |
* <p> Returns a byte array of a length equal to the length
|
|
76 |
* of the received packet, or a byte array of length 0 when an
|
|
77 |
* end of stream is encountered. If end of stream is encountered
|
|
78 |
* after some, but not all bytes of a packet, are read then it
|
30037
|
79 |
* is considered an I/O error and an {@code IOException} is
|
2
|
80 |
* thrown. The first byte of the packet is stored in element
|
30037
|
81 |
* {@code 0} of the byte array, the second in element {@code 1},
|
2
|
82 |
* and so on. The bytes in the byte array are laid out as per the
|
45532
|
83 |
* <a href="{@docRoot}/../specs/jdwp/jdwp-spec.html">
|
2
|
84 |
* JDWP specification</a>. That is, all fields in the packet
|
|
85 |
* are in big endian order as per the JDWP specification.
|
|
86 |
*
|
|
87 |
* <p> This method may be invoked at any time. If another thread has
|
|
88 |
* already initiated a {@link #readPacket readPacket} on this
|
|
89 |
* connection then the invocation of this method will block until the
|
30037
|
90 |
* first operation is complete.
|
2
|
91 |
*
|
|
92 |
* @return the packet read from the target VM
|
|
93 |
*
|
|
94 |
* @throws ClosedConnectionException
|
|
95 |
* If the connection is closed, or another thread closes
|
|
96 |
* the connection while the readPacket is in progress.
|
|
97 |
*
|
|
98 |
* @throws java.io.IOException
|
|
99 |
* If the length of the packet (as indictaed by the first
|
|
100 |
* 4 bytes) is less than 11 bytes, or an I/O error occurs.
|
|
101 |
*
|
|
102 |
*
|
|
103 |
*/
|
|
104 |
public abstract byte[] readPacket() throws IOException;
|
|
105 |
|
|
106 |
/**
|
|
107 |
* Writes a packet to the target VM.
|
|
108 |
*
|
|
109 |
* <p> Attempts to write, or send, a JDWP packet to the target VM.
|
|
110 |
* A write operation only returns after writing the entire packet
|
|
111 |
* to the target VM. Writing the entire packet does not mean
|
|
112 |
* the entire packet has been transmitted to the target VM
|
|
113 |
* but rather that all bytes have been written to the
|
|
114 |
* transport service. A transport service based on a TCP/IP connection
|
|
115 |
* may, for example, buffer some or all of the packet before
|
|
116 |
* transmission on the network.
|
|
117 |
*
|
|
118 |
* <p> The byte array provided to this method should be laid out
|
|
119 |
* as per the <a
|
45532
|
120 |
* href="{@docRoot}/../specs/jdwp/jdwp-spec.html">
|
2
|
121 |
* JDWP specification</a>. That is, all fields in the packet
|
|
122 |
* are in big endian order. The first byte, that is element
|
30037
|
123 |
* {@code pkt[0]}, is the first byte of the {@code length} field.
|
|
124 |
* {@code pkt[1]} is the second byte of the {@code length} field,
|
2
|
125 |
* and so on.
|
|
126 |
*
|
|
127 |
* <p> Writing a packet does not do any integrity checking on
|
|
128 |
* the packet aside from checking the packet length. Checking
|
|
129 |
* the packet length requires checking that the value of the
|
30037
|
130 |
* {@code length} field (as indicated by the first four bytes
|
2
|
131 |
* of the packet) is 11 or greater. Consequently the length of
|
|
132 |
* the byte array provided to this method, that is
|
30037
|
133 |
* {@code pkt.length}, must be 11 or more, and must be equal
|
|
134 |
* or greater than the value of the {@code length} field. If the
|
2
|
135 |
* length of the byte array is greater than the value of
|
30037
|
136 |
* the {@code length} field then all bytes from element
|
|
137 |
* {@code pkt[length]} onwards are ignored. In other words,
|
2
|
138 |
* any additional bytes that follow the packet in the byte
|
|
139 |
* array are ignored and will not be transmitted to the target
|
|
140 |
* VM.
|
|
141 |
*
|
|
142 |
* <p> A write operation may block or may complete immediately.
|
|
143 |
* The exact circumstances when an operation blocks depends on
|
|
144 |
* the transport service. In the case of a TCP/IP connection to
|
|
145 |
* the target VM, the writePacket method may block if there is
|
|
146 |
* network congestion or there is insufficient space to buffer
|
|
147 |
* the packet in the underlying network system.
|
|
148 |
*
|
|
149 |
* <p> This method may be invoked at any time. If another thread has
|
|
150 |
* already initiated a write operation upon this Connection then
|
|
151 |
* a subsequent invocation of this method will block until the first
|
30037
|
152 |
* operation is complete.
|
2
|
153 |
*
|
|
154 |
* @param pkt
|
|
155 |
* The packet to write to the target VM.
|
|
156 |
*
|
|
157 |
* @throws ClosedConnectionException
|
|
158 |
* If the connection is closed, or another thread closes
|
|
159 |
* the connection while the write operation is in progress.
|
|
160 |
*
|
|
161 |
* @throws java.io.IOException
|
|
162 |
* If an I/O error occurs.
|
|
163 |
*
|
|
164 |
* @throws IllegalArgumentException
|
30037
|
165 |
* If the value of the {@code length} field is invalid,
|
2
|
166 |
* or the byte array is of insufficient length.
|
|
167 |
*/
|
|
168 |
public abstract void writePacket(byte pkt[]) throws IOException;
|
|
169 |
|
|
170 |
/**
|
|
171 |
* Closes this connection.
|
|
172 |
*
|
|
173 |
* <p> If the connection is already closed then invoking this method
|
|
174 |
* has no effect. After a connection is closed, any further attempt
|
|
175 |
* calls to {@link #readPacket readPacket} or {@link #writePacket
|
|
176 |
* writePacket} will throw a {@link ClosedConnectionException}.
|
|
177 |
*
|
|
178 |
* <p> Any thread currently blocked in an I/O operation ({@link
|
|
179 |
* #readPacket readPacket} or {@link #writePacket writePacket})
|
|
180 |
* will throw a {@link ClosedConnectionException}).
|
|
181 |
*
|
|
182 |
* <p> This method may be invoked at any time. If some other thread has
|
|
183 |
* already invoked it, however, then another invocation will block until
|
|
184 |
* the first invocation is complete, after which it will return without
|
30037
|
185 |
* effect.
|
2
|
186 |
*
|
|
187 |
* @throws java.io.IOException
|
|
188 |
* If an I/O error occurs
|
|
189 |
*/
|
|
190 |
public abstract void close() throws IOException;
|
|
191 |
|
|
192 |
/**
|
30037
|
193 |
* Tells whether or not this connection is open.
|
2
|
194 |
*
|
30037
|
195 |
* @return {@code true} if and only if this connection is open
|
2
|
196 |
*/
|
|
197 |
public abstract boolean isOpen();
|
|
198 |
}
|