forked from yonglehou/dn-phoenix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Channel.cs
368 lines (319 loc) · 10.9 KB
/
Channel.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Phoenix
{
public class Channel
{
private enum ChannelState
{
Closed,
Errored,
Joined,
Joining,
}
private class Binding
{
public string Event { get; }
public Action<JObject, string> Callback { get; }
public Binding(string event_, Action<JObject, string> callback)
{
Event = event_;
Callback = callback;
}
}
private ChannelState _state;
private string _topic;
private JObject _params;
private Socket _socket;
private IList<Binding> _bindings;
private int _timeout;
private bool _joinedOnce;
private Push _joinPush;
private IList<Push> _pushBuffer;
private RetryTimer _rejoinTimer;
public string Topic { get { return _topic; } }
public Socket Socket { get { return _socket; } }
//constructor(topic, params, socket) {
// this.state = CHANNEL_STATES.closed
// this.topic = topic
// this.params = params || {}
// this.socket = socket
// this.bindings = []
// this.timeout = this.socket.timeout
// this.joinedOnce = false
// this.joinPush = new Push(this, CHANNEL_EVENTS.join, this.params, this.timeout)
// this.pushBuffer = []
// this.rejoinTimer = new Timer(
// () => this.rejoinUntilConnected(),
// this.socket.reconnectAfterMs
// )
// this.joinPush.receive("ok", () => {
// this.state = CHANNEL_STATES.joined
// this.rejoinTimer.reset()
// this.pushBuffer.forEach( pushEvent => pushEvent.send() )
// this.pushBuffer = []
// })
// this.onClose( () => {
// this.socket.log("channel", `close ${this.topic}`)
// this.state = CHANNEL_STATES.closed
// this.socket.remove(this)
// })
// this.onError( reason => {
// this.socket.log("channel", `error ${this.topic}`, reason)
// this.state = CHANNEL_STATES.errored
// this.rejoinTimer.setTimeout()
// })
// this.joinPush.receive("timeout", () => {
// if(this.state !== CHANNEL_STATES.joining){ return }
// this.socket.log("channel", `timeout ${this.topic}`, this.joinPush.timeout)
// this.state = CHANNEL_STATES.errored
// this.rejoinTimer.setTimeout()
// })
// this.on(CHANNEL_EVENTS.reply, (payload, ref) => {
// this.trigger(this.replyEventName(ref), payload)
// })
//}
public Channel(string topic, JObject params_, Socket socket)
{
_state = ChannelState.Closed;
_topic = topic;
_params = params_ ?? Phoenix.EMPTY_JS_OBJECT;
_socket = socket;
_bindings = new List<Binding>();
_timeout = _socket.Timeout;
_joinedOnce = false;
_joinPush = new Push(this, Phoenix.CHANNEL_EVENT_JOIN, _params, _timeout);
_pushBuffer = new List<Push>();
_rejoinTimer = new RetryTimer(RejoinUntilConnected, _socket.ReconnectAfterMs); //jfis - why another timer instead of waiting for socket event?
_joinPush.Receive("ok",
(_) =>
{
_socket.Log("JP REC OK", "");
_state = ChannelState.Joined;
_rejoinTimer.Reset();
foreach (var p in _pushBuffer) p.Send();
_pushBuffer.Clear();
}
);
OnClose(() =>
{
_socket.Log("channel", $"close {_topic}");
_state = ChannelState.Closed;
_socket.Remove(this);
});
OnError(
() => //reason only used for logging
{
_socket.Log("channel", $"error {_topic}"); //, reason);
_state = ChannelState.Errored;
_rejoinTimer.SetTimeout();
}
);
_joinPush.Receive("timeout",
(_) =>
{
if (_state == ChannelState.Joining) return;
_socket.Log("channel", $"timeout {_topic}");//, _joinPush.timeout)
_state = ChannelState.Errored;
_rejoinTimer.SetTimeout();
}
);
On(Phoenix.CHANNEL_EVENT_REPLY, OnReply);
}
private void OnReply(JObject payload, string ref_)
{
Trigger(ReplyEventName(ref_), payload);
}
//rejoinUntilConnected(){
// this.rejoinTimer.setTimeout()
// if(this.socket.isConnected()){
// this.rejoin()
// }
//}
public void RejoinUntilConnected()
{
_socket.Log("timer", "chan rejoin");
_rejoinTimer.SetTimeout();
if (_socket.IsConnected()) //jfis - instead of checking, socket should tell channel
{
Rejoin();
}
}
//join(timeout = this.timeout){
// if(this.joinedOnce){
// throw(`tried to join multiple times. 'join' can only be called a single time per channel instance`)
// } else {
// this.joinedOnce = true
// }
// this.rejoin(timeout)
// return this.joinPush
//}
public Push Join() => Join(_timeout);
public Push Join(int timeout)
{
if (_joinedOnce) //jfis - necessary? can probably just ignore subsequent times
{
//return;
throw new Exception("tried to join multiple times. 'join' can only be called a single time per channel instance");
}
_joinedOnce = true;
Rejoin(timeout);
return _joinPush;
}
//onClose(callback){ this.on(CHANNEL_EVENTS.close, callback) }
public void OnClose(Action callback)
{
On(Phoenix.CHANNEL_EVENT_CLOSE, callback);
}
//onError(callback){
// this.on(CHANNEL_EVENTS.error, reason => callback(reason) )
//}
public void OnError(Action callback)
{
On(Phoenix.CHANNEL_EVENT_ERROR, () => callback());
}
//on(event, callback){ this.bindings.push({event, callback}) }
public void On(string event_, Action callback)
{
_bindings.Add(new Binding(event_, (_, __) => callback()));
}
public void On(string event_, Action<JObject, string> callback)
{
_bindings.Add(new Binding(event_, callback));
}
//off(event){ this.bindings = this.bindings.filter( bind => bind.event !== event ) }
public void Off(string event_)
{
_bindings = _bindings.Where(b => b.Event != event_).ToList();
}
//canPush(){ return this.socket.isConnected() && this.state === CHANNEL_STATES.joined }
public bool CanPush()
{
return _socket.IsConnected() && _state == ChannelState.Joined; //jfis - another _socket call
}
//push(event, payload, timeout = this.timeout){
// if(!this.joinedOnce){
// throw(`tried to push '${event}' to '${this.topic}' before joining. Use channel.join() before pushing events`)
// }
// let pushEvent = new Push(this, event, payload, timeout)
// if(this.canPush()){
// pushEvent.send()
// } else {
// pushEvent.startTimeout()
// this.pushBuffer.push(pushEvent)
// }
// return pushEvent
//}
public Push Push(string event_, JObject payload) => Push(event_, payload, _timeout);
public Push Push(string event_, JObject payload, int timeout)
{
if (!_joinedOnce) //jfis - necessary?
{
throw new Exception($"tried to push '{event_}' to '{_topic}' before joining. Use channel.join() before pushing events");
}
var pushEvent = new Push(this, event_, payload, timeout);
if (CanPush())
{
pushEvent.Send(); //jfis - send now if can
}
else
{
pushEvent.StartTimeout(); //jfis - if cant add to buffer, but what does start timeout do?
_pushBuffer.Add(pushEvent);
}
return pushEvent;
}
//// Leaves the channel
////
//// Unsubscribes from server events, and
//// instructs channel to terminate on server
////
//// Triggers onClose() hooks
////
//// To receive leave acknowledgements, use the a `receive`
//// hook to bind to the server ack, ie:
////
//// channel.leave().receive("ok", () => alert("left!") )
////
//leave(timeout = this.timeout){
// let onClose = () => {
// this.socket.log("channel", `leave ${this.topic}`)
// this.trigger(CHANNEL_EVENTS.close, "leave")
// }
// let leavePush = new Push(this, CHANNEL_EVENTS.leave, {}, timeout)
// leavePush.receive("ok", () => onClose() )
// .receive("timeout", () => onClose() )
// leavePush.send()
// if(!this.canPush()){ leavePush.trigger("ok", {}) }
// return leavePush
//}
public Push Leave() => Leave(_timeout);
public Push Leave(int timeout)
{
Action onClose = () =>
{
_socket.Log("channel", $"leave ${_topic}"); //jfis - seems odd to tie logs to socket
Trigger(Phoenix.CHANNEL_EVENT_CLOSE);//, "leave");
};
var leavePush = new Push(this, Phoenix.CHANNEL_EVENT_LEAVE, Phoenix.EMPTY_JS_OBJECT, timeout);
leavePush
.Receive("ok", (_) => onClose())
.Receive("timeout", (_) => onClose());
leavePush.Send();
if (!CanPush()) //jfis - if cant push simulate ok
{
leavePush.Trigger("ok", Phoenix.EMPTY_JS_OBJECT);
}
return leavePush;
}
//// Overridable message hook
////
//// Receives all events for specialized message handling
//onMessage(event, payload, ref){}
public void OnMessage(string event_, JObject payload, string ref_)
{
//??? TODO
//jfis - can probably just remove this func
payload = payload ?? Phoenix.EMPTY_JS_OBJECT;
_socket.Log($"{event_} | {ref_}", payload.ToString(Newtonsoft.Json.Formatting.None));
}
//// private
//isMember(topic){ return this.topic === topic }
public bool IsMember(string topic)
{
return _topic == topic; //jfis - is a channel a "member" of a topic? seems like wrong wording
}
//sendJoin(timeout){
// this.state = CHANNEL_STATES.joining
// this.joinPush.resend(timeout)
//}
private void SendJoin(int timeout)
{
_state = ChannelState.Joining;
_joinPush.Resend(timeout);
}
//rejoin(timeout = this.timeout){ this.sendJoin(timeout) }
private void Rejoin() => Rejoin(_timeout);
private void Rejoin(int timeout)
{
SendJoin(timeout); //jfis - rejoin is only called once, from rejoin until connected. and always with no params. then only calls sendjoin. seems convoluted. why not call sendJoin?
}
//trigger(triggerEvent, payload, ref){
// this.onMessage(triggerEvent, payload, ref)
// this.bindings.filter( bind => bind.event === triggerEvent )
// .map( bind => bind.callback(payload, ref) )
//}
internal void Trigger(string triggerEvent, JObject payload = null, string ref_ = null)
{
OnMessage(triggerEvent, payload, ref_); //jfis - this might only be valuable in js land
_bindings.Where(b => b.Event == triggerEvent).ToList().ForEach(b => b.Callback(payload, ref_));
}
//replyEventName(ref){ return `chan_reply_${ref}` }
internal string ReplyEventName(string ref_)
{
return $"chan_reply_{ref_}";
}
}
}