public void Listen() { while (true) { if (sendCommanSyncQueue.Count > 0) { lock (_sendLock) { Monitor.Pulse(_sendLock); } lock (_listenLock) { Monitor.Wait(_listenLock); } } if (!redisStream.IsDataAvailable()) { continue; } string[] messageInfo = redisStream.HandleChannelMessage(); if (VerifyChannelMessageData(messageInfo)) { PubSubResponse response = (PubSubResponse)Enum.Parse(typeof(PubSubResponse), messageInfo[0]); switch (response) { case PubSubResponse.subscribe: OnSubscribe(this, new SubscriptionEventArgs(messageInfo[1])); break; case PubSubResponse.unsubscribe: OnUnsubscribe(this, new SubscriptionEventArgs(messageInfo[1])); break; case PubSubResponse.message: OnMessageReceived(this, new MessageEventArgs(messageInfo[1], messageInfo[2])); break; } } } }
public override async Task Subscribe(IAsyncStreamReader <PubSubRequest> requestStream, IServerStreamWriter <PubSubResponse> responseStream, ServerCallContext context) { var requestTask = Task.Run(async() => { while (await requestStream.MoveNext()) { var req = requestStream.Current; Log.Information("Client subscribed to channel: {req}", req); ConcurrentBag <PubSubRequest> concurrentBag; _subscriptions.TryGetValue(req.ChannelName, out concurrentBag); if (concurrentBag == null) { concurrentBag = new ConcurrentBag <PubSubRequest>(); } concurrentBag.Add(req); _subscriptions.AddOrUpdate(req.ChannelName, concurrentBag, (s, bag) => bag); } }); var fsw = new FileSystemWatcher("c:\\tmp\\grpcdemo") { EnableRaisingEvents = true, IncludeSubdirectories = true }; fsw.Created += async(sender, args) => { Log.Information("FileSystemWatcher Created Event Raised: {args}", args); //TODO: See if the channel name is subscribed to by the client--use the name of the directory the file is in as the channel name if (File.Exists(args.FullPath)) { var fi = new FileInfo(args.FullPath); var channelName = fi.Directory.Name; if (!String.IsNullOrEmpty(channelName)) { var msg = PubSubMessage.Parser.ParseJson(await fi.OpenText().ReadToEndAsync()); if (_subscriptions.ContainsKey(channelName)) { Log.Information("Found {channelName} in subscriptions"); foreach (var req in _subscriptions[channelName]) { var resp = new PubSubResponse { ChannelName = channelName, Id = req.Id, Message = msg }; Log.Information("Publishing message to client: {resp}", resp); await responseStream.WriteAsync(resp); } } } } }; while (true) { await Task.Delay(1000, context.CancellationToken); } //await requestTask; }
/// <summary> /// Convert the raw response to a string from the result which is an array of objects /// </summary> /// <param name="RawResult">Raw response which contains the response for all fields</param> /// <param name="ResponseTypeToFetch">Field to grab and return</param> /// <returns>The result of the fetching of the value</returns> private static string ResultToString(IList<object> RawResult, PubSubResponse ResponseTypeToFetch) { //grab the raw response byte[] SpecificResultIndex = (byte[])RawResult[(int)ResponseTypeToFetch]; //convert the byte array to a string and return it return ByteArrayToString(SpecificResultIndex); }