فهرست منبع

RemoteAddress改回IPEndPoint

tanghai 2 سال پیش
والد
کامیت
a34a8f158f

+ 48 - 35
Unity/Assets/Scripts/Core/Network/AChannel.cs

@@ -4,41 +4,54 @@ using System.Net;
 
 namespace ET
 {
-	public enum ChannelType
-	{
-		Connect,
-		Accept,
-	}
-
-	public struct Packet
-	{
-		public const int MinPacketSize = 2;
-		public const int OpcodeLength = 2;
-		public const int ActorIdIndex = 0;
-		public const int ActorIdLength = 16;
-
-		public ushort Opcode;
-		public long ActorId;
-		public MemoryStream MemoryStream;
-	}
-
-	public abstract class AChannel: IDisposable
-	{
-		public long Id;
+    public enum ChannelType
+    {
+        Connect,
+        Accept,
+    }
+
+    public struct Packet
+    {
+        public const int MinPacketSize = 2;
+        public const int OpcodeLength = 2;
+        public const int ActorIdIndex = 0;
+        public const int ActorIdLength = 16;
+
+        public ushort Opcode;
+        public long ActorId;
+        public MemoryStream MemoryStream;
+    }
+
+    public abstract class AChannel: IDisposable
+    {
+        public long Id;
 		
-		public ChannelType ChannelType { get; protected set; }
+        public ChannelType ChannelType { get; protected set; }
 
-		public int Error { get; set; }
-
-
-		public bool IsDisposed
-		{
-			get
-			{
-				return this.Id == 0;	
-			}
-		}
-
-		public abstract void Dispose();
-	}
+        public int Error { get; set; }
+		
+        private IPEndPoint remoteAddress;
+
+        public IPEndPoint RemoteAddress
+        {
+            get
+            {
+                return this.remoteAddress;
+            }
+            set
+            {
+                this.remoteAddress = value;
+            }
+        }
+
+        public bool IsDisposed
+        {
+            get
+            {
+                return this.Id == 0;	
+            }
+        }
+
+        public abstract void Dispose();
+    }
 }

+ 2 - 2
Unity/Assets/Scripts/Core/Network/AService.cs

@@ -67,8 +67,8 @@ namespace ET
         public abstract void Remove(long id, int error = 0);
         
         public abstract bool IsDisposed();
-
-        public abstract void Create(long id, string address);
+        
+        public abstract void Create(long id, IPEndPoint ipEndPoint);
 
         public abstract void Send(long channelId, MemoryBuffer memoryBuffer);
 

+ 1 - 1
Unity/Assets/Scripts/Core/Network/IKcpTransport.cs

@@ -134,7 +134,7 @@ namespace ET
             if (id == 0)
             {
                 id = IdGenerater.Instance.GenerateInstanceId();
-                this.tService.Create(id, endPoint.ToString());
+                this.tService.Create(id, (IPEndPoint)endPoint);
                 this.idEndpoints.Add(id, endPoint);
                 this.channelIds.Enqueue(id);
             }

+ 0 - 14
Unity/Assets/Scripts/Core/Network/KChannel.cs

@@ -41,20 +41,6 @@ namespace ET
 		private MemoryBuffer readMemory;
 		private int needReadSplitCount;
 
-		private IPEndPoint remoteAddress;
-
-		public IPEndPoint RemoteAddress
-		{
-			get
-			{
-				return this.remoteAddress;
-			}
-			set
-			{
-				this.remoteAddress = value;
-			}
-		}
-
 		private void InitKcp()
 		{
 			switch (this.Service.ServiceType)

+ 3 - 3
Unity/Assets/Scripts/Core/Network/KService.cs

@@ -184,6 +184,7 @@ namespace ET
 
                 // accept
                 byte flag = this.cache[0];
+                
                 // conn从100开始,如果为1,2,3则是特殊包
                 uint remoteConn = 0;
                 uint localConn = 0;
@@ -419,7 +420,7 @@ namespace ET
             return channel;
         }
 
