Example #1
0
        private UDPFeed OpenIncomingConnection(IPEndPoint destination)
        {
            UDPFeed feed = new UDPFeed(this, destination, true);

            feed.Compression = Compression;
            Feeds.Add(feed);
            IncomingConnections += 1;
            return(feed);
        }
Example #2
0
        public NetworkConnection OpenConnection(IPEndPoint destination)
        {
            UDPFeed feed = new UDPFeed(this, destination, false);

            feed.Compression = Compression;
            Feeds.Add(feed);
            OutgoingConnections += 1;
            return(feed);
        }
Example #3
0
 internal void CloseFeed(UDPFeed feed)
 {
     if (Feeds.Remove(feed))
     {
         if (feed.IsIncoming)
         {
             IncomingConnections -= 1;
         }
         else
         {
             OutgoingConnections -= 1;
         }
     }
 }
Example #4
0
        public NetworkConnection RecieveConnection()
        {
            if (IncomingConnections >= IncomingConnectionLimit)
            {
                // We cannot allocate any additional incoming connections. Just flush em.
                UnallocatedIncomingData.Clear();
                return(null);
            }

            FlushRecieve();

            UDPFeed feed = null;
            List <AddressedData> remainingIncoming = null;

            foreach (AddressedData incoming in UnallocatedIncomingData)
            {
                if (feed == null)
                {
                    byte[] data = incoming.Data;
                    if (Compression)
                    {
                        data = Compress.Enflate(data);
                    }

                    // Attempt to decode the packet.
                    if (data.Length < (Packet.HeaderSize + Payload.HeaderSize))
                    {
                        continue;
                    }

                    HandshakePayload req = Packet.Peek <HandshakePayload>(data);
                    if (req != null && req.State == NetworkClient.ConnectionState.Opening)
                    {
                        feed = OpenIncomingConnection(incoming.Source);
                        feed.FeedData(incoming.Data); // This data belongs to the new feed.

                        // We will start putting other packets in here for next round.
                        remainingIncoming = new List <AddressedData>();
                        break;
                    }
                }
                else
                {
                    if (!incoming.Source.Equals(feed.Destination))
                    {
                        // The remaining items should be added to the
                        remainingIncoming.Add(incoming);
                    }
                    else
                    {
                        feed.FeedData(incoming.Data);
                    }
                }
            }

            if (remainingIncoming != null)
            {
                UnallocatedIncomingData = remainingIncoming;
            }
            else
            {
                UnallocatedIncomingData.Clear();
            }

            return(feed); // This may be null
        }