Beispiel #1
0
 public DispatchResponder(DispatcherProtocol protocol, DispatcherResponse response)
 {
     _protocol = protocol;
     _response = response;
 }
Beispiel #2
0
        private static void Dispatch(Stream inputStream, Stream outputStream)
        {
            DispatcherProtocol protocol = new DispatcherProtocol(inputStream, outputStream);

            Action <string> traceResponseCallback = s => Console.Error.WriteLine(s);

            if (s_trace_requests)
            {
                protocol.TraceCallback = traceResponseCallback;
            }

            if (s_trace_responses)
            {
                protocol.ResponseCallback = traceResponseCallback;
            }

            if (s_engine_logging && HostLogger.Instance?.LogFilePath == null)
            {
                HostLogger.Instance.LogCallback = s => Console.WriteLine(s);
            }

            IDebugSession debugSession = null;

            protocol.Start((string command, dynamic args, IResponder responder) =>
            {
                if (args == null)
                {
                    args = new { };
                }

                if (command == "initialize")
                {
                    string adapterID = Utilities.GetString(args, "adapterID");
                    if (adapterID == null)
                    {
                        responder.SetBody(new ErrorResponseBody(new Message(1101, "initialize: property 'adapterID' is missing or empty")));
                        return;
                    }

                    DebugProtocolCallbacks debugProtocolCallbacks = new DebugProtocolCallbacks()
                    {
                        Send              = e => protocol.SendEvent(e.type, e),
                        SendRaw           = e => protocol.SendRawEvent(e.type, e),
                        SendLater         = e => protocol.SendEventLater(e.type, e),
                        SetTraceLogger    = t => protocol.TraceCallback = t,
                        SetResponseLogger = t => protocol.ResponseCallback = t,
                        SetEngineLogger   = t =>
                        {
                            HostLogger.EnableHostLogging();
                            HostLogger.Instance.LogCallback = t;
                        }
                    };

                    debugSession = OpenDebugAD7.EngineFactory.CreateDebugSession(adapterID, debugProtocolCallbacks);
                    if (debugSession == null)
                    {
                        responder.SetBody(new ErrorResponseBody(new Message(1103, "initialize: can't create debug session for adapter '{_id}'", new { _id = adapterID })));
                        return;
                    }
                }

                if (debugSession != null)
                {
                    try
                    {
                        DebugResult dr = debugSession.Dispatch(command, args);
                        if (dr != null)
                        {
                            responder.SetBody(dr.Body);

                            if (dr.Events != null)
                            {
                                foreach (var e in dr.Events)
                                {
                                    responder.AddEvent(e.type, e);
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        AggregateException aggregateException = e as AggregateException;
                        bool bodySet = false;
                        if (aggregateException != null)
                        {
                            if (aggregateException.InnerExceptions.Count == 1)
                            {
                                e = aggregateException.InnerException;
                            }
                            else
                            {
                                string exceptionMessages = string.Join(", ", aggregateException.InnerExceptions.Select((x) => Utilities.GetExceptionDescription(x)));
                                responder.SetBody(new ErrorResponseBody(new Message(1104, "error while processing request '{_request}' (exceptions: {_messages})", new { _request = command, _messages = exceptionMessages })));
                                bodySet = true;
                            }
                        }

                        if (!bodySet)
                        {
                            if (Utilities.IsCorruptingException(e))
                            {
                                Utilities.ReportException(e);
                            }

                            if (e is OpenDebugAD7.AD7Exception)
                            {
                                responder.SetBody(new ErrorResponseBody(new Message(1104, e.Message)));
                            }
                            else
                            {
                                responder.SetBody(new ErrorResponseBody(new Message(1104, "error while processing request '{_request}' (exception: {_exception})", new { _request = command, _exception = Utilities.GetExceptionDescription(e) })));
                            }
                        }
                    }

                    if (command == "disconnect")
                    {
                        protocol.Stop();
                    }
                }
            }).Wait();
        }