Ejemplo n.º 1
0
        public void GlobalSetup()
        {
            switch (HubProtocol)
            {
            case Protocol.MsgPack:
                _hubProtocol = new MessagePackHubProtocol();
                break;

            case Protocol.Json:
                _hubProtocol = new JsonHubProtocol();
                break;
            }

            switch (Input)
            {
            case Message.NoArguments:
                _hubMessage = new InvocationMessage(target: "Target", argumentBindingException: null);
                break;

            case Message.FewArguments:
                _hubMessage = new InvocationMessage(target: "Target", argumentBindingException: null, 1, "Foo", 2.0f);
                break;

            case Message.ManyArguments:
                _hubMessage = new InvocationMessage(target: "Target", argumentBindingException: null, 1, "string", 2.0f, true, (byte)9, new byte[] { 5, 4, 3, 2, 1 }, 'c', 123456789101112L);
                break;

            case Message.LargeArguments:
                _hubMessage = new InvocationMessage(target: "Target", argumentBindingException: null, new string('F', 10240), new byte[10240]);
                break;
            }

            _binaryInput = _hubProtocol.WriteToArray(_hubMessage);
            _binder      = new TestBinder(_hubMessage);
        }
Ejemplo n.º 2
0
        public byte[] WriteMessage(IHubProtocol protocol)
        {
            // REVIEW: Revisit lock
            // Could use a reader/writer lock to allow the loop to take place in "unlocked" code
            // Or, could use a fixed size array and Interlocked to manage it.
            // Or, Immutable *ducks*

            lock (_lock)
            {
                for (var i = 0; i < _serializedMessages?.Count; i++)
                {
                    if (_serializedMessages[i].Protocol.Equals(protocol))
                    {
                        return(_serializedMessages[i].Message);
                    }
                }

                var bytes = protocol.WriteToArray(this);

                if (_serializedMessages == null)
                {
                    // Initialize with capacity 2 for the 2 built in protocols
                    _serializedMessages = new List <SerializedMessage>(2);
                }

                // We don't want to balloon memory if someone writes a poor IHubProtocolResolver
                // So we cap how many caches we store and worst case just serialize the message for every connection
                if (_serializedMessages.Count < 10)
                {
                    _serializedMessages.Add(new SerializedMessage(protocol, bytes));
                }

                return(bytes);
            }
        }
Ejemplo n.º 3
0
        public void WriteSingleMessage()
        {
            var bytes = _hubProtocol.WriteToArray(_hubMessage);

            if (bytes.Length != _binaryInput.Length)
            {
                throw new InvalidOperationException("Failed to write message");
            }
        }
Ejemplo n.º 4
0
        public ReadOnlyMemory <byte> GetSerializedMessage(IHubProtocol protocol)
        {
            if (!TryGetCached(protocol.Name, out var serialized))
            {
                if (Message == null)
                {
                    throw new InvalidOperationException(
                              "This message was received from another server that did not have the requested protocol available.");
                }

                serialized = protocol.WriteToArray(Message);
                SetCache(protocol.Name, serialized);
            }

            return(serialized);
        }