Exemple #1
0
        public void GetAddresses(Region region, IpFilter filter, int maxResults, MasterIpCallback callback)
        {
            var master = masterServerEndPoint.Port == 0 ? (MasterServer) new MasterServerWebApi() : new MasterServerUdp(masterServerEndPoint);

            master.GetAddressesLimit = maxResults;
            master.GetAddresses(region, callback, filter);
        }
        /// <summary>
        /// Gets a server list from the Steam master server.
        /// The callback is invoked from a background thread every time a batch of servers is received.
        /// The end of the list is marked with an IPEndPoint of 0.0.0.0:0
        /// In case of a timeout or an exception, the callback is invoked with a NULL parameter value.
        /// </summary>
        /// <exception cref="InvalidOperationException">Thrown if the object is still busy handling a previous call to GetAddresses</exception>
        public void GetAddresses(Region region, MasterIpCallback callback, IpFilter filter)
        {
            ThreadPool.QueueUserWorkItem(x =>
              {
            var udpSocket = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, ProtocolType.Udp);
            udpSocket.SendTimeout = 500;
            udpSocket.ReceiveTimeout = 500;
            udpSocket.Connect(endPoint);
            byte[] recvData = new byte[1400];

            try
            {
              var nextSeed = SeedEndpoint;
              int totalCount = 0;
              do
              {
            var curSeed = nextSeed;
            var endpoints = Util.RunWithRetries(() => SendAndReceive(udpSocket, recvData, region, filter, curSeed), this.Retries);
            ThreadPool.QueueUserWorkItem(y => callback(endpoints));
            totalCount += endpoints.Count;
            nextSeed = endpoints.Last();
              } while (!nextSeed.Equals(SeedEndpoint) && totalCount < GetAddressesLimit);
            }
            catch (Exception)
            {
              callback(null);
            }
            finally
            {
              try { udpSocket.Close(); }
              catch { }
            }
              });
        }
Exemple #3
0
        const string SteamWebApiKey = "B7D245299F6F990504A86FF91EC9D6BD"; // create an account and get a steam web api key at http://steamcommunity.com/dev/apikey

        /// <summary>
        /// Gets a server list from the Steam master server.
        /// The callback is invoked from a background thread every time a batch of servers is received.
        /// The end of the list is marked with an IPEndPoint of 0.0.0.0:0
        /// In case of a timeout or an exception, the callback is invoked with a NULL parameter value.
        /// </summary>
        /// <exception cref="InvalidOperationException">Thrown if the object is still busy handling a previous call to GetAddresses</exception>
        public override void GetAddresses(Region region, MasterIpCallback callback, IpFilter filter)
        {
            ThreadPool.QueueUserWorkItem(x =>
            {
                try
                {
                    using (var cli = new XWebClient())
                    {
                        var filters = MasterUtil.ProcessFilter(filter);
                        var url     = $"https://api.steampowered.com/IGameServersService/GetServerList/v1/?key={SteamWebApiKey}&format=xml&filter={filters}&limit={GetAddressesLimit}";
                        var xml     = cli.DownloadString(url);
                        var ser     = new XmlSerializer(typeof(Response));

                        // replace invalid XML chars ( < 32 ) with char reference
                        var sb = new StringBuilder(xml);
                        for (int i = 0, c = xml.Length; i < c; i++)
                        {
                            if (sb[i] < 32 && !char.IsWhiteSpace(sb[i]))
                            {
                                //{
                                //  sb.Insert(i+1, "#" + ((int)sb[i]).ToString() + ";");
                                //  sb[i] = '&';
                                //}
                                sb[i] = ' ';
                            }
                        }
                        xml = sb.ToString();

                        var resp = (Response)ser.Deserialize(new StringReader(xml));

                        var endpoints = new List <Tuple <IPEndPoint, ServerInfo> >();
                        foreach (var msg in resp.Servers)
                        {
                            try
                            {
                                int i = msg.addr.IndexOf(':');
                                if (i > 0)
                                {
                                    var info = ConvertToServerInfo(msg);
                                    endpoints.Add(new Tuple <IPEndPoint, ServerInfo>(info.EndPoint, info));
                                }
                            }
                            catch
                            {
                            }
                        }
                        callback(new ReadOnlyCollection <Tuple <IPEndPoint, ServerInfo> >(endpoints), null, false);
                    }
                }
                catch (Exception ex)
                {
                    callback(null, ex, false);
                }
            });
        }
