KnownHttpEndpoint.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.Collections;
  3. using System.Net;
  4. using System.Threading;
  5. using XUnity.AutoTranslator.Plugin.Core.Configuration;
  6. namespace XUnity.AutoTranslator.Plugin.Core.Web
  7. {
  8. public abstract class KnownHttpEndpoint : IKnownEndpoint
  9. {
  10. private static readonly TimeSpan MaxUnusedLifespan = TimeSpan.FromSeconds( 50 );
  11. private ServicePoint[] _servicePoints;
  12. private bool _isBusy = false;
  13. private UnityWebClient _client;
  14. private DateTime? _clientLastUse = null;
  15. public KnownHttpEndpoint()
  16. {
  17. }
  18. public bool IsBusy => _isBusy;
  19. public virtual bool SupportsLineSplitting
  20. {
  21. get
  22. {
  23. return false;
  24. }
  25. }
  26. protected void SetupServicePoints( params string[] endpoints )
  27. {
  28. _servicePoints = new ServicePoint[ endpoints.Length ];
  29. for( int i = 0 ; i < endpoints.Length ; i++ )
  30. {
  31. var endpoint = endpoints[ i ];
  32. var servicePoint = ServicePointManager.FindServicePoint( new Uri( endpoint ) );
  33. _servicePoints[ i ] = servicePoint;
  34. }
  35. }
  36. public IEnumerator Translate( string untranslatedText, string from, string to, Action<string> success, Action failure )
  37. {
  38. _isBusy = true;
  39. try
  40. {
  41. var setup = OnBeforeTranslate( Settings.TranslationCount );
  42. if( setup != null )
  43. {
  44. while( setup.MoveNext() )
  45. {
  46. yield return setup.Current;
  47. }
  48. }
  49. Logger.Current.Debug( "Starting translation for: " + untranslatedText );
  50. DownloadResult result = null;
  51. try
  52. {
  53. var client = GetClient();
  54. var url = GetServiceUrl( untranslatedText, from, to );
  55. ApplyHeaders( client.Headers );
  56. result = client.GetDownloadResult( new Uri( url ) );
  57. }
  58. catch( Exception e )
  59. {
  60. Logger.Current.Error( e, "Error occurred while setting up translation request." );
  61. }
  62. if( result != null )
  63. {
  64. yield return result;
  65. try
  66. {
  67. if( result.Succeeded )
  68. {
  69. if( TryExtractTranslated( result.Result, out var translatedText ) )
  70. {
  71. Logger.Current.Debug( $"Translation for '{untranslatedText}' succeded. Result: {translatedText}" );
  72. translatedText = translatedText ?? string.Empty;
  73. success( translatedText );
  74. }
  75. else
  76. {
  77. Logger.Current.Error( "Error occurred while extracting translation." );
  78. failure();
  79. }
  80. }
  81. else
  82. {
  83. Logger.Current.Error( "Error occurred while retrieving translation." + Environment.NewLine + result.Error );
  84. failure();
  85. }
  86. }
  87. catch( Exception e )
  88. {
  89. Logger.Current.Error( e, "Error occurred while retrieving translation." );
  90. failure();
  91. }
  92. }
  93. else
  94. {
  95. failure();
  96. }
  97. }
  98. finally
  99. {
  100. _clientLastUse = DateTime.UtcNow;
  101. _isBusy = false;
  102. }
  103. }
  104. public virtual void OnUpdate()
  105. {
  106. if( !_isBusy && _clientLastUse.HasValue && DateTime.UtcNow - _clientLastUse > MaxUnusedLifespan && !_client.IsBusy
  107. && _servicePoints != null && _servicePoints.Length > 0 )
  108. {
  109. Logger.Current.Debug( $"Closing service points because they were not used for {(int)MaxUnusedLifespan.TotalSeconds} seconds." );
  110. _isBusy = true;
  111. _clientLastUse = null;
  112. ThreadPool.QueueUserWorkItem( delegate ( object state )
  113. {
  114. // never do a job like this on the game loop thread
  115. try
  116. {
  117. foreach( var servicePoint in _servicePoints )
  118. {
  119. servicePoint.CloseConnectionGroup( MyWebClient.ConnectionGroupName );
  120. }
  121. }
  122. finally
  123. {
  124. _isBusy = false;
  125. }
  126. } );
  127. }
  128. }
  129. public virtual bool ShouldGetSecondChanceAfterFailure()
  130. {
  131. return false;
  132. }
  133. public abstract string GetServiceUrl( string untranslatedText, string from, string to );
  134. public abstract void ApplyHeaders( WebHeaderCollection headers );
  135. public abstract bool TryExtractTranslated( string result, out string translated );
  136. public virtual void WriteCookies( HttpWebResponse response )
  137. {
  138. }
  139. public virtual CookieContainer ReadCookies()
  140. {
  141. return null;
  142. }
  143. public virtual IEnumerator OnBeforeTranslate( int translationCount )
  144. {
  145. return null;
  146. }
  147. public UnityWebClient GetClient()
  148. {
  149. if( _client == null )
  150. {
  151. _client = new UnityWebClient( this );
  152. _clientLastUse = DateTime.UtcNow;
  153. }
  154. return _client;
  155. }
  156. }
  157. }