public void frame_with_extension_properties_can_be_codec()
        {
            var bufferManager = new BufferManager(8192, 1);
            var context = new WriterContext(bufferManager);
            var buffer = bufferManager.Dequeue();
            bufferManager.Enqueue(buffer);
            var sut = new HandshakeFrame();
            sut.Identity = "Client";
            sut.OptionalExtensions = new[] { new HandshakeExtension("json", new Dictionary<string, string>{{"Key", "Value"}}), new HandshakeExtension("ack") };
            sut.RequiredExtensions = new[] { new HandshakeExtension("dotnet") };
            sut.VersionMajor = 1;
            sut.Write(context);
            sut.ResetWrite(context);

            var offset = buffer.Offset;
            var receiveFrame = new HandshakeFrame();
            var len = context.GetPackets()[0].Count;
            receiveFrame.Read(buffer.Array, ref offset, ref len);

            receiveFrame.OptionalExtensions[0].Properties["Key"].Should().Be("Value");
        }
        public void send_a_frame()
        {
            var service = Substitute.For<IExtensionService>();
            var mgr = new BufferManager(100, 10);
            var isDisconnected = false;
            using (var helper = new ClientServerHelper()) // wrap for cleanup
            {

                var sut = new SharpMessaging.Connection.Connection("adam", service, true, mgr);
                sut.Disconnected += (o,error) => isDisconnected = true;
                sut.Assign(helper.Server);
                sut.Send(new HandshakeFrame(){Identity = "A"});
                Thread.Sleep(100);

                byte[] buffer = new byte[65535];
                var bytesRead = helper.Client.Receive(buffer, SocketFlags.None);
                var frame = new HandshakeFrame();
                var offset = 0;
                int len = bytesRead;
                frame.Read(buffer, ref offset, ref len);
                frame.Identity.Should().Be("A");
            }
        }
        public void frame_Should_be_able_to_parse_multiple_incoming_buffers_if_reseted_in_between()
        {
            var bufferManager = new BufferManager(8192, 1);
            var context = new WriterContext(bufferManager);
            var buffer = bufferManager.Dequeue();
            bufferManager.Enqueue(buffer);
            var sut = new HandshakeFrame();
            sut.Identity = "Client";
            sut.OptionalExtensions = new[] {new HandshakeExtension("json"), new HandshakeExtension("ack") };
            sut.RequiredExtensions = new[] {new HandshakeExtension("dotnet") };
            sut.VersionMajor = 1;
            sut.Write(context);
            sut.ResetWrite(context);

            var offset = buffer.Offset;
            var len = context.GetPackets()[0].Count;
            sut.Read(buffer.Array, ref offset, ref len);
            sut.ResetRead();
            offset = buffer.Offset;
            len = context.GetPackets()[0].Count;
            sut.Read(buffer.Array, ref offset, ref len);

            len.Should().Be(0);
        }
 public HandshakeFrameReceivedEventArgs(HandshakeFrame handshake)
 {
     if (handshake == null) throw new ArgumentNullException("handshake");
     Handshake = handshake;
 }
Esempio n. 5
0
 private void NegotiateHandshake(HandshakeFrame handshakeFrame)
 {
     ClientName = handshakeFrame.Identity;
     var frame = _extensionService.ServerNegotiate(handshakeFrame, ServerName);
     _state = ServerState.WaitingOnFinalHandshake;
     _connection.Send(frame);
 }
Esempio n. 6
0
        private void FinalizeHandshake(HandshakeFrame handshakeFrame)
        {
            _extensionService.ServerConfirm(handshakeFrame);
            _connection.SetHandshakeCompleted();
            _state = ServerState.Ready;
            var extensionId = _extensionService.FindFirstExtensionId("batch-ack", "ack");
            if (extensionId != 0)
            {
                var name = _extensionService.FindFirstExtensionNamed("batch-ack", "ack");
                var extProperties = handshakeFrame.GetExtension(name);
                var ackExtension = (IAckExtension) _extensionService.Get(extensionId);
                _ackReceiver = ackExtension.CreateAckReceiver(_connection, extensionId, DeliverMessage, extProperties);
                _ackSender = ackExtension.CreateAckSender(_connection, extensionId, extProperties);
            }

            extensionId = _extensionService.FindFirstExtensionId("json", "protobuf", "xml");
            if (extensionId != 0)
            {
                var payloadExtension = (IPayloadExtension) _extensionService.Get(extensionId);
                _payloadSerializer = payloadExtension.CreatePayloadSerializer();
            }

            if (HandshakeCompleted != null)
                HandshakeCompleted();
        }