LoadFromString() public static method

Attempts to create an instance of KeyValue from the given input text.
This method will swallow any exceptions that occur when reading, use ReadAsText if you wish to handle exceptions.
public static LoadFromString ( string input ) : KeyValue
input string The input text to load.
return KeyValue
Exemplo n.º 1
0
        /// <summary>
        /// Connects this instance to the server.
        /// </summary>
        /// <returns><c>true</c> if the connection was a success; otherwise, <c>false</c>.</returns>
        public bool Connect()
        {
            byte[] encryptedKey = null;

            // TODO: handle other universes?
            byte[] universeKey = KeyDictionary.GetPublicKey(EUniverse.Public);
            using (var rsa = new RSACrypto(universeKey))
            {
                encryptedKey = rsa.Encrypt(sessionKey);
            }

            string payload;

            if (appTicket == null)
            {
                payload = String.Format("sessionkey={0}&anonymoususer=1&steamid={1}", WebHelpers.UrlEncode(encryptedKey), steamID.ConvertToUInt64());
            }
            else
            {
                byte[] encryptedTicket = CryptoHelper.SymmetricEncrypt(appTicket, sessionKey);
                payload = String.Format("sessionkey={0}&appticket={1}", WebHelpers.UrlEncode(encryptedKey), WebHelpers.UrlEncode(encryptedTicket));
            }

            webClient.Headers.Clear();
            webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

            string response;

            try
            {
                response = webClient.UploadString(BuildCommand(endPoint, "initsession"), payload);
            }
            catch (WebException e)
            {
                LogWebException("Connect", e);
                return(false);
            }

            var responsekv  = KeyValue.LoadFromString(response);
            var sessionidn  = responsekv.Children.Where(c => c.Name == "sessionid").First();
            var reqcountern = responsekv.Children.Where(c => c.Name == "req-counter").First();

            sessionID  = (ulong)(sessionidn.AsLong(0));
            reqcounter = reqcountern.AsLong(0);

            try
            {
                AuthDepot();
            }
            catch (WebException e)
            {
                LogWebException("AuthDepot", e);
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Fetches a server list from the given content server for the provided CellID.
        /// </summary>
        /// <param name="csServer">The server to request a server list from.</param>
        /// <param name="cellID">The CellID.</param>
        /// <returns>A list of content servers.</returns>
        public static List <ClientEndPoint> FetchServerList(ClientEndPoint csServer, int cellID)
        {
            int serversToRequest = 20;

            using (WebClient webClient = new WebClient())
            {
                Uri request = new Uri(BuildCommand(csServer, "serverlist"), String.Format("{0}/{1}/", cellID, serversToRequest));

                string serverList;
                try
                {
                    serverList = webClient.DownloadString(request);
                }
                catch (WebException e)
                {
                    Console.WriteLine("FetchServerList returned: {0}", e.Message);
                    return(null);
                }

                KeyValue serverkv = KeyValue.LoadFromString(serverList);

                if (serverkv["deferred"].AsString() == "1")
                {
                    return(null);
                }

                List <ClientEndPoint> endpoints = new List <ClientEndPoint>();

                foreach (var child in serverkv.Children)
                {
                    var node     = child.Children.Where(x => x.Name == "host" || x.Name == "Host").First();
                    var typeNode = child.Children.Where(x => x.Name == "type").First();

                    var endpoint_string = node.Value.Split(':');

                    int port = 80;

                    if (endpoint_string.Length > 1)
                    {
                        port = int.Parse(endpoint_string[1]);
                    }

                    endpoints.Add(new ClientEndPoint(endpoint_string[0], port, typeNode.AsString()));
                }

                return(endpoints);
            }
        }