Example #1
0
        public void NetworkDestroy(ushort id)
        {
            NetCreatableInfo creatableInfo;

            if (netObjects.Creatables.TryGetValue(id, out creatableInfo))
            {
                foreach (NetConnectionSnapshotState state in snapshotComponent.ConnectionStates.Values)
                {
                    state.WorldSnapshot.NetEntityListSnapshot.RemoveNetEntitiy(id);
                }

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

                if (OnCreatableDestroyed != null)
                {
                    OnCreatableDestroyed(this, creatableInfo);
                }
            }

            instPackets.Remove(id);

            NetOutboundPacket packet = new NetOutboundPacket(NetDeliveryMethod.Reliable);

            packet.Write((byte)CustomPacketType.Destroy);
            packet.Write(id);

            server.SendPacketToAll(packet);
        }
        void InitiateTerrainTransfer()
        {
            DashCMD.WriteStandard("[HS] Initiating handshake with {0}...", With);

            NetOutboundPacket initPacket = new NetOutboundPacket(NetDeliveryMethod.ReliableOrdered);

            initPacket.Write((byte)CustomPacketType.HandshakeInitiate);

            int packetSize = With.Stats.MTU;

            ThreadPool.QueueUserWorkItem((obj) =>
            {
                terrainData = new HandshakeTerrainData(world.Terrain, packetSize);
                initPacket.Write((ushort)terrainData.Sections.Length);
                initPacket.Write(terrainData.TotalPacketSize);
                initPacket.Write(terrainData.UncompressedSize);

                DashCMD.WriteStandard(
                    "[HS] Prepared terrain packet for {0}. Sections: {1} ({2} bytes max each), Total Size: {3} bytes",
                    With, terrainData.Sections.Length, packetSize, terrainData.TotalPacketSize);

                With.SendPacket(initPacket);
                SendNextTerrainChunk();
            });
        }
Example #3
0
        public void Complete()
        {
            DashCMD.WriteStandard("[HS] Completed handshake with {0}.", client.ServerConnection);

            NetOutboundPacket completePacket = new NetOutboundPacket(NetDeliveryMethod.ReliableOrdered);

            completePacket.Write((byte)CustomPacketType.HandshakeComplete);

            client.SendPacket(completePacket);

            screen.OnHandshakeComplete();
        }
        public void OnServerOutbound(NetOutboundPacket packet, NetConnectionSnapshotState state)
        {
            // Write each player to the snapshot
            WorldSnapshot worldSnapshot = state.WorldSnapshot;

            foreach (ServerMPPlayer plr in players.Values)
            {
                PlayerSnapshot playerSnapshot;
                if (worldSnapshot.TryGetPlayer(plr.StateInfo.Id, out playerSnapshot))
                {
                    plr.OnServerOutbound(playerSnapshot);
                }
            }
        }
Example #5
0
        public override void Update(float deltaTime)
        {
            if (measuringRTT)
            {
                timeSinceSend += deltaTime;
            }

            bool svForceAwait = WorldSnapshot != null
                ? WorldSnapshot.ForceSnapshotAwait : true;
            bool gotServerSnapshot = gotPacket ||
                                     (!DashCMD.GetCVar <bool>("cl_await_sv_snap") && !svForceAwait);

            if (syncTime <= 0 && gotServerSnapshot || syncTime <= -1)
            {
                syncTime += tickrate;
                gotPacket = false;

                // Create and send client state snapshot
                NetOutboundPacket packet = new NetOutboundPacket(NetDeliveryMethod.Unreliable);
                packet.SendImmediately = true;
                int size = packet.Length;
                packet.Write((byte)CustomPacketType.Snapshot);
                packet.Write(pid++);

                lastOutboundPacketStats.PacketHeader = packet.Length - size; size = packet.Length;

                // Write snapshot delta data
                snapshotSystem.OnOutbound(packet, client.ServerConnection);
                lastOutboundPacketStats.Acks = packet.Length - size; size = packet.Length;

                // Write player data
                charSnapshotSystem.OnClientOutbound(packet);
                lastOutboundPacketStats.PlayerData = packet.Length - size; size = packet.Length;

                // Send packet
                client.SendPacket(packet);

                timeSinceSend = 0;
                measuringRTT  = true;
            }
            else
            {
                syncTime -= deltaTime;
            }

            base.Update(deltaTime);
        }
