Protocol Class |
[TypeConverterAttribute(typeof(ExpandableObjectConverter))] public abstract class Protocol : IDisposable
The Protocol type exposes the following members.
| Name | Description | |
|---|---|---|
| Protocol(String, Type) | Creates a new protocol. | |
| Protocol(String, ServiceProvider, ProtocolRequiredService) | Creates a new protocol. | |
| Protocol(String, IEnumerableType, ProtocolRequiredService) | Creates a new protocol. | |
| Protocol(String, IReadOnlyListChannel, ProtocolRequiredService) | Creates a new protocol. | |
| Protocol(String, Type, ProtocolRequiredService) | Creates a new protocol. |
| Name | Description | |
|---|---|---|
| Channels | List of all channels. | |
| DefaultChannel | The default channel. | |
| Name | The protocol name. | |
| Registry | A register of all protocols. | |
| Required | Required protocols and services. |
| Name | Description | |
|---|---|---|
| AddChannel | Adds a channel. | |
| CreateDownStreamAddress | 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. | |
| Dispose | Releases all protocol resources. | |
| FindChannel | Returns the named channel, or null. | |
| NewInstance | Create a new server/client protocol instance. | |
| RegisterService | Registers a service. | |
| RemoveChannel | Removes a channel. | |
| SupportsService | Returns whether a particular service is supported. | |
| ToString |
Returns the name of the protocol.
(Overrides ObjectToString) | |
| UnregisterService | Unregisters a service. |
The following example shows the most basic implementation of a Protocol.
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()); } }