Пример #1
0
        // TODO: [vermorel] Method named 'listen' but actually 'remove'?. Intent to be clarified.
        public void ListenToConnections()
        {
            // TODO: [vermorel] Directly initialize, it will remove two test conditions below, perf overhead should be insignificant.
            List <ClientId> toRemove = null;

            foreach (var kv in _activeConnections)
            {
                if (!kv.Value.IsConnected)
                {
                    if (toRemove == null)
                    {
                        toRemove = new List <ClientId>();
                    }
                    toRemove.Add(kv.Key);
                    continue;
                }

                kv.Value.Send();

                // TODO: [vermorel] Almost but not quite like an iterator. Design should be aligned with other iterators.
                var nextMessage = kv.Value.ReceiveNext();
                while (nextMessage.Length != 0)
                {
                    var toWriteInto = ClientServerMessage.IsSharded(nextMessage)
                        ? _workerInboxes[
                        // TODO: [vermorel] Do not keep the shardling logic inline, isolate with a helper.
                        ClientServerMessage.FirstKeyByte(nextMessage) % _workerInboxes.Length]
                        : _controllerInbox;

                    if (!toWriteInto.TryWrite(nextMessage))
                    {
                        // TODO: [vermorel] Oddly written statement, to be rewritten.
                        new Span <byte>(_errorBuffer).EmitWorkersBusy(nextMessage);
                        kv.Value.TryWrite(_errorBuffer);
                    }

                    nextMessage = kv.Value.ReceiveNext();
                }
            }

            // Delete connections that were found unconnected
            if (toRemove != null)
            {
                foreach (var clientId in toRemove)
                {
                    _activeConnections.Remove(clientId);
                }
            }
        }
Пример #2
0
 public void TestFirstKeyByte()
 {
     Assert.Equal(_bytes[ClientServerMessage.PayloadStart], ClientServerMessage.FirstKeyByte(_bytes));
     Assert.Throws <ArgumentException>(() => ClientServerMessage.FirstKeyByte(new Span <byte>(_bytes, 0, ClientServerMessage.PayloadStart)));
 }