private void Client_UploadDataCompleted(object sender, UploadDataCompletedEventArgs e) { if (OnComplete != null && !e.Cancelled) { if (e.Error == null) { LLSD result = LLSDParser.DeserializeXml(e.Result); try { OnComplete(this, result, e.Error); } catch (Exception ex) { SecondLife.LogStatic(ex.ToString(), Helpers.LogLevel.Error); } } else { if (Helpers.StringContains(e.Error.Message, "502")) { // These are normal, retry the request automatically SecondLife.DebugLogStatic("502 error from capability " + _Client.Location); StartRequest(_PostData, _ContentType); } else { try { OnComplete(this, null, e.Error); } catch (Exception ex) { SecondLife.LogStatic(ex.ToString(), Helpers.LogLevel.Error); } } } } else if (e.Cancelled) { SecondLife.DebugLogStatic("Capability action at " + _Client.Location + " cancelled"); } }
private void Client_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e) { if (OnProgress != null) { try { OnProgress(this, e.BytesReceived, e.BytesSent, e.TotalBytesToReceive, e.TotalBytesToSend); } catch (Exception ex) { SecondLife.LogStatic(ex.ToString(), Helpers.LogLevel.Error); } } }
/// <summary> /// Attempts to convert an LLSD structure to a known Packet type /// </summary> /// <param name="capsEventName">Event name, this must match an actual /// packet name for a Packet to be successfully built</param> /// <param name="body">LLSD to convert to a Packet</param> /// <returns>A Packet on success, otherwise null</returns> public static Packet BuildPacket(string capsEventName, LLSDMap body) { Assembly assembly = Assembly.GetExecutingAssembly(); // Check if we have a subclass of packet with the same name as this event Type type = assembly.GetType("libsecondlife.Packets." + capsEventName + "Packet", false); if (type == null) { return(null); } Packet packet = null; try { // Create an instance of the object packet = (Packet)Activator.CreateInstance(type); // Iterate over all of the fields in the packet class, looking for matches in the LLSD foreach (FieldInfo field in type.GetFields()) { if (body.ContainsKey(field.Name)) { Type blockType = field.FieldType; if (blockType.IsArray) { LLSDArray array = (LLSDArray)body[field.Name]; Type elementType = blockType.GetElementType(); object[] blockArray = (object[])Array.CreateInstance(elementType, array.Count); for (int i = 0; i < array.Count; i++) { LLSDMap map = (LLSDMap)array[i]; blockArray[i] = ParseLLSDBlock(map, elementType); } field.SetValue(packet, blockArray); } else { LLSDMap map = (LLSDMap)((LLSDArray)body[field.Name])[0]; field.SetValue(packet, ParseLLSDBlock(map, blockType)); } } } } catch (Exception e) { SecondLife.LogStatic(e.ToString(), Helpers.LogLevel.Warning); } return(packet); }
public void StartRequest(byte[] postData, string contentType) { _PostData = postData; _ContentType = contentType; if (_Client.IsBusy) { SecondLife.LogStatic("New CAPS request to " + _Client.Location + " initiated, closing previous request", Helpers.LogLevel.Warning); _Client.CancelAsync(); } else { SecondLife.DebugLogStatic("New CAPS request to " + _Client.Location + " initiated"); } // Proxy if (Proxy != null) { _Client.Proxy = Proxy; } // Content-Type _Client.Headers.Clear(); if (!String.IsNullOrEmpty(contentType)) { _Client.Headers.Add(HttpRequestHeader.ContentType, contentType); } else { _Client.Headers.Add(HttpRequestHeader.ContentType, "application/xml"); } if (postData == null) { _Client.DownloadStringAsync(_Client.Location); } else { _Client.UploadDataAsync(_Client.Location, postData); } }
private void Client_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e) { bool raiseEvent = false; if (!_Dead) { if (!_Running) { raiseEvent = true; } // We are connected to the event queue _Running = true; } // Create an EventQueueGet request LLSDMap request = new LLSDMap(); request["ack"] = new LLSD(); request["done"] = LLSD.FromBoolean(false); byte[] postData = LLSDParser.SerializeXmlBytes(request); _Client.UploadDataAsync(_Client.Location, postData); if (raiseEvent) { SecondLife.DebugLogStatic("Capabilities event queue connected"); // The event queue is starting up for the first time if (OnConnected != null) { try { OnConnected(); } catch (Exception ex) { SecondLife.LogStatic(ex.ToString(), Helpers.LogLevel.Error); } } } }
private void Client_UploadDataCompleted(object sender, UploadDataCompletedEventArgs e) { LLSDArray events = null; int ack = 0; if (e.Error != null) { // Error occurred string message = e.Error.Message.ToLower(); // Check what kind of exception happened if (Helpers.StringContains(message, "404") || Helpers.StringContains(message, "410")) { SecondLife.LogStatic("Closing event queue at " + _Client.Location + " due to missing caps URI", Helpers.LogLevel.Info); _Running = false; _Dead = true; } else if (!e.Cancelled && !Helpers.StringContains(message, "502")) { SecondLife.LogStatic("Unrecognized caps exception from " + _Client.Location + ": " + e.Error.Message, Helpers.LogLevel.Warning); } } else if (!e.Cancelled && e.Result != null) { // Got a response LLSD result = LLSDParser.DeserializeXml(e.Result); if (result != null && result.Type == LLSDType.Map) { // Parse any events returned by the event queue LLSDMap map = (LLSDMap)result; events = (LLSDArray)map["events"]; ack = map["id"].AsInteger(); } } else if (e.Cancelled) { // Connection was cancelled SecondLife.DebugLogStatic("Cancelled connection to event queue at " + _Client.Location); } if (_Running) { LLSDMap request = new LLSDMap(); if (ack != 0) { request["ack"] = LLSD.FromInteger(ack); } else { request["ack"] = new LLSD(); } request["done"] = LLSD.FromBoolean(_Dead); byte[] postData = LLSDParser.SerializeXmlBytes(request); _Client.UploadDataAsync(_Client.Location, postData); // If the event queue is dead at this point, turn it off since // that was the last thing we want to do if (_Dead) { _Running = false; SecondLife.DebugLogStatic("Sent event queue shutdown message"); } } if (OnEvent != null && events != null && events.Count > 0) { // Fire callbacks for each event received foreach (LLSDMap evt in events) { string msg = evt["message"].AsString(); LLSDMap body = (LLSDMap)evt["body"]; try { OnEvent(msg, body); } catch (Exception ex) { SecondLife.LogStatic(ex.ToString(), Helpers.LogLevel.Error); } } } }