123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 |
- #if !BESTHTTP_DISABLE_SIGNALR
- using System;
- using System.Collections.Generic;
- using BestHTTP.SignalR.Messages;
- using BestHTTP.SignalR.JsonEncoders;
- namespace BestHTTP.SignalR.Transports
- {
- public delegate void OnTransportStateChangedDelegate(TransportBase transport, TransportStates oldState, TransportStates newState);
- public abstract class TransportBase
- {
- private const int MaxRetryCount = 5;
- #region Public Properties
-
-
-
- public string Name { get; protected set; }
-
-
-
- public abstract bool SupportsKeepAlive { get; }
-
-
-
- public abstract TransportTypes Type { get; }
-
-
-
- public IConnection Connection { get; protected set; }
-
-
-
- public TransportStates State
- {
- get { return _state; }
- protected set
- {
- TransportStates old = _state;
- _state = value;
- if (OnStateChanged != null)
- OnStateChanged(this, old, _state);
- }
- }
- public TransportStates _state;
-
-
-
- public event OnTransportStateChangedDelegate OnStateChanged;
- #endregion
- public TransportBase(string name, Connection connection)
- {
- this.Name = name;
- this.Connection = connection;
- this.State = TransportStates.Initial;
- }
- #region Abstract functions
-
-
-
- public abstract void Connect();
-
-
-
- public abstract void Stop();
-
-
-
- protected abstract void SendImpl(string json);
-
-
-
-
- protected abstract void Started();
-
-
-
- protected abstract void Aborted();
- #endregion
-
-
-
- protected void OnConnected()
- {
- if (this.State != TransportStates.Reconnecting)
- {
-
- Start();
- }
- else
- {
- Connection.TransportReconnected();
- Started();
- this.State = TransportStates.Started;
- }
- }
- #region Start Request Sending
-
-
-
- protected void Start()
- {
- HTTPManager.Logger.Information("Transport - " + this.Name, "Sending Start Request");
- this.State = TransportStates.Starting;
- if (this.Connection.Protocol > ProtocolVersions.Protocol_2_0)
- {
- var request = new HTTPRequest(Connection.BuildUri(RequestTypes.Start, this), HTTPMethods.Get, true, true, OnStartRequestFinished);
- request.Tag = 0;
- request.DisableRetry = true;
- request.Timeout = Connection.NegotiationResult.ConnectionTimeout + TimeSpan.FromSeconds(10);
- Connection.PrepareRequest(request, RequestTypes.Start);
- request.Send();
- }
- else
- {
-
- this.State = TransportStates.Started;
- Started();
- Connection.TransportStarted();
- }
- }
- private void OnStartRequestFinished(HTTPRequest req, HTTPResponse resp)
- {
- switch (req.State)
- {
- case HTTPRequestStates.Finished:
- if (resp.IsSuccess)
- {
- HTTPManager.Logger.Information("Transport - " + this.Name, "Start - Returned: " + resp.DataAsText);
- string response = Connection.ParseResponse(resp.DataAsText);
- if (response != "started")
- {
- Connection.Error(string.Format("Expected 'started' response, but '{0}' found!", response));
- return;
- }
-
- this.State = TransportStates.Started;
- Started();
- Connection.TransportStarted();
- return;
- }
- else
- HTTPManager.Logger.Warning("Transport - " + this.Name, string.Format("Start - request finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2} Uri: {3}",
- resp.StatusCode,
- resp.Message,
- resp.DataAsText,
- req.CurrentUri));
- goto default;
- default:
- HTTPManager.Logger.Information("Transport - " + this.Name, "Start request state: " + req.State.ToString());
-
- int retryCount = (int)req.Tag;
- if (retryCount++ < MaxRetryCount)
- {
- req.Tag = retryCount;
- req.Send();
- }
- else
- Connection.Error("Failed to send Start request.");
- break;
- }
- }
- #endregion
- #region Abort Implementation
-
-
-
- public virtual void Abort()
- {
- if (this.State != TransportStates.Started)
- return;
- this.State = TransportStates.Closing;
- var request = new HTTPRequest(Connection.BuildUri(RequestTypes.Abort, this), HTTPMethods.Get, true, true, OnAbortRequestFinished);
-
- request.Tag = 0;
- request.DisableRetry = true;
- Connection.PrepareRequest(request, RequestTypes.Abort);
- request.Send();
- }
- protected void AbortFinished()
- {
- this.State = TransportStates.Closed;
- Connection.TransportAborted();
- this.Aborted();
- }
- private void OnAbortRequestFinished(HTTPRequest req, HTTPResponse resp)
- {
- switch (req.State)
- {
- case HTTPRequestStates.Finished:
- if (resp.IsSuccess)
- {
- HTTPManager.Logger.Information("Transport - " + this.Name, "Abort - Returned: " + resp.DataAsText);
- if (this.State == TransportStates.Closing)
- AbortFinished();
- }
- else
- {
- HTTPManager.Logger.Warning("Transport - " + this.Name, string.Format("Abort - Handshake request finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2} Uri: {3}",
- resp.StatusCode,
- resp.Message,
- resp.DataAsText,
- req.CurrentUri));
-
- goto default;
- }
- break;
- default:
- HTTPManager.Logger.Information("Transport - " + this.Name, "Abort request state: " + req.State.ToString());
-
- int retryCount = (int)req.Tag;
- if (retryCount++ < MaxRetryCount)
- {
- req.Tag = retryCount;
- req.Send();
- }
- else
- Connection.Error("Failed to send Abort request!");
- break;
- }
- }
- #endregion
- #region Send Implementation
-
-
-
-
- public void Send(string jsonStr)
- {
- try
- {
- HTTPManager.Logger.Information("Transport - " + this.Name, "Sending: " + jsonStr);
- SendImpl(jsonStr);
- }
- catch (Exception ex)
- {
- HTTPManager.Logger.Exception("Transport - " + this.Name, "Send", ex);
- }
- }
- #endregion
- #region Helper Functions
-
-
-
- public void Reconnect()
- {
- HTTPManager.Logger.Information("Transport - " + this.Name, "Reconnecting");
- Stop();
- this.State = TransportStates.Reconnecting;
- Connect();
- }
-
-
-
- public static IServerMessage Parse(IJsonEncoder encoder, string json)
- {
-
- if (string.IsNullOrEmpty(json))
- {
- HTTPManager.Logger.Error("MessageFactory", "Parse - called with empty or null string!");
- return null;
- }
-
- if (json.Length == 2 && json == "{}")
- return new KeepAliveMessage();
- IDictionary<string, object> msg = null;
- try
- {
-
- msg = encoder.DecodeMessage(json);
- }
- catch(Exception ex)
- {
- HTTPManager.Logger.Exception("MessageFactory", "Parse - encoder.DecodeMessage", ex);
- return null;
- }
- if (msg == null)
- {
- HTTPManager.Logger.Error("MessageFactory", "Parse - Json Decode failed for json string: \"" + json + "\"");
- return null;
- }
-
- IServerMessage result = null;
- if (!msg.ContainsKey("C"))
- {
-
- if (!msg.ContainsKey("E"))
- result = new ResultMessage();
- else
- result = new FailureMessage();
- }
- else
- result = new MultiMessage();
- try
- {
- result.Parse(msg);
- }
- catch
- {
- HTTPManager.Logger.Error("MessageFactory", "Can't parse msg: " + json);
- throw;
- }
- return result;
- }
- #endregion
- }
- }
- #endif
|