Exemplo n.º 1
0
        public void TestSharded()
        {
            _bytes[12] = 0x85;
            Assert.True(ClientServerMessage.IsSharded(_bytes));

            _bytes[12] = 0x71;
            Assert.False(ClientServerMessage.IsSharded(_bytes));

            Assert.Throws <ArgumentException>(() => ClientServerMessage.IsSharded(new Span <byte>(_bytes, 0, 11)));
        }
Exemplo n.º 2
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);
                }
            }
        }