提交 9446cf62 编写于 作者: T tanghai

1.网络层修复一些bug

2.组件Destroy事件应该在释放前触发
3.修复: Actor在删除的时候抛出异常
4.修复: AddComponent(Component)方法没有设置parent
感谢:渐渐,哲学绅士 提出上述bug
上级 b0a858bb
...@@ -128,6 +128,11 @@ namespace ETHotfix ...@@ -128,6 +128,11 @@ namespace ETHotfix
return; return;
} }
if (actorTask.ActorMessage == null)
{
return;
}
await self.RunTask(actorTask); await self.RunTask(actorTask);
} }
} }
......
...@@ -74,6 +74,9 @@ namespace ETModel ...@@ -74,6 +74,9 @@ namespace ETModel
{ {
return; return;
} }
// 触发Destroy事件
Game.EventSystem.Destroy(this);
Game.EventSystem.Remove(this.InstanceId); Game.EventSystem.Remove(this.InstanceId);
...@@ -83,9 +86,6 @@ namespace ETModel ...@@ -83,9 +86,6 @@ namespace ETModel
{ {
Game.ObjectPool.Recycle(this); Game.ObjectPool.Recycle(this);
} }
// 触发Destroy事件
Game.EventSystem.Destroy(this);
} }
public virtual void BeginSerialize() public virtual void BeginSerialize()
......
...@@ -33,9 +33,7 @@ namespace ETModel ...@@ -33,9 +33,7 @@ namespace ETModel
{ {
return; return;
} }
base.Dispose();
foreach (Component component in this.GetComponents()) foreach (Component component in this.GetComponents())
{ {
try try
...@@ -47,7 +45,9 @@ namespace ETModel ...@@ -47,7 +45,9 @@ namespace ETModel
Log.Error(e); Log.Error(e);
} }
} }
base.Dispose();
this.components.Clear(); this.components.Clear();
this.componentDict.Clear(); this.componentDict.Clear();
} }
...@@ -59,6 +59,8 @@ namespace ETModel ...@@ -59,6 +59,8 @@ namespace ETModel
{ {
throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {type.Name}"); throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {type.Name}");
} }
component.Parent = this;
if (component is ISerializeToEntity) if (component is ISerializeToEntity)
{ {
......
...@@ -39,7 +39,7 @@ namespace ETModel ...@@ -39,7 +39,7 @@ namespace ETModel
#if ILRuntime #if ILRuntime
if (this.appDomain == null) if (this.appDomain == null)
{ {
return new Type[0]; return new List<Type>();
} }
return this.appDomain.LoadedTypes.Values.Select(x => x.ReflectionType); return this.appDomain.LoadedTypes.Values.Select(x => x.ReflectionType);
......
...@@ -3,23 +3,42 @@ namespace ETModel ...@@ -3,23 +3,42 @@ namespace ETModel
public static class ErrorCode public static class ErrorCode
{ {
public const int ERR_Success = 0; public const int ERR_Success = 0;
public const int ERR_NotFoundActor = 2;
public const int ERR_ActorNoMailBoxComponent = 3; // 100000 以上,避免跟SocketError冲突
public const int ERR_ActorTimeOut = 4; public const int ERR_MyErrorCode = 100000;
public const int ERR_PacketParserError = 5;
public const int ERR_AccountOrPasswordError = 102;
public const int ERR_SessionActorError = 103; public const int ERR_Exception = 200000;
public const int ERR_NotFoundUnit = 104;
public const int ERR_ConnectGateKeyError = 105; public const int ERR_NotFoundActor = 100002;
public const int ERR_ActorNoMailBoxComponent = 100003;
public const int ERR_ActorTimeOut = 100004;
public const int ERR_PacketParserError = 100005;
public const int ERR_RpcFail = 2001; public const int ERR_AccountOrPasswordError = 100102;
public const int ERR_SocketDisconnected = 2002; public const int ERR_SessionActorError = 100103;
public const int ERR_ReloadFail = 2003; public const int ERR_NotFoundUnit = 100104;
public const int ERR_ActorLocationNotFound = 2004; public const int ERR_ConnectGateKeyError = 100105;
public const int ERR_Exception = 100000; public const int ERR_RpcFail = 102001;
public const int ERR_SocketDisconnected = 102002;
public const int ERR_ReloadFail = 102003;
public const int ERR_ActorLocationNotFound = 102004;
public const int ERR_SessionDispose = 100001; public static bool IsRpcNeedThrowException(int error)
{
if (error == 0)
{
return false;
}
if (error > ERR_Exception)
{
return false;
}
return true;
}
} }
} }
\ No newline at end of file
...@@ -39,8 +39,6 @@ namespace ETModel ...@@ -39,8 +39,6 @@ namespace ETModel
public uint RemoteConn; public uint RemoteConn;
public uint lastConnectTime;
// accept // accept
public KChannel(uint conn, uint remoteConn, Socket socket, IPEndPoint remoteEndPoint, KService kService) : base(kService, ChannelType.Accept) public KChannel(uint conn, uint remoteConn, Socket socket, IPEndPoint remoteEndPoint, KService kService) : base(kService, ChannelType.Accept)
{ {
...@@ -103,6 +101,8 @@ namespace ETModel ...@@ -103,6 +101,8 @@ namespace ETModel
kcp.SetOutput(this.Output); kcp.SetOutput(this.Output);
kcp.NoDelay(1, 10, 2, 1); //fast kcp.NoDelay(1, 10, 2, 1); //fast
this.lastRecvTime = this.GetService().TimeNow;
HandleSend(); HandleSend();
} }
...@@ -141,6 +141,12 @@ namespace ETModel ...@@ -141,6 +141,12 @@ namespace ETModel
// 如果还没连接上,发送连接请求 // 如果还没连接上,发送连接请求
if (!this.isConnected) if (!this.isConnected)
{ {
// 5秒连接不上,报错
if (timeNow - this.lastRecvTime > 5 * 1000)
{
this.OnError((int)SocketError.ConnectionRefused);
return;
}
Connect(timeNow); Connect(timeNow);
return; return;
} }
......
...@@ -96,21 +96,22 @@ namespace ETModel ...@@ -96,21 +96,22 @@ namespace ETModel
public void Recv() public void Recv()
{ {
while (this.socket.Available > 0) if (this.socket == null)
{ {
if (this.IsDisposed) return;
{ }
return;
}
while (this.socket.Available > 0)
{
int messageLength = 0; int messageLength = 0;
try try
{ {
messageLength = this.socket.ReceiveFrom(this.cache, ref this.ipEndPoint); messageLength = this.socket.ReceiveFrom(this.cache, ref this.ipEndPoint);
} }
catch (Exception) catch (Exception e)
{ {
return; Log.Error(e);
continue;
} }
// 长度小于4,不是正常的消息 // 长度小于4,不是正常的消息
...@@ -283,6 +284,8 @@ namespace ETModel ...@@ -283,6 +284,8 @@ namespace ETModel
public override void Update() public override void Update()
{ {
this.TimeNow = (uint)TimeHelper.ClientNow();
this.Recv(); this.Recv();
this.TimerOut(); this.TimerOut();
...@@ -321,8 +324,7 @@ namespace ETModel ...@@ -321,8 +324,7 @@ namespace ETModel
return; return;
} }
long timeNow = TimeHelper.ClientNow(); long timeNow = this.TimeNow;
this.TimeNow = (uint)timeNow;
if (timeNow < this.minTime) if (timeNow < this.minTime)
{ {
......
...@@ -76,6 +76,7 @@ namespace ETModel ...@@ -76,6 +76,7 @@ namespace ETModel
this.ConnectAsync(this.RemoteAddress); this.ConnectAsync(this.RemoteAddress);
return; return;
} }
this.StartRecv(); this.StartRecv();
this.StartSend(); this.StartSend();
} }
...@@ -147,15 +148,10 @@ namespace ETModel ...@@ -147,15 +148,10 @@ namespace ETModel
private void OnConnectComplete(object o) private void OnConnectComplete(object o)
{ {
if (this.IsDisposed)
{
throw new Exception("TChannel已经被Dispose, 不能发送消息");
}
SocketAsyncEventArgs e = (SocketAsyncEventArgs) o; SocketAsyncEventArgs e = (SocketAsyncEventArgs) o;
UserTokenInfo userTokenInfo = (UserTokenInfo) e.UserToken; UserTokenInfo userTokenInfo = (UserTokenInfo) e.UserToken;
if (userTokenInfo.InstanceId != this.InstanceId) if (userTokenInfo.InstanceId != this.InstanceId)
{ {
Log.Error($"session disposed!");
return; return;
} }
...@@ -203,15 +199,10 @@ namespace ETModel ...@@ -203,15 +199,10 @@ namespace ETModel
private void OnRecvComplete(object o) private void OnRecvComplete(object o)
{ {
if (this.IsDisposed)
{
throw new Exception("TChannel已经被Dispose, 不能发送消息");
}
SocketAsyncEventArgs e = (SocketAsyncEventArgs)o; SocketAsyncEventArgs e = (SocketAsyncEventArgs)o;
UserTokenInfo userTokenInfo = (UserTokenInfo) e.UserToken; UserTokenInfo userTokenInfo = (UserTokenInfo) e.UserToken;
if (userTokenInfo.InstanceId != this.InstanceId) if (userTokenInfo.InstanceId != this.InstanceId)
{ {
Log.Error($"session disposed!");
return; return;
} }
...@@ -299,15 +290,10 @@ namespace ETModel ...@@ -299,15 +290,10 @@ namespace ETModel
private void OnSendComplete(object o) private void OnSendComplete(object o)
{ {
if (this.IsDisposed)
{
throw new Exception("TChannel已经被Dispose, 不能发送消息");
}
SocketAsyncEventArgs e = (SocketAsyncEventArgs)o; SocketAsyncEventArgs e = (SocketAsyncEventArgs)o;
UserTokenInfo userTokenInfo = (UserTokenInfo) e.UserToken; UserTokenInfo userTokenInfo = (UserTokenInfo) e.UserToken;
if (userTokenInfo.InstanceId != this.InstanceId) if (userTokenInfo.InstanceId != this.InstanceId)
{ {
Log.Error($"session disposed!");
return; return;
} }
......
...@@ -62,7 +62,7 @@ namespace ETModel ...@@ -62,7 +62,7 @@ namespace ETModel
foreach (Action<IResponse> action in this.requestCallback.Values.ToArray()) foreach (Action<IResponse> action in this.requestCallback.Values.ToArray())
{ {
action.Invoke(new ResponseMessage { Error = ErrorCode.ERR_SessionDispose }); action.Invoke(new ResponseMessage { Error = this.Error });
} }
this.Error = 0; this.Error = 0;
...@@ -160,7 +160,7 @@ namespace ETModel ...@@ -160,7 +160,7 @@ namespace ETModel
{ {
try try
{ {
if (response.Error > ErrorCode.ERR_Exception) if (ErrorCode.IsRpcNeedThrowException(response.Error))
{ {
throw new RpcException(response.Error, response.Message); throw new RpcException(response.Error, response.Message);
} }
...@@ -187,7 +187,7 @@ namespace ETModel ...@@ -187,7 +187,7 @@ namespace ETModel
{ {
try try
{ {
if (response.Error > ErrorCode.ERR_Exception) if (ErrorCode.IsRpcNeedThrowException(response.Error))
{ {
throw new RpcException(response.Error, response.Message); throw new RpcException(response.Error, response.Message);
} }
......
...@@ -74,6 +74,9 @@ namespace ETHotfix ...@@ -74,6 +74,9 @@ namespace ETHotfix
{ {
return; return;
} }
// 触发Destroy事件
Game.EventSystem.Destroy(this);
Game.EventSystem.Remove(this.InstanceId); Game.EventSystem.Remove(this.InstanceId);
...@@ -83,9 +86,6 @@ namespace ETHotfix ...@@ -83,9 +86,6 @@ namespace ETHotfix
{ {
Game.ObjectPool.Recycle(this); Game.ObjectPool.Recycle(this);
} }
// 触发Destroy事件
Game.EventSystem.Destroy(this);
} }
public virtual void BeginSerialize() public virtual void BeginSerialize()
......
...@@ -33,9 +33,7 @@ namespace ETHotfix ...@@ -33,9 +33,7 @@ namespace ETHotfix
{ {
return; return;
} }
base.Dispose();
foreach (Component component in this.GetComponents()) foreach (Component component in this.GetComponents())
{ {
try try
...@@ -48,6 +46,8 @@ namespace ETHotfix ...@@ -48,6 +46,8 @@ namespace ETHotfix
} }
} }
base.Dispose();
this.components.Clear(); this.components.Clear();
this.componentDict.Clear(); this.componentDict.Clear();
} }
...@@ -59,6 +59,8 @@ namespace ETHotfix ...@@ -59,6 +59,8 @@ namespace ETHotfix
{ {
throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {type.Name}"); throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {type.Name}");
} }
component.Parent = this;
if (component is ISerializeToEntity) if (component is ISerializeToEntity)
{ {
......
...@@ -40,7 +40,7 @@ namespace ETHotfix ...@@ -40,7 +40,7 @@ namespace ETHotfix
foreach (Action<IResponse> action in this.requestCallback.Values.ToArray()) foreach (Action<IResponse> action in this.requestCallback.Values.ToArray())
{ {
action.Invoke(new ResponseMessage { Error = ErrorCode.ERR_SessionDispose }); action.Invoke(new ResponseMessage { Error = this.session.Error });
} }
this.requestCallback.Clear(); this.requestCallback.Clear();
...@@ -105,7 +105,7 @@ namespace ETHotfix ...@@ -105,7 +105,7 @@ namespace ETHotfix
{ {
try try
{ {
if (response.Error > ErrorCode.ERR_Exception) if (ErrorCode.IsRpcNeedThrowException(response.Error))
{ {
throw new RpcException(response.Error, response.Message); throw new RpcException(response.Error, response.Message);
} }
...@@ -133,7 +133,7 @@ namespace ETHotfix ...@@ -133,7 +133,7 @@ namespace ETHotfix
{ {
try try
{ {
if (response.Error > ErrorCode.ERR_Exception) if (ErrorCode.IsRpcNeedThrowException(response.Error))
{ {
throw new RpcException(response.Error, response.Message); throw new RpcException(response.Error, response.Message);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册