private void ApportionTypeHandlerEvent(ApportionDispatcher apportionDispatcher, ConnectionWorkType type) { switch (type) { case ConnectionWorkType.MainServiceConnection: this.MainServiceConnect(apportionDispatcher); break; case ConnectionWorkType.MainApplicationConnection: this.MainApplicationConnect(apportionDispatcher); break; case ConnectionWorkType.ApplicationServiceConnection: this.ApplicationServiceConnect(apportionDispatcher); break; case ConnectionWorkType.ApplicationConnection: this.ApplicationConnect(apportionDispatcher); break; case ConnectionWorkType.None: break; default: break; } this.LogOutputEventHandler?.Invoke(LogOutLevelType.Debug, $"工作类型:{type.ToString()} 的连接已分配!"); }
public override void OnMessage() { var defineHeadSize = sizeof(int); if (ListByteBuffer.Count < defineHeadSize) { return; } byte[] lenBytes = ListByteBuffer.GetRange(0, defineHeadSize).ToArray(); int packageLen = BitConverter.ToInt32(lenBytes, 0); if (packageLen < 0 || packageLen > ApplicationConfiguartion.Options.MaxPacketSize || packageLen < 25) //数据不合法 或 小于大概ack固定长度 { this.Log(LogOutLevelType.Error, $"Type:{ConnectionWorkType.ToString()} 长度不合法!"); this.CloseSession(); return; } if (packageLen + defineHeadSize > ListByteBuffer.Count) { return; } this._ackRetainData = ListByteBuffer.GetRange(defineHeadSize, packageLen).ToArray(); ListByteBuffer.RemoveRange(0, packageLen + defineHeadSize); var longSize = sizeof(long); var packageBody = GZipHelper.Decompress(_ackRetainData, longSize, _ackRetainData.Length - longSize); var messageHead = TakeMessageHead(packageBody); if (messageHead == ACK_HEAD) { var ack = PacketSerializeHelper.DeserializePacket <AckPacket>(TakeMessage(packageBody)); this._accessId = ack.AccessId; if (ValidityAccessIdWithKey((ConnectionWorkType)ack.Type, ack.AccessId, ack.AccessKey)) { this.ApportionTypeHandlerEvent?.Invoke(this, (ConnectionWorkType)ack.Type); } else { if ((ConnectionWorkType)ack.Type == ConnectionWorkType.MainApplicationConnection) { var midData = MessageHelper.CopyMessageHeadTo(MessageHead.MID_ACCESS_KEY_WRONG); this.CurrentSession.SendAsync(midData.BuilderHeadPacket()); } this.Log(LogOutLevelType.Debug, $"Type:{((ConnectionWorkType)ack.Type).ToString()} AccessId:{ack.AccessId} 或AccessKey:{ack.AccessKey} 验证失败,登陆不成功!"); this.CloseSession(); } } else { this.CloseSession(); this.Log(LogOutLevelType.Warning, $"未知消息,连接被关闭!"); } }