Exemple #4
0
        /// <summary>
        /// Gets a server list from the Steam master server.
        /// The callback is invoked from a background thread every time a batch of servers is received.
        /// The end of the list is marked with an IPEndPoint of 0.0.0.0:0
        /// In case of a timeout or an exception, the callback is invoked with a NULL parameter value.
        /// </summary>
        /// <exception cref="InvalidOperationException">Thrown if the object is still busy handling a previous call to GetAddresses</exception>
        public override void GetAddresses(Region region, MasterIpCallback callback, IpFilter filter)
        {
            ThreadPool.QueueUserWorkItem(x =>
            {
                var udpSocket            = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, ProtocolType.Udp);
                udpSocket.SendTimeout    = 500;
                udpSocket.ReceiveTimeout = 500;
                udpSocket.Connect(endPoint);
                byte[] recvData = new byte[1400];

                try
                {
                    var nextSeed   = SeedEndpoint;
                    int totalCount = 0;
                    bool atEnd     = false;
                    while (!atEnd)
                    {
                        var curSeed   = nextSeed;
                        var endpoints = Util.RunWithRetries(() => SendAndReceive(udpSocket, recvData, region, filter, curSeed), this.Retries);
                        if (endpoints == null)
                        {
                            callback(null, null, false);
                            break;
                        }

                        nextSeed = endpoints.Last();

                        atEnd          = nextSeed.Equals(SeedEndpoint);
                        var serverList = endpoints.Select(ep => new Tuple <IPEndPoint, ServerInfo>(ep, null)).ToList();
                        if (atEnd)
                        {
                            serverList.RemoveAt(serverList.Count - 1); // remove the 0.0.0.0:0 end-of-list marker
                        }
                        ThreadPool.QueueUserWorkItem(y => callback(new ReadOnlyCollection <Tuple <IPEndPoint, ServerInfo> >(serverList), null, !atEnd));
                        totalCount += endpoints.Count;

                        atEnd |= totalCount >= GetAddressesLimit;
                    } //while (!nextSeed.Equals(SeedEndpoint) && totalCount < GetAddressesLimit);
                }
                catch (Exception ex)
                {
                    callback(null, ex, false);
                }
                finally
                {
                    try { udpSocket.Close(); }
                    catch { }
                }
            });
        }
        /// <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);
        }
        public void GetAddresses(Region region, IpFilter filter, int maxResults, MasterIpCallback callback)
        {
            try
            {
                using (var client = new XWebClient())
                {
                    var text = client.DownloadString(this.url);
                    if (text == null)
                    {
                        callback(null, null, false);
                        return;
                    }

                    var lines     = text.Split('\n');
                    var endpoints = new List <Tuple <IPEndPoint, ServerInfo> >(lines.Length);
                    int i         = 0;
                    foreach (var line in lines)
                    {
                        if (string.IsNullOrWhiteSpace(line))
                        {
                            continue;
                        }
                        var parts = line.Split(':');
                        if (parts.Length != 2)
                        {
                            continue;
                        }

                        IPAddress addr;
                        int       port;
                        if (IPAddress.TryParse(parts[0], out addr) && int.TryParse(parts[1].TrimEnd(), out port))
                        {
                            endpoints.Add(new Tuple <IPEndPoint, ServerInfo>(new IPEndPoint(addr, port), null));
                            if (++i == maxResults)
                            {
                                break;
                            }
                        }
                    }
                    callback(new ReadOnlyCollection <Tuple <IPEndPoint, ServerInfo> >(endpoints), null, false);
                }
            }
            catch (Exception ex)
            {
                callback(null, ex, false);
            }
        }
        /// <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);
        }
        public void GetAddresses(Region region, IpFilter filter, int maxResults, MasterIpCallback callback)
        {
            try
              {
            using (var client = new XWebClient())
            {
              var text = client.DownloadString(this.url);
              if (text == null)
              {
            callback(null);
            return;
              }

              var lines = text.Split('\n');
              var endpoints = new List<IPEndPoint>(lines.Length);
              int i = 0;
              foreach (var line in lines)
              {
            if (string.IsNullOrWhiteSpace(line)) continue;
            var parts = line.Split(':');
            if (parts.Length != 2) continue;

            IPAddress addr;
            int port;
            if (IPAddress.TryParse(parts[0], out addr) && int.TryParse(parts[1].TrimEnd(), out port))
            {
              endpoints.Add(new IPEndPoint(addr, port));
              if (++i == maxResults)
                break;
            }
              }
              endpoints.Add(new IPEndPoint(IPAddress.Any, 0));

              callback(new ReadOnlyCollection<IPEndPoint>(endpoints));
            }
              }
              catch
              {
            callback(null);
              }
        }
