添加项目文件。

This commit is contained in:
cli 2025-03-27 13:55:14 +08:00
parent 23f2b777b8
commit 275fdfe2f0
9 changed files with 259 additions and 0 deletions

30
.dockerignore Normal file
View File

@ -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/**

28
Dockerfile Normal file
View File

@ -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"]

36
Program.cs Normal file
View File

@ -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<TcpMonitor>();
a.Add<ServerMonitor>();
});
});
}
}

View File

@ -0,0 +1,10 @@
{
"profiles": {
"TouchSocketTest": {
"commandName": "Project"
},
"Container (Dockerfile)": {
"commandName": "Docker"
}
}
}

44
ServerMonitor.cs Normal file
View File

@ -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<ServerMonitor> 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();
}
}
}

19
TcpCloseMonitor.cs Normal file
View File

@ -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<TcpCloseMonitor> logger) : PluginBase, ITcpReceivedPlugin
{
public Task OnTcpReceived(ITcpSession client, ReceivedDataEventArgs e)
{
throw new NotImplementedException();
}
}
}

48
TcpMonitor.cs Normal file
View File

@ -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<TcpMonitor> _logger;
public TcpMonitor(
ILogger<TcpMonitor> 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();
}
}
}

19
TouchSocketTest.csproj Normal file
View File

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>.</DockerfileContext>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.3" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.3" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageReference Include="TouchSocket.Hosting" Version="3.0.19" />
</ItemGroup>
</Project>

25
TouchSocketTest.sln Normal file
View File

@ -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