private void recv(IAsyncResult res)
        {
            int bytesRev = 0;

            try
            {
                bytesRev = UdpSocket.EndReceive(res);
            }
            catch (ObjectDisposedException)
            {
                return;
            }
            var endpoints = MasterUtil.ProcessPacket(recvData.Take(bytesRev).ToArray());

            //ThreadPool.QueueUserWorkItem(x => Callback(endpoints));
            Callback(endpoints);
            if (!endpoints.Last().Equals(SeedEndpoint))
            {
                Msg = MasterUtil.BuildPacket(endpoints.Last().ToString(), RegionCode, Filter);
                UdpSocket.Send(Msg);
                UdpSocket.BeginReceive(recvData, 0, recvData.Length, SocketFlags.None, recv, null);
            }
            else
            {
                IsListening = false;
            }
        }
Exemplo n.º 2
0
        private ReadOnlyCollection <IPEndPoint> SendAndReceive(Socket udpSocket, byte[] recvData, Region region, IpFilter filter, IPEndPoint seed)
        {
            var msg = MasterUtil.BuildPacket(seed.ToString(), region, filter);

            udpSocket.Send(msg);

            int len = udpSocket.Receive(recvData, 0, recvData.Length, SocketFlags.None);

            byte[] data = new byte[len];
            Array.Copy(recvData, data, data.Length);
            return(MasterUtil.ProcessPacket(data));
        }
        /// <summary>
        /// Starts receiving socket addresses of servers.
        /// </summary>
        /// <param name="region">The region of the world that you wish to find servers in.</param>
        /// <param name="callback">Called when a batch of Socket addresses are received.</param>
        /// <param name="filter">Used to set filter on the type of server required.</param>
        public void GetAddresses(Region region, MasterIpCallback callback, IpFilter filter = null)
        {
            if (IsListening)
            {
                return;
            }

            RegionCode  = region;
            Callback    = callback;
            Filter      = filter;
            IsListening = true;
            IPEndPoint endPoint = SeedEndpoint;

            Msg = MasterUtil.BuildPacket(endPoint.ToString(), RegionCode, Filter);
            UdpSocket.Send(Msg);
            recvData = new byte[1400];
            UdpSocket.BeginReceive(recvData, 0, recvData.Length, SocketFlags.None, recv, null);
        }