Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/jdwp/SocketTransport.java
41162 views
1
/*
2
* Copyright (c) 2001, 2018, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
package nsk.share.jdwp;
25
26
import nsk.share.*;
27
28
import java.io.*;
29
import java.net.*;
30
31
/**
32
* This class represents a socket transport for JDWP.
33
*/
34
public class SocketTransport extends Transport {
35
36
/**
37
* Socket for established JDWP connection.
38
*/
39
private Socket socket = null;
40
41
/**
42
* ServerSocket for listening JDWP connection.
43
*/
44
private ServerSocket serverSocket = null;
45
46
/**
47
* Input stream of socket.
48
*/
49
private InputStream in = null;
50
51
/**
52
* Output stream of socket.
53
*/
54
private OutputStream out = null;
55
56
/**
57
* Port to listen to.
58
*/
59
int listenPort = 0;
60
61
/**
62
* Make <code>SocketTransport</cose> object with providing specified Log.
63
*/
64
public SocketTransport(Log log) {
65
super(log);
66
}
67
68
/**
69
* Bind for connection from target VM to specified port number.
70
* If given port number is 0 then bind to any free port number.
71
*
72
* @return port number which this transport is listening for
73
*/
74
public int bind(int port)
75
throws IOException {
76
serverSocket = new ServerSocket();
77
if (port == 0) {
78
// Only need SO_REUSEADDR if we're using a fixed port. If we
79
// start seeing EADDRINUSE due to collisions in free ports
80
// then we should retry the bind() a few times.
81
display("port == 0, disabling SO_REUSEADDR");
82
serverSocket.setReuseAddress(false);
83
}
84
serverSocket.bind(new InetSocketAddress(port));
85
listenPort = serverSocket.getLocalPort();
86
return listenPort;
87
}
88
89
/**
90
* Attach to the target VM for specified host name and port number.
91
*/
92
public void attach(String ServerName, int PortNumber)
93
throws UnknownHostException, IOException {
94
for (int i = 0; i < Binder.CONNECT_TRIES; i++ ) {
95
try {
96
socket = new Socket(ServerName, PortNumber);
97
display("JDWP socket connection established");
98
// socket.setTcpNoDelay(true);
99
in = socket.getInputStream();
100
out = socket.getOutputStream();
101
return;
102
} catch (IOException e) {
103
display("Attempt #" + i + " to establish JDWP socket connection failed:\n\t" + e);
104
try {
105
Thread.currentThread().sleep(Binder.CONNECT_TRY_DELAY);
106
} catch (InterruptedException ie) {
107
ie.printStackTrace(log.getOutStream());
108
throw new Failure("Thread interrupted while establishing JDWP connection: \n\t"
109
+ ie);
110
}
111
}
112
}
113
throw new IOException("Timeout exceeded while establishing JDWP socket connection to "
114
+ ServerName + ":" + PortNumber);
115
}
116
117
/**
118
* Accept connection from target VM for already boud port.
119
*/
120
public void accept()
121
throws IOException {
122
123
if (serverSocket == null)
124
throw new Failure("Attempt to accept JDWP socket connection from unbound port");
125
126
socket = serverSocket.accept();
127
serverSocket.close();
128
serverSocket = null;
129
130
in = socket.getInputStream();
131
out = socket.getOutputStream();
132
}
133
134
/**
135
* Close socket and streams.
136
*/
137
public void close() throws IOException {
138
139
if (socket == null)
140
return;
141
142
if (out != null) {
143
flush();
144
out.close();
145
}
146
147
if (in != null) {
148
in.close();
149
}
150
151
if (socket != null) {
152
socket.close();
153
socket = null;
154
}
155
156
if (serverSocket != null) {
157
serverSocket.close();
158
serverSocket = null;
159
}
160
}
161
162
/**
163
* Return number of bytes that can be read.
164
*/
165
public int available() throws IOException {
166
return in.available();
167
};
168
169
/**
170
* Flush bytes being buffered for writing if any.
171
*/
172
public void flush() throws IOException {
173
out.flush();
174
}
175
176
/**
177
* Set timeout for reading data in milliseconds.
178
*/
179
public void setReadTimeout(long millisecs) throws IOException {
180
socket.setSoTimeout((int)millisecs);
181
}
182
183
/**
184
* Read the next byte of data from the socket.
185
* The value byte is returned as an int in the range 0 to 255.
186
* If no byte is available, the value -1 is returned.
187
*/
188
public byte read() throws IOException {
189
int b = in.read();
190
if (b < 0) {
191
throw new IOException("JDWP socket connection closed by remote host");
192
}
193
return (byte) b;
194
};
195
196
/**
197
* Write the specified byte to the socket.
198
*/
199
public void write(int b) throws IOException {
200
out.write(b);
201
};
202
}
203
204