aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/VSIX/Tango.BuildExtensions/RemoteDebugCommand.cs
blob: 8c794daed5008883dc98bd5f272c2811e84f51c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//------------------------------------------------------------------------------
// <copyright file="RemoteDebugCommand.cs" company="Company">
//     Copyright (c) Company.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

using System;
using System.ComponentModel.Design;
using System.Globalization;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System.Linq;
using System.IO;
using System.Diagnostics;
using Tango.Transport;
using Tango.Transport.Adapters;
using Tango.Transport.Transporters;
using System.IO.Compression;
using Tango.Core.IO;
using Tango.PMR.IO;
using Google.Protobuf;
using EnvDTE;
using System.Collections.Generic;

namespace Tango.BuildExtensions
{
    /// <summary>
    /// Command handler
    /// </summary>
    internal sealed class RemoteDebugCommand : VSIXBase
    {
        private DebuggerEvents _dteDebuggerEvents;
        private ITransporter transporter;
        private bool _attached;
        private string processId;
        private OleMenuCommand _menuCommand;
        private IList<Project> _projects;


        /// <summary>
        /// Command ID.
        /// </summary>
        public const int CommandId = 4129;

        /// <summary>
        /// Command menu group (command set GUID).
        /// </summary>
        public static readonly Guid CommandSet = new Guid("c03a7b01-8109-4ec5-8f90-858bed027e5d");

        /// <summary>
        /// VS Package that provides this command, not null.
        /// </summary>
        private readonly Package package;

        /// <summary>
        /// Initializes a new instance of the <see cref="RemoteDebugCommand"/> class.
        /// Adds our command handlers for menu (commands must exist in the command table file)
        /// </summary>
        /// <param name="package">Owner package, not null.</param>
        private RemoteDebugCommand(Package package) : base(package)
        {
            if (package == null)
            {
                throw new ArgumentNullException("package");
            }

            _dteDebuggerEvents = DTE.Events.DebuggerEvents;

            _dteDebuggerEvents.OnEnterDesignMode += _dteDebuggerEvents_OnEnterDesignMode;

            DTE.Events.SolutionEvents.Opened += () =>
            {
                _projects = GetSolutionProjects().ToList();
            };

            this.package = package;

            OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
            if (commandService != null)
            {
                var menuCommandID = new CommandID(CommandSet, CommandId);
                _menuCommand = new OleMenuCommand(this.MenuItemCallback, menuCommandID);
                _menuCommand.Visible = false;
                _menuCommand.BeforeQueryStatus += _menuCommand_BeforeQueryStatus;
                commandService.AddCommand(_menuCommand);
            }
        }

        private void _menuCommand_BeforeQueryStatus(object sender, EventArgs e)
        {
            (sender as OleMenuCommand).Visible = false;

            if (DTE.Debugger.CurrentMode == dbgDebugMode.dbgDesignMode
                && DTE.Solution != null
                && DTE.Solution.IsOpen)
            {
                String projectName = DTE.Solution.Properties.Item("StartupProject").Value.ToStringSafe();

                if (projectName != null)
                {
                    var project = _projects.SingleOrDefault(x => x.Name == projectName);
                    String path = GetProjectOutputFilePath(project);

                    if (Path.GetExtension(path.ToLower()) == ".exe")
                    {
                        (sender as OleMenuCommand).Visible = true;
                    }
                }
            }
        }

#pragma warning disable VSTHRD100 // Avoid async void methods
        private async void _dteDebuggerEvents_OnEnterDesignMode(dbgEventReason Reason)
#pragma warning restore VSTHRD100 // Avoid async void methods
        {
            if (_attached)
            {
                if (Reason == dbgEventReason.dbgEventReasonStopDebugging)
                {
                    _attached = false;

                    if (transporter != null)
                    {
                        OpenVSProgress("Tango Remote Runner", "Closing remote process...", true);

                        await transporter.SendRequest<KillProcessRequest, KillProcessResponse>(new KillProcessRequest()
                        {
                            ProcessID = processId,
                        });
                        await transporter.Disconnect();
                        transporter = null;

                        await System.Threading.Tasks.Task.Delay(2000);

                        CloseVSProgress();
                    }
                }
            }
        }

        /// <summary>
        /// Gets the instance of the command.
        /// </summary>
        public static RemoteDebugCommand Instance
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the service provider from the owner package.
        /// </summary>
        private IServiceProvider ServiceProvider
        {
            get
            {
                return this.package;
            }
        }

        /// <summary>
        /// Initializes the singleton instance of the command.
        /// </summary>
        /// <param name="package">Owner package, not null.</param>
        public static void Initialize(Package package)
        {
            Instance = new RemoteDebugCommand(package);
        }

