/// <summary> /// Store config var from browser down onto SD Card and restart the device. /// </summary> /// <param name="e">Request object from the browser</param> /// <param name="ret">Return parameters</param> private void SaveConfig(Request e, Object ret) { try { // Open LastPost, and config.js to overwrite FileStream fs = new FileStream(Settings.ConfigFile, FileMode.Create, FileAccess.Write); PostFileReader post = new PostFileReader(); // before new config settings are written down, config must be stored as a JS var, so write in the preamble byte[] preamble = Encoding.UTF8.GetBytes("var config="); fs.Write(preamble, 0, preamble.Length); for (int i = 0; i < post.Length / Settings.FileBufferSize; i++) { fs.Write(post.Read(Settings.FileBufferSize), 0, Settings.FileBufferSize); } fs.Write(post.Read((int)(post.Length % Settings.FileBufferSize)), 0, (int)(post.Length % Settings.FileBufferSize)); //finally, close the js var declaration preamble = Encoding.UTF8.GetBytes(";"); fs.Write(preamble, 0, preamble.Length); fs.Flush(); fs.Close(); post.Close(); } catch (Exception ex) { Debug.Print(ex.ToString()); ret = XMLResponse.GenerateErrorHashtable("file access", ResponseErrorType.InternalOperationError); } }
public static void Main(string[] args) { XMLResponse xmlResponse = new XMLResponse(); XMLResponseParser jsonResponse = new XMLResponseParser(); Console.WriteLine(xmlResponse); // xml response from api Console.WriteLine(jsonResponse); // json response form api }
/// <summary> /// Poqwpulate player object with stats from server /// </summary> public void UpdateStats() { XMLResponse response = new XMLResponse(); SendCommand("Person"); do { response = FindResponse("Person"); } while (response == null); _player.kingdomName = response.message[0].body[0]; _player.race = response.message[1].body[0]; _player.level = response.message[1].body[1]; _player.name = response.message[1].body[2]; _player.movement = Convert.ToInt32(response.message[2].body[0].Substring(0, response.message[2].body[0].IndexOf(" "))); _player.structures = Convert.ToInt32(response.message[2].body[1].Substring(0, response.message[2].body[1].IndexOf(" "))); _player.gold = Convert.ToInt32(response.message[2].body[2].Substring(0, response.message[2].body[2].IndexOf(" "))); _player.land = Convert.ToInt32(response.message[3].body[0].Substring(0, response.message[3].body[0].IndexOf(" "))); _player.peasants = Convert.ToInt32(response.message[3].body[1].Substring(0, response.message[3].body[1].IndexOf(" "))); _player.food = Convert.ToInt32(response.message[3].body[2].Substring(0, response.message[3].body[2].IndexOf(" "))); _player.city = response.message[5].body[0]; _player.continent = response.message[5].body[1]; _player.experience = Convert.ToInt32(response.message[6].body[0]); _player.protection = (response.message[6].body[1] == "YES") ? true : false; _player.taxes = response.message[6].body[2]; }
public XMLResponse ParseResponse(string _response) { int bodyId = 0; int id = 0; XMLResponse response = new XMLResponse(); XElement XML; XML = XElement.Parse(_response); response.user = XML.Element("User").Value; response.command = XML.Element("Command").Value; response.token = XML.Element("Token").Value; foreach (XElement msgElem in XML.Elements("Message")) { response.message.Add(new XMLMessage()); response.message[id].id = msgElem.Attribute("Id").Value; foreach (XElement bodyElem in msgElem.Elements("Body")) { response.message[id].body.Add(bodyElem.Value); bodyId += 1; } bodyId = 0; id += 1; } return response; }
/// <summary> /// Login to the server with string parameter as password (character name selected on object instantiation) /// </summary> /// <param name="password"></param> /// <returns></returns>qw public bool Login(string password) { XMLResponse response = new XMLResponse(); SendCommand("Validate", new string[] { password }); do { response = FindResponse("Validate"); } while (response == null); if (response.message[0].id == "46") { _loggedIn = true; _token = response.token; UpdateStats(); return true; } UpdateStats(); return false; }
MobileRemoteStop(MobileRemoteStopRequest Request) { #region Initial checks if (Request == null) { throw new ArgumentNullException(nameof(Request), "The given MobileRemoteStop request must not be null!"); } Request = _CustomMobileRemoteStopRequestMapper(Request); if (Request == null) { throw new ArgumentNullException(nameof(Request), "The mapped MobileRemoteStop request must not be null!"); } HTTPResponse <Acknowledgement <MobileRemoteStopRequest> > result = null; #endregion #region Send OnMobileRemoteStopRequest event var StartTime = DateTime.UtcNow; try { if (OnMobileRemoteStopRequest != null) { await Task.WhenAll(OnMobileRemoteStopRequest.GetInvocationList(). Cast <OnMobileRemoteStopRequestDelegate>(). Select(e => e(StartTime, Request.Timestamp.Value, this, ClientId, Request.EventTrackingId, Request.SessionId, Request.RequestTimeout ?? RequestTimeout.Value))). ConfigureAwait(false); } } catch (Exception e) { e.Log(nameof(MobileClient) + "." + nameof(OnMobileRemoteStopRequest)); } #endregion using (var _OICPClient = new SOAPClient(Hostname, URLPathPrefix + MobileAuthorizationURL, VirtualHostname, RemotePort, RemoteCertificateValidator, ClientCertificateSelector, UserAgent, RequestTimeout, DNSClient)) { result = await _OICPClient.Query(_CustomMobileRemoteStopSOAPRequestMapper(Request, SOAP.Encapsulation(Request.ToXML(CustomMobileRemoteStopRequestSerializer))), "eRoamingMobileRemoteStop", RequestLogDelegate : OnMobileRemoteStartSOAPRequest, ResponseLogDelegate : OnMobileRemoteStartSOAPResponse, CancellationToken : Request.CancellationToken, EventTrackingId : Request.EventTrackingId, RequestTimeout : Request.RequestTimeout ?? RequestTimeout.Value, #region OnSuccess OnSuccess : XMLResponse => XMLResponse.ConvertContent(Request, (request, xml, onexception) => Acknowledgement <MobileRemoteStopRequest> .Parse(request, xml, CustomAcknowledgementMobileRemoteStopParser, CustomStatusCodeParser, onexception)), #endregion #region OnSOAPFault OnSOAPFault : (timestamp, soapclient, httpresponse) => { SendSOAPError(timestamp, this, httpresponse.Content); return(new HTTPResponse <Acknowledgement <MobileRemoteStopRequest> >( httpresponse, new Acknowledgement <MobileRemoteStopRequest>( Request, StatusCodes.DataError, httpresponse.Content.ToString() ), IsFault: true )); }, #endregion #region OnHTTPError OnHTTPError : (timestamp, soapclient, httpresponse) => { SendHTTPError(timestamp, this, httpresponse); return(new HTTPResponse <Acknowledgement <MobileRemoteStopRequest> >( httpresponse, new Acknowledgement <MobileRemoteStopRequest>( Request, StatusCodes.DataError, httpresponse.HTTPStatusCode.ToString(), httpresponse.HTTPBody.ToUTF8String() ), IsFault: true )); }, #endregion #region OnException OnException : (timestamp, sender, exception) => { SendException(timestamp, sender, exception); return(HTTPResponse <Acknowledgement <MobileRemoteStopRequest> > .ExceptionThrown( new Acknowledgement <MobileRemoteStopRequest>( Request, StatusCodes.ServiceNotAvailable, exception.Message, exception.StackTrace ), Exception: exception )); } #endregion ); } if (result == null) { result = HTTPResponse <Acknowledgement <MobileRemoteStopRequest> > .OK( new Acknowledgement <MobileRemoteStopRequest>( Request, StatusCodes.SystemError, "HTTP request failed!" ) ); } #region Send OnMobileRemoteStopResponse event var Endtime = DateTime.UtcNow; try { if (OnMobileRemoteStopResponse != null) { await Task.WhenAll(OnMobileRemoteStopResponse.GetInvocationList(). Cast <OnMobileRemoteStopResponseDelegate>(). Select(e => e(Endtime, Request.Timestamp.Value, this, ClientId, Request.EventTrackingId, Request.SessionId, Request.RequestTimeout ?? RequestTimeout.Value, result.Content, Endtime - StartTime))). ConfigureAwait(false); } } catch (Exception e) { e.Log(nameof(MobileClient) + "." + nameof(OnMobileRemoteStopResponse)); } #endregion return(result); }