|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the Apache 2.0 License |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | +// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone |
| 5 | + |
| 6 | +using System.Net; |
| 7 | + |
| 8 | +namespace BootstrapBlazor.Components; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Provides a base implementation for a TCP socket client, enabling connection, data transmission, and reception over |
| 12 | +/// TCP. |
| 13 | +/// </summary> |
| 14 | +/// <remarks>This abstract class serves as a foundation for implementing TCP socket clients. It provides methods |
| 15 | +/// for connecting to a remote endpoint, sending and receiving data, and managing connection state. Derived classes can |
| 16 | +/// extend or customize the behavior as needed.</remarks> |
| 17 | +public abstract class TcpSocketClientBase : ITcpSocketClient |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// <inheritdoc/> |
| 21 | + /// </summary> |
| 22 | + public abstract bool IsConnected { get; } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// <inheritdoc/> |
| 26 | + /// </summary> |
| 27 | + public IPEndPoint? LocalEndPoint { get; set; } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// <inheritdoc/> |
| 31 | + /// </summary> |
| 32 | + public int ReceiveBufferSize { get; set; } = 1024 * 64; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// <inheritdoc/> |
| 36 | + /// </summary> |
| 37 | + public bool IsAutoReceive { get; set; } = true; |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// <inheritdoc/> |
| 41 | + /// </summary> |
| 42 | + public Func<ReadOnlyMemory<byte>, ValueTask>? ReceivedCallBack { get; set; } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// <inheritdoc/> |
| 46 | + /// </summary> |
| 47 | + public int ConnectTimeout { get; set; } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// <inheritdoc/> |
| 51 | + /// </summary> |
| 52 | + public int SendTimeout { get; set; } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// <inheritdoc/> |
| 56 | + /// </summary> |
| 57 | + public int ReceiveTimeout { get; set; } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// <inheritdoc/> |
| 61 | + /// </summary> |
| 62 | + public virtual void SetDataHandler(IDataPackageHandler handler) |
| 63 | + { |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// <inheritdoc/> |
| 69 | + /// </summary> |
| 70 | + /// <param name="endPoint"></param> |
| 71 | + /// <param name="token"></param> |
| 72 | + /// <returns></returns> |
| 73 | + public abstract ValueTask<bool> ConnectAsync(IPEndPoint endPoint, CancellationToken token = default); |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// <inheritdoc/> |
| 77 | + /// </summary> |
| 78 | + /// <param name="data"></param> |
| 79 | + /// <param name="token"></param> |
| 80 | + /// <returns></returns> |
| 81 | + public abstract ValueTask<bool> SendAsync(ReadOnlyMemory<byte> data, CancellationToken token = default); |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// <inheritdoc/> |
| 85 | + /// </summary> |
| 86 | + /// <param name="token"></param> |
| 87 | + /// <returns></returns> |
| 88 | + public abstract ValueTask<Memory<byte>> ReceiveAsync(CancellationToken token = default); |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// <inheritdoc/> |
| 92 | + /// </summary> |
| 93 | + public virtual ValueTask Close() |
| 94 | + { |
| 95 | + return DisposeAsync(true); |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Releases the resources used by the current instance of the class. |
| 100 | + /// </summary> |
| 101 | + /// <remarks>This method is called to free both managed and unmanaged resources. If the <paramref |
| 102 | + /// name="disposing"/> parameter is <see langword="true"/>, the method releases managed resources in addition to |
| 103 | + /// unmanaged resources. Override this method in a derived class to provide custom cleanup logic.</remarks> |
| 104 | + /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only |
| 105 | + /// unmanaged resources.</param> |
| 106 | + protected virtual ValueTask DisposeAsync(bool disposing) |
| 107 | + { |
| 108 | + if (disposing) |
| 109 | + { |
| 110 | + LocalEndPoint = null; |
| 111 | + } |
| 112 | + return ValueTask.CompletedTask; |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// <inheritdoc/> |
| 117 | + /// </summary> |
| 118 | + public async ValueTask DisposeAsync() |
| 119 | + { |
| 120 | + await DisposeAsync(true); |
| 121 | + GC.SuppressFinalize(this); |
| 122 | + } |
| 123 | +} |
0 commit comments