MessageHandleResult LoginHandler( NetConnection connection, ByteBuffer byteBuffer, NetIncomingMessage message) { Msg_SC_Login msg = InstancePool.Get <Msg_SC_Login>(); Msg_SC_Login.GetRootAsMsg_SC_Login(byteBuffer, msg); if (!msg.Success) { GameStateLog.Error("Login failed:" + msg.Error); TransitTo <Error>(msg.Error); } else { game.id = msg.Id; GameStateLog.Info("Login success, game id:" + msg.Id); Msg_SC_UpdatePlayers updateplayers = InstancePool.Get <Msg_SC_UpdatePlayers>(); msg.GetPlayers(updateplayers); PlayerManagerClient.Instance.UpdatePlayers(updateplayers); GameStateLog.Info("Update players"); TransitTo <DataFullUpdate>(); } return(MessageHandleResult.Finished); }
MessageHandleResult LoginHandler( NetConnection connection, ByteBuffer byteBuffer, NetIncomingMessage message) { bool success = false; string error = null; int id = 0; Player player = Get(connection); if (null != player) { error = string.Format("player[{0},{1}] has already been auth", player.id, player.playerName); TSLog.ErrorFormat(error); } else { int index = mAuthingConnections.FindIndex(v => v.connection == connection); if (index < 0) { error = string.Format("connection[{0}] is not found in waiting list", connection.RemoteEndPoint); TSLog.ErrorFormat(error); } else { mAuthingConnections.RemoveAt(index); Msg_CS_Login msg = InstancePool.Get <Msg_CS_Login>(); Msg_CS_Login.GetRootAsMsg_CS_Login(byteBuffer, msg); Color color = (new Color()).FromInt(msg.Color); Player newPlayer = Player.New(mIdGen.Alloc(), msg.Name, color, connection); mPlayers.Add(newPlayer); success = true; id = newPlayer.id; TSLog.InfoFormat("new player[{0},{1}]", newPlayer.id, newPlayer.playerName); } } using (var builder = MessageBuilder.Get()) { var fbb = builder.fbb; StringOffset errorOffset = default(StringOffset); Offset <Msg_SC_UpdatePlayers> playersOffset = default(Offset <Msg_SC_UpdatePlayers>); if (success) { playersOffset = SyncPlayerList(fbb); } else { errorOffset = fbb.CreateString(error); } Msg_SC_Login.StartMsg_SC_Login(fbb); Msg_SC_Login.AddSuccess(fbb, success); if (success) { Msg_SC_Login.AddId(fbb, id); } else { Msg_SC_Login.AddError(fbb, errorOffset); } Msg_SC_Login.AddPlayers(fbb, playersOffset); fbb.Finish(Msg_SC_Login.EndMsg_SC_Login(fbb).Value); NetOutgoingMessage msg = Server.Instance.netlayer.CreateMessage(MessageID.Msg_SC_Login, fbb); connection.SendMessage(msg, NetDeliveryMethod.ReliableOrdered, 0); } if (success) { FlatBufferBuilder fbb = new FlatBufferBuilder(64); fbb.Finish(SyncNewPlayer(fbb, id).Value); foreach (var p in mPlayers) { if (p.connection != connection) { NetOutgoingMessage msg = Server.Instance.netlayer.CreateMessage(MessageID.Msg_SC_UpdatePlayers, fbb); p.connection.SendMessage(msg, NetDeliveryMethod.ReliableOrdered, 0); } } } return(MessageHandleResult.Finished); }