2
Copyright (C) 2009 Volker Berlin (vberlin@inetsoftware.de)
4
This software is provided 'as-is', without any express or implied
5
warranty. In no event will the authors be held liable for any damages
6
arising from the use of this software.
8
Permission is granted to anyone to use this software for any purpose,
9
including commercial applications, and to alter it and redistribute it
10
freely, subject to the following restrictions:
12
1. The origin of this software must not be misrepresented; you must not
13
claim that you wrote the original software. If you use this software
14
in a product, an acknowledgment in the product documentation would be
15
appreciated but is not required.
16
2. Altered source versions must be plainly marked as such, and must not be
17
misrepresented as being the original software.
18
3. This notice may not be removed or altered from any source distribution.
25
using System.Collections.Generic;
28
using System.Net.Sockets;
31
namespace ikvm.debugger
35
private readonly JdwpParameters parameters;
37
private TcpClient client;
42
/// Shared buffer for reading and monitor for reading
44
private readonly byte[] readHeader = new byte[11];
46
private readonly Object writeMonitor = new Object();
48
internal JdwpConnection(JdwpParameters parameters)
50
this.parameters = parameters;
53
internal void Connect()
55
if (parameters.Server)
57
IPHostEntry ipEntry = Dns.GetHostEntry(parameters.Host);
58
IPAddress ipAddress = ipEntry.AddressList[0];
59
TcpListener listener = new TcpListener(ipAddress, parameters.Port);
61
client = listener.AcceptTcpClient();
65
client = new TcpClient(parameters.Host, parameters.Port);
67
stream = client.GetStream(); //TODO Bug in BufferedStream, work not asynchron
68
//stream = new BufferedStream(client.GetStream());
69
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
70
byte[] hello = encoding.GetBytes("JDWP-Handshake");
71
stream.Write(hello, 0, hello.Length);
72
byte[] b = new byte[hello.Length];
74
while (received < b.Length)
76
int n = stream.Read(b, received, b.Length - received);
80
Console.WriteLine("handshake failed - connection prematurally closed");
85
for (int j = 0; j < hello.Length; j++)
90
Console.WriteLine("handshake failed - unrecognized message from target VM");
96
internal Packet ReadPacket()
100
DebuggerUtils.ReadFully(stream, readHeader);
101
return Packet.Read(readHeader, stream);
105
internal void SendPacket(Packet packet)