forked from migueldeicaza/redis-sharp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSubscriber.cs
156 lines (124 loc) · 3.63 KB
/
Subscriber.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// Subscriber.cs: ECMA CLI Binding to the Redis key-value storage system
//
// Authors:
// Miguel de Icaza ([email protected])
// Jonathan R. Steele ([email protected])
//
// Copyright 2010 Novell, Inc.
//
// Licensed under the same terms of reddis: new BSD license.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Linq;
namespace RedisSharp {
internal class Subscriber : RedisBase {
System.Threading.Thread worker;
Dictionary<string,Action<byte[]>> callBacks;
bool continueWorking;
internal Subscriber(String host, int port) : base(host, port)
{ }
public void Add(string channel, Action<byte[]> callBack)
{
AddToCallBack(channel, callBack);
if (!channel.Contains("*"))
SendCommand("SUBSCRIBE {0}\r\n", channel);
else
SendCommand("PSUBSCRIBE {0}\r\n", channel);
}
public void Remove(string channel)
{
lock(callBacks) {
if (callBacks == null) return;
if (!callBacks.ContainsKey(channel)) return;
callBacks.Remove(channel);
continueWorking = (callBacks.Count > 0);
if (channel.Contains("*"))
SendCommand("punsubscribe {0}\r\n", channel);
else
SendCommand("unsubscribe {0}\r\n", channel);
}
/* Wait for the worker thread to finish up */
if (!continueWorking) worker.Join();
}
public void RemoveAll()
{
continueWorking = false;
SendCommand("UNSUBSCRIBE\r\n");
worker.Join();
callBacks.Clear();
callBacks = null;
}
/// <summary>
/// Worker ThreadStart method to handle incoming messages from Redis.
/// </summary>
void SubscritionWorker()
{
byte[] message;
string channel = String.Empty;
while (continueWorking)
{
message = ReadData();
/* JS (09/27/2010):
* Determine the type of message coming in.
* message correlates to something subscribed via the SUBSCRIBE command
* pmessage correlates to something subscribed via the PSUBSCRIBE command.
* (This will also give us the actual channel along with the pattern)
*/
switch (Encoding.ASCII.GetString(message)) {
case "message":
message = ReadData(); /* Channel */
channel = Encoding.ASCII.GetString(message);
message = ReadData(); /* Data */
break;
case "pmessage":
message = ReadData(); /* Channel with mask */
channel = Encoding.ASCII.GetString(message);
ReadData(); /* This is the REAL channel, we don't care about that .. yet ;-) */
message = ReadData(); /* Data */
break;
default:
channel = string.Empty;
break;
}
if (channel == string.Empty) continue;
/* Determine which action we're calling */
lock(callBacks) {
callBacks[channel](message);
Log("Callback: {0}", channel);
}
}
}
/// <summary>
/// Add an Action to the dictionary of callbacks
/// </summary>
void AddToCallBack(string channel, Action<byte[]> callBack)
{
/* JS (09/26/2010):
* If the dictionary of callbacks is null, create that,
* and start the thread to listen for them
*/
if (callBacks == null || callBacks.Count == 0) {
RequireMinimumVersion("2.0.0");
callBacks = new Dictionary<string, Action<byte[]>>();
continueWorking = true;
worker = new System.Threading.Thread(SubscritionWorker);
worker.Start();
}
lock(callBacks) {
if (callBacks.ContainsKey(channel)) return;
callBacks.Add(channel, callBack);
}
}
protected override void Dispose (bool disposing)
{
if (continueWorking) {
RemoveAll();
}
base.Dispose (disposing);
}
}
}