Просмотр исходного кода

1.增加websocket错误提示
2.简化网络库代码

tanghai 7 лет назад
Родитель
Сommit
69e27c7aa3

+ 42 - 26
Server/Model/Module/Message/Network/WebSocket/WService.cs

@@ -14,20 +14,20 @@ namespace ETModel
         
         public RecyclableMemoryStreamManager MemoryStreamManager = new RecyclableMemoryStreamManager();
 
-        public WService(IEnumerable<string> prefixs)
+        public WService(IEnumerable<string> prefixs, Action<AChannel> acceptCallback)
         {
             this.InstanceId = IdGenerater.GenerateId();
+
+            this.AcceptCallback += acceptCallback;
             
             this.httpListener = new HttpListener();
-            
-            foreach (string prefix in prefixs)
-            {
-                this.httpListener.Prefixes.Add(prefix);
-            }
+
+            StartAccept(prefixs);
         }
         
         public WService()
         {
+            this.InstanceId = IdGenerater.GenerateId();
         }
         
         public override AChannel GetChannel(long id)
@@ -68,33 +68,49 @@ namespace ETModel
             
         }
 
-        public override async void Start()
+        public async void StartAccept(IEnumerable<string> prefixs)
         {
-            if (this.httpListener == null)
+            try
             {
-                return;
-            }
-            
-            httpListener.Start();
-
-            while (true)
-            {
-                try
+                foreach (string prefix in prefixs)
                 {
-                    HttpListenerContext httpListenerContext = await this.httpListener.GetContextAsync();
-                
-                    HttpListenerWebSocketContext webSocketContext = await httpListenerContext.AcceptWebSocketAsync(null);
-                    
-                    WChannel channel = new WChannel(webSocketContext, this);
+                    this.httpListener.Prefixes.Add(prefix);
+                }
                 
-                    this.channels[channel.Id] = channel;
-                    
-                    this.OnAccept(channel);
+                httpListener.Start();
+
+                while (true)
+                {
+                    try
+                    {
+                        HttpListenerContext httpListenerContext = await this.httpListener.GetContextAsync();
+
+                        HttpListenerWebSocketContext webSocketContext = await httpListenerContext.AcceptWebSocketAsync(null);
+
+                        WChannel channel = new WChannel(webSocketContext, this);
+
+                        this.channels[channel.Id] = channel;
+
+                        this.OnAccept(channel);
+                    }
+                    catch (Exception e)
+                    {
+                        Log.Error(e);
+                    }
                 }
-                catch (Exception e)
+            }
+            catch (HttpListenerException e)
+            {
+                if (e.ErrorCode == 5)
                 {
-                    Log.Error(e);
+                    throw new Exception($"CMD管理员中输入: netsh http add urlacl url=http://*:8080/ user=Everyone", e);
                 }
+
+                Log.Error(e);
+            }
+            catch (Exception e)
+            {
+                Log.Error(e);
             }
         }
     }

+ 0 - 2
Unity/Assets/Scripts/Module/Message/Network/AService.cs

@@ -40,7 +40,5 @@ namespace ETModel
 		public abstract void Remove(long channelId);
 
 		public abstract void Update();
-
-		public abstract void Start();
 	}
 }

+ 7 - 5
Unity/Assets/Scripts/Module/Message/Network/KCP/KService.cs

@@ -57,8 +57,12 @@ namespace ETModel
 
 		private EndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 0);
 
-		public KService(IPEndPoint ipEndPoint)
+		public KService(IPEndPoint ipEndPoint, Action<AChannel> acceptCallback)
 		{
+			this.InstanceId = ETModel.IdGenerater.GenerateId();
+			
+			this.AcceptCallback += acceptCallback;
+			
 			this.StartTime = TimeHelper.ClientNow();
 			this.TimeNow = (uint)(TimeHelper.ClientNow() - this.StartTime);
 			this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
@@ -78,6 +82,8 @@ namespace ETModel
 
 		public KService()
 		{
+			this.InstanceId = ETModel.IdGenerater.GenerateId();
+			
 			this.StartTime = TimeHelper.ClientNow();
 			this.TimeNow = (uint)(TimeHelper.ClientNow() - this.StartTime);
 			this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
@@ -114,10 +120,6 @@ namespace ETModel
 			Instance = null;
 		}
 
-		public override void Start()
-		{
-		}
-
 		public void Recv()
 		{
 			if (this.socket == null)

+ 7 - 9
Unity/Assets/Scripts/Module/Message/Network/TCP/TService.cs

@@ -20,18 +20,24 @@ namespace ETModel
 		/// <summary>
 		/// 即可做client也可做server
 		/// </summary>
-		public TService(IPEndPoint ipEndPoint)
+		public TService(IPEndPoint ipEndPoint, Action<AChannel> acceptCallback)
 		{
+			this.InstanceId = IdGenerater.GenerateId();
+			this.AcceptCallback += acceptCallback;
+			
 			this.acceptor = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 			this.acceptor.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
 			this.innArgs.Completed += this.OnComplete;
 			
 			this.acceptor.Bind(ipEndPoint);
 			this.acceptor.Listen(1000);
+
+			this.AcceptAsync();
 		}
 
 		public TService()
 		{
+			this.InstanceId = IdGenerater.GenerateId();
 		}
 		
 		public override void Dispose()
@@ -53,14 +59,6 @@ namespace ETModel
 			this.innArgs.Dispose();
 		}
 
-		public override void Start()
-		{
-			if (this.acceptor != null)
-			{
-				this.AcceptAsync();
-			}
-		}
-
 		private void OnComplete(object sender, SocketAsyncEventArgs e)
 		{
 			switch (e.LastOperation)

+ 3 - 16
Unity/Assets/Scripts/Module/Message/NetworkComponent.cs

@@ -35,10 +35,6 @@ namespace ETModel
 						break;
 #endif
 				}
-
-				this.Service.AcceptCallback += this.OnAccept;
-				
-				this.Start();
 			}
 			catch (Exception e)
 			{
@@ -55,23 +51,19 @@ namespace ETModel
 				{
 					case NetworkProtocol.KCP:
 						ipEndPoint = NetworkHelper.ToIPEndPoint(address);
-						this.Service = new KService(ipEndPoint);
+						this.Service = new KService(ipEndPoint, this.OnAccept);
 						break;
 					case NetworkProtocol.TCP:
 						ipEndPoint = NetworkHelper.ToIPEndPoint(address);
-						this.Service = new TService(ipEndPoint);
+						this.Service = new TService(ipEndPoint, this.OnAccept);
 						break;
 #if SERVER
 					case NetworkProtocol.WebSocket:
 						string[] prefixs = address.Split(';');
-						this.Service = new WService(prefixs);
+						this.Service = new WService(prefixs, this.OnAccept);
 						break;
 #endif
 				}
-				
-				this.Service.AcceptCallback += this.OnAccept;
-				
-				this.Start();
 			}
 			catch (Exception e)
 			{
@@ -79,11 +71,6 @@ namespace ETModel
 			}
 		}
 
-		public void Start()
-		{
-			this.Service.Start();
-		}
-
 		public int Count
 		{
 			get { return this.sessions.Count; }