Click or drag to resize

Protocol Class

Represents one protocol.
Inheritance Hierarchy
SystemObject
  Demo3D.NetProtocol
    More

Namespace: Demo3D.Net
Assembly: Demo3D.IO (in Demo3D.IO.dll) Version: 19.00.00
Syntax
C#
[TypeConverterAttribute(typeof(ExpandableObjectConverter))]
public abstract class Protocol : IDisposable

The Protocol type exposes the following members.

Constructors
Properties
 NameDescription
Public propertyChannels List of all channels.
Public propertyDefaultChannel The default channel.
Public propertyName The protocol name.
Public propertyStatic memberRegistry A register of all protocols.
Public propertyRequired Required protocols and services.
Top
Methods
 NameDescription
Public methodAddChannel Adds a channel.
Protected methodCreateDownStreamAddress Creates the downstream address given an upstream address and a downstream protocol. Normally this does not need to be overridden, but there are some cases where it's required. For example, the downstream address returned by COTP for "cotp://host/tsap" would be "tpkt://host:102". Or you can return null to stop the protocol stack from building downstream protocol instances. You might do this if you want to take control of building the downstream protocol instances, or nor build them at all.
Public methodDispose Releases all protocol resources.
Public methodFindChannel Returns the named channel, or null.
Protected methodNewInstance Create a new server/client protocol instance.
Public methodRegisterService Registers a service.
Public methodRemoveChannel Removes a channel.
Public methodSupportsService Returns whether a particular service is supported.
Public methodToString Returns the name of the protocol.
(Overrides ObjectToString)
Public methodUnregisterService Unregisters a service.
Top
Example

The following example shows the most basic implementation of a Protocol.

C#
using System;
using System.Threading.Tasks;
using Demo3D.Net;

namespace Examples.Net.ExampleProtocol {
    /// <summary>
    /// An interface that describes the services (the IO methods) that your protocol supports.
    /// Typically a protocol would implement one of the standard services (such as <see cref="IPacketIOService"/>).
    /// </summary>
    interface IExampleService {
        // Put whatever methods users of your protocol would expect in here.
    }

    /// <summary>
    /// An instance of a protocol.  Must inherit from <see cref="ProtocolInstance"/>, and should
    /// support a set of services.  The simplest way to support services is to implement the
    /// services interface (<see cref="IExampleService"/>) and to advertise those services in the
    /// <see cref="ExampleProtocol.NewInstance(ProtocolHead, ProtocolAddress)"/> constructor.
    /// </summary>
    /// <remarks>
    /// See <see cref="TCPExample.Client.TcpClientProtocol.TcpClientConnection"/> for an
    /// example implementing the TCP protocol.
    /// </remarks>
    public class ExampleProtocolInstance : ProtocolInstance, IExampleService {
        public ExampleProtocolInstance(Protocol protocol, ProtocolHead head, ProtocolAddress address) : base(protocol, head, address, false, null) { }

        /// <summary>
        /// Initializes the socket.
        /// </summary>
        /// <param name="sync">If true, the Task returned must be guaranteed to be complete.</param>
        protected override Task InitializeAsync(bool sync) {
            // Any initialization code goes here.
            return Task.CompletedTask;
        }

        /// <summary>
        /// Indicates whether the instance is still running.
        /// </summary>
        protected override bool InternalRunning => base.InternalRunning;  // Return true if the connection is established

        /// <summary>
        /// Opens or reopens the instance.
        /// </summary>
        /// <param name="sync">If true, the Task returned must be guaranteed to be complete.</param>
        /// <param name="flags">Additional flags.</param>
        protected override Task InternalOpenAsync(bool sync, Flags flags) {
            // Open your connection here.
            return Task.CompletedTask;
        }

        /// <summary>
        /// Forcibly shuts down the underlying protocol, causing all other users to throw an error.
        /// </summary>
        protected override Task InternalShutdownAsync(bool sync) {
            // Forcibly shut the connection down.
            return Task.CompletedTask;
        }

        /// <summary>
        /// Performs a controlled close.
        /// </summary>
        protected override Task InternalCloseAsync(bool sync) {
            // Called to close the connection.
            return InternalShutdownAsync(sync);
        }

        // Implement any IExampleService methods here.
    }

    /// <summary>
    /// A protocol inherits from <see cref="Protocol"/>.
    /// </summary>
    /// See <see cref="TCPExample.Client.TcpClientProtocol"/> for an example implementing the TCP protocol,
    /// or <see cref="TCPExample.Server.TcpServerProtocol"/> for an example implementing a TCP server.
    /// </remarks>
    sealed class ExampleProtocol : Protocol {
        /// <summary>
        /// Constructs a new protocol.
        /// You need to give your protocol a name, and advertise which services it supports.
        /// </summary>
        public ExampleProtocol() : base("Example", typeof(IExampleService)) { }

        /// <summary>
        /// Create a new server/client protocol instance.
        /// At the minimum, you need to implement this method to return a new instance of your protocol.
        /// </summary>
        /// <param name="head">The socket head which is a required parameter to the ProtocolInstance constructor.</param>
        /// <param name="protocolAddress">The protocol address.</param>
        /// <returns>A new ProtocolInstance.</returns>
        protected override ProtocolInstance NewInstance(ProtocolHead head, ProtocolAddress protocolAddress) {
            return new ExampleProtocolInstance(this, head, protocolAddress);
        }

        /// <summary>
        /// Registers the protocol with Demo3D.Net.
        /// You need to call this method somewhere in your code in order to register this protocol with Demo3D.Net.
        /// </summary>
        public static IDisposable Register() => Registry.Register(new ExampleProtocol());
    }
}
See Also
Inheritance Hierarchy