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