* [LIBOMV-506] Complete rewrite of CapsBase to use HttpWebRequest instead of our homebrewed WebRequest hack. We lose the upload progress callback but gain IOCP thread instead of System.Thread usage and Keep-Alive support
* Content-Types described in http://tools.ietf.org/html/draft-hamrick-llsd-00 are used for CAPS requests. This *may* be incompatible with the current SL grid, needs testing * Modified CapsClient requests to require OSDFormat enum and timeout values git-svn-id: http://libopenmetaverse.googlecode.com/svn/libopenmetaverse/trunk@2680 52acb1d6-8a22-11de-b505-999d5b087335
This commit is contained in:
@@ -33,41 +33,39 @@ namespace OpenMetaverse.Http
|
||||
{
|
||||
public class EventQueueClient
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <summary>=</summary>
|
||||
public const int REQUEST_TIMEOUT = 1000 * 120;
|
||||
|
||||
public delegate void ConnectedCallback();
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="eventName"></param>
|
||||
/// <param name="body"></param>
|
||||
public delegate void EventCallback(string eventName, OSDMap body);
|
||||
|
||||
/// <summary></summary>
|
||||
public ConnectedCallback OnConnected;
|
||||
/// <summary></summary>
|
||||
public EventCallback OnEvent;
|
||||
|
||||
public IWebProxy Proxy;
|
||||
public bool Running { get { return _Running; } }
|
||||
|
||||
public bool Running { get { return _Running && _Client.IsBusy; } }
|
||||
|
||||
protected CapsBase _Client;
|
||||
protected Uri _Address;
|
||||
protected bool _Dead;
|
||||
protected bool _Running;
|
||||
protected HttpWebRequest _Request;
|
||||
|
||||
public EventQueueClient(Uri eventQueueLocation)
|
||||
{
|
||||
_Client = new CapsBase(eventQueueLocation, null);
|
||||
_Client.OpenWriteCompleted += new CapsBase.OpenWriteCompletedEventHandler(Client_OpenWriteCompleted);
|
||||
_Client.UploadDataCompleted += new CapsBase.UploadDataCompletedEventHandler(Client_UploadDataCompleted);
|
||||
_Address = eventQueueLocation;
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
_Dead = false;
|
||||
_Client.OpenWriteAsync(_Client.Location);
|
||||
|
||||
// Create an EventQueueGet request
|
||||
OSDMap request = new OSDMap();
|
||||
request["ack"] = new OSD();
|
||||
request["done"] = OSD.FromBoolean(false);
|
||||
|
||||
byte[] postData = OSDParser.SerializeLLSDXmlBytes(request);
|
||||
|
||||
_Request = CapsBase.UploadDataAsync(_Address, null, "application/xml", postData, REQUEST_TIMEOUT, OpenWriteHandler, null, RequestCompletedHandler);
|
||||
}
|
||||
|
||||
public void Stop(bool immediate)
|
||||
@@ -77,60 +75,71 @@ namespace OpenMetaverse.Http
|
||||
if (immediate)
|
||||
_Running = false;
|
||||
|
||||
if (_Client.IsBusy)
|
||||
_Client.CancelAsync();
|
||||
if (_Request != null)
|
||||
_Request.Abort();
|
||||
}
|
||||
|
||||
#region Callback Handlers
|
||||
|
||||
private void Client_OpenWriteCompleted(object sender, CapsBase.OpenWriteCompletedEventArgs e)
|
||||
void OpenWriteHandler(HttpWebRequest request)
|
||||
{
|
||||
bool raiseEvent = false;
|
||||
_Running = true;
|
||||
_Request = request;
|
||||
|
||||
if (!_Dead)
|
||||
Logger.Log.Debug("Capabilities event queue connected");
|
||||
|
||||
// The event queue is starting up for the first time
|
||||
if (OnConnected != null)
|
||||
{
|
||||
if (!_Running) raiseEvent = true;
|
||||
|
||||
// We are connected to the event queue
|
||||
_Running = true;
|
||||
}
|
||||
|
||||
// Create an EventQueueGet request
|
||||
OSDMap request = new OSDMap();
|
||||
request["ack"] = new OSD();
|
||||
request["done"] = OSD.FromBoolean(false);
|
||||
|
||||
byte[] postData = OSDParser.SerializeLLSDXmlBytes(request);
|
||||
|
||||
_Client.UploadDataAsync(_Client.Location, postData);
|
||||
|
||||
if (raiseEvent)
|
||||
{
|
||||
Logger.Log.Debug("Capabilities event queue connected");
|
||||
|
||||
// The event queue is starting up for the first time
|
||||
if (OnConnected != null)
|
||||
{
|
||||
try { OnConnected(); }
|
||||
catch (Exception ex) { Logger.Log.Error(ex.Message, ex); }
|
||||
}
|
||||
try { OnConnected(); }
|
||||
catch (Exception ex) { Logger.Log.Error(ex.Message, ex); }
|
||||
}
|
||||
}
|
||||
|
||||
private void Client_UploadDataCompleted(object sender, CapsBase.UploadDataCompletedEventArgs e)
|
||||
void RequestCompletedHandler(HttpWebRequest request, HttpWebResponse response, byte[] responseData, Exception error)
|
||||
{
|
||||
// We don't care about this request now that it has completed
|
||||
_Request = null;
|
||||
|
||||
OSDArray events = null;
|
||||
int ack = 0;
|
||||
|
||||
if (e.Error != null)
|
||||
if (responseData != null)
|
||||
{
|
||||
// Got a response
|
||||
OSDMap result = OSDParser.DeserializeLLSDXml(responseData) as OSDMap;
|
||||
|
||||
if (result != null)
|
||||
{
|
||||
events = result["events"] as OSDArray;
|
||||
ack = result["id"].AsInteger();
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Log.Warn("Got an unparseable response from the event queue: \"" +
|
||||
System.Text.Encoding.UTF8.GetString(responseData) + "\"");
|
||||
}
|
||||
}
|
||||
else if (error != null)
|
||||
{
|
||||
#region Error handling
|
||||
|
||||
HttpStatusCode code = HttpStatusCode.OK;
|
||||
if (e.Error is WebException && ((WebException)e.Error).Response != null)
|
||||
code = ((HttpWebResponse)((WebException)e.Error).Response).StatusCode;
|
||||
|
||||
if (error is WebException)
|
||||
{
|
||||
WebException webException = (WebException)error;
|
||||
|
||||
if (webException.Response != null)
|
||||
code = ((HttpWebResponse)webException.Response).StatusCode;
|
||||
else if (webException.Status == WebExceptionStatus.RequestCanceled)
|
||||
goto HandlingDone;
|
||||
}
|
||||
|
||||
if (error is WebException && ((WebException)error).Response != null)
|
||||
code = ((HttpWebResponse)((WebException)error).Response).StatusCode;
|
||||
|
||||
if (code == HttpStatusCode.NotFound || code == HttpStatusCode.Gone)
|
||||
{
|
||||
Logger.Log.InfoFormat("Closing event queue at {0} due to missing caps URI", _Client.Location);
|
||||
Logger.Log.InfoFormat("Closing event queue at {0} due to missing caps URI", _Address);
|
||||
|
||||
_Running = false;
|
||||
_Dead = true;
|
||||
@@ -143,55 +152,50 @@ namespace OpenMetaverse.Http
|
||||
// interprets this as a generic error and returns a 502 to us
|
||||
// that we ignore
|
||||
}
|
||||
else if (!e.Cancelled)
|
||||
else
|
||||
{
|
||||
// Try to log a meaningful error message
|
||||
if (code != HttpStatusCode.OK)
|
||||
{
|
||||
Logger.Log.WarnFormat("Unrecognized caps connection problem from {0}: {1}",
|
||||
_Client.Location, code);
|
||||
_Address, code);
|
||||
}
|
||||
else if (e.Error.InnerException != null)
|
||||
else if (error.InnerException != null)
|
||||
{
|
||||
Logger.Log.WarnFormat("Unrecognized caps exception from {0}: {1}",
|
||||
_Client.Location, e.Error.InnerException.Message);
|
||||
_Address, error.InnerException.Message);
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Log.WarnFormat("Unrecognized caps exception from {0}: {1}",
|
||||
_Client.Location, e.Error.Message);
|
||||
_Address, error.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (!e.Cancelled && e.Result != null)
|
||||
{
|
||||
// Got a response
|
||||
OSD result = OSDParser.DeserializeLLSDXml(e.Result);
|
||||
if (result != null && result.Type == OSDType.Map)
|
||||
{
|
||||
// Parse any events returned by the event queue
|
||||
OSDMap map = (OSDMap)result;
|
||||
|
||||
events = (OSDArray)map["events"];
|
||||
ack = map["id"].AsInteger();
|
||||
}
|
||||
#endregion Error handling
|
||||
}
|
||||
else if (e.Cancelled)
|
||||
else
|
||||
{
|
||||
// Connection was cancelled
|
||||
Logger.Log.Debug("Cancelled connection to event queue at " + _Client.Location);
|
||||
Logger.Log.Warn("No response from the event queue but no reported error either");
|
||||
}
|
||||
|
||||
HandlingDone:
|
||||
|
||||
#region Resume the connection
|
||||
|
||||
if (_Running)
|
||||
{
|
||||
OSDMap request = new OSDMap();
|
||||
if (ack != 0) request["ack"] = OSD.FromInteger(ack);
|
||||
else request["ack"] = new OSD();
|
||||
request["done"] = OSD.FromBoolean(_Dead);
|
||||
OSDMap osdRequest = new OSDMap();
|
||||
if (ack != 0) osdRequest["ack"] = OSD.FromInteger(ack);
|
||||
else osdRequest["ack"] = new OSD();
|
||||
osdRequest["done"] = OSD.FromBoolean(_Dead);
|
||||
|
||||
byte[] postData = OSDParser.SerializeLLSDXmlBytes(request);
|
||||
byte[] postData = OSDParser.SerializeLLSDXmlBytes(osdRequest);
|
||||
|
||||
_Client.UploadDataAsync(_Client.Location, postData);
|
||||
// Resume the connection. The event handler for the connection opening
|
||||
// just sets class _Request variable to the current HttpWebRequest
|
||||
CapsBase.UploadDataAsync(_Address, null, "application/xml", postData, REQUEST_TIMEOUT,
|
||||
delegate(HttpWebRequest newRequest) { _Request = newRequest; }, null, RequestCompletedHandler);
|
||||
|
||||
// If the event queue is dead at this point, turn it off since
|
||||
// that was the last thing we want to do
|
||||
@@ -202,6 +206,10 @@ namespace OpenMetaverse.Http
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Resume the connection
|
||||
|
||||
#region Handle incoming events
|
||||
|
||||
if (OnEvent != null && events != null && events.Count > 0)
|
||||
{
|
||||
// Fire callbacks for each event received
|
||||
@@ -214,8 +222,8 @@ namespace OpenMetaverse.Http
|
||||
catch (Exception ex) { Logger.Log.Error(ex.Message, ex); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Callback Handlers
|
||||
#endregion Handle incoming events
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user