diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..fe1152b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,30 @@ +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md +!**/.gitignore +!.git/HEAD +!.git/config +!.git/packed-refs +!.git/refs/heads/** \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1c34a64 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,28 @@ +# 请参阅 https://aka.ms/customizecontainer 以了解如何自定义调试容器,以及 Visual Studio 如何使用此 Dockerfile 生成映像以更快地进行调试。 + +# 此阶段用于在快速模式(默认为调试配置)下从 VS 运行时 +FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base +USER $APP_UID +WORKDIR /app + + +# 此阶段用于生成服务项目 +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build +ARG BUILD_CONFIGURATION=Release +WORKDIR /src +COPY ["TouchSocketTest.csproj", "."] +RUN dotnet restore "./TouchSocketTest.csproj" +COPY . . +WORKDIR "/src/." +RUN dotnet build "./TouchSocketTest.csproj" -c $BUILD_CONFIGURATION -o /app/build + +# 此阶段用于发布要复制到最终阶段的服务项目 +FROM build AS publish +ARG BUILD_CONFIGURATION=Release +RUN dotnet publish "./TouchSocketTest.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false + +# 此阶段在生产中使用,或在常规模式下从 VS 运行时使用(在不使用调试配置时为默认值) +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "TouchSocketTest.dll"] \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..6559b2d --- /dev/null +++ b/Program.cs @@ -0,0 +1,36 @@ + +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using TouchSocket.Core; +using TouchSocket.Sockets; +using TouchSocketTest; + +public class Program +{ + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + private static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureServices((hostContext, services) => { ConfigureServices(services, hostContext); }); + + + private static void ConfigureServices(IServiceCollection services, HostBuilderContext hostContext) + { + services.AddTcpService(config => + { + config.SetListenIPHosts(9500) + .SetMaxCount(100000) + //.SetTcpDataHandlingAdapter(()=>new StandardFixedHeaderDataHandlingAdapter()) + //.SetGetDefaultNewId(() => Guid.NewGuid().ToString())//定义ClientId的生成策略 + .ConfigurePlugins(a => + { + a.Add(); + a.Add(); + }); + }); + } +} + diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json new file mode 100644 index 0000000..ae81a3a --- /dev/null +++ b/Properties/launchSettings.json @@ -0,0 +1,10 @@ +{ + "profiles": { + "TouchSocketTest": { + "commandName": "Project" + }, + "Container (Dockerfile)": { + "commandName": "Docker" + } + } +} \ No newline at end of file diff --git a/ServerMonitor.cs b/ServerMonitor.cs new file mode 100644 index 0000000..63b7d13 --- /dev/null +++ b/ServerMonitor.cs @@ -0,0 +1,44 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TouchSocket.Core; +using TouchSocket.Sockets; + +namespace TouchSocketTest +{ + public partial class ServerMonitor(ILogger logger) : PluginBase, IServerStartedPlugin, IServerStopedPlugin + { + public Task OnServerStarted(IServiceBase sender, ServiceStateEventArgs e) + { + switch (sender) + { + case ITcpService service: + { + foreach (var item in service.Monitors) + { + logger.LogInformation($"TCP {item.Option.IpHost}"); + } + + break; + } + case UdpSession session: + //logger.LogInformation($"UDP {session.Monitor.IPHost}"); + break; + } + + logger.LogInformation(e.ServerState == ServerState.Running + ? $"服务器成功启动" + : $"服务器启动失败,状态:{e.ServerState},异常:{e.Exception}"); + return e.InvokeNext(); + } + + public Task OnServerStoped(IServiceBase sender, ServiceStateEventArgs e) + { + logger.LogInformation("服务已停止"); + return e.InvokeNext(); + } + } +} diff --git a/TcpCloseMonitor.cs b/TcpCloseMonitor.cs new file mode 100644 index 0000000..1be4727 --- /dev/null +++ b/TcpCloseMonitor.cs @@ -0,0 +1,19 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TouchSocket.Core; +using TouchSocket.Sockets; + +namespace TouchSocketTest +{ + public partial class TcpCloseMonitor(ILogger logger) : PluginBase, ITcpReceivedPlugin + { + public Task OnTcpReceived(ITcpSession client, ReceivedDataEventArgs e) + { + throw new NotImplementedException(); + } + } +} diff --git a/TcpMonitor.cs b/TcpMonitor.cs new file mode 100644 index 0000000..d0fe903 --- /dev/null +++ b/TcpMonitor.cs @@ -0,0 +1,48 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TouchSocket.Core; +using TouchSocket.Sockets; + +namespace TouchSocketTest +{ + public partial class TcpMonitor : PluginBase, ITcpReceivedPlugin, ITcpConnectingPlugin,ITcpConnectedPlugin,ITcpClosedPlugin + { + private readonly ILogger _logger; + + public TcpMonitor( + ILogger logger) + { + _logger = logger; + } + + + + public async Task OnTcpReceived(ITcpSession client, ReceivedDataEventArgs e) + { + _logger.LogInformation($"收到{client.IP}{client.GetIPPort()}的消息{e.ByteBlock.Span.ToString(Encoding.UTF8)}"); + await e.InvokeNext(); + } + + public async Task OnTcpConnecting(ITcpSession client, ConnectingEventArgs e) + { + _logger.LogInformation($"{client.IP}{client.GetIPPort()}正在连接!"); + await e.InvokeNext(); + } + + public async Task OnTcpConnected(ITcpSession client, ConnectedEventArgs e) + { + _logger.LogInformation($"{client.IP}{client.GetIPPort()}已连接!"); + await e.InvokeNext(); + } + + public async Task OnTcpClosed(ITcpSession client, ClosedEventArgs e) + { + _logger.LogInformation($"{client.IP}{client.GetIPPort()}已关闭!"); + await e.InvokeNext(); + } + } +} diff --git a/TouchSocketTest.csproj b/TouchSocketTest.csproj new file mode 100644 index 0000000..b7b8893 --- /dev/null +++ b/TouchSocketTest.csproj @@ -0,0 +1,19 @@ + + + + Exe + net8.0 + enable + enable + Linux + . + + + + + + + + + + diff --git a/TouchSocketTest.sln b/TouchSocketTest.sln new file mode 100644 index 0000000..dd2a7a1 --- /dev/null +++ b/TouchSocketTest.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.13.35825.156 d17.13 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TouchSocketTest", "TouchSocketTest.csproj", "{596B9A65-A239-4E70-80A4-DD7B251237D9}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {596B9A65-A239-4E70-80A4-DD7B251237D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {596B9A65-A239-4E70-80A4-DD7B251237D9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {596B9A65-A239-4E70-80A4-DD7B251237D9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {596B9A65-A239-4E70-80A4-DD7B251237D9}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7E0D16C6-C760-4442-9BE5-DA56F4F381CF} + EndGlobalSection +EndGlobal