Наша сборка Qt VS Tools
giy
2022-06-13 175679ae608f0b295d761588d332f635b21bdf30
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt VS Tools.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
 
using System;
using System.Runtime.Serialization;
 
namespace QtVsTools.Qml.Debug.V4
{
    using Json;
 
    [DataContract]
    class Message : Serializable<Message>
    {
        //  <message type>
        //  ...
        protected Message()
        { }
 
        public static T Create<T>(ProtocolDriver driver)
            where T : Message, new()
        {
            return new T { Driver = driver };
        }
 
        public static bool Send<T>(ProtocolDriver driver, Action<T> initMsg = null)
            where T : Message, new()
        {
            T _this = Create<T>(driver);
            if (initMsg != null)
                initMsg(_this);
            return _this.Send();
        }
 
        public string Type { get; set; }
 
        protected sealed override void InitializeObject(object initArgs)
        {
            if (initArgs is string)
                Type = initArgs as string;
        }
 
        protected override bool? IsCompatible(Message that)
        {
            System.Diagnostics.Debug.Assert(IsPrototype);
 
            if (that == null)
                return false;
            if (string.IsNullOrEmpty(Type))
                return null;
            return (this.Type == that.Type);
        }
 
        protected ProtocolDriver Driver { get; private set; }
 
        public virtual bool Send()
        {
            return Driver.SendMessage(this);
        }
    }
 
    [DataContract]
    class Request : Message
    {
        //  "v8request"
        //  { "seq"     : <number>,
        //    "type"    : "request",
        //    "command" : <command>
        //    ...
        //  }
        public const string MSG_TYPE = "v8request";
        public const string MSG_SUBTYPE = "request";
        protected Request() : base()
        {
            Type = MSG_TYPE;
            SubType = MSG_SUBTYPE;
        }
 
        [DataMember(Name = "type")]
        public string SubType { get; set; }
 
        [DataMember(Name = "command")]
        public string Command { get; set; }
 
        [DataMember(Name = "seq")]
        public int SequenceNum { get; set; }
 
        protected override bool? IsCompatible(Message msg)
        {
            System.Diagnostics.Debug.Assert(IsPrototype);
 
            if (base.IsCompatible(msg) == false)
                return false;
 
            var that = msg as Request;
            if (that == null)
                return null;
 
            if (string.IsNullOrEmpty(SubType))
                return null;
 
            if (string.IsNullOrEmpty(Command))
                return null;
 
            return this.SubType == that.SubType && this.Command == that.Command;
        }
 
        Response response = null;
        public Response Response
        {
            get { return response; }
            set { Atomic(() => response == null, () => response = value); }
        }
 
        object tag = null;
        public object Tag
        {
            get { return tag; }
            set { Atomic(() => tag == null, () => tag = value); }
        }
 
        public virtual ProtocolDriver.PendingRequest SendAsync()
        {
            return Driver.SendRequest(this);
        }
 
        public new Response Send()
        {
            return SendAsync().WaitForResponse();
        }
 
        public static new Response Send<T>(ProtocolDriver driver, Action<T> initMsg = null)
            where T : Request, new()
        {
            T _this = Create<T>(driver);
            if (initMsg != null)
                initMsg(_this);
            return _this.Send();
        }
    }
 
    [DataContract]
    [SkipDeserialization]
    class Request<TResponse> : Request
        where TResponse : Response
    {
        public new TResponse Response
        { get { return base.Response as TResponse; } }
 
        public new virtual TResponse Send()
        {
            var pendingRequest = SendAsync();
            if (!pendingRequest.RequestSent)
                return null;
 
            if (pendingRequest.WaitForResponse() == null)
                return null;
 
            return Response;
        }
    }
 
    [DataContract]
    [SkipDeserialization]
    class Request<TResponse, TArgs> : Request<TResponse>
        where TResponse : Response
        where TArgs : class, new()
    {
        [DataMember(Name = "arguments", EmitDefaultValue = false)]
        TArgs args = null;
 
        public virtual TArgs Arguments
        {
            get { return ThreadSafe(() => (args != null) ? args : (args = new TArgs())); }
            set { args = value; }
        }
    }
 
