protected void SendClientInfo()
        {
            var   m       = OutboundMessage.CreateReliable((byte)EngineMessage.InitialData, false, SequenceChannel.System);
            short numVars = 0;
            var   allVars = Variable.GetEnumerable();

            foreach (var v in allVars)
            {
                if (v.HasFlags(VariableFlags.Client))
                {
                    numVars++;
                }
            }
            m.Write(numVars);

            foreach (var v in allVars)
            {
                if (v.HasFlags(VariableFlags.Client))
                {
                    m.Write(v.Name);
                    m.Write(v.Value);
                }
            }
            Send(m);
        }
Exemple #2
0
        protected virtual bool MessageReceived(Client c, InboundMessage m)
        {
            OutboundMessage o;

            switch ((EngineMessage)m.Type)
            {
            case EngineMessage.InitialData:
            {
                short num = m.ReadShort();
                for (int i = 0; i < num; i++)
                {
                    c.SetVariable(m.ReadString(), m.ReadString());
                }

                string name = c.Name;

                // tell all other clients about this new client
                o = OutboundMessage.CreateReliable((byte)EngineMessage.ClientConnected, false, SequenceChannel.System);
                o.Write(name);
                Client.SendToAllExcept(o, c);

                ClientConnected(c);

                // send ServerInfo to the newly-connected client, which as well as the network table hash,
                // tells them how/if we've modified their name, the names of everyone else on the server, and all the server variables

                // is it worth having name be a variable here?

                // how would name change work?
                // when you try to change the name, it always fails, but sends a "name change" to the server
                // that sends a "name change" to everyone else, and a special one back to you that actaully updates the variable
                o = OutboundMessage.CreateReliable((byte)EngineMessage.InitialData, false, SequenceChannel.System);
                o.Write(NetworkedEntity.NetworkTableHash);
                o.Write(CurrentTick);
                o.Write(c.Name);

                List <Client> otherClients = Client.GetAllExcept(c);
                o.Write((byte)otherClients.Count);
                foreach (Client other in otherClients)
                {
                    o.Write(other.Name);
                }

                ushort numVars = 0;
                var    allVars = Variable.GetEnumerable();

                foreach (var v in allVars)
                {
                    if (v.HasFlags(VariableFlags.Server))
                    {
                        numVars++;
                    }
                }
                o.Write(numVars);

                foreach (var v in allVars)
                {
                    if (v.HasFlags(VariableFlags.Server))
                    {
                        o.Write(v.Name);
                        o.Write(v.Value);
                    }
                }

                c.Send(o);
                c.NextSnapshotTime = FrameTime + c.SnapshotInterval;
                c.FullyConnected   = true;
                return(true);
            }

            case EngineMessage.ClientNameChange:
            {
                string oldName = c.Name;
                string name    = m.ReadString();
                c.Name = name;
                name   = c.Name;       // if it wasn't unique, it'll have been changed

                // tell everyone else about the change
                o = OutboundMessage.CreateReliable((byte)EngineMessage.ClientNameChange, false, SequenceChannel.Chat);
                o.Write(name);
                o.Write(false);
                o.Write(oldName);
                Client.SendToAllExcept(o, c);

                // tell this client their "tidied" name
                o = OutboundMessage.CreateReliable((byte)EngineMessage.ClientNameChange, false, SequenceChannel.Chat);
                o.Write(name);
                o.Write(true);
                c.Send(o);

                if (IsDedicated)
                {
                    Console.WriteLine("{0} changed name to {1}", oldName, name);
                }

                return(true);
            }

            case EngineMessage.VariableChange:
            {
                string name = m.ReadString();
                string val  = m.ReadString();

                c.SetVariable(name, val);
                return(true);
            }

            case EngineMessage.ClientUpdate:
            {
                if (!c.FullyConnected)
                {
                    return(true);        // this might come through before the InitialData message
                }
                c.LastAcknowledgedTick = m.ReadUInt();
                UpdateReceived(c, m);
                return(true);
            }

            default:
                return(false);
            }
        }