示例#1
0
        private async Task SendAsync(Protocol.Message message, CancellationToken cancellationToken)
        {
            var token = JToken.FromObject(message, new JsonSerializer()
            {
                NullValueHandling = NullValueHandling.Include
            });

            await SendAsync(token, cancellationToken).ConfigureAwait(false);
        }
示例#2
0
 private async Task SendAsync(Protocol.Message message, CancellationToken cancellationToken)
 {
     var token = JToken.FromObject(message, new JsonSerializer()
     {
         NullValueHandling = NullValueHandling.Include,
         ContractResolver = new CamelCasePropertyNamesContractResolver()
     });
     await SendAsync(token, cancellationToken).ConfigureAwait(false);
 }
示例#3
0
 /// <summary>
 /// Forward the PreviewMessage event on the UI thread.
 /// </summary>
 private bool LbForwardPreviewMessage(byte[] message, Protocol.Message decoded)
 {
     if (PreviewMessage != null)
     {
         if (ui.InvokeRequired)
         {
             return((bool)ui.Invoke(PreviewMessage, message, decoded));
         }
         return(PreviewMessage(message, decoded));
     }
     return(false);
 }
示例#4
0
 public Message(Header header,
                Protocol.Message content = null,
                Header parentHeader      = null,
                string signature         = null,
                IReadOnlyDictionary <string, object> metaData     = null,
                IReadOnlyList <IReadOnlyList <byte> > identifiers = null,
                IReadOnlyList <IReadOnlyList <byte> > buffers     = null)
 {
     Header       = header;
     ParentHeader = parentHeader;
     Buffers      = buffers ?? new List <IReadOnlyList <byte> >();
     Identifiers  = identifiers ?? new List <IReadOnlyList <byte> >();
     MetaData     = metaData ?? new Dictionary <string, object>();
     Content      = content ?? Protocol.Message.Empty;
     Signature    = signature ?? string.Empty;
 }
示例#5
0
 public void SerializeData <T>(Socket socket, Protocol.Message <T> msg)
 {
     try
     {
         IFormatter    formatter = new BinaryFormatter();
         NetworkStream strm      = new NetworkStream(socket);
         formatter.Serialize(strm, msg);
     }
     catch (SerializationException e)
     {
         new Exceptions.SerializationException(e);
         socket.Close();
     }
     catch (Exception e)
     {
         new Exceptions.UnknowException(e);
         socket.Close();
     }
 }
示例#6
0
 public Protocol.Message <T> DeserializeData <T>(Socket socket)
 {
     try
     {
         NetworkStream        strm      = new NetworkStream(socket);
         IFormatter           formatter = new BinaryFormatter();
         Protocol.Message <T> msg       = (Protocol.Message <T>)formatter.Deserialize(strm);
         return(msg);
     }
     catch (SerializationException e)
     {
         new Exceptions.SerializationException(e);
     }
     catch (Exception e)
     {
         new Exceptions.UnknowException(e);
     }
     socket.Close();
     return(null);
 }
示例#7
0
        public override void Dispatch(NetworkClient client, Protocol.Message message)
        {
            List <MessageHandler> handlers;

            if (m_handlers.TryGetValue(message.MessageId, out handlers))
            {
                try
                {
                    foreach (var handler in handlers)
                    {
                        m_taskPool.AddMessage(new SelfRunningMessage(new HandledMessage <NetworkClient>(handler.Action, client, message)));
                    }
                }
                catch (Exception ex)
                {
                    Log.Write(LogLevel.Info, string.Format("[Handler : {0}] Force disconnection of client {1} : {2}", message, client, ex));
                    client.Disconnect();
                }
            }
        }
