Esempio n. 1
0
 public void Initialize()
 {
     tickrate            = AppConfig.Instance.tickrate;
     updaterate          = AppConfig.Instance.updaterate;
     inputchoke          = Mathf.Max(1, Mathf.CeilToInt(AppConfig.Instance.cmdrate / tickrate));
     mSnapshotOverTick   = (uint)Mathf.FloorToInt(updaterate / tickrate);
     Time.fixedDeltaTime = tickrate;
     TSLog.InfoFormat("tickrate:{0}, updaterate:{1}, ss/t:{2}", tickrate, updaterate, mSnapshotOverTick);
 }
Esempio n. 2
0
 public void OnNewConnection(NetConnection connection)
 {
     mAuthingConnections.Add(new AuthingConnection()
     {
         timer      = authTimeout,
         connection = connection,
     });
     TSLog.InfoFormat("incoming connection:{0}", connection.RemoteEndPoint);
 }
Esempio n. 3
0
 void Update()
 {
     for (int i = 0; i < mAuthingConnections.Count;)
     {
         AuthingConnection uc = mAuthingConnections[i];
         if ((uc.timer -= Time.deltaTime) <= 0f)
         {
             TSLog.InfoFormat("remove auth timeout connection:{0}", uc.connection.RemoteEndPoint);
             uc.connection.Disconnect("auth timeout");
             mAuthingConnections.RemoveAt(i);
         }
         else
         {
             ++i;
         }
     }
 }
        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);
        }