-        public override void Create(long id, string address)
+        public override void Create(long id, IPEndPoint ipEndPoint)
         {
             if (this.localConnChannels.TryGetValue(id, out KChannel kChannel))
             {
@@ -430,8 +431,7 @@ namespace ET
             {
                 // 低32bit是localConn
                 uint localConn = (uint)id;
-                IPEndPoint endPoint = NetworkHelper.ToIPEndPoint(address);
-                kChannel = new KChannel(localConn, endPoint, this);
+                kChannel = new KChannel(localConn, ipEndPoint, this);
                 this.localConnChannels.Add(kChannel.LocalConn, kChannel);
             }
             catch (Exception e)

+ 0 - 14
Unity/Assets/Scripts/Core/Network/TChannel.cs

@@ -23,20 +23,6 @@ namespace ET
 		private bool isConnected;
 
 		private readonly PacketParser parser;
-		
-		private IPEndPoint remoteAddress;
-
-		public IPEndPoint RemoteAddress
-		{
-			get
-			{
-				return this.remoteAddress;
-			}
-			set
-			{
-				this.remoteAddress = value;
-			}
-		}
 
 		private readonly byte[] sendCache = new byte[Packet.OpcodeLength + Packet.ActorIdLength];
 		

+ 3 - 4
Unity/Assets/Scripts/Core/Network/TService.cs

@@ -112,18 +112,17 @@ namespace ET
 			OnAcceptComplete(this.innArgs.SocketError, this.innArgs.AcceptSocket);
 		}
 
-		public override void Create(long id, string address)
+		public override void Create(long id, IPEndPoint ipEndPoint)
 		{
 			if (this.idChannels.TryGetValue(id, out TChannel _))
 			{
 				return;
 			}
 
-			IPEndPoint endPoint = NetworkHelper.ToIPEndPoint(address);
-			TChannel channel = new(id, endPoint, this);
+			TChannel channel = new(id, ipEndPoint, this);
 			this.idChannels.Add(channel.Id, channel);
 		}
-		
+
 		public TChannel Get(long id)
 		{
 			TChannel channel = null;

+ 2 - 4
Unity/Assets/Scripts/Core/Network/WChannel.cs

@@ -22,8 +22,6 @@ namespace ET
 
         private bool isConnected;
         
-        public IPEndPoint RemoteAddress { get; set; }
-
         private CancellationTokenSource cancellationTokenSource = new();
         
         public WChannel(long id, HttpListenerWebSocketContext webSocketContext, WService service)
@@ -43,7 +41,7 @@ namespace ET
             });
         }
 
-        public WChannel(long id, WebSocket webSocket, string connectUrl, WService service)
+        public WChannel(long id, WebSocket webSocket, IPEndPoint ipEndPoint, WService service)
         {
             this.Service = service;
             this.Id = id;
@@ -52,7 +50,7 @@ namespace ET
 
             isConnected = false;
             
-            this.Service.ThreadSynchronizationContext.Post(()=>this.ConnectAsync(connectUrl).Coroutine());
+            this.Service.ThreadSynchronizationContext.Post(()=>this.ConnectAsync($"ws://{ipEndPoint}").Coroutine());
         }
 
         public override void Dispose()

+ 18 - 9
Unity/Assets/Scripts/Core/Network/WService.cs

@@ -16,18 +16,19 @@ namespace ET
 
         public ThreadSynchronizationContext ThreadSynchronizationContext;
 
-        public WService(ThreadSynchronizationContext threadSynchronizationContext, IEnumerable<string> prefixs)
+        public WService(IEnumerable<string> prefixs)
         {
-            this.ThreadSynchronizationContext = threadSynchronizationContext;
+            this.ThreadSynchronizationContext = new ThreadSynchronizationContext();
             
             this.httpListener = new HttpListener();
 
             StartAccept(prefixs).Coroutine();
         }
         
-        public WService(ThreadSynchronizationContext threadSynchronizationContext)
+        public WService()
         {
-            this.ThreadSynchronizationContext = threadSynchronizationContext;
+            this.ServiceType = ServiceType.Outer;
+            this.ThreadSynchronizationContext = new ThreadSynchronizationContext();
         }
         
         private long GetId
@@ -38,15 +39,16 @@ namespace ET
             }
         }
         
-        public override void Create(long id, string address)
+        public override void Create(long id, IPEndPoint ipEndpoint)
         {
 			ClientWebSocket webSocket = new();
-            WChannel channel = new(id, webSocket, address, this);
+            WChannel channel = new(id, webSocket, ipEndpoint, this);
             this.channels[channel.Id] = channel;
         }
 
         public override void Update()
         {
+            this.ThreadSynchronizationContext.Update();
         }
 
         public override void Remove(long id, int error = 0)
@@ -68,13 +70,20 @@ namespace ET
             return this.ThreadSynchronizationContext == null;
         }
 
