-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathConductor.cs
More file actions
611 lines (564 loc) · 29.3 KB
/
Copy pathConductor.cs
File metadata and controls
611 lines (564 loc) · 29.3 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
//-----------------------------------------------------------------------
// <copyright file="Conductor.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.Concurrent;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Event;
using Akka.Pattern;
using Akka.Remote.Transport;
using Akka.Util;
using DotNetty.Transport.Channels;
using Akka.Configuration;
namespace Akka.Remote.TestKit
{
/// <summary>
/// The conductor is the one orchestrating the test: it governs the
/// <see cref="Akka.Remote.TestKit.Controller"/>'s ports to which all
/// Players connect, it issues commands to their
/// <see cref="FailureInjectorTransportAdapter"/> and provides support
/// for barriers using the <see cref="Akka.Remote.TestKit.BarrierCoordinator"/>.
/// All of this is bundled inside the <see cref="TestConductor"/>
/// </summary>
partial class TestConductor //Conductor trait in JVM version
{
IActorRef _controller;
public IActorRef Controller
{
get
{
if(_controller == null) throw new IllegalStateException("TestConductorServer was not started");
return _controller;
}
}
/// <summary>
/// Start the <see cref="Controller"/>, which in turn will
/// bind to a TCP port as specified in the `akka.testconductor.port` config
/// property, where 0 denotes automatic allocation. Since the latter is
/// actually preferred, a `Future[Int]` is returned which will be completed
/// with the port number actually chosen, so that this can then be communicated
/// to the players for their proper start-up.
///
/// This method also invokes Player.startClient,
/// since it is expected that the conductor participates in barriers for
/// overall coordination. The returned Future will only be completed once the
/// client’s start-up finishes, which in fact waits for all other players to
/// connect.
/// </summary>
/// <param name="participants">participants gives the number of participants which shall connect
/// before any of their startClient() operations complete
/// </param>
/// <param name="name"></param>
/// <param name="controllerPort"></param>
/// <returns></returns>
public Task<IPEndPoint> StartController(int participants, RoleName name, IPEndPoint controllerPort)
{
return StartControllerAsync(participants, name, controllerPort, CancellationToken.None);
}
/// <summary>
/// Start the <see cref="Controller"/>, which in turn will
/// bind to a TCP port as specified in the `akka.testconductor.port` config
/// property, where 0 denotes automatic allocation. Since the latter is
/// actually preferred, a `Future[Int]` is returned which will be completed
/// with the port number actually chosen, so that this can then be communicated
/// to the players for their proper start-up.
///
/// This method also invokes Player.startClient,
/// since it is expected that the conductor participates in barriers for
/// overall coordination. The returned Future will only be completed once the
/// client’s start-up finishes, which in fact waits for all other players to
/// connect.
/// </summary>
/// <param name="participants">participants gives the number of participants which shall connect
/// before any of their startClient() operations complete
/// </param>
/// <param name="name"></param>
/// <param name="controllerPort"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<IPEndPoint> StartControllerAsync(int participants, RoleName name, IPEndPoint controllerPort, CancellationToken cancellationToken = default)
{
if(_controller != null) throw new IllegalStateException("TestConductorServer was already started");
_controller = _system.ActorOf(Props.Create(() => new Controller(participants, controllerPort)),
"controller");
var node = await _controller.Ask<IPEndPoint>(TestKit.Controller.GetSockAddr.Instance, Settings.QueryTimeout, cancellationToken);
await StartClient(name, node);
return node;
}
/// <summary>
/// Make the remoting pipeline on the node throttle data sent to or received
/// from the given remote peer. Throttling works by delaying packet submission
/// within the netty pipeline until the packet would have been completely sent
/// according to the given rate, the previous packet completion and the current
/// packet length. In case of large packets they are split up if the calculated
/// end pause would exceed `akka.testconductor.packet-split-threshold`
/// (roughly). All of this uses the system’s scheduler, which is not
/// terribly precise and will execute tasks later than they are schedule (even
/// on average), but that is countered by using the actual execution time for
/// determining how much to send, leading to the correct output rate, but with
/// increased latency.
///
/// ====Note====
/// To use this feature you must activate the failure injector and throttler
/// transport adapters by specifying `testTransport(on = true)` in your MultiNodeConfig.
/// </summary>
/// <param name="node">is the symbolic name of the node which is to be affected</param>
/// <param name="target">is the symbolic name of the other node to which connectivity shall be throttled</param>
/// <param name="direction">can be either `Direction.Send`, `Direction.Receive` or `Direction.Both`</param>
/// <param name="rateMBit">is the maximum data rate in MBit</param>
/// <returns></returns>
public Task<Done> Throttle(RoleName node, RoleName target, ThrottleTransportAdapter.Direction direction,
float rateMBit)
{
return ThrottleAsync(node, target, direction, rateMBit, CancellationToken.None);
}
/// <summary>
/// Async version of Throttle with cancellation token support.
/// </summary>
public Task<Done> ThrottleAsync(RoleName node, RoleName target, ThrottleTransportAdapter.Direction direction,
float rateMBit, CancellationToken cancellationToken = default)
{
RequireTestConductorTransport();
return Controller.Ask<Done>(new Throttle(node, target, direction, rateMBit), Settings.QueryTimeout, cancellationToken);
}
/// <summary>
/// Switch the helios pipeline of the remote support into blackhole mode for
/// sending and/or receiving: it will just drop all messages right before
/// submitting them to the Socket or right after receiving them from the
/// Socket.
///
/// ====Note====
/// To use this feature you must activate the failure injector and throttler
/// transport adapters by specifying `testTransport(on = true)` in your MultiNodeConfig.
/// </summary>
/// <param name="node">is the symbolic name of the node which is to be affected</param>
/// <param name="target">is the symbolic name of the other node to which connectivity shall be impeded</param>
/// <param name="direction">can be either `Direction.Send`, `Direction.Receive` or `Direction.Both`</param>
/// <returns></returns>
public Task<Done> Blackhole(RoleName node, RoleName target, ThrottleTransportAdapter.Direction direction)
{
return BlackholeAsync(node, target, direction, CancellationToken.None);
}
/// <summary>
/// Async version of Blackhole with cancellation token support.
/// Switch the helios pipeline of the remote support into blackhole mode for
/// sending and/or receiving: it will just drop all messages right before
/// submitting them to the Socket or right after receiving them from the
/// Socket.
///
/// ====Note====
/// To use this feature you must activate the failure injector and throttler
/// transport adapters by specifying `testTransport(on = true)` in your MultiNodeConfig.
/// </summary>
/// <param name="node">is the symbolic name of the node which is to be affected</param>
/// <param name="target">is the symbolic name of the other node to which connectivity shall be impeded</param>
/// <param name="direction">can be either `Direction.Send`, `Direction.Receive` or `Direction.Both`</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Task indicating completion</returns>
public Task<Done> BlackholeAsync(RoleName node, RoleName target, ThrottleTransportAdapter.Direction direction, CancellationToken cancellationToken = default)
{
return ThrottleAsync(node, target, direction, 0f, cancellationToken);
}
private void RequireTestConductorTransport()
{
// Verifies that the Throttle and FailureInjector TransportAdapters are active
if(!Transport.DefaultAddress.Protocol.Contains(".trttl.gremlin."))
throw new ConfigurationException("To use this feature you must activate the failure injector adapters " +
"(trttl, gremlin) by specifying `TestTransport(on = true)` in your MultiNodeConfig.");
}
/// <summary>
/// Switch the Helios pipeline of the remote support into pass through mode for
/// sending and/or receiving.
///
/// ====Note====
/// To use this feature you must activate the failure injector and throttler
/// transport adapters by specifying `testTransport(on = true)` in your MultiNodeConfig.
/// </summary>
/// <param name="node">is the symbolic name of the node which is to be affected</param>
/// <param name="target">is the symbolic name of the other node to which connectivity shall be impeded</param>
/// <param name="direction">can be either `Direction.Send`, `Direction.Receive` or `Direction.Both`</param>
/// <returns></returns>
public Task<Done> PassThrough(RoleName node, RoleName target, ThrottleTransportAdapter.Direction direction)
{
return PassThroughAsync(node, target, direction, CancellationToken.None);
}
/// <summary>
/// Async version of PassThrough with cancellation token support.
/// Switch the Helios pipeline of the remote support into pass through mode for
/// sending and/or receiving.
///
/// ====Note====
/// To use this feature you must activate the failure injector and throttler
/// transport adapters by specifying `testTransport(on = true)` in your MultiNodeConfig.
/// </summary>
/// <param name="node">is the symbolic name of the node which is to be affected</param>
/// <param name="target">is the symbolic name of the other node to which connectivity shall be impeded</param>
/// <param name="direction">can be either `Direction.Send`, `Direction.Receive` or `Direction.Both`</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Task indicating completion</returns>
public Task<Done> PassThroughAsync(RoleName node, RoleName target, ThrottleTransportAdapter.Direction direction, CancellationToken cancellationToken = default)
{
return ThrottleAsync(node, target, direction, -1f, cancellationToken);
}
/// <summary>
/// Tell the remote support to TCP_RESET the connection to the given remote
/// peer. It works regardless of whether the recipient was initiator or
/// responder.
/// </summary>
/// <param name="node">is the symbolic name of the node which is to be affected</param>
/// <param name="target">is the symbolic name of the other node to which connectivity shall be impeded</param>
/// <returns></returns>
public Task<Done> Disconnect(RoleName node, RoleName target)
{
return DisconnectAsync(node, target, CancellationToken.None);
}
/// <summary>
/// Tell the remote support to TCP_RESET the connection to the given remote
/// peer. It works regardless of whether the recipient was initiator or
/// responder.
/// </summary>
/// <param name="node">is the symbolic name of the node which is to be affected</param>
/// <param name="target">is the symbolic name of the other node to which connectivity shall be impeded</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns></returns>
public Task<Done> DisconnectAsync(RoleName node, RoleName target, CancellationToken cancellationToken = default)
{
return Controller.Ask<Done>(new Disconnect(node, target, false), Settings.QueryTimeout, cancellationToken);
}
/// <summary>
/// Tell the remote support to TCP_RESET the connection to the given remote
/// peer. It works regardless of whether the recipient was initiator or
/// responder.
/// </summary>
/// <param name="node">is the symbolic name of the node which is to be affected</param>
/// <param name="target">is the symbolic name of the other node to which connectivity shall be impeded</param>
/// <returns></returns>
public Task<Done> Abort(RoleName node, RoleName target)
{
return AbortAsync(node, target, CancellationToken.None);
}
/// <summary>
/// Tell the remote support to TCP_RESET the connection to the given remote
/// peer. It works regardless of whether the recipient was initiator or
/// responder.
/// </summary>
/// <param name="node">is the symbolic name of the node which is to be affected</param>
/// <param name="target">is the symbolic name of the other node to which connectivity shall be impeded</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns></returns>
public Task<Done> AbortAsync(RoleName node, RoleName target, CancellationToken cancellationToken = default)
{
return Controller.Ask<Done>(new Disconnect(node, target, true), Settings.QueryTimeout, cancellationToken);
}
/// <summary>
/// Tell the actor system at the remote node to shut itself down. The node will also be
/// removed, so that the remaining nodes may still pass subsequent barriers.
/// </summary>
/// <param name="node">is the symbolic name of the node which is to be affected</param>
/// <param name="exitValue">is the return code which shall be given to System.exit</param>
/// <exception cref="InvalidOperationException">TBD</exception>
/// <returns>TBD</returns>
public Task<Done> Exit(RoleName node, int exitValue)
{
// Use the async version with no cancellation token for consistency
return ExitAsync(node, exitValue, CancellationToken.None);
}
/// <summary>
/// Async version of Exit with cancellation token support.
/// Tell the actor system at the remote node to shut itself down. The node will also be
/// removed, so that the remaining nodes may still pass subsequent barriers.
/// </summary>
/// <param name="node">is the symbolic name of the node which is to be affected</param>
/// <param name="exitValue">is the return code which shall be given to System.exit</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Task indicating completion</returns>
public async Task<Done> ExitAsync(RoleName node, int exitValue, CancellationToken cancellationToken = default)
{
try
{
var result = await Controller.Ask(new Terminate(node, new Right<bool, int>(exitValue)), Settings.QueryTimeout, cancellationToken);
if (result is Done) return Done.Instance;
if (result is FSMBase.Failure failure && failure.Cause is Controller.ClientDisconnectedException)
return Done.Instance;
throw new InvalidOperationException($"Expected Done but received {result}");
}
catch (TaskCanceledException)
{
throw new TimeoutException($"ExitAsync operation was cancelled for node {node}");
}
}
/// <summary>
/// Tell the actor system at the remote node to shut itself down without
/// awaiting termination of remote-deployed children. The node will also be
/// removed, so that the remaining nodes may still pass subsequent barriers.
/// </summary>
/// <param name="node">is the symbolic name of the node which is to be affected</param>
/// <param name="abort">TBD</param>
/// <exception cref="InvalidOperationException">TBD</exception>
/// <returns>Task indicating completion</returns>
public Task<Done> Shutdown(RoleName node, bool abort = false)
{
// Use the async version with no cancellation token for consistency
return ShutdownAsync(node, abort, CancellationToken.None);
}
/// <summary>
/// Tell the actor system at the remote node to shut itself down without
/// awaiting termination of remote-deployed children. The node will also be
/// removed, so that the remaining nodes may still pass subsequent barriers.
/// </summary>
/// <param name="node">is the symbolic name of the node which is to be affected</param>
/// <param name="abort">TBD</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Task indicating completion</returns>
public async Task<Done> ShutdownAsync(RoleName node, bool abort = false, CancellationToken cancellationToken = default)
{
// the recover is needed to handle ClientDisconnectedException exception,
// which is normal during shutdown
var result = await Controller.Ask(new Terminate(node, new Left<bool, int>(abort)), Settings.QueryTimeout, cancellationToken);
return result switch
{
Done or FSMBase.Failure { Cause: TestKit.Controller.ClientDisconnectedException } => Done.Instance,
_ => throw new InvalidOperationException($"Expected Done but received {result}")
};
}
/// <summary>
/// Obtain the list of remote host names currently registered.
/// </summary>
public Task<IEnumerable<RoleName>> GetNodes()
{
// Use the async version with no cancellation token for consistency
return GetNodesAsync(CancellationToken.None);
}
/// <summary>
/// Async version of GetNodes with cancellation token support.
/// Obtain the list of remote host names currently registered.
/// </summary>
public Task<IEnumerable<RoleName>> GetNodesAsync(CancellationToken cancellationToken = default)
{
return Controller.Ask<IEnumerable<RoleName>>(TestKit.Controller.GetNodes.Instance, Settings.QueryTimeout, cancellationToken);
}
/// <summary>
/// Remove a remote host from the list, so that the remaining nodes may still
/// pass subsequent barriers. This must be done before the client connection
/// breaks down in order to affect an "orderly" removal (i.e. without failing
/// present and future barriers).
/// </summary>
/// <param name="node">is the symbolic name of the node which is to be removed</param>
/// <returns></returns>
public Task<Done> RemoveNode(RoleName node)
{
// Use the async version with no cancellation token for consistency
return RemoveNodeAsync(node, CancellationToken.None);
}
/// <summary>
/// Async version of RemoveNode with cancellation token support.
/// Remove a remote host from the list, so that the remaining nodes may still
/// pass subsequent barriers. This must be done before the client connection
/// breaks down in order to affect an "orderly" removal (i.e. without failing
/// present and future barriers).
/// </summary>
/// <param name="node">is the symbolic name of the node which is to be removed</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Task indicating completion</returns>
public Task<Done> RemoveNodeAsync(RoleName node, CancellationToken cancellationToken = default)
{
return Controller.Ask<Done>(new Remove(node), Settings.QueryTimeout, cancellationToken);
}
}
internal class ConductorHandler : ChannelHandlerAdapter
{
private readonly ILoggingAdapter _log;
private readonly IActorRef _controller;
private readonly ConcurrentDictionary<IChannel, IActorRef> _clients = new();
/// <summary>
/// A single <see cref="ConductorHandler"/> gets shared across all of the connections between
/// server and clients.
/// </summary>
public override bool IsSharable => true;
public ConductorHandler(IActorRef controller, ILoggingAdapter log)
{
_controller = controller;
_log = log;
}
public override void ChannelActive(IChannelHandlerContext context)
{
_log.Debug("connection from {0}", context.Channel.RemoteAddress);
// Duration of this Ask operation needs to be infinite
var channel = context.Channel;
channel.Configuration.AutoRead = false;
_controller.Ask<IActorRef>(new Controller.CreateServerFSM(channel),
TimeSpan.FromMilliseconds(Int32.MaxValue)).ContinueWith(tr =>
{
var fsm = tr.Result;
_log.Debug("created server FSM {0}", fsm);
_clients.AddOrUpdate(channel, fsm, (_, _) => fsm);
channel.Configuration.AutoRead = true;
});
}
public override void ChannelInactive(IChannelHandlerContext context)
{
var channel = context.Channel;
_log.Debug("disconnect from {0}", channel.RemoteAddress);
if (_clients.TryGetValue(channel, out var fsm))
{
fsm.Tell(new Controller.ClientDisconnected(new RoleName(null)));
IActorRef removedActor;
_clients.TryRemove(channel, out removedActor);
}
}
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)
{
if (_clients.TryGetValue(channel, out var fsm))
fsm.Tell(message);
else
_log.Warning("Failed to get client for {0}", channel);
}
else
{
_log.Debug("client {0} sent garbage `{1}`, disconnecting", channel.RemoteAddress, message);
channel.CloseAsync();
}
}
public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
{
var channel = context.Channel;
_log.Warning("handled network error from {0}: {1} {2}", channel.RemoteAddress, exception.Message, exception.StackTrace);
}
public override Task CloseAsync(IChannelHandlerContext context)
{
_log.Info("Server: disconnecting {0} from {1}", context.Channel.LocalAddress, context.Channel.RemoteAddress);
return base.CloseAsync(context);
}
}
/// <summary>
/// The server part of each client connection is represented by a ServerFSM.
/// The Initial state handles reception of the new client’s
/// <see cref="Hello"/> message (which is needed for all subsequent
/// node name translations).
///
/// In the Ready state, messages from the client are forwarded to the controller
/// and <see cref="EndpointManager.Send"/> requests are sent, but the latter is
/// treated specially: all client operations are to be confirmed by a
/// <see cref="Done"/> message, and there can be only one such
/// request outstanding at a given time (i.e. a Send fails if the previous has
/// not yet been acknowledged).
///
/// INTERNAL API.
/// </summary>
internal class ServerFSM : FSM<ServerFSM.State, IActorRef>, ILoggingFSM
{
private readonly ILoggingAdapter _log = Context.GetLogger();
readonly IChannel _channel;
readonly IActorRef _controller;
RoleName _roleName;
public enum State
{
Initial,
Ready
}
public ServerFSM(IActorRef controller, IChannel channel)
{
_controller = controller;
_channel = channel;
InitFSM();
}
protected void InitFSM()
{
StartWith(State.Initial, null);
WhenUnhandled(@event =>
{
var clientDisconnected = @event.FsmEvent as Controller.ClientDisconnected;
if (clientDisconnected != null)
{
if(@event.StateData != null)
@event.StateData.Tell(new Failure(new Controller.ClientDisconnectedException("client disconnected in state " + StateName + ": " + _channel)));
return Stop();
}
return null;
});
OnTermination(_ =>
{
// Name the sender explicitly. The controller compares it against the FSM it has
// registered for this role, which is how it tells a disconnect that belongs to
// this connection apart from one that a newer connection already replaced.
_controller.Tell(new Controller.ClientDisconnected(_roleName), Self);
_channel.CloseAsync();
});
When(State.Initial, @event =>
{
var hello = @event.FsmEvent as Hello;
if (hello != null)
{
_roleName = new RoleName(hello.Name);
_controller.Tell(new Controller.NodeInfo(_roleName, hello.Address, Self));
return GoTo(State.Ready);
}
if (@event.FsmEvent is INetworkOp)
{
_log.Warning("client {0}, sent not Hello in first message (instead {1}), disconnecting", _channel.RemoteAddress, @event.FsmEvent);
_channel.CloseAsync();
return Stop();
}
if (@event.FsmEvent is IToClient)
{
_log.Warning("cannot send {0} in state Initial", @event.FsmEvent);
return Stay();
}
if (@event.FsmEvent is StateTimeout)
{
_log.Info("closing channel to {0} because of Hello timeout", _channel.RemoteAddress);
_channel.CloseAsync();
return Stop();
}
return null;
}, TimeSpan.FromSeconds(10));
When(State.Ready, @event =>
{
if (@event.FsmEvent is Done && @event.StateData != null)
{
@event.StateData.Tell(@event.FsmEvent);
return Stay().Using(null);
}
if (@event.FsmEvent is IServerOp)
{
_controller.Tell(@event.FsmEvent);
return Stay();
}
if (@event.FsmEvent is INetworkOp)
{
_log.Warning("client {0} sent unsupported message {1}", _channel.RemoteAddress, @event.FsmEvent);
return Stop();
}
var toClient = @event.FsmEvent as IToClient;
if (toClient != null)
{
if (toClient.Msg is IUnconfirmedClientOp)
{
_channel.WriteAndFlushAsync(toClient.Msg);
return Stay();
}
if (@event.StateData == null)
{
_channel.WriteAndFlushAsync(toClient.Msg);
return Stay().Using(Sender);
}
_log.Warning("cannot send {0} while waiting for previous ACK", toClient.Msg);
return Stay();
}
return null;
});
Initialize();
}
}
}