示例#8
0
        private async Task<Protocol.Message> DispatchAsync(Protocol.Message message, CancellationToken cancellationToken)
        {
            if (message is Protocol.Request<Protocol.Initialize> initialize)
            {
                var body = MakeCapabilities();
                var response = Protocol.Response.From(NextSeq, initialize, body);
                await SendAsync(response, cancellationToken).ConfigureAwait(false);
                return Protocol.Event.From(NextSeq, "initialized", new { });
            }
            else if (message is Protocol.Request<Protocol.Launch> launch)
            {
                return Protocol.Response.From(NextSeq, launch, new { });
            }
            else if (message is Protocol.Request<Protocol.Attach> attach)
            {
                return Protocol.Response.From(NextSeq, attach, new { });
            }
            else if (message is Protocol.Request<Protocol.SetBreakpoints> setBreakpoints)
            {
                var arguments = setBreakpoints.Arguments;
                var file = Path.GetFileName(arguments.Source.Path);
                await OutputAsync($"Set breakpoints for {file}", null, cancellationToken).ConfigureAwait(false);

                var breakpoints = this.breakpoints.SetBreakpoints(arguments.Source, arguments.Breakpoints);
                foreach (var breakpoint in breakpoints)
                {
                    if (breakpoint.Verified)
                    {
                        var item = this.breakpoints.ItemFor(breakpoint);
                        await OutputAsync($"Set breakpoint at {codeModel.NameFor(item)}", item, cancellationToken).ConfigureAwait(false);
                    }
                }

                return Protocol.Response.From(NextSeq, setBreakpoints, new { breakpoints });
            }
            else if (message is Protocol.Request<Protocol.SetFunctionBreakpoints> setFunctionBreakpoints)
            {
                var arguments = setFunctionBreakpoints.Arguments;
                await OutputAsync($"Set function breakpoints.", null, cancellationToken).ConfigureAwait(false);
                var breakpoints = this.breakpoints.SetBreakpoints(arguments.Breakpoints);
                foreach (var breakpoint in breakpoints)
                {
                    if (breakpoint.Verified)
                    {
                        var item = this.breakpoints.ItemFor(breakpoint);
                        await OutputAsync($"Set breakpoint at {codeModel.NameFor(item)}", item, cancellationToken).ConfigureAwait(false);
                    }
                }

                return Protocol.Response.From(NextSeq, setFunctionBreakpoints, new { breakpoints });
            }
            else if (message is Protocol.Request<Protocol.SetExceptionBreakpoints> setExceptionBreakpoints)
            {
                var arguments = setExceptionBreakpoints.Arguments;
                this.events.Reset(arguments.Filters);

                return Protocol.Response.From(NextSeq, setExceptionBreakpoints, new { });
            }
            else if (message is Protocol.Request<Protocol.Threads> threads)
            {
                var body = new
                {
                    threads = this.threads.Select(t => new { id = t.Key, name = t.Value.Name }).ToArray()
                };

                return Protocol.Response.From(NextSeq, threads, body);
            }
            else if (message is Protocol.Request<Protocol.StackTrace> stackTrace)
            {
                var arguments = stackTrace.Arguments;
                var thread = this.threads[arguments.ThreadId];

                var frames = thread.Frames;
                var stackFrames = new List<Protocol.StackFrame>();
                foreach (var frame in frames)
                {
                    var stackFrame = new Protocol.StackFrame()
                    {
                        Id = EncodeFrame(thread, frame),
                        Name = frame.Name
                    };

                    if (this.sourceMap.TryGetValue(frame.Item, out var range))
                    {
                        DebuggerSourceMap.Assign(stackFrame, range);
                    }

                    stackFrames.Add(stackFrame);
                }

                return Protocol.Response.From(NextSeq, stackTrace, new { stackFrames });
            }
            else if (message is Protocol.Request<Protocol.Scopes> scopes)
            {
                var arguments = scopes.Arguments;
                DecodeFrame(arguments.FrameId, out var thread, out var frame);
                const bool expensive = false;

                var body = new
                {
                    scopes = new[]
                    {
                        new { expensive, name = frame.Name, variablesReference = EncodeValue(thread, frame.Data) }
                    }
                };

                return Protocol.Response.From(NextSeq, scopes, body);
            }
            else if (message is Protocol.Request<Protocol.Variables> vars)
            {
                var arguments = vars.Arguments;
                DecodeValue(arguments.VariablesReference, out var thread, out var context);

                var names = this.dataModel.Names(context);

                var body = new
                {
                    variables = (from name in names
                                 let value = dataModel[context, name]
                                 let variablesReference = EncodeValue(thread, value)
                                 select new { name = dataModel.ToString(name), value = dataModel.ToString(value), variablesReference })
                                .ToArray()
                };

                return Protocol.Response.From(NextSeq, vars, body);
            }
            else if (message is Protocol.Request<Protocol.SetVariable> setVariable)
            {
                var arguments = setVariable.Arguments;
                DecodeValue(arguments.VariablesReference, out var thread, out var context);

                var value = this.dataModel[context, arguments.Name] = JToken.Parse(arguments.Value);

                var body = new
                {
                    value = dataModel.ToString(value),
                    variablesReference = EncodeValue(thread, value)
                };

                return Protocol.Response.From(NextSeq, setVariable, body);
            }
            else if (message is Protocol.Request<Protocol.Evaluate> evaluate)
            {
                var arguments = evaluate.Arguments;
                DecodeFrame(arguments.FrameId, out var thread, out var frame);
                var expression = arguments.Expression.Trim('"');
                var result = frame.Evaluate(expression);
                if (result != null)
                {
                    var body = new
                    {
                        result = dataModel.ToString(result),
                        variablesReference = EncodeValue(thread, result),
                    };

                    return Protocol.Response.From(NextSeq, evaluate, body);
                }
                else
                {
                    return Protocol.Response.Fail(NextSeq, evaluate, string.Empty);
                }
            }
            else if (message is Protocol.Request<Protocol.Continue> cont)
            {
                bool found = this.threads.TryGetValue(cont.Arguments.ThreadId, out var thread);
                if (found)
                {
                    thread.Run.Post(Phase.Continue);
                }

                return Protocol.Response.From(NextSeq, cont, new { allThreadsContinued = false });
            }
            else if (message is Protocol.Request<Protocol.Pause> pause)
            {
                bool found = this.threads.TryGetValue(pause.Arguments.ThreadId, out var thread);
                if (found)
                {
                    thread.Run.Post(Phase.Pause);
                }

                return Protocol.Response.From(NextSeq, pause, new { });
            }
            else if (message is Protocol.Request<Protocol.Next> next)
            {
                bool found = this.threads.TryGetValue(next.Arguments.ThreadId, out var thread);
                if (found)
                {
                    thread.Run.Post(Phase.Next);
                }

                return Protocol.Response.From(NextSeq, next, new { });
            }
            else if (message is Protocol.Request<Protocol.Terminate> terminate)
            {
                if (this.terminate != null)
                {
                    this.terminate();
                }

                return Protocol.Response.From(NextSeq, terminate, new { });
            }
            else if (message is Protocol.Request<Protocol.Disconnect> disconnect)
            {
                var arguments = disconnect.Arguments;
                if (arguments.TerminateDebuggee && this.terminate != null)
                {
                    this.terminate();
                }

                // if attach, possibly run all threads
                return Protocol.Response.From(NextSeq, disconnect, new { });
            }
            else if (message is Protocol.Request request)
            {
                return Protocol.Response.From(NextSeq, request, new { });
            }
            else if (message is Protocol.Event @event)
            {
                throw new NotImplementedException();
            }
            else
            {
                throw new NotImplementedException();
            }
        }