public static StartMachineConfig GetThisMachineConfig() { string[] localIP = NetworkHelper.GetAddressIPs(); StartMachineConfig startMachineConfig = null; foreach (StartMachineConfig config in StartMachineConfigCategory.Instance.GetAll().Values) { if (!WatcherHelper.IsThisMachine(config.InnerIP, localIP)) { continue; } startMachineConfig = config; break; } if (startMachineConfig == null) { throw new Exception("not found this machine ip config!"); } return(startMachineConfig); }
public void Awake(NetworkProtocol protocol, string address) { try { IPEndPoint ipEndPoint; switch (protocol) { case NetworkProtocol.KCP: ipEndPoint = NetworkHelper.ToIPEndPoint(address); this.Service = new KService(ipEndPoint, (channel) => { this.OnAccept(channel); }) { Parent = this }; break; case NetworkProtocol.TCP: ipEndPoint = NetworkHelper.ToIPEndPoint(address); this.Service = new TService(ipEndPoint, (channel) => { this.OnAccept(channel); }) { Parent = this }; break; case NetworkProtocol.WebSocket: string[] prefixs = address.Split(';'); this.Service = new WService(prefixs, (channel) => { this.OnAccept(channel); }) { Parent = this }; break; } } catch (Exception e) { throw new Exception($"NetworkComponent Awake Error {address}", e); } }
/// <summary> /// 登陆网关服 /// </summary> private static async ETTask <G2C_LoginGate> ConnectGate(Scene zoneScene, R2C_Login r2CLogin) { try { Log.Debug($"连接网关服 address:{r2CLogin.Address}"); // 创建一个gate Session,并且保存到SessionComponent中 Session gateSession = zoneScene.GetComponent <NetKcpComponent>().Create(NetworkHelper.ToIPEndPoint(r2CLogin.Address)); gateSession.AddComponent <SessionCallbackComponent>().DisposeCallback = GateSessionDispose; G2C_LoginGate response = await gateSession.Call(new C2G_LoginGate() { Key = r2CLogin.Key, GateId = r2CLogin.GateId }) as G2C_LoginGate; gateSession.AddComponent <PingComponent>(); zoneScene.GetComponent <SessionComponent>().Session = gateSession; if (response == null) { throw new Exception($"登陆网关服失败 Error:response is null"); } if (!string.IsNullOrEmpty(response.Message)) { throw new Exception($"登陆网关服失败 Error:{response.Message}"); } Log.Debug($"连接网关服成功"); return(response); } catch (Exception e) { throw new Exception($"连接网关服异常 {e}"); } }
/// <summary> /// 登陆验证服 /// </summary> private static async ETTask <R2C_Login> ConnectRealm(Scene zoneScene, string address, string account) { try { Log.Debug($"连接验证服 address:{address}"); // 创建一个ETModel层的Session R2C_Login response; using (Session relamSession = zoneScene.GetComponent <NetKcpComponent>().Create(NetworkHelper.ToIPEndPoint(address))) { relamSession.AddComponent <SessionCallbackComponent>().DisposeCallback = RealmSessionDispose; response = await relamSession.Call(new C2R_Login() { Account = account, Password = "******" }) as R2C_Login; } if (response == null) { throw new Exception($"登陆验证服失败 Error:response is null"); } if (!string.IsNullOrEmpty(response.Message)) { throw new Exception($"登陆验证服失败 Error:{response.Message}"); } Log.Debug($"连接验证服成功"); return(response); } catch (Exception e) { throw new Exception($"连接验证服异常 {e}"); } }
protected override async ETTask Run(EventType.AppStart args) { switch (Game.Options.AppType) { case AppType.ExcelExporter: { Game.Options.Console = 1; ExcelExporter.Export(); return; } case AppType.Proto2CS: { Game.Options.Console = 1; Proto2CS.Export(); return; } } Game.Scene.AddComponent <ConfigComponent>(); await ConfigComponent.Instance.LoadAsync(); StartProcessConfig processConfig = StartProcessConfigCategory.Instance.Get(Game.Options.Process); Game.Scene.AddComponent <TimerComponent>(); Game.Scene.AddComponent <OpcodeTypeComponent>(); Game.Scene.AddComponent <MessageDispatcherComponent>(); Game.Scene.AddComponent <CoroutineLockComponent>(); // 发送普通actor消息 Game.Scene.AddComponent <ActorMessageSenderComponent>(); // 发送location actor消息 Game.Scene.AddComponent <ActorLocationSenderComponent>(); // 访问location server的组件 Game.Scene.AddComponent <LocationProxyComponent>(); Game.Scene.AddComponent <ActorMessageDispatcherComponent>(); // 数值订阅组件 Game.Scene.AddComponent <NumericWatcherComponent>(); Game.Scene.AddComponent <NetThreadComponent>(); switch (Game.Options.AppType) { case AppType.Server: { Game.Scene.AddComponent <NetInnerComponent, IPEndPoint>(processConfig.InnerIPPort); var processScenes = StartSceneConfigCategory.Instance.GetByProcess(Game.Options.Process); foreach (StartSceneConfig startConfig in processScenes) { await SceneFactory.Create(Game.Scene, startConfig.Id, startConfig.InstanceId, startConfig.Zone, startConfig.Name, startConfig.Type, startConfig); } break; } case AppType.Watcher: { StartMachineConfig startMachineConfig = WatcherHelper.GetThisMachineConfig(); WatcherComponent watcherComponent = Game.Scene.AddComponent <WatcherComponent>(); watcherComponent.Start(Game.Options.CreateScenes); Game.Scene.AddComponent <NetInnerComponent, IPEndPoint>(NetworkHelper.ToIPEndPoint($"{startMachineConfig.InnerIP}:{startMachineConfig.WatcherPort}")); break; } case AppType.GameTool: break; } if (Game.Options.Console == 1) { Game.Scene.AddComponent <ConsoleComponent>(); } }
public static async ETTask Login(Entity fuiComponent, string address, string account, string password) { try { // 创建一个ETModel层的Session R2C_Login r2CLogin; Scene zoneScene = fuiComponent.DomainScene(); Session session = zoneScene.GetComponent <NetKcpComponent>().Create(NetworkHelper.ToIPEndPoint(address)); { r2CLogin = (R2C_Login)await session.Call(new C2R_Login() { Account = account, Password = password }); } session.Dispose(); // 创建一个gate Session,并且保存到SessionComponent中 Session gateSession = zoneScene.GetComponent <NetKcpComponent>().Create(NetworkHelper.ToIPEndPoint(r2CLogin.Address)); gateSession.AddComponent <PingComponent>(); zoneScene.AddComponent <SessionComponent>().Session = gateSession; G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await gateSession.Call( new C2G_LoginGate() { Key = r2CLogin.Key, GateId = r2CLogin.GateId }); Log.Debug("登陆gate成功!"); await Game.EventSystem.Publish(new EventType.LoginFinish() { ZoneScene = zoneScene }); } catch (Exception e) { Log.Error(e); } }
private void Recv() { if (this.socket == null) { return; } while (socket != null && this.socket.Available > 0) { int messageLength = this.socket.ReceiveFrom(this.cache, ref this.ipEndPoint); // 长度小于1,不是正常的消息 if (messageLength < 1) { continue; } // accept byte flag = this.cache[0]; // conn从100开始,如果为1,2,3则是特殊包 uint remoteConn = 0; uint localConn = 0; try { KChannel kChannel = null; switch (flag) { #if NOT_CLIENT case KcpProtocalType.SYN: // accept { // 长度!=5,不是SYN消息 if (messageLength < 9) { break; } string realAddress = null; remoteConn = BitConverter.ToUInt32(this.cache, 1); if (messageLength > 9) { realAddress = this.cache.ToStr(9, messageLength - 9); } remoteConn = BitConverter.ToUInt32(this.cache, 1); localConn = BitConverter.ToUInt32(this.cache, 5); this.waitConnectChannels.TryGetValue(remoteConn, out kChannel); if (kChannel == null) { localConn = CreateRandomLocalConn(this.random); // 已存在同样的localConn,则不处理,等待下次sync if (this.localConnChannels.ContainsKey(localConn)) { break; } long id = this.CreateAcceptChannelId(localConn); if (this.idChannels.ContainsKey(id)) { break; } kChannel = new KChannel(id, localConn, remoteConn, this.socket, this.CloneAddress(), this); this.idChannels.Add(kChannel.Id, kChannel); this.waitConnectChannels.Add(kChannel.RemoteConn, kChannel); // 连接上了或者超时后会删除 this.localConnChannels.Add(kChannel.LocalConn, kChannel); kChannel.RealAddress = realAddress; IPEndPoint realEndPoint = kChannel.RealAddress == null? kChannel.RemoteAddress : NetworkHelper.ToIPEndPoint(kChannel.RealAddress); this.OnAccept(kChannel.Id, realEndPoint); } if (kChannel.RemoteConn != remoteConn) { break; } // 地址跟上次的不一致则跳过 if (kChannel.RealAddress != realAddress) { Log.Error($"kchannel syn address diff: {kChannel.Id} {kChannel.RealAddress} {realAddress}"); break; } try { byte[] buffer = this.cache; buffer.WriteTo(0, KcpProtocalType.ACK); buffer.WriteTo(1, kChannel.LocalConn); buffer.WriteTo(5, kChannel.RemoteConn); Log.Info($"kservice syn: {kChannel.Id} {remoteConn} {localConn}"); this.socket.SendTo(buffer, 0, 9, SocketFlags.None, kChannel.RemoteAddress); } catch (Exception e) { Log.Error(e); kChannel.OnError(ErrorCode.ERR_SocketCantSend); } break; } #endif case KcpProtocalType.ACK: // connect返回 // 长度!=9,不是connect消息 if (messageLength != 9) { break; } remoteConn = BitConverter.ToUInt32(this.cache, 1); localConn = BitConverter.ToUInt32(this.cache, 5); kChannel = this.GetByLocalConn(localConn); if (kChannel != null) { Log.Info($"kservice ack: {kChannel.Id} {remoteConn} {localConn}"); kChannel.RemoteConn = remoteConn; kChannel.HandleConnnect(); } break; case KcpProtocalType.FIN: // 断开 // 长度!=13,不是DisConnect消息 if (messageLength != 13) { break; } remoteConn = BitConverter.ToUInt32(this.cache, 1); localConn = BitConverter.ToUInt32(this.cache, 5); int error = BitConverter.ToInt32(this.cache, 9); // 处理chanel kChannel = this.GetByLocalConn(localConn); if (kChannel == null) { break; } // 校验remoteConn,防止第三方攻击 if (kChannel.RemoteConn != remoteConn) { break; } Log.Info($"kservice recv fin: {kChannel.Id} {localConn} {remoteConn} {error}"); kChannel.OnError(ErrorCode.ERR_PeerDisconnect); break; case KcpProtocalType.MSG: // 断开 // 长度<9,不是Msg消息 if (messageLength < 9) { break; } // 处理chanel remoteConn = BitConverter.ToUInt32(this.cache, 1); localConn = BitConverter.ToUInt32(this.cache, 5); kChannel = this.GetByLocalConn(localConn); if (kChannel == null) { // 通知对方断开 this.Disconnect(localConn, remoteConn, ErrorCode.ERR_KcpNotFoundChannel, (IPEndPoint) this.ipEndPoint, 1); break; } // 校验remoteConn,防止第三方攻击 if (kChannel.RemoteConn != remoteConn) { break; } kChannel.HandleRecv(this.cache, 5, messageLength - 5); break; } } catch (Exception e) { Log.Error($"kservice error: {flag} {remoteConn} {localConn}\n{e}"); } } }
public override AChannel ConnectChannel(string address) { IPEndPoint ipEndPoint2 = NetworkHelper.ToIPEndPoint(address); return(this.ConnectChannel(ipEndPoint2)); }
public static async ETTask Login(Scene zoneScene, string address, string account, string password, Action onError = null) { try { // 创建一个ETModel层的Session R2C_Login r2CLogin; Session session = null; try { session = zoneScene.GetComponent <NetKcpComponent>().Create(NetworkHelper.ToIPEndPoint(address)); { r2CLogin = (R2C_Login)await session.Call(new C2R_Login() { Account = account, Password = password }); } } finally { session?.Dispose(); } long channelId = RandomHelper.RandInt64(); var routercomponent = zoneScene.AddComponent <GetRouterComponent, long, long>(r2CLogin.GateId, channelId); string routerAddress = await routercomponent.Tcs; if (routerAddress == "") { zoneScene.RemoveComponent <GetRouterComponent>(); throw new Exception("routerAddress 失败"); } Log.Debug("routerAddress 获取成功:" + routerAddress); zoneScene.RemoveComponent <GetRouterComponent>(); // 创建一个gate Session,并且保存到SessionComponent中 Session gateSession = zoneScene.GetComponent <NetKcpComponent>().Create(channelId, NetworkHelper.ToIPEndPoint(routerAddress)); gateSession.AddComponent <RouterDataComponent>().Gateid = r2CLogin.GateId; gateSession.AddComponent <PingComponent>(); zoneScene.AddComponent <SessionComponent>().Session = gateSession; G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await gateSession.Call( new C2G_LoginGate() { Key = r2CLogin.Key, GateId = r2CLogin.GateId }); Log.Debug("登陆gate成功!"); await Game.EventSystem.PublishAsync(new EventType.LoginFinish() { ZoneScene = zoneScene }); } catch (Exception e) { onError?.Invoke(); Log.Error(e); } }
public static async ETVoid Login(Scene zoneScene, string address, string account) { try { // 创建一个ET层的Session R2C_Login r2CLogin; using (Session session = zoneScene.GetComponent <NetKcpComponent>().Create(NetworkHelper.ToIPEndPoint(address))) { r2CLogin = (R2C_Login)await session.Call(new C2R_Login() { Account = account, Password = "******" }); } // 创建一个gate Session,并且保存到SessionComponent中 Session gateSession = zoneScene.GetComponent <NetKcpComponent>().Create(NetworkHelper.ToIPEndPoint(r2CLogin.Address)); gateSession.AddComponent <PingComponent>(); zoneScene.AddComponent <SessionComponent>().Session = gateSession; G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await gateSession.Call( new C2G_LoginGate() { Key = r2CLogin.Key, GateId = r2CLogin.GateId }); Log.Info("登陆gate成功!"); await Game.EventSystem.Publish(new EventType.LoginFinish() { ZoneScene = zoneScene }); } catch (Exception e) { Log.Error(e); } }
public static async ETVoid Register(Scene zoneScene, string address, string account, string password) { try { // 创建一个ETModel层的Session R2C_Register r2CRegister; using (Session session = zoneScene.GetComponent <NetKcpComponent>().Create(NetworkHelper.ToIPEndPoint(address))) { r2CRegister = (R2C_Register)await session.Call(new C2R_Register() { Account = account, Password = password }); } if (r2CRegister.Error != ErrorCode.ERR_Success) { Log.Info("注册失败!"); return; } Log.Info("注册成功!"); //await Game.EventSystem.Publish(new EventType.LoginFinish() { ZoneScene = zoneScene }); } catch (Exception e) { Log.Error(e); } }
protected override async ETTask Run(EventType.AppStart args) { Game.Scene.AddComponent <ConfigComponent>(); await ConfigComponent.Instance.LoadAsync(); //读取StartProcessConfig中id为Game.Options.Process(这里值为1)的整行配置 //ET6.0由于使用了protobuffer作为导表工具,所以请去Excel文件夹查看原数据 StartProcessConfig processConfig = StartProcessConfigCategory.Instance.Get(GlobalDefine.Options.Process); Game.Scene.AddComponent <TimerComponent>(); Game.Scene.AddComponent <OpcodeTypeComponent>(); Game.Scene.AddComponent <MessageDispatcherComponent>(); Game.Scene.AddComponent <CoroutineLockComponent>(); // 发送普通actor消息 Game.Scene.AddComponent <ActorMessageSenderComponent>(); // 发送location actor消息 Game.Scene.AddComponent <ActorLocationSenderComponent>(); // 访问location server的组件 Game.Scene.AddComponent <LocationProxyComponent>(); Game.Scene.AddComponent <ActorMessageDispatcherComponent>(); // 数值订阅组件 Game.Scene.AddComponent <NumericWatcherComponent>(); Game.Scene.AddComponent <NetThreadComponent>(); //根据自身的类型来决定要添加的组件 switch (GlobalDefine.Options.AppType) { case AppType.Server: { //是一个Sever就添加一个内网组件,用于内网通信 Game.Scene.AddComponent <NetInnerComponent, IPEndPoint>(processConfig.InnerIPPort); //以GloabDefine.Options.Process为凭证获取自己要添加的Scene //注意在StartSceneConfig.xlsx中有两份配置,第一份是给Server用的,第二份是给Robot用的 List <StartSceneConfig> processScenes = StartSceneConfigCategory.Instance.GetByProcess(GlobalDefine.Options.Process); foreach (StartSceneConfig startConfig in processScenes) { await SceneFactory.Create(Game.Scene, startConfig.Id, startConfig.InstanceId, startConfig.Zone, startConfig.Name, startConfig.Type, startConfig); } break; } case AppType.Watcher: { //是一个守护进程就额外添加一个WatcherComponent,守护进程可以用来负责拉起和管理全部的进程 StartMachineConfig startMachineConfig = WatcherHelper.GetThisMachineConfig(); WatcherComponent watcherComponent = Game.Scene.AddComponent <WatcherComponent>(); watcherComponent.Start(GlobalDefine.Options.CreateScenes); Game.Scene.AddComponent <NetInnerComponent, IPEndPoint>(NetworkHelper.ToIPEndPoint($"{startMachineConfig.InnerIP}:{startMachineConfig.WatcherPort}")); break; } case AppType.GameTool: break; } if (GlobalDefine.Options.Console == 1) { Game.Scene.AddComponent <ConsoleComponent>(); } }
private async ETTask RunAsync(EventType.AppStart args) { Game.Scene.AddComponent <SkillStepComponent>(); Game.Scene.AddComponent <ConfigComponent>(); await ConfigComponent.Instance.LoadAsync(); StartProcessConfig processConfig = StartProcessConfigCategory.Instance.Get(Game.Options.Process); Game.Scene.AddComponent <TimerComponent>(); Game.Scene.AddComponent <OpcodeTypeComponent>(); Game.Scene.AddComponent <MessageDispatcherComponent>(); Game.Scene.AddComponent <SessionStreamDispatcher>(); Game.Scene.AddComponent <CoroutineLockComponent>(); // 发送普通actor消息 Game.Scene.AddComponent <ActorMessageSenderComponent>(); // 发送location actor消息 Game.Scene.AddComponent <ActorLocationSenderComponent>(); // 访问location server的组件 Game.Scene.AddComponent <LocationProxyComponent>(); Game.Scene.AddComponent <ActorMessageDispatcherComponent>(); // 数值订阅组件 Game.Scene.AddComponent <NumericWatcherComponent>(); // 技能订阅组件 Game.Scene.AddComponent <SkillWatcherComponent>(); // BUFF订阅组件 Game.Scene.AddComponent <BuffWatcherComponent>(); Game.Scene.AddComponent <NetThreadComponent>(); Game.Scene.AddComponent <NavmeshComponent, Func <string, byte[]> >(RecastFileReader.Read); switch (Game.Options.AppType) { case AppType.Server: { Game.Scene.AddComponent <NetInnerComponent, IPEndPoint, int>(processConfig.InnerIPPort, SessionStreamDispatcherType.SessionStreamDispatcherServerInner); var processScenes = StartSceneConfigCategory.Instance.GetByProcess(Game.Options.Process); foreach (StartSceneConfig startConfig in processScenes) { await SceneFactory.Create(Game.Scene, startConfig.Id, startConfig.InstanceId, startConfig.Zone, startConfig.Name, startConfig.Type, startConfig); } break; } case AppType.Watcher: { StartMachineConfig startMachineConfig = WatcherHelper.GetThisMachineConfig(); WatcherComponent watcherComponent = Game.Scene.AddComponent <WatcherComponent>(); watcherComponent.Start(Game.Options.CreateScenes); Game.Scene.AddComponent <NetInnerComponent, IPEndPoint, int>(NetworkHelper.ToIPEndPoint($"{startMachineConfig.InnerIP}:{startMachineConfig.WatcherPort}"), SessionStreamDispatcherType.SessionStreamDispatcherServerInner); break; } case AppType.GameTool: break; } if (Game.Options.Console == 1) { Game.Scene.AddComponent <ConsoleComponent>(); } }