Exemplo n.º 1
0
        /// <summary>
        ///     Converts this grid into a format that can be sent as a packet.
        /// </summary>
        /// <returns>Packet that can be sent to recreate this zone grid on another peer.</returns>
        public ZoneGridPacket ToPacket()
        {
            ZoneGridPacket packet = new ZoneGridPacket();

            packet.Zones = new ZoneGridPacketZoneInfo[m_zones.Count];
            for (int i = 0; i < m_zones.Count; i++)
            {
                Zone zone = m_zones[m_zones.Keys.ToArray()[i]];

                packet.Zones[i] = new ZoneGridPacketZoneInfo();
                packet.Zones[i].ID = zone.ID;
                packet.Zones[i].ParentID = zone.ParentID;
                packet.Zones[i].ChildZone1ID = zone.ChildZone1ID;
                packet.Zones[i].ChildZone2ID = zone.ChildZone2ID;
                packet.Zones[i].SplitOrientation = zone.SplitOrientation;

                packet.Zones[i].SuperPeers = new ZoneGridPacketSuperPeerInfo[zone.SuperPeers.Count];
                for (int k = 0; k < zone.SuperPeers.Count; k++)
                {
                    ZoneSuperPeer peer = zone.SuperPeers[k];

                    packet.Zones[i].SuperPeers[k].ID                = peer.ID;
                    packet.Zones[i].SuperPeers[k].ZoneID            = peer.ZoneID;
                    packet.Zones[i].SuperPeers[k].ClientID          = peer.ClientID;
                    packet.Zones[i].SuperPeers[k].ClientIPAddress   = peer.ClientIPAddress;
                    packet.Zones[i].SuperPeers[k].ClientListenPort  = peer.ClientListenPort;
                }
            }

            return packet;
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Converts a packet representation of this zone grid into the zone grid.
        /// </summary>
        /// <param name="packet">Packet to convert.</param>
        public void FromPacket(ZoneGridPacket packet)
        {
            List<ZoneSuperPeer> old_superpeers = new List<ZoneSuperPeer>();
            List<ZoneSuperPeer> new_superpeers = new List<ZoneSuperPeer>();
            List<Zone> old_zones = new List<Zone>(m_zones.Values);

            // Make a full list of all super peers that currently exist.
            foreach (Zone zone in m_zones.Values)
            {
                old_superpeers.AddRange(zone.SuperPeers);
            }

            // Dispose of general stuff.
            m_zones.Clear();
            m_gained_superpeers.Clear();
            m_lost_superpeers.Clear();

            // Rebuild the zone grid.
            foreach (ZoneGridPacketZoneInfo zoneInfo in packet.Zones)
            {
                Zone zone = null;

                foreach (Zone z in old_zones)
                {
                    if (z.ID == zoneInfo.ID)
                    {
                        zone = z;
                        zone.ParentID = zoneInfo.ParentID;
                        zone.ChildZone1ID = zoneInfo.ChildZone1ID;
                        zone.ChildZone2ID = zoneInfo.ChildZone2ID;
                        zone.Parent = null;
                        zone.ChildZone1 = null;
                        zone.ChildZone2 = null;
                        zone.SplitOrientation = zoneInfo.SplitOrientation;
                        break;
                    }
                }

                if (zone == null)
                {
                    zone = new Zone(zoneInfo.ID, zoneInfo.ParentID, zoneInfo.ChildZone1ID, zoneInfo.ChildZone2ID, zoneInfo.SplitOrientation);
                }

                List<ZoneSuperPeer> oldZoneSuperPeers = new List<ZoneSuperPeer>(zone.SuperPeers);
                zone.SuperPeers.Clear();

                foreach (ZoneGridPacketSuperPeerInfo peerInfo in zoneInfo.SuperPeers)
                {
                    ZoneSuperPeer peer = null;

                    foreach (ZoneSuperPeer p in oldZoneSuperPeers)
                    {
                        if (p.ID == peerInfo.ID)
                        {
                            peer = p;
                            break;
                        }
                    }

                    if (peer == null)
                    {
                        peer = new ZoneSuperPeer();
                    }

                    peer.ID                 = peerInfo.ID;
                    peer.ZoneID             = peerInfo.ZoneID;
                    peer.ClientID           = peerInfo.ClientID;
                    peer.ClientIPAddress    = peerInfo.ClientIPAddress;
                    peer.ClientListenPort   = peerInfo.ClientListenPort;

                    zone.SuperPeers.Add(peer);
                }

                new_superpeers.AddRange(zone.SuperPeers);

                AddZone(zone);
            }

            // Calculate which super-peers are new.
            foreach (ZoneSuperPeer peer in old_superpeers)
            {
                bool found = false;
                foreach (ZoneSuperPeer peer2 in new_superpeers)
                {
                    if (peer.ZoneID == peer2.ZoneID &&
                        peer.ClientID == peer2.ClientID)
                    {
                        found = true;
                        break;
                    }
                }

                if (found == false)
                {
                    m_lost_superpeers.Add(peer);
                }
            }

            // Calculate which super-peers have been list.
            foreach (ZoneSuperPeer peer in new_superpeers)
            {
                bool found = false;
                foreach (ZoneSuperPeer peer2 in old_superpeers)
                {
                    if (peer.ZoneID == peer2.ZoneID &&
                        peer.ClientID == peer2.ClientID)
                    {
                        found = true;
                        break;
                    }
                }

                if (found == false)
                {
                    m_gained_superpeers.Add(peer);
                }
            }
        }