基本类库,和测试
This commit is contained in:
parent
e641ba243a
commit
73f1a15018
BIN
.vs/AxibugTransfer/v17/.suo
Normal file
BIN
.vs/AxibugTransfer/v17/.suo
Normal file
Binary file not shown.
9
AxibugTransfer.Tcp/AxibugTransfer.Tcp.csproj
Normal file
9
AxibugTransfer.Tcp/AxibugTransfer.Tcp.csproj
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
147
AxibugTransfer.Tcp/TcpTransfer.cs
Normal file
147
AxibugTransfer.Tcp/TcpTransfer.cs
Normal file
@ -0,0 +1,147 @@
|
|||||||
|
using System.Net.Sockets;
|
||||||
|
|
||||||
|
namespace AxibugTransfer.Tcp
|
||||||
|
{
|
||||||
|
public struct transferInfo
|
||||||
|
{
|
||||||
|
public int cfgIndex;
|
||||||
|
public int clientTransferIndex;
|
||||||
|
public TcpClient client1;
|
||||||
|
public TcpClient client2;
|
||||||
|
public TansferDir tansferDir;
|
||||||
|
public bool bNeedStatistics;
|
||||||
|
}
|
||||||
|
public enum TansferDir
|
||||||
|
{
|
||||||
|
Local2Target,
|
||||||
|
Target2Local,
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TcpTransfer
|
||||||
|
{
|
||||||
|
#region 静态管理
|
||||||
|
public static Dictionary<int, TcpTransfer> _DictTransInstance { get; private set; } = new Dictionary<int, TcpTransfer>();
|
||||||
|
public static Dictionary<int, (transferInfo, transferInfo)> _DictClientTancefer { get; private set; } = new Dictionary<int, (transferInfo, transferInfo)>();
|
||||||
|
public static int InstanceIndex = 0;
|
||||||
|
public static int ClientTransferIndex = 0;
|
||||||
|
public static void AddSendDataLenght(int index, long lenght)
|
||||||
|
{
|
||||||
|
if (_DictTransInstance.ContainsKey(index)) _DictTransInstance[index].m_SendDataLenght += lenght;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void AddReceiveDataLenght(int index, long lenght)
|
||||||
|
{
|
||||||
|
if (_DictTransInstance.ContainsKey(index)) _DictTransInstance[index].m_ReceiveDataLenght += lenght;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void AddClientTancefer(int Index, transferInfo totargetTansfer, transferInfo tolocalTansfer)
|
||||||
|
{
|
||||||
|
_DictClientTancefer[Index] = (tolocalTansfer, totargetTansfer);
|
||||||
|
}
|
||||||
|
public static void RemoveClientTancefer(int Index)
|
||||||
|
{
|
||||||
|
if(_DictClientTancefer.ContainsKey(Index))
|
||||||
|
_DictClientTancefer.Remove(Index);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
public TcpTransfer(int localPort, string targetIP, int targetPort, bool needStatistics = false)
|
||||||
|
{
|
||||||
|
m_InstanceIndex = InstanceIndex++;
|
||||||
|
m_LocalPort = localPort;
|
||||||
|
m_targetIP = targetIP;
|
||||||
|
m_targetPort = targetPort;
|
||||||
|
_DictTransInstance[m_InstanceIndex] = this;
|
||||||
|
|
||||||
|
Console.WriteLine($"实例{m_InstanceIndex}初始化,localPort {localPort},targetIP {targetIP},targetPort {targetPort},needStatistics {needStatistics}");
|
||||||
|
}
|
||||||
|
|
||||||
|
~TcpTransfer()
|
||||||
|
{
|
||||||
|
_DictTransInstance.Remove(m_InstanceIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
int m_InstanceIndex = 0;
|
||||||
|
long m_SendDataLenght = 0;
|
||||||
|
long m_ReceiveDataLenght = 0;
|
||||||
|
int m_LocalPort;
|
||||||
|
string m_targetIP;
|
||||||
|
int m_targetPort;
|
||||||
|
bool m_bNeedStatistics = false;
|
||||||
|
|
||||||
|
public void Start()
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
System.ComponentModel.BackgroundWorker hunterwork = new System.ComponentModel.BackgroundWorker();
|
||||||
|
hunterwork.DoWork += delegate (object sender, System.ComponentModel.DoWorkEventArgs e)
|
||||||
|
{
|
||||||
|
StartListener();
|
||||||
|
};
|
||||||
|
hunterwork.RunWorkerAsync();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void StartListener()
|
||||||
|
{
|
||||||
|
TcpListener tl = new TcpListener(m_LocalPort);
|
||||||
|
tl.Start();
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
TcpClient tc1 = tl.AcceptTcpClient();
|
||||||
|
TcpClient tc2 = new TcpClient(m_targetIP, m_targetPort);
|
||||||
|
tc1.SendTimeout = 30000;
|
||||||
|
tc1.ReceiveTimeout = 30000;
|
||||||
|
tc2.SendTimeout = 30000;
|
||||||
|
tc2.ReceiveTimeout = 30000;
|
||||||
|
|
||||||
|
transferInfo obj1 = new transferInfo() { client1 = tc1, client2 = tc2, cfgIndex = m_InstanceIndex, tansferDir = TansferDir.Local2Target, bNeedStatistics = m_bNeedStatistics };
|
||||||
|
transferInfo obj2 = new transferInfo() { client1 = tc2, client2 = tc1, cfgIndex = m_InstanceIndex, tansferDir = TansferDir.Target2Local, bNeedStatistics = m_bNeedStatistics };
|
||||||
|
|
||||||
|
AddClientTancefer(ClientTransferIndex++, obj1, obj2);
|
||||||
|
|
||||||
|
ThreadPool.QueueUserWorkItem(new WaitCallback(tcp_transfer), (object)obj1);
|
||||||
|
ThreadPool.QueueUserWorkItem(new WaitCallback(tcp_transfer), (object)obj2);
|
||||||
|
Console.WriteLine($"实例{m_InstanceIndex} 建立新的连接 {ClientTransferIndex},{tc1.Client.AddressFamily.ToString()}");
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void tcp_transfer(object obj)
|
||||||
|
{
|
||||||
|
transferInfo info = (transferInfo)obj;
|
||||||
|
NetworkStream ns1 = info.client1.GetStream();
|
||||||
|
NetworkStream ns2 = info.client2.GetStream();
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
byte[] bt = new byte[10240];
|
||||||
|
int count = ns1.Read(bt, 0, bt.Length);
|
||||||
|
ns2.Write(bt, 0, count);
|
||||||
|
|
||||||
|
if (!info.bNeedStatistics) continue;
|
||||||
|
//统计
|
||||||
|
if (info.tansferDir == TansferDir.Local2Target)
|
||||||
|
TcpTransfer.AddSendDataLenght(info.cfgIndex, count);
|
||||||
|
else
|
||||||
|
TcpTransfer.AddReceiveDataLenght(info.cfgIndex, count);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
ns1.Dispose();
|
||||||
|
ns2.Dispose();
|
||||||
|
info.client1.Close();
|
||||||
|
info.client2.Close();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TcpTransfer.RemoveClientTancefer(info.clientTransferIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
14
AxibugTransfer.Test/AxibugTransfer.Test.csproj
Normal file
14
AxibugTransfer.Test/AxibugTransfer.Test.csproj
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\AxibugTransfer.Tcp\AxibugTransfer.Tcp.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
5
AxibugTransfer.Test/Program.cs
Normal file
5
AxibugTransfer.Test/Program.cs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
using AxibugTransfer.Tcp;
|
||||||
|
|
||||||
|
new TcpTransfer(80, "Your IP", 8080, true).Start();
|
||||||
|
|
||||||
|
Console.ReadLine();
|
||||||
31
AxibugTransfer.sln
Normal file
31
AxibugTransfer.sln
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 17
|
||||||
|
VisualStudioVersion = 17.0.31903.59
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AxibugTransfer.Tcp", "AxibugTransfer.Tcp\AxibugTransfer.Tcp.csproj", "{B5A89E34-0283-44CB-8683-8E41888CA949}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AxibugTransfer.Test", "AxibugTransfer.Test\AxibugTransfer.Test.csproj", "{A27FCE74-123D-477C-BB5B-2879D0B39D22}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{B5A89E34-0283-44CB-8683-8E41888CA949}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{B5A89E34-0283-44CB-8683-8E41888CA949}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{B5A89E34-0283-44CB-8683-8E41888CA949}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{B5A89E34-0283-44CB-8683-8E41888CA949}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{A27FCE74-123D-477C-BB5B-2879D0B39D22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{A27FCE74-123D-477C-BB5B-2879D0B39D22}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{A27FCE74-123D-477C-BB5B-2879D0B39D22}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{A27FCE74-123D-477C-BB5B-2879D0B39D22}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {84359E7A-FB5F-429C-8CB2-5FA95276D856}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
Loading…
Reference in New Issue
Block a user