123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- //------------------------------------------------------------------------------
- // 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
- // 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
- // CSDN博客:https://blog.csdn.net/qq_40374647
- // 哔哩哔哩视频:https://space.bilibili.com/94253567
- // Gitee源代码仓库:https://gitee.com/RRQM_Home
- // Github源代码仓库:https://github.com/RRQM
- // API首页:https://www.yuque.com/rrqm/touchsocket/index
- // 交流QQ群:234762506
- // 感谢您的下载和使用
- //------------------------------------------------------------------------------
- //------------------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using TouchSocket.Core;
- using TouchSocket.Resources;
- namespace TouchSocket.Sockets
- {
- /// <summary>
- /// Tcp服务器基类
- /// </summary>
- public abstract class TcpServiceBase : BaseSocket, ITcpService
- {
- /// <summary>
- /// <inheritdoc/>
- /// </summary>
- public abstract TouchSocketConfig Config { get; }
- /// <summary>
- /// <inheritdoc/>
- /// </summary>
- public abstract IContainer Container { get; }
- /// <summary>
- /// <inheritdoc/>
- /// </summary>
- public int Count => SocketClients.Count;
- /// <summary>
- /// <inheritdoc/>
- /// </summary>
- public abstract NetworkMonitor[] Monitors { get; }
- /// <summary>
- /// 插件管理器
- /// </summary>
- public abstract IPluginsManager PluginsManager { get; }
- /// <summary>
- /// <inheritdoc/>
- /// </summary>
- public abstract string ServerName { get; }
- /// <summary>
- /// <inheritdoc/>
- /// </summary>
- public abstract ServerState ServerState { get; }
- /// <summary>
- /// <inheritdoc/>
- /// </summary>
- public abstract SocketClientCollection SocketClients { get; }
- /// <summary>
- /// <inheritdoc/>
- /// </summary>
- public abstract bool UseSsl { get; }
- /// <summary>
- /// <inheritdoc/>
- /// </summary>
- public abstract Func<string> GetDefaultNewID { get; }
- /// <summary>
- /// <inheritdoc/>
- /// </summary>
- public abstract int MaxCount { get; }
- /// <summary>
- /// <inheritdoc/>
- /// </summary>
- public abstract bool UsePlugin { get; }
- /// <summary>
- /// <inheritdoc/>
- /// </summary>
- public abstract void Clear();
- /// <summary>
- /// <inheritdoc/>
- /// </summary>
- /// <returns></returns>
- public string[] GetIDs()
- {
- return SocketClients.GetIDs().ToArray();
- }
- /// <summary>
- /// <inheritdoc/>
- /// </summary>
- /// <param name="oldID"></param>
- /// <param name="newID"></param>
- public abstract void ResetID(string oldID, string newID);
- /// <summary>
- /// <inheritdoc/>
- /// </summary>
- /// <param name="serverConfig"></param>
- /// <returns></returns>
- public abstract IService Setup(TouchSocketConfig serverConfig);
- /// <summary>
- /// <inheritdoc/>
- /// </summary>
- /// <param name="port"></param>
- /// <returns></returns>
- public abstract IService Setup(int port);
- /// <summary>
- /// <inheritdoc/>
- /// </summary>
- /// <returns></returns>
- public abstract IService Start();
- /// <summary>
- /// <inheritdoc/>
- /// </summary>
- /// <returns></returns>
- public abstract IService Stop();
- internal void OnInternalConnected(ISocketClient socketClient, TouchSocketEventArgs e)
- {
- OnClientConnected(socketClient, e);
- }
- internal void OnInternalConnecting(ISocketClient socketClient, OperationEventArgs e)
- {
- OnClientConnecting(socketClient, e);
- }
- internal void OnInternalDisconnected(ISocketClient socketClient, DisconnectEventArgs e)
- {
- OnClientDisconnected(socketClient, e);
- }
- internal void OnInternalDisconnecting(ISocketClient socketClient, DisconnectEventArgs e)
- {
- OnClientDisconnecting(socketClient, e);
- }
- internal void OnInternalReceivedData(ISocketClient socketClient, ByteBlock byteBlock, IRequestInfo requestInfo)
- {
- OnClientReceivedData(socketClient, byteBlock, requestInfo);
- }
- /// <summary>
- /// 客户端连接完成
- /// </summary>
- /// <param name="socketClient"></param>
- /// <param name="e"></param>
- protected abstract void OnClientConnected(ISocketClient socketClient, TouchSocketEventArgs e);
- /// <summary>
- /// 客户端请求连接
- /// </summary>
- /// <param name="socketClient"></param>
- /// <param name="e"></param>
- protected abstract void OnClientConnecting(ISocketClient socketClient, OperationEventArgs e);
- /// <summary>
- /// 客户端断开连接
- /// </summary>
- /// <param name="socketClient"></param>
- /// <param name="e"></param>
- protected abstract void OnClientDisconnected(ISocketClient socketClient, DisconnectEventArgs e);
- /// <summary>
- /// 即将断开连接(仅主动断开时有效)。
- /// <para>
- /// 当主动调用Close断开时,可通过<see cref="TouchSocketEventArgs.IsPermitOperation"/>终止断开行为。
- /// </para>
- /// </summary>
- /// <param name="socketClient"></param>
- /// <param name="e"></param>
- protected abstract void OnClientDisconnecting(ISocketClient socketClient, DisconnectEventArgs e);
- /// <summary>
- /// 收到数据时
- /// </summary>
- /// <param name="socketClient"></param>
- /// <param name="byteBlock"></param>
- /// <param name="requestInfo"></param>
- protected abstract void OnClientReceivedData(ISocketClient socketClient, ByteBlock byteBlock, IRequestInfo requestInfo);
- #region ID发送
- /// <summary>
- /// 发送字节流
- /// </summary>
- /// <param name="id">用于检索TcpSocketClient</param>
- /// <param name="buffer"></param>
- /// <param name="offset"></param>
- /// <param name="length"></param>
- /// <exception cref="KeyNotFoundException"></exception>
- /// <exception cref="NotConnectedException"></exception>
- /// <exception cref="OverlengthException"></exception>
- /// <exception cref="Exception"></exception>
- public void Send(string id, byte[] buffer, int offset, int length)
- {
- if (SocketClients.TryGetSocketClient(id, out ISocketClient client))
- {
- client.Send(buffer, offset, length);
- }
- else
- {
- throw new ClientNotFindException(TouchSocketStatus.ClientNotFind.GetDescription(id));
- }
- }
- /// <summary>
- /// <inheritdoc/>
- /// </summary>
- /// <param name="id"></param>
- /// <param name="requestInfo"></param>
- public void Send(string id, IRequestInfo requestInfo)
- {
- if (SocketClients.TryGetSocketClient(id, out ISocketClient client))
- {
- client.Send(requestInfo);
- }
- else
- {
- throw new ClientNotFindException(TouchSocketStatus.ClientNotFind.GetDescription(id));
- }
- }
- /// <summary>
- /// 发送字节流
- /// </summary>
- /// <param name="id">用于检索TcpSocketClient</param>
- /// <param name="buffer"></param>
- /// <param name="offset"></param>
- /// <param name="length"></param>
- /// <exception cref="KeyNotFoundException"></exception>
- /// <exception cref="NotConnectedException"></exception>
- /// <exception cref="OverlengthException"></exception>
- /// <exception cref="Exception"></exception>
- public Task SendAsync(string id, byte[] buffer, int offset, int length)
- {
- if (SocketClients.TryGetSocketClient(id, out ISocketClient client))
- {
- return client.SendAsync(buffer, offset, length);
- }
- else
- {
- throw new ClientNotFindException(TouchSocketStatus.ClientNotFind.GetDescription(id));
- }
- }
- /// <summary>
- /// <inheritdoc/>
- /// </summary>
- /// <param name="id"></param>
- /// <param name="requestInfo"></param>
- public Task SendAsync(string id, IRequestInfo requestInfo)
- {
- if (SocketClients.TryGetSocketClient(id, out ISocketClient client))
- {
- return client.SendAsync(requestInfo);
- }
- else
- {
- throw new ClientNotFindException(TouchSocketStatus.ClientNotFind.GetDescription(id));
- }
- }
- /// <summary>
- /// <inheritdoc/>
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public abstract bool SocketClientExist(string id);
- #endregion ID发送
- }
- }
|