void OnCreatePlayer(NetState netstate, PacketReader reader) { if (!netstate.IsVerifyLogin) return; var p1 = reader.ReadUTF8String(); var p2 = (TradeAge.Server.Entity.Character.Sex)reader.ReadByte(); module.OnCreatePlayer(netstate,p1,p2); }
/// <summary> /// 客户端登陆服务器,采用简单模式,就只有2个参数 /// </summary> /// <param name="netstate"></param> /// <param name="accountName"></param> /// <param name="password"></param> /// <param name="serverId"></param> public void OnLoginServer(NetState netstate, string accountName, string password, int serverId = 0) { var account = WorldEntityManager.AccountNames.GetValue(accountName); if (account == null) { // 账号不存在,则创建账号 account = new Account { Name = accountName, Password = password, CreateTime = OneServer.NowTime, ServerId = serverId, Id = WorldSeqGen.AccountSeq.NextSeq(), }; WorldEntityManager.AccountNames.SetValue(accountName, account); WorldEntityManager.Accounts.SetValue(account.Id, account); ClientProxy.Login.LoginServerResult(netstate, LoginServerResult.Success); // 保存到数据 } else { if (account.Password != password) { // 账户存在,但是密码验证失败,返回错误 ClientProxy.Login.LoginServerResult(netstate, LoginServerResult.PassOrAccountError); return; } } netstate.IsVerifyLogin = true; }
/// <summary> /// 创建玩家 /// </summary> /// <param name="netstate"></param> /// <param name="playerName"></param> /// <param name="sex"></param> public void OnCreatePlayer(NetState netstate, string playerName, Sex sex) { // 如果已经创建过(例如玩家连续点了两次创建角色) var player = WorldEntityManager.PlayerCache.GetEntity(netstate.BizId); if (player != null) { ClientProxy.Login.CreatePlayerResult(netstate, CraetePlayerResult.Fail); return; } player = new Player { Id = netstate.BizId, AccountId = netstate.BizId, Name = playerName, Sex = sex, NetState = netstate }; DB.GameDB.InsertEntity(player); WorldEntityManager.PlayerCache.AddOrReplace(player); Logs.Info("角色 {0}({1}) 创建成功", playerName, player.Id); netstate.Player = player; PlayerEnterGame(player); ClientProxy.Login.CreatePlayerResult(netstate, CraetePlayerResult.Success); }
/// <summary> /// 客户端过来的心跳包 /// </summary> /// <param name="netstate"></param> /// <param name="id">心跳包id,服务器确认的时候,把这个返回给客户端</param> public void Heartbeat(NetState netstate, int id) { ClientProxy.Game.SyncServerTime(netstate, OneServer.NowTime, id); // 记录下心跳包的时间,服务器可以用这个值,判断客户端是否掉线 var player = (Player)netstate.Player; player.LastHeartbeat = OneServer.NowTime; }
public void SpriteEnter(NetState netstate, TradeAge.Server.Entity.Character.SimplePlayer obj) { var pw = PacketWriter.AcquireContent(1102); PacketProfile packetProfile = PacketProfile.GetOutgoingProfile( 1102 ); if ( packetProfile != null ) packetProfile.RegConstruct(); SimplePlayerWriteProxy.Write(obj, pw);netstate.Send(pw);PacketWriter.ReleaseContent(pw); }
public void SpriteLeave(NetState netstate,int playerId) { var pw = PacketWriter.AcquireContent(1104); PacketProfile packetProfile = PacketProfile.GetOutgoingProfile( 1104 ); if ( packetProfile != null ) packetProfile.RegConstruct(); pw.Write(playerId); netstate.Send(pw);PacketWriter.ReleaseContent(pw); }
public void CreatePlayerResult(NetState netstate,TradeAge.Server.Entity.Login.CraetePlayerResult result) { var pw = PacketWriter.AcquireContent(1003); PacketProfile packetProfile = PacketProfile.GetOutgoingProfile( 1003 ); if ( packetProfile != null ) packetProfile.RegConstruct(); pw.Write((byte)result); netstate.Send(pw);PacketWriter.ReleaseContent(pw); }
public void LoginServerResult(NetState netstate, TradeAge.Server.Entity.Login.LoginServerResult result) { var pw = new PacketWriter(1001); PacketProfile packetProfile = PacketProfile.GetOutgoingProfile(1001); if (packetProfile != null) packetProfile.RegConstruct(); pw.Write((byte)result); netstate.Send(pw); pw.Dispose(); }
public void EnterSceneInfo(NetState netstate,TradeAge.Server.Entity.Common.Vector3 postion,TradeAge.Server.Entity.Common.Vector3 direction) { var pw = PacketWriter.AcquireContent(1101); PacketProfile packetProfile = PacketProfile.GetOutgoingProfile( 1101 ); if ( packetProfile != null ) packetProfile.RegConstruct(); Vector3WriteProxy.Write(postion, pw); Vector3WriteProxy.Write(direction, pw); netstate.Send(pw);PacketWriter.ReleaseContent(pw); }
public void GetTime(NetState netstate, long clientTime) { var now = OneServer.NowTime; ClientProxy.GetPingTime.GetTimeResult(netstate, now.Ticks); //var package = TimeBack.GetPackage(OneServer.NowTime); //netstate.Send(package); }
public void SyncInitDataFinish(NetState netstate) { var pw = PacketWriter.AcquireContent(1004); PacketProfile packetProfile = PacketProfile.GetOutgoingProfile( 1004 ); if ( packetProfile != null ) packetProfile.RegConstruct(); netstate.Send(pw); if ( packetProfile != null ) packetProfile.Record(pw.Length); PacketWriter.ReleaseContent(pw); }
public void SyncServerTime(NetState netstate,DateTime serverTime,int id) { var pw = PacketWriter.AcquireContent(2); PacketProfile packetProfile = PacketProfile.GetOutgoingProfile( 2 ); if ( packetProfile != null ) packetProfile.RegConstruct(); pw.Write(serverTime.Ticks); pw.Write(id); netstate.Send(pw); if ( packetProfile != null ) packetProfile.Record(pw.Length); PacketWriter.ReleaseContent(pw); }
/// <summary> /// /// </summary> public void Execute(NetState netState) { var sw = Stopwatch.StartNew(); // 只有在客户端未断开连接时才执行业务回调 if (netState.Running) PacketHandler.OnReceive(netState, PacketReader); sw.Stop(); PacketHandler.CallTimes++; PacketHandler.ElapsedTicks += sw.Elapsed.Ticks; }
/// <summary> /// 玩家通报一次移动 /// </summary> /// <param name="netstate">网络客户端</param> /// <param name="time">当前客户端时间</param> /// <param name="postion">当前位置</param> /// <param name="direction">朝向(方向以及速度)</param> public void OnMove(NetState netstate, DateTime time, Vector2 postion, Vector2 direction) { var player = (Player)netstate.Player; if (player == null) return; //Console.WriteLine(postion.ToString() + direction.ToString()); // 理论上这里需要对玩家的位置做验证,这里先忽略,全部信任 player.Postion = postion; player.Direction = direction; // 广播给场景里的其它玩家 var onlinePlayer = WorldEntityManager.OnlinePlayers.ToArray(); foreach (var p in onlinePlayer) { if (p.Id != player.Id) { ClientProxy.Scene.SpriteMove(p.NetState, player.Id, time, postion, direction); } } }
/// <summary> /// 玩家通报一次移动 /// </summary> /// <remarks> /// 我也不想要这么多参数 /// 但是unity3d的对象的参数,不是以属性的方式存在 /// 不能被协议代码生成工具识别,只好把参数拆开 /// </remarks> /// <param name="netstate">网络客户端</param> /// <param name="time">当前客户端时间</param> /// <param name="postion"></param> /// <param name="rotation"></param> /// <param name="speed"></param> /// <param name="rotationRate"></param> /// <param name="speedUpType"></param> public void OnMove(NetState netstate, DateTime time, Vector3 postion, Quaternion rotation, float speed, float rotationRate, SpeedUpTypes speedUpType) { var player = (Player)netstate.Player; if (player == null) return; //Console.WriteLine(postion.ToString() + direction.ToString()); // 理论上这里需要对玩家的位置做验证,这里先忽略,全部信任 //player.Postion = postion; //player.Direction = direction; // 广播给场景里的其它玩家 var onlinePlayer = WorldEntityManager.OnlinePlayers.ToArray(); foreach (var p in onlinePlayer) { if (p.Id != player.Id) { ClientProxy.Scene.SpriteMove(p.NetState, player.Id, time, postion, rotation , speed, rotationRate, speedUpType); } } }
void OnMove(NetState netstate, PacketReader reader){ if (!netstate.IsVerifyLogin) return; var p1 = Vector3ReadProxy.Read(reader); var p2 = Vector3ReadProxy.Read(reader); module.OnMove(netstate,p1,p2); }
public void EnterSceneInfo(NetState netstate,TradeAge.Server.Entity.Character.SimplePlayer player) { var pw = PacketWriter.AcquireContent(1101); PacketProfile packetProfile = PacketProfile.GetOutgoingProfile( 1101 ); if ( packetProfile != null ) packetProfile.RegConstruct(); SimplePlayerWriteProxy.Write(player, pw); netstate.Send(pw); if ( packetProfile != null ) packetProfile.Record(pw.Length); PacketWriter.ReleaseContent(pw); }
public void SpriteMove(NetState netstate,int spriteId,DateTime time,TradeAge.Server.Entity.Common.Vector2 postion,TradeAge.Server.Entity.Common.Vector2 direction) { var pw = PacketWriter.AcquireContent(1103); PacketProfile packetProfile = PacketProfile.GetOutgoingProfile( 1103 ); if ( packetProfile != null ) packetProfile.RegConstruct(); pw.Write(spriteId); pw.Write(time.Ticks); Vector2WriteProxy.Write(postion, pw); Vector2WriteProxy.Write(direction, pw); netstate.Send(pw); if ( packetProfile != null ) packetProfile.Record(pw.Length); PacketWriter.ReleaseContent(pw); }
public void SpriteLeave(NetState netstate,System.Int32[] spriteId) { var pw = PacketWriter.AcquireContent(1104); PacketProfile packetProfile = PacketProfile.GetOutgoingProfile( 1104 ); if ( packetProfile != null ) packetProfile.RegConstruct(); int spriteIdlen = spriteId == null ? 0:spriteId.Length;pw.Write(spriteIdlen); for(int i = 0;i < spriteIdlen ;i++){ pw.Write(spriteId[i]); } netstate.Send(pw); if ( packetProfile != null ) packetProfile.Record(pw.Length); PacketWriter.ReleaseContent(pw); }
/// <summary> /// 创建了新的NetState需要的初始化 /// </summary> internal void OnNetStateInit( NetState newNetState ) { EventHandler<NetStateInitEventArgs> tempEventArgs = m_EventNetStateCreate; if ( tempEventArgs != null ) { NetStateInitEventArgs netStateInitEventArgs = new NetStateInitEventArgs( newNetState ); tempEventArgs( this, netStateInitEventArgs ); } }
/// <summary> /// 处理接收到的数据 /// </summary> /// <param name="netState"></param> internal void OnProcessReceive( NetState netState ) { EventHandler<NetStateEventArgs> tempEventArgs = m_EventProcessReceive; if ( tempEventArgs != null ) { NetStateEventArgs netStateEventArgs = new NetStateEventArgs( netState ); tempEventArgs( this, netStateEventArgs ); } }
/// <summary> /// 创建玩家 /// </summary> /// <param name="netstate"></param> /// <param name="playerName"></param> /// <param name="sex"></param> public void OnCreatePlayer(NetState netstate, string playerName, Sex sex) { }
/// <summary> /// 网络上面有数据过来 /// </summary> /// <param name="netState"></param> internal void OnReceive( NetState netState ) { // 表示当前已加入在处理列表中(减少处理列表的长度) if ( netState.InProcessQueue() == false ) return; m_NetStateQueue.Enqueue( netState ); Debug.Assert( m_World != null, "MessagePump.OnReceive(...) - m_World == null error!" ); // 有数据过来需要发送全局信号处理数据包 m_World.SendWorldSignal(); }
/// <summary> /// 再次处理没处理完全的数据(仅调用一次) /// </summary> /// <param name="netState"></param> private void OnceAgainReceive( NetState netState ) { if ( netState.Running ) { if ( netState.ReceiveBuffer.Length > 0 ) OnReceive( netState ); } }
void OnLoginServer(NetState netstate, PacketReader reader){ var p1 = reader.ReadUTF8String(); var p2 = reader.ReadUTF8String(); var p3 = reader.ReadInt32(); module.OnLoginServer(netstate,p1,p2,p3); }
public void SpriteEnter(NetState netstate,TradeAge.Server.Entity.Character.SceneSprite[] sprite) { var pw = PacketWriter.AcquireContent(1102); PacketProfile packetProfile = PacketProfile.GetOutgoingProfile( 1102 ); if ( packetProfile != null ) packetProfile.RegConstruct(); int spritelen = sprite == null ? 0:sprite.Length;pw.Write(spritelen); for(int i = 0;i < spritelen ;i++){ SceneSpriteWriteProxy.Write(sprite[i], pw); } netstate.Send(pw); if ( packetProfile != null ) packetProfile.Record(pw.Length); PacketWriter.ReleaseContent(pw); }
/// <summary> /// 检查是否有新的客户端接过来 /// </summary> private void CheckListener() { for ( int iIndex = 0; iIndex < m_Listeners.Length; iIndex++ ) { Listener listener = m_Listeners[iIndex]; // 获取连接的客户端 ClientSocketManager[] acceptedManager = listener.Slice(); for ( int iIndex2 = 0; iIndex2 < acceptedManager.Length; iIndex2++ ) { ClientSocketManager clientSocketManager = acceptedManager[iIndex2]; if ( clientSocketManager == null ) continue; // 把连接过来的客户端放置入NetState中,当前的实例会保存在NetState.Instances中 NetState netState = new NetState( clientSocketManager, this ); OnNetStateInit( netState ); netState.Start(); } } }
void OnReadTest(NetState netstate, PacketReader reader) { module.OnReadTest(netstate, reader); }
/// <summary> /// 客户端登陆服务器,采用简单模式,就只有2个参数 /// </summary> /// <param name="netstate"></param> /// <param name="accountName"></param> /// <param name="password"></param> /// <param name="serverId"></param> public void OnLoginServer(NetState netstate, string accountName, string password, int serverId = 0) { var account = WorldEntityManager.AccountNames.GetValue(accountName); if (account == null) { // 账号不存在,则创建账号 account = new Account { Name = accountName, Password = password, CreateTime = OneServer.NowTime, ServerId = serverId, Id = WorldSeqGen.AccountSeq.NextSeq(), }; WorldEntityManager.AccountNames.SetValue(accountName, account); WorldEntityManager.Accounts.SetValue(account.Id, account); // 保存到数据,demo阶段,已xml的方式写入文件里,减少对第三方系统的依赖 DB.GameDB.InsertEntity(account); } else { if (account.Password != password) { // 账户存在,但是密码验证失败,返回错误 ClientProxy.Login.LoginServerResult(netstate, LoginServerResult.PassOrAccountError); return; } } netstate.BizId = account.Id; netstate.IsVerifyLogin = true; // 先瞅瞅内存里玩家数据有没有被缓存着 var player = WorldEntityManager.PlayerCache.GetEntity(account.Id); if (player == null) { player = DB.GameDB.LoadEntity<Player>(account.Id); if(player != null) WorldEntityManager.PlayerCache.AddOrReplace(player); } var isCreatePlayer = player != null; ClientProxy.Login.LoginServerResult(netstate, LoginServerResult.Success, isCreatePlayer); if (isCreatePlayer) { if (player.NetState != null) { // 玩家重复登陆,需要将之前的玩家连接关闭 var oldCon = player.NetState; oldCon.IsVerifyLogin = false; oldCon.Player = null; oldCon.BizId = 0; oldCon.NetSocket.CloseSocket(); } // 给玩家绑定网络连接账号 player.NetState = netstate; netstate.Player = player; netstate.IsVerifyLogin = true; // 创建过角色,这里就发送玩家的数据到客户端 PlayerEnterGame(player); } }
/// <summary> /// 检查是否有新的客户端接出来 /// </summary> private void CheckConnecter() { for ( int iIndex = 0; iIndex < m_Connecters.Length; iIndex++ ) { Connecter connecter = m_Connecters[iIndex]; // 检查是否已经处理过 if ( connecter.IsNeedSlice == false ) continue; // 获取连接出去的客户端 ClientSocketManager clientSocketManager = connecter.Slice(); if ( clientSocketManager != null ) { // 把连接出来的客户端放置入NetState中,当前的实例会保存在NetState.Instances中 NetState netState = new NetState( clientSocketManager, this ); OnNetStateInit( netState ); netState.Start(); } } }