/// <summary> /// Starts the client. /// </summary> public async Task Start(CancellationToken cancellationToken) { using (await lck.LockAsync()) { if (State != RnetClientState.Stopped) throw new RnetException("Client is already started."); // start up receiver using a background task cts = new CancellationTokenSource(); receiveTask = Task.Run(async () => await ReceiveLoop(cts.Token)); // currenty started State = RnetClientState.Started; } OnStateChanged(new RnetClientStateEventArgs(State)); }
/// <summary> /// Initializes a new instance. /// </summary> /// <param name="state"></param> internal RnetClientStateEventArgs(RnetClientState state) { State = state; }
/// <summary> /// Stops the client. /// </summary> public async Task Stop(CancellationToken cancellationToken) { using (await lck.LockAsync()) { if (State != RnetClientState.Started) return; // signal shutdown cts.Cancel(); State = RnetClientState.Stopped; // wait for receiving task to terminate await receiveTask; receiveTask = null; } OnStateChanged(new RnetClientStateEventArgs(State)); }