PostSendTransportBase.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #if !BESTHTTP_DISABLE_SIGNALR
  2. using System.Collections.Generic;
  3. using BestHTTP.SignalR.Messages;
  4. namespace BestHTTP.SignalR.Transports
  5. {
  6. /// <summary>
  7. /// A base class for implementations that must send the data in unique requests. These are currently the LongPolling and ServerSentEvents transports.
  8. /// </summary>
  9. public abstract class PostSendTransportBase : TransportBase
  10. {
  11. /// <summary>
  12. /// Sent out send requests. Keeping a reference to them for future use.
  13. /// </summary>
  14. protected List<HTTPRequest> sendRequestQueue = new List<HTTPRequest>();
  15. public PostSendTransportBase(string name, Connection con)
  16. : base(name, con)
  17. {
  18. }
  19. #region Send Implementation
  20. protected override void SendImpl(string json)
  21. {
  22. var request = new HTTPRequest(Connection.BuildUri(RequestTypes.Send, this), HTTPMethods.Post, true, true, OnSendRequestFinished);
  23. request.FormUsage = Forms.HTTPFormUsage.UrlEncoded;
  24. request.AddField("data", json);
  25. Connection.PrepareRequest(request, RequestTypes.Send);
  26. // SaveLocal a lower priority then the default. This way requests that are sent out alongside with SignalR sent requests can be processed sooner.
  27. request.Priority = -1;
  28. request.Send();
  29. sendRequestQueue.Add(request);
  30. }
  31. void OnSendRequestFinished(HTTPRequest req, HTTPResponse resp)
  32. {
  33. sendRequestQueue.Remove(req);
  34. // error reason if there is any. We will call the manager's Error function if it's not empty.
  35. string reason = string.Empty;
  36. switch (req.State)
  37. {
  38. // The request finished without any problem.
  39. case HTTPRequestStates.Finished:
  40. if (resp.IsSuccess)
  41. {
  42. HTTPManager.Logger.Information("Transport - " + this.Name, "Send - Request Finished Successfully! " + resp.DataAsText);
  43. if (!string.IsNullOrEmpty(resp.DataAsText))
  44. {
  45. IServerMessage msg = TransportBase.Parse(Connection.JsonEncoder, resp.DataAsText);
  46. if (msg != null)
  47. Connection.OnMessage(msg);
  48. }
  49. }
  50. else
  51. reason = string.Format("Send - Request Finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2}",
  52. resp.StatusCode,
  53. resp.Message,
  54. resp.DataAsText);
  55. break;
  56. // The request finished with an unexpected error. The request's Exception property may contain more info about the error.
  57. case HTTPRequestStates.Error:
  58. reason = "Send - Request Finished with Error! " + (req.Exception != null ? (req.Exception.Message + "\n" + req.Exception.StackTrace) : "No Exception");
  59. break;
  60. // The request aborted, initiated by the user.
  61. case HTTPRequestStates.Aborted:
  62. reason = "Send - Request Aborted!";
  63. break;
  64. // Connecting to the server is timed out.
  65. case HTTPRequestStates.ConnectionTimedOut:
  66. reason = "Send - Connection Timed Out!";
  67. break;
  68. // The request didn't finished in the given time.
  69. case HTTPRequestStates.TimedOut:
  70. reason = "Send - Processing the request Timed Out!";
  71. break;
  72. }
  73. if (!string.IsNullOrEmpty(reason))
  74. Connection.Error(reason);
  75. }
  76. #endregion
  77. }
  78. }
  79. #endif