KnownHttpEndpoint.cs 6.1 KB

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