internal T Invoke <T>(Func <T> method, int attempts, AttemptCallback attemptcallback, bool throwExceptions) where T : class { int AttemptCounter = 0; while (true) { try { AttemptCounter++; if (attemptcallback != null) { ThreadPool.QueueUserWorkItem(x => attemptcallback(AttemptCounter)); } T reply = method(); return(reply); } catch (Exception ex) { if (AttemptCounter >= attempts) { if (throwExceptions) { throw; } else { return(null); } } } } }
/// <summary> /// Attempts to execute a callback multiple times if a specific exception occurs. /// This is useful for deleting a file that might be accessed by antivirus apps. /// </summary> internal static void TryCallback(AttemptCallback callback, object obj, Type filterExceptionType) { // Initialization for the multi-attempt logic bool success = false; int maxAttempts = 5; int timeBetweenAttempts = 1000; // sleep time, in milliseconds int attempts = 0; while (!success && attempts < maxAttempts) { attempts++; try { callback(obj); success = true; } catch (Exception ex) { if (attempts == maxAttempts || ex.GetType() != filterExceptionType) { throw ex; } else { Thread.Sleep(timeBetweenAttempts); } } } }
/// <summary> /// Gets the appropriate masterserver query instance /// </summary> /// <param name="endPoint">Master server endpoint.</param> /// <param name="sendTimeout">Sets Socket's SendTimeout Property.</param> /// <param name="receiveTimeout">Sets Socket's ReceiveTimeout Property.</param> /// <param name="retries">Number of times to retry if first attempt fails.</param> /// <param name="attemptCallback">Called on every attempt made to fetch batch.</param> /// <returns>Master server instance</returns> public static Server GetServerInstance(IPEndPoint endPoint, int sendTimeout = 3000, int receiveTimeout = 3000, int retries = 3, AttemptCallback attemptCallback = null) { Server server; var conInfo = new ConnectionInfo { SendTimeout = sendTimeout, ReceiveTimeout = receiveTimeout, Retries = retries, EndPoint = endPoint }; server = new Server(conInfo, attemptCallback, endPoint); return(server); }
protected override void Dispose(bool disposing) { if (!IsDisposed) { if (disposing) { StopReceiving(); if (cts != null) { cts.Dispose(); cts = null; } TaskList.Clear(); Callback = null; ErrorCallback = null; AttemptCallback = null; } base.Dispose(disposing); IsDisposed = true; } }
protected override void Dispose(bool disposing) { if (IsDisposed) { return; } if (disposing) { StopReceiving(); if (_cts != null) { _cts.Dispose(); _cts = null; } _taskList.Clear(); _callback = null; _errorCallback = null; _attemptCallback = null; } base.Dispose(disposing); IsDisposed = true; }
/// <summary> /// Retrieves server rules. /// </summary> /// <param name="callback">called on every attempt made to connect to server(max. attempts = <see cref="ConnectionInfo.Retries"/> + 1).</param> /// <returns>Collection of <see cref="Rule"/> instances.</returns> public virtual QueryMasterCollection <Rule> GetRules(AttemptCallback callback = null) { ThrowIfDisposed(); return(Invoke <QueryMasterCollection <Rule> >(getRules, ConInfo.Retries + 1, callback, ConInfo.ThrowExceptions)); }
/// <summary> /// Retrieves information about the server. /// </summary> /// <param name="callback">Called on every attempt made to connect to server(max. attempts = Retries + 1).</param> /// <returns>Instance of ServerInfo.</returns> public virtual ServerInfo GetInfo(AttemptCallback callback = null) { ThrowIfDisposed(); return(Invoke <ServerInfo>(getInfo, ConInfo.Retries + 1, callback, ConInfo.ThrowExceptions)); }
internal Server(ConnectionInfo conInfo, AttemptCallback attemptCallback) { ConInfo = conInfo; AttemptCallback = attemptCallback; }
/// <summary> /// Retrieves information about the players currently on the server. /// </summary> /// <param name="callback">called on every attempt made to connect to server(max. attempts = Retries + 1).</param> /// <returns>Collection of <see cref="PlayerInfo"/> instances.</returns> public virtual QueryMasterCollection <PlayerInfo> GetPlayers(AttemptCallback callback = null) { ThrowIfDisposed(); return(Invoke(getPlayers, ConInfo.Retries + 1, callback, ConInfo.ThrowExceptions)); }
internal Server(ConnectionInfo conInfo, AttemptCallback attemptCallback, IPEndPoint remoteEndPoint) { _conInfo = conInfo; _attemptCallback = attemptCallback; _remoteEndPoint = remoteEndPoint; }