示例#1
0
        public Handshake(AOSClient client, MultiplayerScreen screen, NetInboundPacket packet)
        {
            this.client = client;
            this.screen = screen;

            Start(packet);
        }
        public void OnServerInbound(NetInboundPacket packet, NetConnectionSnapshotState state)
        {
            bool playerSentData = packet.ReadBool();

            if (!playerSentData)
            {
                return;
            }

            ushort entId = packet.ReadUInt16();

            ServerMPPlayer player;

            if (players.TryGetValue(entId, out player))
            {
                // Read packet to snapshot
                player.ClientSnapshot.Deserialize(packet);
                // Update player
                player.OnServerInbound();
            }
            else if (DashCMD.GetCVar <bool>("log_css"))
            {
                DashCMD.WriteError("[CSS] Received client snapshot for player with id {0}, which does not exist!", entId);
            }
        }
        public void OnInbound(NetInboundPacket packet)
        {
            SnapshotConnectionInterface sci = GetOrCreateInterface(packet.Sender);

            ushort numDeltaSnapshots = packet.ReadUInt16();

            for (int i = 0; i < numDeltaSnapshots; i++)
            {
                ushort snapshotId = packet.ReadUInt16();
                ushort numAcks    = packet.ReadUInt16();

                Snapshot snapshot;
                if (sci.TryGetDeltaSnapshot(snapshotId, true, out snapshot))
                {
                    for (int k = 0; k < numAcks; k++)
                    {
                        snapshot.Acknowledge(packet.ReadByte());
                    }
                }
                else
                {
                    packet.Position += numAcks * 1;
                }
            }
        }
        public override bool HandlePacket(NetInboundPacket packet, CustomPacketType type)
        {
            if (type == CustomPacketType.Snapshot)
            {
                ushort pid = packet.ReadUInt16();
                NetConnectionSnapshotState connState;
                if (ConnectionStates.TryGetValue(packet.Sender, out connState))
                {
                    //connState.GotPacket = true;
                    if (connState.MeasuringRTT)
                    {
                        connState.MeasuringRTT  = false;
                        connState.RoundTripTime = Interpolation.Linear(connState.RoundTripTime,
                                                                       connState.RTT_TimeSinceLastSend, 0.15f);
                    }

                    ushort ppid = connState.LastInboundSnapshotId;
                    connState.LastInboundSnapshotId = pid;
                    if (pid <= ppid && pid != 0)
                    {
                        DashCMD.WriteWarning("[SnapshotNC] Dropping late client snapshot...");
                        return(true);
                    }

                    snapshotSystem.OnInbound(packet);
                    charSnapshotSystem.OnServerInbound(packet, connState);
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
示例#5
0
        public void Update(float deltaTime)
        {
            // Update internal messenger
            base.Update();

            // Read packets
            for (int i = 0; i < 1000 && AvailablePackets > 0; i++)
            {
                NetInboundPacket packet = ReadPacket();

                if (packet.Position >= packet.Length)
                {
                    DashCMD.WriteError("[AOSServer] Received invalid custom packet from {0}! (bad packet position)",
                                       packet.Sender);
                }
                else
                {
                    CustomPacketType type = (CustomPacketType)packet.ReadByte();

                    // Try and handle the packet
                    if (!HandlePacket(packet, type))
                    {
                        DashCMD.WriteWarning("[AOSServer] Received unknown custom packet {0}, from {1}",
                                             type, packet.Sender);
                    }
                }
            }

            // Update each component
            foreach (NetComponent c in components.Values)
            {
                c.Update(deltaTime);
            }
        }
示例#6
0
        public override bool HandlePacket(NetInboundPacket packet, CustomPacketType type)
        {
            if (type == CustomPacketType.Instantiate)
            {
                if (HoldInstantiationPackets)
                {
                    heldInstantiationPackets.Enqueue(packet);
                }
                else
                {
                    HandleInstantiationPacket(packet);
                }
            }
            else if (type == CustomPacketType.Destroy)
            {
                if (HoldDestroyPackets)
                {
                    heldDestroyPackets.Enqueue(packet);
                }
                else
                {
                    HandleDestroyPacket(packet);
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }
示例#7
0
        void HandleDestroyPacket(NetInboundPacket packet)
        {
            ushort           id = packet.ReadUInt16();
            NetCreatableInfo creatableInfo;

            if (netObjects.Creatables.TryGetValue(id, out creatableInfo))
            {
                //DashCMD.WriteLine("[ObjectNC] Destroying creatable with id {0}...", id);

                NetEntityListSnapshot entList = snapshotComponent.WorldSnapshot.NetEntityListSnapshot;
                entList.RemoveNetEntitiy(id);

                netObjects.Remove(id);
                creatableInfo.Creatable.OnNetworkDestroy();

                if (OnCreatableDestroyed != null)
                {
                    OnCreatableDestroyed(this, creatableInfo);
                }
            }
            else
            {
                DashCMD.WriteError("[ObjectNC] Received destroy for unknown creatable, id: {0}", id);
            }
        }
        bool OnCustomPacket(NetInboundPacket packet, CustomPacketType type)
        {
            if (type == CustomPacketType.HandshakeComplete || type == CustomPacketType.WorldSectionAck)
            {
                handshakeComponent.OnPacketInbound(packet, type);
                return(true);
            }

            return(false);
        }
 public void OnPacketInbound(NetInboundPacket packet, CustomPacketType type)
 {
     if (type == CustomPacketType.HandshakeComplete)
     {
         // Were all done here
         component.HandshakeCompleted(this);
     }
     else if (type == CustomPacketType.WorldSectionAck)
     {
         SendNextTerrainChunk();
     }
 }
示例#10
0
        void Start(NetInboundPacket packet)
        {
            DashCMD.WriteStandard("[HS] Started handshake with {0}...", client.ServerConnection);

            ushort numTerrainSections = packet.ReadUInt16();

            terrainDataFullSize     = packet.ReadInt32();
            terrainUncompressedSize = packet.ReadInt32();

            terrainData = new byte[numTerrainSections][];
            DashCMD.WriteStandard("[HS] Awaiting {0} terrain data sections...", numTerrainSections);
        }
示例#11
0
        public override bool HandlePacket(NetInboundPacket packet, CustomPacketType type)
        {
            if (type == CustomPacketType.Snapshot)
            {
                if (measuringRTT)
                {
                    rtt          = Interpolation.Linear(rtt, timeSinceSend, 0.15f);
                    measuringRTT = false;
                }

                gotPacket = true;

                ushort pid = packet.ReadUInt16();

                if (pid <= lastServerPId && pid != 0)
                {
                    DashCMD.WriteWarning("[SnapshotNC] Dropping late server snapshot...");
                    return(true);
                }

                snapshotSystem.OnInbound(packet);

                ushort snapshotLength = packet.ReadUInt16();

                if (WorldSnapshot != null)
                {
                    // Read the snapshot
                    WorldSnapshot.Deserialize(packet);

                    // Update players
                    charSnapshotSystem.OnClientInbound(WorldSnapshot);

                    // Invoke event
                    if (OnWorldSnapshotInbound != null)
                    {
                        OnWorldSnapshotInbound(this, WorldSnapshot);
                    }
                }
                else
                {
                    packet.Position += snapshotLength;
                    DashCMD.WriteWarning("[SnapshotNC] Received snapshot before worldsnapshot was intialized!");
                }

                lastServerPId = pid;

                return(true);
            }
            else
            {
                return(false);
            }
        }
示例#12
0
        public void OnPacketInbound(NetInboundPacket packet, CustomPacketType type)
        {
            Handshake h;

            if (handshakes.TryGetValue(packet.Sender, out h))
            {
                h.OnPacketInbound(packet, type);
            }
            else
            {
                DashCMD.WriteError("[HS] Received handshake completion packet, but connection {0} is not in a handshake!",
                                   packet.Sender);
            }
        }
        bool OnCustomPacket(NetInboundPacket packet, CustomPacketType type)
        {
            if (type == CustomPacketType.HandshakeInitiate)
            {
                if (handshake != null)
                {
                    DashCMD.WriteError("Got handshake initiate packet, but we are already in a handshake!");
                    return(false);
                }

                // Begin the handshake on our end
                handshake = new Handshake(client, this, packet);
                loadingBar.SetHandshake(handshake);

                // We don't want to have the user staring at a blank skybox,
                // so lets show them pictures :D
                StaticGui.ShowBackground = true;
                return(true);
            }
            else if (type == CustomPacketType.WorldSection)
            {
                if (handshake != null)
                {
                    // Notify the handshake we have received another
                    // piece of the world data.
                    handshake.OnLevelChunkInbound(packet);
                }
                else
                {
                    DashCMD.WriteError("Got handshake world section packet, but we are not in a handshake!");

                    // We did acknowledge the packet,
                    // but since we are not in the right state
                    // act as if its unknown (little extra protection
                    // from a rogue server).
                    return(false);
                }

                return(true);
            }

            return(false);
        }
示例#14
0
        void HandleInstantiationPacket(NetInboundPacket packet)
        {
            string eventName = packet.ReadString();
            ushort id        = packet.ReadUInt16();
            bool   isOwner   = packet.ReadBool();

            NetInstantiationCallback callback;

            if (instCallbacks.TryGetValue(eventName, out callback))
            {
                if (netObjects.Creatables.ContainsKey(id))
                {
                    DashCMD.WriteError("[ObjectNC] Creatable with id {0} is already instantiated!", id);
                    return;
                }

                //DashCMD.WriteLine("[ObjectNC] Instantiating creatable with id {0}...", id);

                INetCreatable    creatable = callback(id, isOwner, packet);
                NetCreatableInfo info      = new NetCreatableInfo(packet.Sender, creatable, id, isOwner);
                netObjects.Add(id, info);

                INetEntity entity = creatable as INetEntity;
                if (entity != null && snapshotComponent.WorldSnapshot != null)
                {
                    NetEntityListSnapshot entList = snapshotComponent.WorldSnapshot.NetEntityListSnapshot;
                    entList.AddNetEntity(info, entity);
                }

                creatable.OnNetworkInstantiated(info);

                if (OnCreatableInstantiated != null)
                {
                    OnCreatableInstantiated(this, info);
                }
            }
            else
            {
                DashCMD.WriteError("[ObjectNC] Received instantiation for unknown type: {0}", eventName);
            }
        }
示例#15
0
        bool HandlePacket(NetInboundPacket packet, CustomPacketType type)
        {
            // Give each component a chance to try and handle the packet type,
            // if none process it we've received an unknown packet.
            foreach (NetComponent c in components.Values)
            {
                if (c.HandlePacket(packet, type))
                {
                    return(true);
                }
            }

            // Attempt to defer packet to the custom handlers
            for (int i = 0; i < packetHooks.Count; i++)
            {
                if (packetHooks[i](packet, type))
                {
                    return(true);
                }
            }

            return(false);
        }
示例#16
0
        public virtual bool Deserialize(NetInboundPacket packet)
        {
            int  fieldCount = packet.ReadUInt16();
            bool readAll    = true;

            for (int i = 0; i < fieldCount; i++)
            {
                byte   fieldIndex = packet.ReadByte();
                byte   dataType   = packet.ReadByte();
                object data       = ReadField(packet, dataType);

                if (fieldIndex < fields.Count)
                {
                    fields[fieldIndex] = data;
                }
                else
                {
                    readAll = false;
                }
            }

            return(readAll);
        }
示例#17
0
        object ReadField(NetInboundPacket packet, byte type)
        {
            byte innerType;

            switch ((FieldType)type)
            {
            case FieldType.Bool:
                return(packet.ReadBool());

            case FieldType.Float:
                return(packet.ReadFloat());

            case FieldType.Byte:
                return(packet.ReadByte());

            case FieldType.ByteFlag:
                return(packet.ReadByteFlag());

            case FieldType.UInt16:
                return(packet.ReadUInt16());

            case FieldType.NestedSnapshot:
                // Nested snapshot
                byte id = packet.ReadByte();
                innerType = packet.ReadByte();
                return(ReadField(packet, innerType));

            case FieldType.Array:
                // Array
                byte index = packet.ReadByte();
                innerType = packet.ReadByte();
                return(ReadField(packet, innerType));

            default:
                throw new InvalidOperationException("Failed to read field of type id: " + type);
            }
        }
示例#18
0
        public void OnLevelChunkInbound(NetInboundPacket packet)
        {
            ushort dataLength = packet.ReadUInt16();

            terrainData[terrainDataI] = packet.ReadBytes(dataLength);

            terrainDataRead += dataLength;

            if (OnTerrainProgressReported != null)
            {
                OnTerrainProgressReported(terrainDataRead, terrainDataFullSize);
            }

            DashCMD.WriteStandard("[HS] Received terrain data {0}/{1} bytes", terrainDataRead, terrainDataFullSize);
            terrainDataI++;

            if (terrainDataI < terrainData.Length)
            {
                // Send terrain ack to ask for next part
                NetOutboundPacket ack = new NetOutboundPacket(NetDeliveryMethod.ReliableOrdered);
                ack.Write((byte)CustomPacketType.WorldSectionAck);

                client.SendPacket(ack);
            }
            else
            {
                if (OnTerrainProgressReported != null)
                {
                    OnTerrainProgressReported(terrainDataFullSize, terrainDataFullSize);
                }

                // Uncompress the data and notify the screen we are done downloading.
                HandshakeTerrainData data = new HandshakeTerrainData(terrainData, terrainUncompressedSize);
                screen.OnHandshakeDoneDownloading(data);
            }
        }
示例#19
0
 /// <summary>
 /// Attempts to process packet with this component.
 /// Returns true if handled, false is unhandled.
 /// </summary>
 /// <returns>true if handled, false is unhandled</returns>
 public virtual bool HandlePacket(NetInboundPacket packet, CustomPacketType type)
 {
     return(false);
 }