Browse Source

简化了网络库代码,去掉了TimeManager.cs

tanghai 9 years ago
parent
commit
1899e071f8

+ 0 - 10
Unity/Assets/Scripts/Base/Network/AService.cs

@@ -27,16 +27,6 @@ namespace Base
 
 		public abstract void Update();
 
-		private readonly TimerManager timerManager = new TimerManager();
-
-		public TimerManager Timer
-		{
-			get
-			{
-				return timerManager;
-			}
-		}
-
 		public Action<long, SocketError> OnError;
 
 		public void OnChannelError(long channelId, SocketError error)

+ 27 - 37
Unity/Assets/Scripts/Base/Network/TNet/TChannel.cs

@@ -5,22 +5,21 @@ using System.Net.Sockets;
 
 namespace Base
 {
-	public class TChannel: AChannel
+	public class TChannel : AChannel
 	{
-		private const int SendInterval = 0;
-		private TSocket socket;
+		private readonly TSocket socket;
 
 		private readonly TBuffer recvBuffer = new TBuffer();
 		private readonly TBuffer sendBuffer = new TBuffer();
 
-		private long sendTimer;
+		private bool isSending;
 		private readonly PacketParser parser;
 		private readonly string remoteAddress;
 		private bool isConnected;
 
 		public Action<long, SocketError> OnError;
 
-		public TChannel(TSocket socket, string host, int port, TService service): base(service)
+		public TChannel(TSocket socket, string host, int port, TService service) : base(service)
 		{
 			this.socket = socket;
 			this.parser = new PacketParser(this.recvBuffer);
@@ -33,10 +32,13 @@ namespace Base
 			{
 				return;
 			}
-			
+
+			long id = this.Id;
+
 			base.Dispose();
 
 			this.socket.Dispose();
+			this.service.Remove(id);
 		}
 
 		public override void ConnectAsync()
@@ -61,29 +63,21 @@ namespace Base
 			if (error != SocketError.Success)
 			{
 				Log.Error($"connect error: {error}");
-                return;
+				return;
 			}
 			this.isConnected = true;
-			this.SetStartSendFlag();
+			this.StartSend();
 			this.StartRecv();
 		}
 
-		private void SetStartSendFlag()
-		{
-			if (this.sendTimer == 0)
-			{
-				this.sendTimer = this.service.Timer.Add(TimeHelper.ClientNow() + SendInterval, this.StartSend);
-			}
-		}
-
 		public override void Send(byte[] buffer, byte channelID = 0, PacketFlags flags = PacketFlags.Reliable)
 		{
 			byte[] size = BitConverter.GetBytes(buffer.Length);
 			this.sendBuffer.SendTo(size);
 			this.sendBuffer.SendTo(buffer);
-			if (this.isConnected)
+			if (!this.isSending && this.isConnected)
 			{
-				this.SetStartSendFlag();
+				this.StartSend();
 			}
 		}
 
@@ -96,17 +90,9 @@ namespace Base
 			{
 				this.sendBuffer.SendTo(buffer);
 			}
-			if (this.isConnected)
-			{
-				this.SetStartSendFlag();
-			}
-		}
-
-		public long SendTimer
-		{
-			get
+			if (!this.isSending && this.isConnected)
 			{
-				return this.sendTimer;
+				this.StartSend();
 			}
 		}
 
@@ -121,12 +107,15 @@ namespace Base
 
 		private void StartSend()
 		{
+			// 没有数据需要发送
 			if (this.sendBuffer.Count == 0)
 			{
-				this.sendTimer = 0;
+				this.isSending = false;
 				return;
 			}
 
+			this.isSending = true;
+
 			int sendSize = TBuffer.ChunkSize - this.sendBuffer.FirstIndex;
 			if (sendSize > this.sendBuffer.Count)
 			{
@@ -135,15 +124,15 @@ namespace Base
 
 			if (!this.socket.SendAsync(this.sendBuffer.First, this.sendBuffer.FirstIndex, sendSize))
 			{
-				this.OnSend(this.Id, sendSize, SocketError.Success);
+				this.OnSend(sendSize, SocketError.Success);
 				return;
 			}
-			this.socket.OnSend = (n, e) => this.OnSend(this.Id, n, e);
+			this.socket.OnSend = this.OnSend;
 		}
 
-		private void OnSend(long channelId, int n, SocketError error)
+		private void OnSend(int n, SocketError error)
 		{
-			if (this.service.GetChannel(channelId) == null)
+			if (this.Id == 0)
 			{
 				return;
 			}
@@ -160,6 +149,7 @@ namespace Base
 				this.sendBuffer.FirstIndex = 0;
 				this.sendBuffer.RemoveFirst();
 			}
+
 			this.StartSend();
 		}
 