        // %ifnot% $toolWindow$
        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// See the constructor to see how the menu item is associated with this function using
        /// OleMenuCommandService service and MenuCommand class.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event args.</param>
        private void MenuItemCallback(object sender, EventArgs e)
        {
            RunRemote();
        }

        private void RunRemote()
        {
            _attached = false;

            try
            {
                String projectName = DTE.Solution.Properties.Item("StartupProject").Value.ToString();

                RemoteDebugForm dlg = new RemoteDebugForm(projectName);

                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
#pragma warning disable VSTHRD105 // Avoid method overloads that assume TaskScheduler.Current
                    System.Threading.Tasks.Task.Factory.StartNew(async () =>
#pragma warning restore VSTHRD105 // Avoid method overloads that assume TaskScheduler.Current
                    {
                        try
                        {
                            OpenVSProgress("Tango Remote Runner", "Connecting to " + dlg.SelectedRemoteRunner.HostName + "...", true);

                            ITransportAdapter adapter = new TcpTransportAdapter(dlg.SelectedRemoteRunner.Address, dlg.SelectedRemoteRunner.Port);
                            transporter = new BasicTransporter(adapter);
                            await transporter.Connect();

                            SetVSProgress("Building " + projectName + "...");

                            var project = GetSolutionProjects().SingleOrDefault(x => x.Name == projectName);

                            String filePath = GetProjectOutputFilePath(project);
                            String folder = Path.GetDirectoryName(filePath);
                            String fileName = Path.GetFileName(filePath);

                            DTE.Solution.SolutionBuild.BuildProject("Debug", project.FullName, true);

                            var tempFolder = TemporaryManager.Default.CreateFolder();
                            var zipFile = tempFolder.CreateFile(".zip");

                            await zipFile.DeleteAsync();
                            SetVSProgress("Packaging project...");
                            ZipFile.CreateFromDirectory(folder, zipFile.Path);

                            System.IO.FileInfo zipFileInfo = new System.IO.FileInfo(zipFile);

                            CloseVSProgress();

                            OpenVSProgress("Tango Remote Runner", "Uploading project...", false);

                            var upResponse = await transporter.SendRequest<FileUploadRequest, FileUploadResponse>(new FileUploadRequest()
                            {
                                Path = zipFile.FileName,
                                Length = zipFileInfo.Length,
                            });

                            var uploadResponse = upResponse.Message;

                            int currentFilePosition = 0;

                            do
                            {
                                using (FileStream fs = new FileStream(zipFileInfo.FullName, FileMode.Open))
                                {
                                    fs.Position = currentFilePosition;

                                    FileChunkUploadRequest chunkRequest = new FileChunkUploadRequest();
                                    chunkRequest.UploadID = uploadResponse.UploadID;

                                    long remaining = zipFileInfo.Length - currentFilePosition;

                                    byte[] buffer = new byte[uploadResponse.MaxChunkLength < remaining ? uploadResponse.MaxChunkLength : remaining];
                                    await fs.ReadAsync(buffer, 0, buffer.Length);
                                    chunkRequest.Buffer = ByteString.CopyFrom(buffer);

                                    await transporter.SendRequest<FileChunkUploadRequest, FileChunkUploadResponse>(chunkRequest);

                                    currentFilePosition += buffer.Length;

                                    SetVSProgress("Uploading project...", "", currentFilePosition, (int)zipFileInfo.Length);
                                }

                            } while (currentFilePosition < zipFileInfo.Length);

                            Wait(1000);

                            CloseVSProgress();

                            OpenVSProgress("Tango Remote Runner", "Executing remote process...", true);

                            var response = await transporter.SendRequest<ExecuteProcessRequest, ExecuteProcessResponse>(new ExecuteProcessRequest()
                            {
                                UploadID = uploadResponse.UploadID,
                                FileName = fileName,
                            }, TimeSpan.FromSeconds(30));

                            processId = response.Message.ProcessID;

                            SetVSProgress("Attaching Debugger...");

                            var debugger = DTE.Debugger as EnvDTE80.Debugger2;
                            var transport = debugger.Transports.Item("Remote");
                            var process = debugger.GetProcesses(transport, dlg.SelectedRemoteRunner.Address).Item(fileName) as EnvDTE80.Process2;
                            process.Attach();
                            _attached = true;


                            CloseVSProgress();
                        }
                        catch (Exception ex)
                        {
                            CloseVSProgress();
                            ShowMessage(ex.Message);
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                CloseVSProgress();
                ShowMessage(ex.Message);
            }
        }
    }
}