예제 #1
0
 void StartLogin()
 {
     using (var builder = MessageBuilder.Get())
     {
         FlatBufferBuilder fbb = builder.fbb;
         var nameOffset        = fbb.CreateString(game.playerName);
         Msg_CS_Login.StartMsg_CS_Login(fbb);
         Msg_CS_Login.AddName(fbb, nameOffset);
         Msg_CS_Login.AddColor(fbb, UnityEngine.Random.ColorHSV().ToInt());
         fbb.Finish(Msg_CS_Login.EndMsg_CS_Login(fbb).Value);
         var msg = game.netlayer.CreateMessage(MessageID.Msg_CS_Login, fbb);
         game.netlayer.SendMessage(msg, NetDeliveryMethod.ReliableOrdered);
     }
 }
        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);
        }