// // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. // using Microsoft.SqlTools.EditorServices.Protocol.MessageProtocol.Serializers; using System.Threading.Tasks; namespace Microsoft.SqlTools.EditorServices.Protocol.MessageProtocol.Channel { /// /// Defines a base implementation for servers and their clients over a /// single kind of communication channel. /// public abstract class ChannelBase { /// /// Gets a boolean that is true if the channel is connected or false if not. /// public bool IsConnected { get; protected set; } /// /// Gets the MessageReader for reading messages from the channel. /// public MessageReader MessageReader { get; protected set; } /// /// Gets the MessageWriter for writing messages to the channel. /// public MessageWriter MessageWriter { get; protected set; } /// /// Starts the channel and initializes the MessageDispatcher. /// /// The type of message protocol used by the channel. public void Start(MessageProtocolType messageProtocolType) { IMessageSerializer messageSerializer = null; if (messageProtocolType == MessageProtocolType.LanguageServer) { messageSerializer = new JsonRpcMessageSerializer(); } else { messageSerializer = new V8MessageSerializer(); } this.Initialize(messageSerializer); } /// /// Returns a Task that allows the consumer of the ChannelBase /// implementation to wait until a connection has been made to /// the opposite endpoint whether it's a client or server. /// /// A Task to be awaited until a connection is made. public abstract Task WaitForConnection(); /// /// Stops the channel. /// public void Stop() { this.Shutdown(); } /// /// A method to be implemented by subclasses to handle the /// actual initialization of the channel and the creation and /// assignment of the MessageReader and MessageWriter properties. /// /// The IMessageSerializer to use for message serialization. protected abstract void Initialize(IMessageSerializer messageSerializer); /// /// A method to be implemented by subclasses to handle shutdown /// of the channel once Stop is called. /// protected abstract void Shutdown(); } }