//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System.Diagnostics;
using Newtonsoft.Json.Linq;
namespace Microsoft.SqlTools.EditorServices.Protocol.MessageProtocol
{
///
/// Defines all possible message types.
///
public enum MessageType
{
Unknown,
Request,
Response,
Event
}
///
/// Provides common details for protocol messages of any format.
///
[DebuggerDisplay("MessageType = {MessageType.ToString()}, Method = {Method}, Id = {Id}")]
public class Message
{
///
/// Gets or sets the message type.
///
public MessageType MessageType { get; set; }
///
/// Gets or sets the message's sequence ID.
///
public string Id { get; set; }
///
/// Gets or sets the message's method/command name.
///
public string Method { get; set; }
///
/// Gets or sets a JToken containing the contents of the message.
///
public JToken Contents { get; set; }
///
/// Gets or sets a JToken containing error details.
///
public JToken Error { get; set; }
///
/// Creates a message with an Unknown type.
///
/// A message with Unknown type.
public static Message Unknown()
{
return new Message
{
MessageType = MessageType.Unknown
};
}
///
/// Creates a message with a Request type.
///
/// The sequence ID of the request.
/// The method name of the request.
/// The contents of the request.
/// A message with a Request type.
public static Message Request(string id, string method, JToken contents)
{
return new Message
{
MessageType = MessageType.Request,
Id = id,
Method = method,
Contents = contents
};
}
///
/// Creates a message with a Response type.
///
/// The sequence ID of the original request.
/// The method name of the original request.
/// The contents of the response.
/// A message with a Response type.
public static Message Response(string id, string method, JToken contents)
{
return new Message
{
MessageType = MessageType.Response,
Id = id,
Method = method,
Contents = contents
};
}
///
/// Creates a message with a Response type and error details.
///
/// The sequence ID of the original request.
/// The method name of the original request.
/// The error details of the response.
/// A message with a Response type and error details.
public static Message ResponseError(string id, string method, JToken error)
{
return new Message
{
MessageType = MessageType.Response,
Id = id,
Method = method,
Error = error
};
}
///
/// Creates a message with an Event type.
///
/// The method name of the event.
/// The contents of the event.
/// A message with an Event type.
public static Message Event(string method, JToken contents)
{
return new Message
{
MessageType = MessageType.Event,
Method = method,
Contents = contents
};
}
}
}