~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/debugger/JdwpConnection.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2009 Volker Berlin (vberlin@inetsoftware.de)
 
3
 
 
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.
 
7
 
 
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:
 
11
 
 
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.
 
19
 
 
20
  Jeroen Frijters
 
21
  jeroen@frijters.net
 
22
  
 
23
*/
 
24
using System;
 
25
using System.Collections.Generic;
 
26
using System.IO;
 
27
using System.Net;
 
28
using System.Net.Sockets;
 
29
using System.Text;
 
30
 
 
31
namespace ikvm.debugger
 
32
{
 
33
    class JdwpConnection
 
34
    {
 
35
        private readonly JdwpParameters parameters;
 
36
 
 
37
        private TcpClient client;
 
38
 
 
39
        Stream stream;
 
40
 
 
41
        /// <summary>
 
42
        /// Shared buffer for reading and monitor for reading
 
43
        /// </summary>
 
44
        private readonly byte[] readHeader = new byte[11];
 
45
 
 
46
        private readonly Object writeMonitor = new Object();
 
47
 
 
48
        internal JdwpConnection(JdwpParameters parameters)
 
49
        {
 
50
            this.parameters = parameters;
 
51
        }
 
52
 
 
53
        internal void Connect()
 
54
        {
 
55
            if (parameters.Server)
 
56
            {
 
57
                IPHostEntry ipEntry = Dns.GetHostEntry(parameters.Host);
 
58
                IPAddress ipAddress = ipEntry.AddressList[0];
 
59
                TcpListener listener = new TcpListener(ipAddress, parameters.Port);
 
60
                listener.Start();
 
61
                client = listener.AcceptTcpClient();
 
62
            }
 
63
            else
 
64
            {
 
65
                client = new TcpClient(parameters.Host, parameters.Port);
 
66
            }
 
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];
 
73
            int received = 0;
 
74
            while (received < b.Length)
 
75
            {
 
76
                int n = stream.Read(b, received, b.Length - received);
 
77
                if (n < 0)
 
78
                {
 
79
                    client.Close();
 
80
                    Console.WriteLine("handshake failed - connection prematurally closed");
 
81
                    Environment.Exit(2);
 
82
                }
 
83
                received += n;
 
84
            }
 
85
            for (int j = 0; j < hello.Length; j++)
 
86
            {
 
87
                if (b[j] != hello[j])
 
88
                {
 
89
                    client.Close();
 
90
                    Console.WriteLine("handshake failed - unrecognized message from target VM");
 
91
                    Environment.Exit(2);
 
92
                }
 
93
            }
 
94
        }
 
95
 
 
96
        internal Packet ReadPacket()
 
97
        {
 
98
            lock (readHeader)
 
99
            {
 
100
                DebuggerUtils.ReadFully(stream, readHeader);
 
101
                return Packet.Read(readHeader, stream);
 
102
            }
 
103
        }
 
104
 
 
105
        internal void SendPacket(Packet packet)
 
106
        {
 
107
            lock (writeMonitor)
 
108
            {
 
109
                packet.Send(stream);
 
110
            }
 
111
        }
 
112
    }
 
113
}