-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathPlayer.cs
More file actions
732 lines (669 loc) · 28.7 KB
/
Copy pathPlayer.cs
File metadata and controls
732 lines (669 loc) · 28.7 KB
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
//-----------------------------------------------------------------------
// <copyright file="Player.cs" company="Akka.NET Project">
// Copyright (C) 2009-2022 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2025 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Actor.Internal;
using Akka.Annotations;
using Akka.Event;
using Akka.Pattern;
using Akka.Remote.Transport;
using Akka.Util.Internal;
using DotNetty.Transport.Channels;
using Akka.Configuration;
namespace Akka.Remote.TestKit;
/// <summary>
/// The Player is the client component of the
/// test conductor extension. It registers with
/// the conductor's controller
/// in order to participate in barriers and enable network failure injection
/// </summary>
partial class TestConductor //Player trait in JVM version
{
private IActorRef _client;
public IActorRef Client
{
get
{
if(_client == null) throw new IllegalStateException("TestConductor client not yet started");
if(_system.WhenTerminated.IsCompleted) throw new IllegalStateException("TestConductor unavailable because system is terminated; you need to StartNewSystem() before this point");
return _client;
}
}
/// <summary>
/// Connect to the conductor on the given port (the host is taken from setting
/// `akka.testconductor.host`). The connection is made asynchronously, but you
/// should await completion of the returned Future because that implies that
/// all expected participants of this test have successfully connected (i.e.
/// this is a first barrier in itself). The number of expected participants is
/// set in <see cref="TestConductor"/>`.startController()`.
/// </summary>
public Task<Done> StartClient(RoleName name, IPEndPoint controllerAddr)
{
// Use the async version with no cancellation token for consistency
return StartClientAsync(name, controllerAddr, CancellationToken.None);
}
/// <summary>
/// Connect to the conductor on the given port (the host is taken from setting
/// `akka.testconductor.host`). The connection is made asynchronously, but you
/// should await completion of the returned Future because that implies that
/// all expected participants of this test have successfully connected (i.e.
/// this is a first barrier in itself). The number of expected participants is
/// set in <see cref="TestConductor"/>`.startController()`.
/// </summary>
public Task<Done> StartClientAsync(RoleName name, IPEndPoint controllerAddr, CancellationToken cancellationToken = default)
{
if(_client != null)
throw new IllegalStateException("TestConductorClient already started");
_client = _system.ActorOf(Props.Create(() => new ClientFSM(name, controllerAddr)), "TestConductorClient");
var a = _system.ActorOf(Props.Create<WaitForClientFSMToConnect>());
return a.Ask<Done>(_client, cancellationToken);
}
private class WaitForClientFSMToConnect : UntypedActor
{
IActorRef _waiting;
protected override void OnReceive(object message)
{
if (message is IActorRef fsm)
{
_waiting = Sender;
fsm.Tell(new FSMBase.SubscribeTransitionCallBack(Self));
return;
}
if (message is FSMBase.Transition<ClientFSM.State> transition)
{
switch (transition.From)
{
case ClientFSM.State.Connecting when transition.To == ClientFSM.State.AwaitDone:
return;
case ClientFSM.State.AwaitDone when transition.To == ClientFSM.State.Connected:
_waiting.Tell(Done.Instance);
Context.Stop(Self);
return;
default:
_waiting.Tell(new Exception("unexpected transition: " + transition));
Context.Stop(Self);
break;
}
}
if (message is not FSMBase.CurrentState<ClientFSM.State> { State: ClientFSM.State.Connected }) return;
_waiting.Tell(Done.Instance);
Context.Stop(Self);
}
}
/// <summary>
/// Enter the named barriers, one after the other, in the order given. Will
/// throw an exception in case of timeouts or other errors.
/// </summary>
public void Enter(RoleName roleName, string name)
{
// Use sync-over-async pattern to maintain single source of truth
try
{
EnterAsync(Settings.BarrierTimeout, roleName, ImmutableList.Create(name), CancellationToken.None).GetAwaiter().GetResult();
}
catch (AggregateException ex) when (ex.InnerException != null)
{
throw ex.InnerException;
}
}
/// <summary>
/// Async version of Enter. Enter the named barrier.
/// Will throw an exception in case of timeouts or other errors.
/// </summary>
public Task EnterAsync(RoleName roleName, string name, CancellationToken cancellationToken = default)
{
return EnterAsync(Settings.BarrierTimeout, roleName, ImmutableList.Create(name), cancellationToken);
}
/// <summary>
/// Enter the named barriers, one after the other, in the order given. Will
/// throw an exception in case of timeouts or other errors.
/// </summary>
public void Enter(TimeSpan timeout, RoleName roleName, ImmutableList<string> names)
{
// Use sync-over-async pattern to maintain single source of truth
try
{
EnterAsync(timeout, roleName, names, CancellationToken.None).GetAwaiter().GetResult();
}
catch (AggregateException ex) when (ex.InnerException != null)
{
throw ex.InnerException;
}
}
/// <summary>
/// Async version of Enter. Enter the named barriers, one after the other, in the order given.
/// Will throw an exception in case of timeouts or other errors.
/// </summary>
public async Task EnterAsync(TimeSpan timeout, RoleName roleName, ImmutableList<string> names, CancellationToken cancellationToken = default)
{
_system.Log.Debug("entering barriers {0}", names.Aggregate((a, b) => "(" + a + "," + b + ")"));
var stop = Deadline.Now + timeout;
foreach (var name in names)
{
var barrierTimeout = stop.TimeLeft;
if (barrierTimeout.Ticks < 0)
{
_client.Tell(new ToServer<FailBarrier>(new FailBarrier(name, roleName)));
throw new TimeoutException("Server timed out while waiting for barrier " + name);
}
try
{
var askTimeout = barrierTimeout + Settings.QueryTimeout;
// Use async ask with cancellation token
// A failed barrier now faults this ask (see the Status.Failure note in ClientFSM), so the
// exception propagates to the caller instead of the spec silently continuing unsynchronized.
await _client.Ask(new ToServer<EnterBarrier>(new EnterBarrier(name, barrierTimeout, roleName)), askTimeout, cancellationToken);
}
catch (TaskCanceledException ex)
{
_client.Tell(new ToServer<FailBarrier>(new FailBarrier(name, roleName)));
throw new TimeoutException("Client timed out while waiting for barrier " + name, ex);
}
catch (OperationCanceledException ex)
{
_client.Tell(new ToServer<FailBarrier>(new FailBarrier(name, roleName)));
throw new TimeoutException("Operation was cancelled while waiting for barrier " + name, ex);
}
_system.Log.Debug("passed barrier {0}", name);
}
}
public Task<Address> GetAddressFor(RoleName name)
{
return GetAddressForAsync(name, CancellationToken.None);
}
/// <summary>
/// Async version of GetAddressFor with cancellation token support.
/// </summary>
public Task<Address> GetAddressForAsync(RoleName name, CancellationToken cancellationToken = default)
{
return _client.Ask<Address>(new ToServer<GetAddress>(new GetAddress(name)), Settings.QueryTimeout, cancellationToken);
}
}
/// <summary>
/// This is the controlling entity on the player
/// side: in a first step it registers itself with a symbolic name and its remote
/// address at the <see cref="Controller"/>, then waits for the
/// `Done` message which signals that all other expected test participants have
/// done the same. After that, it will pass barrier requests to and from the
/// coordinator and react to the Conductors’s
/// requests for failure injection.
///
/// Note that you can't perform requests concurrently, e.g. enter barrier
/// from one thread and ask for node address from another thread.
///
/// INTERNAL API.
/// </summary>
[InternalApi]
internal class ClientFSM : FSM<ClientFSM.State, ClientFSM.Data>, ILoggingFSM
{
public enum State
{
Connecting,
AwaitDone,
Connected,
Failed
}
internal class Data
{
public IChannel Channel { get; }
public (string, IActorRef)? RunningOp { get; }
public Data(IChannel channel, (string, IActorRef)? runningOp)
{
Channel = channel;
RunningOp = runningOp;
}
private bool Equals(Data other)
{
return Equals(Channel, other.Channel) && Equals(RunningOp, other.RunningOp);
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
return obj is Data data && Equals(data);
}
/// <inheritdoc/>
public override int GetHashCode()
{
unchecked
{
return ((Channel != null ? Channel.GetHashCode() : 0) * 397)
^ (RunningOp != null ? RunningOp.GetHashCode() : 0);
}
}
/// <summary>
/// Compares two specified <see cref="Data"/> for equality.
/// </summary>
/// <param name="left">The first <see cref="Data"/> used for comparison</param>
/// <param name="right">The second <see cref="Data"/> used for comparison</param>
/// <returns><c>true</c> if both <see cref="Data"/> are equal; otherwise <c>false</c></returns>
public static bool operator ==(Data left, Data right)
{
return Equals(left, right);
}
/// <summary>
/// Compares two specified <see cref="Data"/> for inequality.
/// </summary>
/// <param name="left">The first <see cref="Data"/> used for comparison</param>
/// <param name="right">The second <see cref="Data"/> used for comparison</param>
/// <returns><c>true</c> if both <see cref="Data"/> are not equal; otherwise <c>false</c></returns>
public static bool operator !=(Data left, Data right)
{
return !Equals(left, right);
}
public Data Copy((string, IActorRef)? runningOp)
{
return new Data(Channel, runningOp);
}
}
internal class Connected : INoSerializationVerificationNeeded
{
public IChannel Channel { get; }
public Connected(IChannel channel)
{
Channel = channel;
}
protected bool Equals(Connected other)
{
return Equals(Channel, other.Channel);
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
return obj is Connected connected && Equals(connected);
}
/// <inheritdoc/>
public override int GetHashCode()
{
return (Channel != null ? Channel.GetHashCode() : 0);
}
/// <summary>
/// Compares two specified <see cref="Connected"/> for equality.
/// </summary>
/// <param name="left">The first <see cref="Connected"/> used for comparison</param>
/// <param name="right">The second <see cref="Connected"/> used for comparison</param>
/// <returns><c>true</c> if both <see cref="Connected"/> are equal; otherwise <c>false</c></returns>
public static bool operator ==(Connected left, Connected right)
{
return Equals(left, right);
}
/// <summary>
/// Compares two specified <see cref="Connected"/> for inequality.
/// </summary>
/// <param name="left">The first <see cref="Connected"/> used for comparison</param>
/// <param name="right">The second <see cref="Connected"/> used for comparison</param>
/// <returns><c>true</c> if both <see cref="Connected"/> are not equal; otherwise <c>false</c></returns>
public static bool operator !=(Connected left, Connected right)
{
return !Equals(left, right);
}
}
/// <summary>
/// TBD
/// </summary>
internal class ConnectionFailure : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="ConnectionFailure"/> class.
/// </summary>
/// <param name="message">The message that describes the error.</param>
public ConnectionFailure(string message) : base(message)
{
}
}
internal class Disconnected
{
private Disconnected() { }
public static Disconnected Instance { get; } = new();
}
private readonly ILoggingAdapter _log = Context.GetLogger();
private readonly TestConductorSettings _settings;
private readonly PlayerHandler _handler;
private readonly RoleName _name;
public ClientFSM(RoleName name, IPEndPoint controllerAddr)
{
_settings = TestConductor.Get(Context.System).Settings;
_handler = new PlayerHandler(controllerAddr, _settings.ClientReconnects, _settings.ReconnectBackoff,
_settings.ClientSocketWorkerPoolSize, Self, Logging.GetLogger(Context.System, "PlayerHandler"),
Context.System.Scheduler);
_name = name;
InitFSM();
}
public void InitFSM()
{
StartWith(State.Connecting, new Data(null, null));
When(State.Connecting, @event =>
{
if (@event.FsmEvent is IClientOp)
{
return Stay().Replying(new Status.Failure(new IllegalStateException("not connected yet")));
}
var connected = @event.FsmEvent as Connected;
if (connected != null)
{
connected.Channel.WriteAndFlushAsync(new Hello(_name.Name, TestConductor.Get(Context.System).Address));
return GoTo(State.AwaitDone).Using(new Data(connected.Channel, null));
}
if (@event.FsmEvent is ConnectionFailure)
{
return GoTo(State.Failed);
}
if (@event.FsmEvent is StateTimeout)
{
_log.Error($"Failed to connect to test conductor within {_settings.ConnectTimeout.TotalMilliseconds} ms.");
return GoTo(State.Failed);
}
return null;
}, _settings.ConnectTimeout);
When(State.AwaitDone, @event =>
{
switch (@event.FsmEvent)
{
case Done:
_log.Debug("received Done: starting test");
return GoTo(State.Connected);
case INetworkOp:
_log.Error("Received {0} instead of Done", @event.FsmEvent);
return GoTo(State.Failed);
case IServerOp:
return Stay().Replying(new Status.Failure(new IllegalStateException("not connected yet")));
case StateTimeout:
_log.Error("connect timeout to TestConductor");
return GoTo(State.Failed);
default:
return null;
}
}, _settings.BarrierTimeout);
When(State.Connected, @event =>
{
if (@event.FsmEvent is Disconnected)
{
_log.Info("disconnected from TestConductor");
throw new ConnectionFailure("disconnect");
}
if(@event.FsmEvent is ToServer<Done> && @event.StateData.Channel != null)
{
@event.StateData.Channel.WriteAndFlushAsync(Done.Instance);
return Stay();
}
var toServer = @event.FsmEvent as IToServer;
if (toServer != null && @event.StateData.Channel != null &&
@event.StateData.RunningOp == null)
{
@event.StateData.Channel.WriteAndFlushAsync(toServer.Msg);
string token = null;
var enterBarrier = @event.FsmEvent as ToServer<EnterBarrier>;
if (enterBarrier != null) token = enterBarrier.Msg.Name;
else
{
var getAddress = @event.FsmEvent as ToServer<GetAddress>;
if (getAddress != null) token = getAddress.Msg.Node.Name;
}
return Stay().Using(@event.StateData.Copy(runningOp: (token, Sender)));
}
if (toServer != null && @event.StateData.Channel != null &&
@event.StateData.RunningOp != null)
{
_log.Error("cannot write {0} while waiting for {1}", toServer.Msg, @event.StateData.RunningOp);
return Stay();
}
if (@event.FsmEvent is IClientOp && @event.StateData.Channel != null)
{
var barrierResult = @event.FsmEvent as BarrierResult;
if (barrierResult != null)
{
if (@event.StateData.RunningOp == null)
{
_log.Warning("did not expect {0}", @event.FsmEvent);
}
else
{
object response;
// NOTE: these MUST be Status.Failure. The unqualified name `Failure` binds to the
// inherited FSMBase.Failure (an FSM termination reason), which the ask completion
// switch in FutureActorRef does not treat as a fault - it falls through to
// `case T t: TrySetResult(t)`, so the ask completes SUCCESSFULLY and the caller
// walks through a barrier that never synchronized.
if (barrierResult.Name != @event.StateData.RunningOp.Value.Item1)
{
response =
new Status.Failure(
new Exception("wrong barrier " + barrierResult + " received while waiting for " +
@event.StateData.RunningOp.Value.Item1));
}
else if (!barrierResult.Success)
{
response =
new Status.Failure(
new Exception("barrier failed:" + @event.StateData.RunningOp.Value.Item1));
}
else
{
response = barrierResult.Name;
}
@event.StateData.RunningOp.Value.Item2.Tell(response);
}
return Stay().Using(@event.StateData.Copy(runningOp: null));
}
var addressReply = @event.FsmEvent as AddressReply;
if (addressReply != null)
{
if (@event.StateData.RunningOp == null)
{
_log.Warning("did not expect {0}", @event.FsmEvent);
}
else
{
@event.StateData.RunningOp.Value.Item2.Tell(addressReply.Addr);
}
return Stay().Using(@event.StateData.Copy(runningOp: null));
}
var throttleMsg = @event.FsmEvent as ThrottleMsg;
if (@event.FsmEvent is ThrottleMsg)
{
ThrottleMode mode;
if (throttleMsg.RateMBit < 0.0f) mode = Unthrottled.Instance;
else if (throttleMsg.RateMBit == 0.0f) mode = Blackhole.Instance;
else mode = new Transport.TokenBucket(1000, throttleMsg.RateMBit*125000, 0, 0);
var cmdTask =
TestConductor.Get(Context.System)
.Transport.ManagementCommand(new SetThrottle(throttleMsg.Target, throttleMsg.Direction,
mode));
var self = Self;
cmdTask.ContinueWith(t =>
{
if (t.IsFaulted)
throw new ConfigurationException("Throttle was requested from the TestConductor, but no transport " +
"adapters available that support throttling. Specify 'testTransport(on=true)' in your MultiNodeConfig");
self.Tell(new ToServer<Done>(Done.Instance));
});
return Stay();
}
if (@event.FsmEvent is DisconnectMsg)
return Stay(); //FIXME is this the right EC for the future below?
var terminateMsg = @event.FsmEvent as TerminateMsg;
if (terminateMsg != null)
{
_log.Info("Received TerminateMsg - shutting down...");
if (terminateMsg.ShutdownOrExit.IsLeft && terminateMsg.ShutdownOrExit.ToLeft().Value == false)
{
Context.System.Terminate();
return Stay();
}
if (terminateMsg.ShutdownOrExit.IsLeft && terminateMsg.ShutdownOrExit.ToLeft().Value == true)
{
Context.System.AsInstanceOf<ActorSystemImpl>().Abort();
return Stay();
}
if (terminateMsg.ShutdownOrExit.IsRight)
{
Environment.Exit(terminateMsg.ShutdownOrExit.ToRight().Value);
return Stay();
}
}
if (@event.FsmEvent is Done) return Stay(); //FIXME what should happen?
}
return null;
});
When(State.Failed, @event =>
{
if (@event.FsmEvent is IClientOp)
{
return Stay().Replying(new Status.Failure(new Exception("cannot do " + @event.FsmEvent + " while failed")));
}
if (@event.FsmEvent is INetworkOp)
{
_log.Warning("ignoring network message {0} while Failed", @event.FsmEvent);
return Stay();
}
return null;
});
OnTermination(e =>
{
_log.Info("Terminating connection to multi-node test controller due to [{0}]", e.Reason);
if (e.StateData.Channel != null)
{
var disconnectTimeout = TimeSpan.FromSeconds(2); //todo: make into setting loaded from HOCON
if (!e.StateData.Channel.CloseAsync().Wait(disconnectTimeout))
{
_log.Warning("Failed to disconnect from conductor within {0}", disconnectTimeout);
}
}
});
Initialize();
}
}
/// <summary>
/// This handler only forwards messages received from the conductor to the <see cref="ClientFSM"/>
///
/// INTERNAL API.
/// </summary>
internal class PlayerHandler : ChannelHandlerAdapter
{
private readonly IPEndPoint _server;
private int _reconnects;
private readonly TimeSpan _backoff;
private readonly int _poolSize;
private readonly IActorRef _fsm;
private readonly ILoggingAdapter _log;
private readonly IScheduler _scheduler;
private bool _loggedDisconnect = false;
private Deadline _nextAttempt;
/// <summary>
/// Shareable, since the handler may be added multiple times during reconnect
/// </summary>
public override bool IsSharable => true;
public PlayerHandler(IPEndPoint server, int reconnects, TimeSpan backoff, int poolSize, IActorRef fsm,
ILoggingAdapter log, IScheduler scheduler)
{
_server = server;
_reconnects = reconnects;
_backoff = backoff;
_poolSize = poolSize;
_fsm = fsm;
_log = log;
_scheduler = scheduler;
Reconnect();
}
private static string FormatConnectionFailure(IChannelHandlerContext context, Exception exception)
{
var sb = new StringBuilder();
sb.AppendLine($"Connection between [Local: {context.Channel.LocalAddress}] and [Remote: {context.Channel.RemoteAddress}] has failed.");
sb.AppendLine($"Cause: {exception}");
sb.AppendLine($"Trace: {exception.StackTrace}");
return sb.ToString();
}
public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
{
_log.Debug("channel {0} exception {1}", context.Channel, exception);
if (exception is ConnectException && _reconnects > 0)
{
_reconnects -= 1;
if (_nextAttempt.IsOverdue)
{
Reconnect();
}
else
{
_scheduler.Advanced.ScheduleOnce(_nextAttempt.TimeLeft, Reconnect);
}
return;
}
_fsm.Tell(new ClientFSM.ConnectionFailure(FormatConnectionFailure(context, exception)));
}
private void Reconnect()
{
_log.Debug("Connecting...");
_nextAttempt = Deadline.Now + _backoff;
RemoteConnection.CreateConnection(Role.Client, _server, _poolSize, this).ContinueWith(_ =>
{
_log.Debug("Failed to connect.... Retrying again in {0}s. {1} attempts left.", _nextAttempt.TimeLeft,_reconnects);
if (_reconnects > 0)
{
_reconnects -= 1;
if (_nextAttempt.IsOverdue)
{
Reconnect();
}
else
{
_scheduler.Advanced.ScheduleOnce(_nextAttempt.TimeLeft, Reconnect);
}
}
}, TaskContinuationOptions.NotOnRanToCompletion);
}
public override void ChannelActive(IChannelHandlerContext context)
{
_log.Debug("connected to {0}", context.Channel.RemoteAddress);
_fsm.Tell(new ClientFSM.Connected(context.Channel));
context.FireChannelActive();
}
public override void ChannelInactive(IChannelHandlerContext context)
{
if (!_loggedDisconnect) //added this to help mute log messages
{
_loggedDisconnect = true;
_log.Debug("disconnected from {0}", context.Channel.RemoteAddress);
}
_fsm.Tell(PoisonPill.Instance);
// run outside of the Helios / DotNetty threadpool
Task.Factory.StartNew(() =>
{
RemoteConnection.Shutdown(context.Channel);
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
RemoteConnection.ReleaseAll(); // yep, let it run asynchronously.
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
}, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
context.FireChannelInactive();
}
public override void ChannelRead(IChannelHandlerContext context, object message)
{
var channel = context.Channel;
_log.Debug("message from {0}, {1}", channel.RemoteAddress, message);
if (message is INetworkOp)
{
_fsm.Tell(message);
return;
}
_log.Info("server {0} sent garbage '{1}', disconnecting", channel.RemoteAddress, message);
channel.CloseAsync();
}
public override Task CloseAsync(IChannelHandlerContext context)
{
_log.Info("Client: disconnecting {0} from {1}", context.Channel.LocalAddress, context.Channel.RemoteAddress);
return base.CloseAsync(context);
}
}