Example #6
0
        public void OnClientOutbound(NetOutboundPacket packet)
        {
            if (ourPlayer != null)
            {
                packet.Write(true);
                packet.Write(ourPlayer.StateInfo.Id);

                // Grab current client snapshot
                ourPlayer.OnClientOutbound(snapshotComponent.SnapshotRoundTripTime);
                ourPlayer.ClientSnapshot.Serialize(packet);
                ourPlayer.OnPostClientOutbound();
            }
            else
            {
                // Notify the server that we don't have any player information
                // to send.
                packet.Write(false);
            }
        }
        public void OnOutbound(NetOutboundPacket packet, NetConnection connection)
        {
            // For each snapshot with delta support,
            // we will write every snapshot version
            // that was acknowledged.

            SnapshotConnectionInterface sci = GetOrCreateInterface(connection);

            packet.Write((ushort)sci.InboundDeltaSnapshots.Count);
            foreach (KeyValuePair <ushort, Snapshot> pair in sci.InboundDeltaSnapshots)
            {
                packet.Write(pair.Key);
                packet.Write((ushort)pair.Value.AcknowledgedDeltaIds.Count);

                foreach (byte deltaId in pair.Value.AcknowledgedDeltaIds)
                {
                    packet.Write(deltaId);
                }

                pair.Value.AcknowledgedDeltaIds.Clear();
            }
        }
        bool SendNextTerrainChunk()
        {
            if (terrainDataI == terrainData.Sections.Length)
            {
                // All done
                return(true);
            }
            else
            {
                NetOutboundPacket terrainPacket = new NetOutboundPacket(NetDeliveryMethod.ReliableOrdered);
                terrainPacket.Write((byte)CustomPacketType.WorldSection);

                byte[] data = terrainData.Sections[terrainDataI];
                terrainPacket.Write((ushort)data.Length);
                terrainPacket.WriteBytes(data);

                With.SendPacket(terrainPacket);

                terrainDataI++;
                return(false);
            }
        }
Example #9
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);
            }
        }
Example #10
0
        public void NetworkInstantiate(INetCreatable creatable, string instEventName,
                                       NetConnection clientOwner, params object[] args)
        {
            ushort netId = lastNetEntId++;

            if (netId == 0)
            {
                netId++;
            }

            NetCreatableInfo info = new NetCreatableInfo(clientOwner, creatable, netId, true);

            INetEntity entity = creatable as INetEntity;

            if (entity != null)
            {
                foreach (NetConnectionSnapshotState state in snapshotComponent.ConnectionStates.Values)
                {
                    state.WorldSnapshot.NetEntityListSnapshot.AddNetEntity(info, entity);
                }
            }

            creatable.OnNetworkInstantiated(info);

            if (OnCreatableInstantiated != null)
            {
                OnCreatableInstantiated(this, info);
            }

            netObjects.Add(netId, info);

            foreach (NetConnection conn in server.Connections.Values)
            {
                NetOutboundPacket packet = new NetOutboundPacket(NetDeliveryMethod.Reliable);
                packet.Write((byte)CustomPacketType.Instantiate);
                packet.Write(instEventName);
                packet.Write(netId);
                packet.Write(conn == clientOwner);

                for (int i = 0; i < args.Length; i++)
                {
                    packet.WriteDynamic(args[i]);
                }

                conn.SendPacket(packet);
            }

            NetOutboundPacket epacket = new NetOutboundPacket(NetDeliveryMethod.Reliable);

            epacket.Write((byte)CustomPacketType.Instantiate);
            epacket.Write(instEventName);
            epacket.Write(netId);
            epacket.Write(false);

            for (int i = 0; i < args.Length; i++)
            {
                epacket.WriteDynamic(args[i]);
            }

            instPackets.Add(netId, epacket);
        }
        void SendSnapshotTo(NetConnection conn, NetConnectionSnapshotState connState, float deltaTime)
        {
            WorldSnapshot worldSnapshot = connState.WorldSnapshot;

            ushort epid = connState.OutboundSnapshotId;

            connState.OutboundSnapshotId++;

            //connState.TimeSinceLastSend -= deltaTime;
            //connState.GotPacket = false;

            connState.WorldSnapshot.MaxClientTickrate  = DashCMD.GetCVar <ushort>("ag_max_cl_tickrate");
            connState.WorldSnapshot.ForceSnapshotAwait = DashCMD.GetCVar <bool>("ag_cl_force_await_snap");

            NetOutboundPacket packet = new NetOutboundPacket(NetDeliveryMethod.Unreliable);

            packet.SendImmediately = true;
            int size = packet.Length;

            packet.Write((byte)CustomPacketType.Snapshot);
            packet.Write(epid);
            int _packetheader = packet.Length - size; size = packet.Length;

            // Write snapshot system data
            snapshotSystem.OnOutbound(packet, conn);
            int _acks = packet.Length - size; size = packet.Length;

            // Write players
            charSnapshotSystem.OnServerOutbound(packet, connState);

            // Invoke event
            if (OnWorldSnapshotOutbound != null)
            {
                OnWorldSnapshotOutbound(this, worldSnapshot);
            }

            // Serialize snapshot
            NetBuffer buffer = new NetBuffer();

            worldSnapshot.Serialize(buffer);

            packet.Write((ushort)buffer.Length);
            packet.WriteBytes(buffer.Data, 0, buffer.Length);


            int _playerdata  = packet.Length - size; size = packet.Length;
            int _terraindata = connState.WorldSnapshot.TerrainSnapshot.LastByteSize;

            _playerdata -= _terraindata;

            // Send packet
            conn.SendPacket(packet);

            if (connState != null)
            {
                SnapshotStats stats = connState.Stats;
                stats.PacketHeader = _packetheader;
                stats.Acks         = _acks;
                stats.PlayerData   = _playerdata;
                stats.TerrainData  = _terraindata;
            }
        }