Exemple #9
0
        const string SteamWebApiKey = "B7D245299F6F990504A86FF91EC9D6BD"; // create an account and get a steam web api key at http://steamcommunity.com/dev/apikey

        /// <summary>
        /// Gets a server list from the Steam master server.
        /// The callback is invoked from a background thread every time a batch of servers is received.
        /// The end of the list is marked with an IPEndPoint of 0.0.0.0:0
        /// In case of a timeout or an exception, the callback is invoked with a NULL parameter value.
        /// </summary>
        /// <exception cref="InvalidOperationException">Thrown if the object is still busy handling a previous call to GetAddresses</exception>
        public override void GetAddresses(Region region, MasterIpCallback callback, IpFilter filter)
        {
            ThreadPool.QueueUserWorkItem(x =>
            {
                try
                {
                    using (var cli = new XWebClient())
                    {
                        var filters = MasterUtil.ProcessFilter(filter);
                        var url     = $"https://api.steampowered.com/IGameServersService/GetServerList/v1/?key={SteamWebApiKey}&format=xml&filter={filters}&limit={GetAddressesLimit}";
                        var xml     = cli.DownloadString(url);
                        var ser     = new XmlSerializer(typeof(Response));
                        var resp    = (Response)ser.Deserialize(new StringReader(xml));

                        var endpoints = new List <Tuple <IPEndPoint, ServerInfo> >();
                        foreach (var msg in resp.Servers)
                        {
                            try
                            {
                                int i = msg.addr.IndexOf(':');
                                if (i > 0)
                                {
                                    var info = ConvertToServerInfo(msg);
                                    endpoints.Add(new Tuple <IPEndPoint, ServerInfo>(info.EndPoint, info));
                                }
                            }
                            catch
                            {
                            }
                        }
                        callback(new ReadOnlyCollection <Tuple <IPEndPoint, ServerInfo> >(endpoints), null, false);
                    }
                }
                catch (Exception ex)
                {
                    callback(null, ex, false);
                }
            });
        }
 public abstract void GetAddresses(Region region, MasterIpCallback callback, IpFilter filter);
 public void GetAddresses(Region region, IpFilter filter, int maxResults, MasterIpCallback callback)
 {
     var master = new MasterServer(masterServerEndPoint);
       master.GetAddressesLimit = maxResults;
       master.GetAddresses(region, callback, filter);
 }
Exemple #12
0
        /// <summary>
        /// Gets a server list from the Steam master server.
        /// The callback is invoked from a background thread every time a batch of servers is received.
        /// The end of the list is marked with an IPEndPoint of 0.0.0.0:0
        /// In case of a timeout or an exception, the callback is invoked with a NULL parameter value.
        /// </summary>
        /// <exception cref="InvalidOperationException">Thrown if the object is still busy handling a previous call to GetAddresses</exception>
        public void GetAddresses(Region region, MasterIpCallback callback, IpFilter filter)
        {
            ThreadPool.QueueUserWorkItem(x =>
            {
#if true
                try
                {
                    using (var cli = new XWebClient())
                    {
                        var filters = MasterUtil.ProcessFilter(filter);
                        var url     = $"https://api.steampowered.com/IGameServersService/GetServerList/v1/?key={SteamWebApiKey}&format=xml&filter={filters}&limit={GetAddressesLimit}";
                        var xml     = cli.DownloadString(url);
                        var ser     = new XmlSerializer(typeof(Response));
                        var resp    = (Response)ser.Deserialize(new StringReader(xml));

                        var endpoints = new List <Tuple <IPEndPoint, ServerInfo> >();
                        foreach (var msg in resp.Servers)
                        {
                            try
                            {
                                int i = msg.addr.IndexOf(':');
                                if (i > 0)
                                {
                                    var info = ConvertToServerInfo(msg);
                                    endpoints.Add(new Tuple <IPEndPoint, ServerInfo>(info.EndPoint, info));
                                }
                            }
                            catch
                            {
                            }
                        }
                        callback(new ReadOnlyCollection <Tuple <IPEndPoint, ServerInfo> >(endpoints), null);
                    }
                }
                catch (Exception ex)
                {
                    callback(null, ex);
                }
#else
                var udpSocket            = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, ProtocolType.Udp);
                udpSocket.SendTimeout    = 500;
                udpSocket.ReceiveTimeout = 500;
                udpSocket.Connect(endPoint);
                byte[] recvData = new byte[1400];

                try
                {
                    var nextSeed   = SeedEndpoint;
                    int totalCount = 0;
                    do
                    {
                        var curSeed   = nextSeed;
                        var endpoints = Util.RunWithRetries(() => SendAndReceive(udpSocket, recvData, region, filter, curSeed), this.Retries);
                        if (endpoints == null)
                        {
                            callback(null);
                            break;
                        }
                        ThreadPool.QueueUserWorkItem(y => callback(endpoints, null));
                        totalCount += endpoints.Count;
                        nextSeed    = endpoints.Last();
                    } while (!nextSeed.Equals(SeedEndpoint) && totalCount < GetAddressesLimit);
                }
                catch (Exception ex)
                {
                    callback(null, ex);
                }
                finally
                {
                    try { udpSocket.Close(); }
                    catch { }
                }
#endif
            });
        }