-        protected void Get(long id, string address)
+        protected void Get(long id, IPEndPoint ipEndPoint)
         {
             if (!this.channels.TryGetValue(id, out _))
             {
-                this.Create(id, address);
+                this.Create(id, ipEndPoint);
             }
         }
+        
+        public WChannel Get(long id)
+        {
+            WChannel channel = null;
+            this.channels.TryGetValue(id, out channel);
+            return channel;
+        }
 
         public override void Dispose()
         {
@@ -105,7 +114,7 @@ namespace ET
                         HttpListenerWebSocketContext webSocketContext = await httpListenerContext.AcceptWebSocketAsync(null);
 
                         WChannel channel = new WChannel(this.GetId, webSocketContext, this);
-
+                        channel.RemoteAddress = httpListenerContext.Request.RemoteEndPoint;
                         this.channels[channel.Id] = channel;
 
                         this.AcceptCallback(channel.Id, channel.RemoteAddress);

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Client/Demo/NetClient/Router/RouterCheckComponentSystem.cs

@@ -19,7 +19,7 @@ namespace ET.Client
             Fiber fiber = self.Fiber();
             Scene root = fiber.Root;
             
-            IPEndPoint realAddress = NetworkHelper.ToIPEndPoint(session.RemoteAddress);
+            IPEndPoint realAddress = session.RemoteAddress;
             NetComponent netComponent = root.GetComponent<NetComponent>();
             
             while (true)

+ 2 - 2
Unity/Assets/Scripts/Hotfix/Server/Module/Message/ProcessOuterSenderSystem.cs

@@ -104,14 +104,14 @@ namespace ET.Server
         private static void OnAccept(this ProcessOuterSender self, long channelId, IPEndPoint ipEndPoint)
         {
             Session session = self.AddChildWithId<Session, AService>(channelId, self.AService);
-            session.RemoteAddress = ipEndPoint.ToString();
+            session.RemoteAddress = ipEndPoint;
             //session.AddComponent<SessionIdleCheckerComponent, int, int, int>(NetThreadComponent.checkInteral, NetThreadComponent.recvMaxIdleTime, NetThreadComponent.sendMaxIdleTime);
         }
 
         private static Session CreateInner(this ProcessOuterSender self, long channelId, IPEndPoint ipEndPoint)
         {
             Session session = self.AddChildWithId<Session, AService>(channelId, self.AService);
-            session.RemoteAddress = ipEndPoint.ToString();
+            session.RemoteAddress = ipEndPoint;
             self.AService.Create(channelId, session.RemoteAddress);
 
             //session.AddComponent<InnerPingComponent>();

+ 4 - 4
Unity/Assets/Scripts/Hotfix/Share/Module/Message/NetComponentSystem.cs

@@ -52,7 +52,7 @@ namespace ET
         private static void OnAccept(this NetComponent self, long channelId, IPEndPoint ipEndPoint)
         {
             Session session = self.AddChildWithId<Session, AService>(channelId, self.AService);
-            session.RemoteAddress = ipEndPoint.ToString();
+            session.RemoteAddress = ipEndPoint;
 
             if (self.IScene.SceneType != SceneType.BenchmarkServer)
             {
@@ -83,7 +83,7 @@ namespace ET
         {
             long channelId = NetServices.Instance.CreateConnectChannelId();
             Session session = self.AddChildWithId<Session, AService>(channelId, self.AService);
-            session.RemoteAddress = realIPEndPoint.ToString();
+            session.RemoteAddress = realIPEndPoint;
             if (self.IScene.SceneType != SceneType.BenchmarkClient)
             {
                 session.AddComponent<SessionIdleCheckerComponent>();
@@ -98,12 +98,12 @@ namespace ET
         {
             long channelId = localConn;
             Session session = self.AddChildWithId<Session, AService>(channelId, self.AService);
-            session.RemoteAddress = realIPEndPoint.ToString();
+            session.RemoteAddress = realIPEndPoint;
             if (self.IScene.SceneType != SceneType.BenchmarkClient)
             {
                 session.AddComponent<SessionIdleCheckerComponent>();
             }
-            self.AService.Create(session.Id, routerIPEndPoint.ToString());
+            self.AService.Create(session.Id, routerIPEndPoint);
             return session;
         }
     }

+ 1 - 1
Unity/Assets/Scripts/Model/Share/Module/Message/Session.cs

@@ -173,7 +173,7 @@ namespace ET
             set;
         }
 
-        public string RemoteAddress
+        public IPEndPoint RemoteAddress
         {
             get;
             set;