@@ -168,14 +158,14 @@ namespace Base
 			int size = TBuffer.ChunkSize - this.recvBuffer.LastIndex;
 			if (!this.socket.RecvAsync(this.recvBuffer.Last, this.recvBuffer.LastIndex, size))
 			{
-				this.OnRecv(this.Id, size, SocketError.Success);
+				this.OnRecv(size, SocketError.Success);
 			}
-			this.socket.OnRecv = (n, e) => this.OnRecv(this.Id, n, e);
+			this.socket.OnRecv = this.OnRecv;
 		}
 
-		private void OnRecv(long channelId, int n, SocketError error)
+		private void OnRecv(int n, SocketError error)
 		{
-			if (this.service.GetChannel(channelId) == null)
+			if (this.Id == 0)
 			{
 				return;
 			}

+ 1 - 3
Unity/Assets/Scripts/Base/Network/TNet/TService.cs

@@ -9,7 +9,7 @@ namespace Base
 		private TPoller poller = new TPoller();
 		
 		private readonly Dictionary<long, TChannel> idChannels = new Dictionary<long, TChannel>();
-
+		
 		private void Dispose(bool disposing)
 		{
 			if (this.poller == null)
@@ -58,7 +58,6 @@ namespace Base
 				return;
 			}
 			this.idChannels.Remove(id);
-			this.Timer.Remove(channel.SendTimer);
 			channel.Dispose();
 		}
 
@@ -82,7 +81,6 @@ namespace Base
 		public override void Update()
 		{
 			this.poller.Update();
-			this.Timer.Update();
 		}
 	}
 }

+ 0 - 72
Unity/Assets/Scripts/Base/Network/TimerManager.cs

@@ -1,72 +0,0 @@
-using System;
-using System.Collections.Generic;
-
-namespace Base
-{
-	public class TimerManager
-	{
-		private class Timer
-		{
-			public long Id { get; set; }
-			public long Time { get; set; }
-			public Action Action { get; set; }
-		}
-
-		private readonly Dictionary<long, Timer> timers = new Dictionary<long, Timer>();
-
-		/// <summary>
-		/// key: time, value: timer id
-		/// </summary>
-		private readonly MultiMap<long, long> timeGuid = new MultiMap<long, long>();
-
-		private readonly Queue<long> timeoutTimer = new Queue<long>();
-
-		public long Add(long time, Action action)
-		{
-			Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = time, Action = action };
-			this.timers[timer.Id] = timer;
-			this.timeGuid.Add(timer.Time, timer.Id);
-			return timer.Id;
-		}
-
-		public void Remove(long id)
-		{
-			Timer timer;
-			if (!this.timers.TryGetValue(id, out timer))
-			{
-				return;
-			}
-			this.timers.Remove(timer.Id);
-			this.timeGuid.Remove(timer.Time, timer.Id);
-		}
-
-		public void Update()
-		{
-			long timeNow = TimeHelper.ClientNow();
-			foreach (long time in this.timeGuid.Keys)
-			{
-				if (time > timeNow)
-				{
-					break;
-				}
-				this.timeoutTimer.Enqueue(time);
-			}
-
-			while (this.timeoutTimer.Count > 0)
-			{
-				long key = this.timeoutTimer.Dequeue();
-				long[] timeoutIds = this.timeGuid.GetAll(key);
-				foreach (long id in timeoutIds)
-				{
-					Timer timer;
-					if (!this.timers.TryGetValue(id, out timer))
-					{
-						continue;
-					}
-					this.Remove(id);
-					timer.Action();
-				}
-			}
-		}
-	}
-}

+ 0 - 12
Unity/Assets/Scripts/Base/Network/TimerManager.cs.meta

@@ -1,12 +0,0 @@
-fileFormatVersion: 2
-guid: 25874ee32fa73c64c902d800b4543be3
-timeCreated: 1474948189
-licenseType: Pro
-MonoImporter:
-  serializedVersion: 2
-  defaultReferences: []
-  executionOrder: 0
-  icon: {instanceID: 0}
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

+ 2 - 3
Unity/Unity.CSharp.csproj

@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
 <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -107,7 +107,6 @@
     <Compile Include="Assets\Scripts\Base\Network\TNet\TPoller.cs" />
     <Compile Include="Assets\Scripts\Base\Network\TNet\TService.cs" />
     <Compile Include="Assets\Scripts\Base\Network\TNet\TSocket.cs" />
-    <Compile Include="Assets\Scripts\Base\Network\TimerManager.cs" />
     <Compile Include="Assets\Scripts\Base\Network\UNet\Library.cs" />
     <Compile Include="Assets\Scripts\Base\Network\UNet\NativeMethods.cs" />
     <Compile Include="Assets\Scripts\Base\Network\UNet\NativeStructs.cs" />
@@ -171,4 +170,4 @@
   </ItemGroup>
   <ItemGroup />
   <Import Project="$(MSBuildExtensionsPath)\SyntaxTree\UnityVS\2015\UnityVS.CSharp.targets" />
-</Project>
+</Project>