//------------------------------------------------------------------------------ // 此代码版权(除特别声明或在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 { /// /// Tcp服务器基类 /// public abstract class TcpServiceBase : BaseSocket, ITcpService { /// /// /// public abstract TouchSocketConfig Config { get; } /// /// /// public abstract IContainer Container { get; } /// /// /// public int Count => SocketClients.Count; /// /// /// public abstract NetworkMonitor[] Monitors { get; } /// /// 插件管理器 /// public abstract IPluginsManager PluginsManager { get; } /// /// /// public abstract string ServerName { get; } /// /// /// public abstract ServerState ServerState { get; } /// /// /// public abstract SocketClientCollection SocketClients { get; } /// /// /// public abstract bool UseSsl { get; } /// /// /// public abstract Func GetDefaultNewID { get; } /// /// /// public abstract int MaxCount { get; } /// /// /// public abstract bool UsePlugin { get; } /// /// /// public abstract void Clear(); /// /// /// /// public string[] GetIDs() { return SocketClients.GetIDs().ToArray(); } /// /// /// /// /// public abstract void ResetID(string oldID, string newID); /// /// /// /// /// public abstract IService Setup(TouchSocketConfig serverConfig); /// /// /// /// /// public abstract IService Setup(int port); /// /// /// /// public abstract IService Start(); /// /// /// /// 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); } /// /// 客户端连接完成 /// /// /// protected abstract void OnClientConnected(ISocketClient socketClient, TouchSocketEventArgs e); /// /// 客户端请求连接 /// /// /// protected abstract void OnClientConnecting(ISocketClient socketClient, OperationEventArgs e); /// /// 客户端断开连接 /// /// /// protected abstract void OnClientDisconnected(ISocketClient socketClient, DisconnectEventArgs e); /// /// 即将断开连接(仅主动断开时有效)。 /// /// 当主动调用Close断开时,可通过终止断开行为。 /// /// /// /// protected abstract void OnClientDisconnecting(ISocketClient socketClient, DisconnectEventArgs e); /// /// 收到数据时 /// /// /// /// protected abstract void OnClientReceivedData(ISocketClient socketClient, ByteBlock byteBlock, IRequestInfo requestInfo); #region ID发送 /// /// 发送字节流 /// /// 用于检索TcpSocketClient /// /// /// /// /// /// /// 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)); } } /// /// /// /// /// 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)); } } /// /// 发送字节流 /// /// 用于检索TcpSocketClient /// /// /// /// /// /// /// 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)); } } /// /// /// /// /// 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)); } } /// /// /// /// /// public abstract bool SocketClientExist(string id); #endregion ID发送 } }