    [DataContract]
    class ServerMessage : Message
    {
        //  "v8message"
        //  { "seq"  : <number>,
        //    "type" : <string>
        //    ...
        //  }
        public const string MSG_TYPE = "v8message";
        protected ServerMessage() : base()
        {
            Type = MSG_TYPE;
        }
 
        [DataMember(Name = "type")]
        public string SubType { get; set; }
 
        [DataMember(Name = "seq")]
        public int SequenceNum { get; set; }
 
        protected override bool? IsCompatible(Message msg)
        {
            System.Diagnostics.Debug.Assert(IsPrototype);
 
            if (base.IsCompatible(msg) == false)
                return false;
 
            var that = msg as ServerMessage;
            if (that == null)
                return null;
 
            if (string.IsNullOrEmpty(SubType))
                return null;
 
            return this.SubType == that.SubType;
        }
    }
 
    [DataContract]
    class Response : ServerMessage
    {
        //  "v8message"
        //  { "seq"         : <number>,
        //    "type"        : "response",
        //    "request_seq" : <number>,
        //    "command"     : <command>,
        //    "running"     : <is the VM running after sending this response>,
        //    "success"     : <success?>,
        //    "message"     : [error message]
        //    ...
        //  }
        public const string MSG_SUBTYPE = "response";
        protected Response() : base()
        {
            SubType = MSG_SUBTYPE;
        }
 
        [DataMember(Name = "command")]
        public string Command { get; set; }
 
        [DataMember(Name = "request_seq")]
        public int RequestSeq { get; set; }
 
        [DataMember(Name = "success")]
        public bool Success { get; set; }
 
        [DataMember(Name = "running")]
        public bool Running { get; set; }
 
        [DataMember(Name = "message")]
        public string Message { get; set; }
 
        protected override bool? IsCompatible(Message msg)
        {
            System.Diagnostics.Debug.Assert(IsPrototype);
 
            if (base.IsCompatible(msg) == false)
                return false;
 
            var that = msg as Response;
            if (that == null)
                return null;
 
            // If response is unsuccessful, no need to continue searching, just use this class,
            // it already has all the data needed for error processing (i.e. the error message)
            if (!that.Success)
                return true;
 
            if (string.IsNullOrEmpty(Command))
                return null;
 
            return this.Command == that.Command;
        }
    }
 
    [DataContract]
    [SkipDeserialization]
    class Response<TBody> : Response
        where TBody : class, new()
    {
        [DataMember(Name = "body", EmitDefaultValue = false)]
        TBody body = null;
 
        public TBody Body
        {
            get { return ThreadSafe(() => (body != null) ? body : (body = new TBody())); }
            set { body = value; }
        }
    }
 
    [DataContract]
    class Event : ServerMessage
    {
        //  "v8message"
        //  { "seq"   : <number>,
        //    "type"  : "event",
        //    "event" : <string>,
        //    ...
        //  }
        public const string MSG_SUBTYPE = "event";
        protected Event() : base()
        {
            SubType = MSG_SUBTYPE;
        }
 
        [DataMember(Name = "event")]
        public string EventType { get; set; }
 
        protected override bool? IsCompatible(Message msg)
        {
            System.Diagnostics.Debug.Assert(IsPrototype);
 
            if (base.IsCompatible(msg) == false)
                return false;
 
            var that = msg as Event;
            if (that == null)
                return null;
 
            if (string.IsNullOrEmpty(EventType))
                return null;
 
            return this.EventType == that.EventType;
        }
    }
 
    [DataContract]
    [SkipDeserialization]
    class Event<TBody> : Event
        where TBody : class, new()
    {
        [DataMember(Name = "body", EmitDefaultValue = false)]
        TBody body = null;
 
        public TBody Body
        {
            get { return ThreadSafe(() => (body != null) ? body : (body = new TBody())); }
            set { body = value; }
        }
    }
}