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