public void assigns_identity_properly() { var service = Substitute.For<IExtensionService>(); var mgr = new BufferManager(100, 10); var sut = new SharpMessaging.Connection.Connection("adam", service, true, mgr); sut.Identity.Should().Be("adam"); }
public void can_write_to_our_context() { var buffer = new byte[] {1, 2, 3, 4, 5, 6, 7}; var mgr = new BufferManager(100, 10); var context = new WriterContext(mgr); var sut = new ByteBufferWriter(buffer, 0, buffer.Length); sut.Write(context); context.GetPackets()[0].Buffer.Should().BeSubsetOf(buffer); }
public void partial_bytes_are_copied() { var buffer = new byte[] { 1, 2, 3, 4, 5, 6, 7 }; var mgr = new BufferManager(100, 10); var context = new WriterContext(mgr); context.BytesLeftToEnqueue = 4; var sut = new ByteBufferWriter(buffer, 0, buffer.Length); sut.Write(context); context.GetPackets()[0].Buffer[3].Should().Be(4); }
public void can_do_partial_write() { var buffer = new byte[] { 1, 2, 3, 4, 5, 6, 7 }; var mgr = new BufferManager(100, 10); var context = new WriterContext(mgr); context.BytesLeftToEnqueue = 4; var sut = new ByteBufferWriter(buffer, 0, buffer.Length); var actual = sut.Write(context); actual.Should().BeFalse(); }
public void serialize_and_deserialize_should_be_compatible() { var mgr = new BufferManager(65535, 10); var ctx = new WriterContext(mgr); var msg1 =new SharpMessaging.Frames.ErrorFrame("Help!"); msg1.Write(ctx); var buf = ctx.GetPackets()[0].Buffer; var offset = ctx.GetPackets()[0].Offset; var len = ctx.GetPackets()[0].Count; var msg2 = new SharpMessaging.Frames.ErrorFrame(); msg2.Read(buf, ref offset, ref len); msg2.ErrorMessage.Should().Be("Help!"); }
public void can_continue_on_a_partial_write() { var buffer = new byte[] { 1, 2, 3, 4, 5, 6, 7 }; var mgr = new BufferManager(100, 10); var context = new WriterContext(mgr); context.BytesLeftToEnqueue = 4; var sut = new ByteBufferWriter(buffer, 0, buffer.Length); context.BytesLeftToEnqueue = 4; sut.Write(context); context.BytesLeftToEnqueue = 10; var actual = sut.Write(context); actual.Should().BeTrue(); var packet = context.GetPackets()[1]; packet.Buffer[packet.Offset + 2].Should().Be(7); }
public ServerClient(string identity, IExtensionRegistry extensionRegistry, BufferManager bufferManager) { ServerName = identity; _bufferManager = bufferManager; _extensionService = new ExtensionService(extensionRegistry, DeliverMessage); _connection = new Connection.Connection(ServerName, _extensionService, true, _bufferManager) { ExtensionFrameReceived = OnExtensionFrame, MessageFrameReceived = OnMessageFrame, WriteCompleted = OnWriteCompleted, }; _connection.HandshakeReceived += OnHandshakeFrame; _connection.Disconnected += HandleRemoteDisconnect; _state = ServerState.WaitingOnInitialHandshake; ServerName = "FastSocket v" + Major + "." + Minor; }
public void uses_the_assigned_socket() { 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); helper.Client.Shutdown(SocketShutdown.Send); Thread.Sleep(100); } isDisconnected.Should().BeTrue(); }
public void code_decode_test() { var bufMgr = new BufferManager(65535, 1); var context = new WriterContext(bufMgr); var buf = bufMgr.Dequeue(); bufMgr.Enqueue(buf); var sut = new MessageFrame(); sut.SequenceNumber = 22; var payload = Encoding.UTF8.GetBytes("Hello world"); sut.PayloadBuffer = new ArraySegment<byte>(payload, 0, payload.Length); sut.Write(context); var pos = 0; var len = context.GetPackets().First().Count; var frame2 = new MessageFrame(); frame2.Read(buf.Array, ref pos, ref len); frame2.SequenceNumber.Should().Be(22); Encoding.ASCII.GetString(sut.PayloadBuffer.Array, 0, sut.PayloadBuffer.Count).Should().Be("Hello world"); }
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 Connection(string identity, IExtensionService extensionRegistry, bool isServer, BufferManager bufferManager) { _identity = identity ?? "FastSocket v" + Major + "" + Minor; _readArgs = new SocketAsyncEventArgs(); _readArgs.Completed += OnReadCompleted; _writeArgs = new SocketAsyncEventArgs(); _writeArgs.Completed += OnWriteCompleted; //_writeArgs.SendPacketsFlags = TransmitFileOptions.UseKernelApc; _extensionFrameProcessor = new ExtensionFrameProcessor(extensionRegistry.CreateFrame, OnExtension); _writerContext = new WriterContext(bufferManager); ExtensionFrameReceived = frame => { }; HandshakeReceived = (o, e) => { }; MessageFrameReceived = frame => { }; WriteCompleted = i => { }; Disconnected = (o, e) => { }; Fault = (o, e) => { }; }
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 WriterContext(BufferManager bufferManager) { _bufferManager = bufferManager; }
public void serialize_Simplistic_filter() { var bufferManager = new BufferManager(8192, 1); var context = new WriterContext(bufferManager); var buffer = bufferManager.Dequeue(); bufferManager.Enqueue(buffer); var expected = new byte[] { 0, //flags, 0, 2, // sequence number, 2, //destination length, (byte)'M', (byte)'Q', 0, 16, // filter length (byte)'l', (byte)'a', (byte)'s', (byte)'t', (byte)'_', (byte)'n', (byte)'a', (byte)'m', (byte)'e', //filter part 1 (byte)':', (byte)'k', (byte)'a', (byte)'l', (byte)'l', (byte)'e', (byte)';', //filter part 2 6, //payload length (byte) 'm', (byte) 'o', (byte) 't', (byte) 'h', (byte)'e', (byte)'r' //payload }; var sut = new MessageFrame(); sut.SequenceNumber = 2; sut.Destination = "MQ"; sut.Properties.Add("last_name", "kalle"); var payload = Encoding.ASCII.GetBytes("mother"); sut.PayloadBuffer = new ArraySegment<byte>(payload, 0, payload.Length); var actual = sut.Write(context); for (int i = 0; i < expected.Length; i++) { if (expected[i] != buffer.Array[i]) throw new InvalidOperationException("Differs at " + i); } }
public void doing_a_partial_payload_write_properly() { var bufMgr = new BufferManager(5010, 1); var context = new WriterContext(bufMgr); var buf1 = bufMgr.Dequeue(); bufMgr.Enqueue(buf1); var sut = new MessageFrame(); sut.SequenceNumber = 22; var payload = Encoding.ASCII.GetBytes("Hello world".PadRight(10000)); sut.PayloadBuffer = new ArraySegment<byte>(payload, 0, payload.Length); var actual1 = sut.Write(context); var pos = 0; var len = context.GetPackets().First().Count; var frame2 = new MessageFrame(); frame2.Read(buf1.Array, ref pos, ref len); var actual2 = sut.Write(context); pos = 0; len = context.GetPackets().Last().Count; frame2.Read(buf1.Array, ref pos, ref len); frame2.SequenceNumber.Should().Be(22); frame2.IsFlaggedAsSmall.Should().BeFalse(); Encoding.ASCII.GetString(sut.PayloadBuffer.Array, 0, sut.PayloadBuffer.Count).TrimEnd(' ').Should().Be("Hello world"); actual1.Should().BeFalse(); actual2.Should().BeTrue(); }