void StopConnection() { if (isStopping) { return; } Guid[] allRemoteClients; lock (startingLock) { if (updatingRedis == null) { return; } if (isStopping) { return; } isStopping = true; RemoveAllClients(); updatingRedisCancellation.Cancel(); updatingRedis.Wait(); updatingRedis = null; updatingRedisCancellation.Dispose(); updatingRedisCancellation = null; updatingRedisWaitingCancellation.Dispose(); updatingRedisWaitingCancellation = null; subscriber.UnsubscribeAll(); redisConnection.Close(); redisConnection.Dispose(); redisDatabase = null; redisConnection = null; allRemoteClients = GetAllRemoteClients().ToArray(); foreach (var remoteClientId in allRemoteClients) { clientTable.Remove(remoteClientId); } clientTable = new ClientTable(privateChannelNamePrefix); clients = new ConcurrentDictionary <Guid, ClientEntity>(); targets = new ConcurrentDictionary <RedisChannel, Guid>(); AdapterStopped?.Invoke(this, EventArgs.Empty); } if (RemoteClientRemoved != null) { foreach (var remoteClientId in allRemoteClients) { RemoteClientRemoved(this, new ClientIdEventArgs(remoteClientId)); } } }
void ReadingProcessor() { using (BinaryReader binaryReader = new BinaryReader(inputStream)) { byte commandCode; try { while (!shuttingdownToken.IsCancellationRequested) { commandCode = binaryReader.ReadByte(); lastStreamWorkingTime = DateTime.Now; if (commandCode <= 127) //Private channel data { int length = GetInt32FromBytes(commandCode, binaryReader.ReadByte(), binaryReader.ReadByte(), binaryReader.ReadByte()); OnPrivateMessageReceived(length, binaryReader); } else if (commandCode == 254) //Ping { OnPingReceived(); } else if (commandCode == 253) //Pong { //Do nothing } else if (commandCode == 130) //Add or update client with virtual host setting { var clientId = binaryReader.ReadGuid(); OnAddOrUpdateClientReceived(clientId, binaryReader); } else if (commandCode == 129) //Add or update client without virtual host setting { var clientId = binaryReader.ReadGuid(); OnAddOrUpdateClientReceived(clientId); } else if (commandCode == 131) //Remove { var clientId = binaryReader.ReadGuid(); OnRemoveClientReceived(clientId); } else if (commandCode == 128) //Hello { OnHelloReceived(); } else if (commandCode == 255) //Link closed { binaryReader.Close(); StopProcessing(true); break; } } } catch (Exception ex) { if (!shuttingdownToken.IsCancellationRequested) //Or, it is closed by terminating reading process. { if (ConnectionErrorOccurred != null) { ConnectionExceptionEventArgs e = new ConnectionExceptionEventArgs(ex, true, false); ConnectionErrorOccurred(this, e); } StopProcessing(true); } } } AdapterStopped?.Invoke(this, EventArgs.Empty); }