From efe6affe85226c6ae6355c3de39eb11984b0267f Mon Sep 17 00:00:00 2001 From: cdoyle-kx <74536948+cdoyle-kx@users.noreply.github.com> Date: Tue, 22 Dec 2020 15:14:10 +0000 Subject: [PATCH] Issue 38 (#52) --- ...onnectionBooleanArraySerialisationTests.cs | 4 +- .../ConnectionByteArraySerialisationTests.cs | 4 +- .../ConnectionCharArraySerialisationTests.cs | 4 +- .../ConnectionDateArraySerialisationTests.cs | 4 +- ...nnectionDateTimeArraySerialisationTests.cs | 4 +- ...ConnectionDoubleArraySerialisationTests.cs | 4 +- .../ConnectionFloatArraySerialisationTests.cs | 4 +- .../ConnectionGuidArraySerialisationTests.cs | 4 +- .../ConnectionIntArraySerialisationTests.cs | 4 +- ...nectionKTimeSpanArraySerialisationTests.cs | 4 +- .../ConnectionLongArraySerialisationTests.cs | 4 +- ...ConnectionMinuteArraySerialisationTests.cs | 4 +- .../ConnectionMonthArraySerialisationTests.cs | 4 +- ...ConnectionObjectArraySerialisationTests.cs | 4 +- ...ConnectionSecondArraySerialisationTests.cs | 4 +- .../ConnectionShortArraySerialisationTests.cs | 4 +- ...ConnectionStringArraySerialisationTests.cs | 4 +- ...nnectionTimeSpanArraySerialisationTests.cs | 4 +- .../Connection/ConnectionAsyncWriteTests.cs | 117 ++++++++ .../ConnectionSerialisationTests.cs | 122 ++++----- .../Connection/ConnectionSyncWriteTests.cs | 165 ++++++++++++ kx.Test/Connection/ConnectionTests.cs | 2 +- kx.Test/kx.Test.csproj | 1 + kx/c.cs | 253 +++++++++++++++--- 24 files changed, 592 insertions(+), 140 deletions(-) create mode 100644 kx.Test/Connection/ConnectionAsyncWriteTests.cs create mode 100644 kx.Test/Connection/ConnectionSyncWriteTests.cs diff --git a/kx.Benchmark.Test/Connection/ConnectionBooleanArraySerialisationTests.cs b/kx.Benchmark.Test/Connection/ConnectionBooleanArraySerialisationTests.cs index 83fb26a..995737a 100644 --- a/kx.Benchmark.Test/Connection/ConnectionBooleanArraySerialisationTests.cs +++ b/kx.Benchmark.Test/Connection/ConnectionBooleanArraySerialisationTests.cs @@ -19,7 +19,7 @@ public void ConnectionSerialisesAndDeserialisesBooleanArrayInput() { bool[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -34,7 +34,7 @@ public void ConnectionSerialisesAndDeserialisesBooleanArrayInputWithZipEnabled() { bool[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { connection.IsZipEnabled = true; diff --git a/kx.Benchmark.Test/Connection/ConnectionByteArraySerialisationTests.cs b/kx.Benchmark.Test/Connection/ConnectionByteArraySerialisationTests.cs index e89cc4a..0392979 100644 --- a/kx.Benchmark.Test/Connection/ConnectionByteArraySerialisationTests.cs +++ b/kx.Benchmark.Test/Connection/ConnectionByteArraySerialisationTests.cs @@ -19,7 +19,7 @@ public void ConnectionSerialisesAndDeserialisesByteArrayInput() { byte[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -34,7 +34,7 @@ public void ConnectionSerialisesAndDeserialisesByteArrayInputWithZipEnabled() { byte[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { connection.IsZipEnabled = true; diff --git a/kx.Benchmark.Test/Connection/ConnectionCharArraySerialisationTests.cs b/kx.Benchmark.Test/Connection/ConnectionCharArraySerialisationTests.cs index 935995c..8c38c83 100644 --- a/kx.Benchmark.Test/Connection/ConnectionCharArraySerialisationTests.cs +++ b/kx.Benchmark.Test/Connection/ConnectionCharArraySerialisationTests.cs @@ -20,7 +20,7 @@ public void ConnectionSerialisesAndDeserialisesCharArrayInput() { char[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -35,7 +35,7 @@ public void ConnectionSerialisesAndDeserialisesCharArrayInputWithZipEnabled() { char[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { connection.IsZipEnabled = true; diff --git a/kx.Benchmark.Test/Connection/ConnectionDateArraySerialisationTests.cs b/kx.Benchmark.Test/Connection/ConnectionDateArraySerialisationTests.cs index 3223bce..419cbae 100644 --- a/kx.Benchmark.Test/Connection/ConnectionDateArraySerialisationTests.cs +++ b/kx.Benchmark.Test/Connection/ConnectionDateArraySerialisationTests.cs @@ -20,7 +20,7 @@ public void ConnectionSerialisesAndDeserialisesDateArrayInput() { c.Date[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -35,7 +35,7 @@ public void ConnectionSerialisesAndDeserialisesDateArrayInputWithZipEnabled() { c.Date[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { connection.IsZipEnabled = true; diff --git a/kx.Benchmark.Test/Connection/ConnectionDateTimeArraySerialisationTests.cs b/kx.Benchmark.Test/Connection/ConnectionDateTimeArraySerialisationTests.cs index 785ee5a..db3442a 100644 --- a/kx.Benchmark.Test/Connection/ConnectionDateTimeArraySerialisationTests.cs +++ b/kx.Benchmark.Test/Connection/ConnectionDateTimeArraySerialisationTests.cs @@ -20,7 +20,7 @@ public void ConnectionSerialisesAndDeserialisesDateTimeArrayInput() { DateTime[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -35,7 +35,7 @@ public void ConnectionSerialisesAndDeserialisesDateTimeArrayInputWithZipEnabled( { DateTime[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { connection.IsZipEnabled = true; diff --git a/kx.Benchmark.Test/Connection/ConnectionDoubleArraySerialisationTests.cs b/kx.Benchmark.Test/Connection/ConnectionDoubleArraySerialisationTests.cs index 024c4c1..c17598e 100644 --- a/kx.Benchmark.Test/Connection/ConnectionDoubleArraySerialisationTests.cs +++ b/kx.Benchmark.Test/Connection/ConnectionDoubleArraySerialisationTests.cs @@ -19,7 +19,7 @@ public void ConnectionSerialisesAndDeserialisesDoubleArrayInput() { double[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -34,7 +34,7 @@ public void ConnectionSerialisesAndDeserialisesDoubleArrayInputWithZipEnabled() { double[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { connection.IsZipEnabled = true; diff --git a/kx.Benchmark.Test/Connection/ConnectionFloatArraySerialisationTests.cs b/kx.Benchmark.Test/Connection/ConnectionFloatArraySerialisationTests.cs index abd8b7f..63b29ca 100644 --- a/kx.Benchmark.Test/Connection/ConnectionFloatArraySerialisationTests.cs +++ b/kx.Benchmark.Test/Connection/ConnectionFloatArraySerialisationTests.cs @@ -19,7 +19,7 @@ public void ConnectionSerialisesAndDeserialisesFloatArrayInput() { float[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -34,7 +34,7 @@ public void ConnectionSerialisesAndDeserialisesFloatArrayInputWithZipEnabled() { float[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { connection.IsZipEnabled = true; diff --git a/kx.Benchmark.Test/Connection/ConnectionGuidArraySerialisationTests.cs b/kx.Benchmark.Test/Connection/ConnectionGuidArraySerialisationTests.cs index 75aa422..1d6cb9d 100644 --- a/kx.Benchmark.Test/Connection/ConnectionGuidArraySerialisationTests.cs +++ b/kx.Benchmark.Test/Connection/ConnectionGuidArraySerialisationTests.cs @@ -20,7 +20,7 @@ public void ConnectionSerialisesAndDeserialisesGuidArrayInput() { Guid[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -35,7 +35,7 @@ public void ConnectionSerialisesAndDeserialisesGuidArrayInputWithZipEnabled() { Guid[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { connection.IsZipEnabled = true; diff --git a/kx.Benchmark.Test/Connection/ConnectionIntArraySerialisationTests.cs b/kx.Benchmark.Test/Connection/ConnectionIntArraySerialisationTests.cs index 11f7cdb..c2b2b72 100644 --- a/kx.Benchmark.Test/Connection/ConnectionIntArraySerialisationTests.cs +++ b/kx.Benchmark.Test/Connection/ConnectionIntArraySerialisationTests.cs @@ -19,7 +19,7 @@ public void ConnectionSerialisesAndDeserialisesIntArrayInput() { int[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -34,7 +34,7 @@ public void ConnectionSerialisesAndDeserialisesIntArrayInputWithZipEnabled() { int[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { connection.IsZipEnabled = true; diff --git a/kx.Benchmark.Test/Connection/ConnectionKTimeSpanArraySerialisationTests.cs b/kx.Benchmark.Test/Connection/ConnectionKTimeSpanArraySerialisationTests.cs index ab26852..fb45783 100644 --- a/kx.Benchmark.Test/Connection/ConnectionKTimeSpanArraySerialisationTests.cs +++ b/kx.Benchmark.Test/Connection/ConnectionKTimeSpanArraySerialisationTests.cs @@ -19,7 +19,7 @@ public void ConnectionSerialisesAndDeserialisesKTimeSpanArrayInput() { c.KTimespan[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -34,7 +34,7 @@ public void ConnectionSerialisesAndDeserialisesKTimeSpanArrayInputWithZipEnabled { c.KTimespan[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { connection.IsZipEnabled = true; diff --git a/kx.Benchmark.Test/Connection/ConnectionLongArraySerialisationTests.cs b/kx.Benchmark.Test/Connection/ConnectionLongArraySerialisationTests.cs index fc77b13..782f48c 100644 --- a/kx.Benchmark.Test/Connection/ConnectionLongArraySerialisationTests.cs +++ b/kx.Benchmark.Test/Connection/ConnectionLongArraySerialisationTests.cs @@ -19,7 +19,7 @@ public void ConnectionSerialisesAndDeserialisesLongArrayInput() { long[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -34,7 +34,7 @@ public void ConnectionSerialisesAndDeserialisesLongArrayInputWithZipEnabled() { long[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { connection.IsZipEnabled = true; diff --git a/kx.Benchmark.Test/Connection/ConnectionMinuteArraySerialisationTests.cs b/kx.Benchmark.Test/Connection/ConnectionMinuteArraySerialisationTests.cs index 40796b6..3d4559b 100644 --- a/kx.Benchmark.Test/Connection/ConnectionMinuteArraySerialisationTests.cs +++ b/kx.Benchmark.Test/Connection/ConnectionMinuteArraySerialisationTests.cs @@ -19,7 +19,7 @@ public void ConnectionSerialisesAndDeserialisesMinuteArrayInput() { c.Minute[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -34,7 +34,7 @@ public void ConnectionSerialisesAndDeserialisesMinuteArrayInputWithZipEnabled() { c.Minute[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { connection.IsZipEnabled = true; diff --git a/kx.Benchmark.Test/Connection/ConnectionMonthArraySerialisationTests.cs b/kx.Benchmark.Test/Connection/ConnectionMonthArraySerialisationTests.cs index 6c210d0..b619343 100644 --- a/kx.Benchmark.Test/Connection/ConnectionMonthArraySerialisationTests.cs +++ b/kx.Benchmark.Test/Connection/ConnectionMonthArraySerialisationTests.cs @@ -19,7 +19,7 @@ public void ConnectionSerialisesAndDeserialisesMonthArrayInput() { c.Month[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -34,7 +34,7 @@ public void ConnectionSerialisesAndDeserialisesMonthArrayInputWithZipEnabled() { c.Month[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { connection.IsZipEnabled = true; diff --git a/kx.Benchmark.Test/Connection/ConnectionObjectArraySerialisationTests.cs b/kx.Benchmark.Test/Connection/ConnectionObjectArraySerialisationTests.cs index c3883f8..ab5d40d 100644 --- a/kx.Benchmark.Test/Connection/ConnectionObjectArraySerialisationTests.cs +++ b/kx.Benchmark.Test/Connection/ConnectionObjectArraySerialisationTests.cs @@ -19,7 +19,7 @@ public void ConnectionSerialisesAndDeserialisesObjectArrayInput() { object[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -34,7 +34,7 @@ public void ConnectionSerialisesAndDeserialisesObjectArrayInputWithZipEnabled() { object[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { connection.IsZipEnabled = true; diff --git a/kx.Benchmark.Test/Connection/ConnectionSecondArraySerialisationTests.cs b/kx.Benchmark.Test/Connection/ConnectionSecondArraySerialisationTests.cs index 04464b8..65b2a4c 100644 --- a/kx.Benchmark.Test/Connection/ConnectionSecondArraySerialisationTests.cs +++ b/kx.Benchmark.Test/Connection/ConnectionSecondArraySerialisationTests.cs @@ -19,7 +19,7 @@ public void ConnectionSerialisesAndDeserialisesSecondArrayInput() { c.Second[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -34,7 +34,7 @@ public void ConnectionSerialisesAndDeserialisesSecondArrayInputWithZipEnabled() { c.Second[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { connection.IsZipEnabled = true; diff --git a/kx.Benchmark.Test/Connection/ConnectionShortArraySerialisationTests.cs b/kx.Benchmark.Test/Connection/ConnectionShortArraySerialisationTests.cs index 160c0ac..451f556 100644 --- a/kx.Benchmark.Test/Connection/ConnectionShortArraySerialisationTests.cs +++ b/kx.Benchmark.Test/Connection/ConnectionShortArraySerialisationTests.cs @@ -20,7 +20,7 @@ public void ConnectionSerialisesAndDeserialisesByteArrayInput() { short[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -35,7 +35,7 @@ public void ConnectionSerialisesAndDeserialisesByteArrayInputWithZipEnabled() { short[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { connection.IsZipEnabled = true; diff --git a/kx.Benchmark.Test/Connection/ConnectionStringArraySerialisationTests.cs b/kx.Benchmark.Test/Connection/ConnectionStringArraySerialisationTests.cs index d00f549..5b2f4a4 100644 --- a/kx.Benchmark.Test/Connection/ConnectionStringArraySerialisationTests.cs +++ b/kx.Benchmark.Test/Connection/ConnectionStringArraySerialisationTests.cs @@ -19,7 +19,7 @@ public void ConnectionSerialisesAndDeserialisesStringArrayInput() { string[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -34,7 +34,7 @@ public void ConnectionSerialisesAndDeserialisesStringArrayInputWithZipEnabled() { string[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { connection.IsZipEnabled = true; diff --git a/kx.Benchmark.Test/Connection/ConnectionTimeSpanArraySerialisationTests.cs b/kx.Benchmark.Test/Connection/ConnectionTimeSpanArraySerialisationTests.cs index a7a6b93..0be7911 100644 --- a/kx.Benchmark.Test/Connection/ConnectionTimeSpanArraySerialisationTests.cs +++ b/kx.Benchmark.Test/Connection/ConnectionTimeSpanArraySerialisationTests.cs @@ -20,7 +20,7 @@ public void ConnectionSerialisesAndDeserialisesTimeSpanArrayInput() { TimeSpan[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -35,7 +35,7 @@ public void ConnectionSerialisesAndDeserialisesTimeSpanArrayInputWithZipEnabled( { TimeSpan[] expected = _data; - using (var connection = new c()) + using (var connection = new c(3)) { connection.IsZipEnabled = true; diff --git a/kx.Test/Connection/ConnectionAsyncWriteTests.cs b/kx.Test/Connection/ConnectionAsyncWriteTests.cs new file mode 100644 index 0000000..c92b29d --- /dev/null +++ b/kx.Test/Connection/ConnectionAsyncWriteTests.cs @@ -0,0 +1,117 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Moq; +using NUnit.Framework; + +namespace kx.Test.Connection +{ + [TestFixture] + public class ConnectionAsyncWriteTests + { + [Test] + public void ConnectionWritesExpectedObjectParameterToClientStreamAsynchronously() + { + object expected = "param1"; + + List bytesWritten = new List(); + + Mock testStream = CreateTestStream(bytesWritten); + + using (var connection = new c(testStream.Object)) + { + connection.ks(expected); + + object result = connection.Deserialize(bytesWritten.ToArray()); + + Assert.AreEqual(expected, result); + } + } + + [Test] + public void ConnectionWritesExpectedStringExpressionToClientStreamAsynchronously() + { + const string expected = "test_expression"; + + List bytesWritten = new List(); + + Mock testStream = CreateTestStream(bytesWritten); + + using (var connection = new c(testStream.Object)) + { + connection.ks(expected); + + object result = connection.Deserialize(bytesWritten.ToArray()); + + Assert.AreEqual(expected, result); + } + } + + [Test] + public void ConnectionWritesExpectedStringExpressionAndParameterToClientStreamAsynchronously() + { + const string expression = "test_expression"; + object parameter1 = 1; + + List bytesWritten = new List(); + + Mock testStream = CreateTestStream(bytesWritten); + + using (var connection = new c(testStream.Object)) + { + connection.ks(expression, parameter1); + + object[] result = connection.Deserialize(bytesWritten.ToArray()) as object[]; + + Assert.IsNotNull(result); + Assert.AreEqual(2, result.Length); + Assert.AreEqual(expression, new string(result[0] as char[])); + Assert.AreEqual(parameter1, result[1]); + } + } + + [Test] + public void ConnectionWritesExpectedStringExpressionAndParametersToClientStreamAsynchronously() + { + const string expression = "test_expression"; + object parameter1 = 1; + object parameter2 = 2; + + List bytesWritten = new List(); + + Mock testStream = CreateTestStream(bytesWritten); + + using (var connection = new c(testStream.Object)) + { + connection.ks(expression, parameter1, parameter2); + + object[] result = connection.Deserialize(bytesWritten.ToArray()) as object[]; + + Assert.IsNotNull(result); + Assert.AreEqual(3, result.Length); + Assert.AreEqual(expression, new string(result[0] as char[])); + Assert.AreEqual(parameter1, result[1]); + Assert.AreEqual(parameter2, result[2]); + } + } + + private Mock CreateTestStream(List bytesWritten) + { + Mock testStream = new Mock(); + + //record bytes written to test-stream. + testStream.Setup(s => s.Write(It.IsAny(), It.IsAny(), It.IsAny())) + .Callback((a, b, c) => + { + var content = a + .Skip(b) + .Take(c) + .ToArray(); + + bytesWritten.AddRange(content); + }); + + return testStream; + } + } +} \ No newline at end of file diff --git a/kx.Test/Connection/ConnectionSerialisationTests.cs b/kx.Test/Connection/ConnectionSerialisationTests.cs index 6d144e8..43d192b 100644 --- a/kx.Test/Connection/ConnectionSerialisationTests.cs +++ b/kx.Test/Connection/ConnectionSerialisationTests.cs @@ -9,10 +9,12 @@ namespace kx.Test.Connection [TestFixture] public class ConnectionSerialisationTests { + private readonly int _testVersionNumber = 3; + [Test] public void ConnectionSerialiseThrowsIfInputIsNull() { - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { Assert.Throws(() => connection.Serialize(1, null)); } @@ -48,7 +50,7 @@ public void ConnectionSerialiseThrowsIfTimeSpanSerialisationIsNotSupported() [Test] public void ConnectionDeserialiseThrowsIfBufferIsNull() { - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { Assert.Throws(() => connection.Deserialize(null)); } @@ -67,7 +69,7 @@ public void ConnectionDeserialiseThrowsIfBufferIsException() //end of the error message buffer.Add(0); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { Assert.Throws(() => connection.Deserialize(buffer.ToArray())); } @@ -89,7 +91,7 @@ public void ConnectionDeserialiseThrowsExceptionWithExpectedMessage() //end of the error message buffer.Add(0); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { try { @@ -109,7 +111,7 @@ public void ConnectionSerialisesAndDeserialisesBooleanTrueInput() { const bool expected = true; - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -124,7 +126,7 @@ public void ConnectionSerialisesAndDeserialisesBooleanFalseInput() { const bool expected = false; - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -139,7 +141,7 @@ public void ConnectionSerialisesAndDeserialisesGuidInput() { Guid expected = Guid.NewGuid(); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -154,7 +156,7 @@ public void ConnectionSerialisesAndDeserialisesByteInput() { const byte expected = 47; - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -169,7 +171,7 @@ public void ConnectionSerialisesAndDeserialisesShortInput() { const short expected = 47; - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -184,7 +186,7 @@ public void ConnectionSerialisesAndDeserialisesIntInput() { const int expected = 47; - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -199,7 +201,7 @@ public void ConnectionSerialisesAndDeserialisesLongInput() { const long expected = 47L; - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -214,7 +216,7 @@ public void ConnectionSerialisesAndDeserialisesFloatInput() { const float expected = 47.14F; - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -229,7 +231,7 @@ public void ConnectionSerialisesAndDeserialisesDoubleInput() { const double expected = 47.14; - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -244,7 +246,7 @@ public void ConnectionSerialisesAndDeserialisesCharInput() { const char expected = 'k'; - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -259,7 +261,7 @@ public void ConnectionSerialisesAndDeserialisesStringInput() { const string expected = "Test_Input"; - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -274,7 +276,7 @@ public void ConnectionSerialisesAndDeserialisesDateTimeInput() { DateTime expected = new DateTime(2020, 11, 04, 0, 0, 0, DateTimeKind.Utc); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -289,7 +291,7 @@ public void ConnectionSerialisesAndDeserialisesMonthInput() { c.Month expected = new c.Month(47); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -304,7 +306,7 @@ public void ConnectionSerialisesAndDeserialisesDateInput() { c.Date expected = new c.Date(47); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -319,7 +321,7 @@ public void ConnectionSerialisesAndDeserialisesKTimespanInput() { c.KTimespan expected = new c.KTimespan(4700); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -334,7 +336,7 @@ public void ConnectionSerialisesAndDeserialisesMinuteInput() { c.Minute expected = new c.Minute(47); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -349,7 +351,7 @@ public void ConnectionSerialisesAndDeserialisesSecondInput() { c.Second expected = new c.Second(47); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -364,7 +366,7 @@ public void ConnectionSerialisesAndDeserialisesTimeSpanInput() { TimeSpan expected = new TimeSpan(470000); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -379,7 +381,7 @@ public void ConnectionSerialisesAndDeserialisesDictInput() { c.Dict expected = new c.Dict(new string[] { "Key_1" }, new object[] { "Value_1" }); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -396,7 +398,7 @@ public void ConnectionSerialisesAndDeserialisesFlipInput() { c.Flip expected = new c.Flip(new c.Dict(new string[] { "Key_1" }, new object[] { "Value_1" })); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -413,7 +415,7 @@ public void ConnectionSerialisesAndDeserialisesObjectArrayInput() { object[] expected = CreateTestArray(i => string.Format("Hello_{0}", i), 50); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -428,7 +430,7 @@ public void ConnectionSerialisesAndDeserialisesBooleanArrayInput() { bool[] expected = CreateTestArray(i => i % 2 == 0, 50); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -443,7 +445,7 @@ public void ConnectionSerialisesAndDeserialisesGuidArrayInput() { Guid[] expected = CreateTestArray(i => Guid.NewGuid(), 50); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -458,7 +460,7 @@ public void ConnectionSerialisesAndDeserialisesByteArrayInput() { byte[] expected = CreateTestArray(i => (byte)i, 50); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -473,7 +475,7 @@ public void ConnectionSerialisesAndDeserialisesShortArrayInput() { short[] expected = CreateTestArray(i => (short)i, 50); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -488,7 +490,7 @@ public void ConnectionSerialisesAndDeserialisesIntArrayInput() { int[] expected = CreateTestArray(i => i, 50); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -503,7 +505,7 @@ public void ConnectionSerialisesAndDeserialisesLongArrayInput() { long[] expected = CreateTestArray(i => (long)i, 50); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -519,7 +521,7 @@ public void ConnectionSerialisesAndDeserialisesFloatArrayInput() { float[] expected = CreateTestArray(i => (float)i / 2, 50); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -534,7 +536,7 @@ public void ConnectionSerialisesAndDeserialisesDoubleArrayInput() { double[] expected = CreateTestArray(i => (double)i / 2, 50); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -549,7 +551,7 @@ public void ConnectionSerialisesAndDeserialisesCharArrayInput() { char[] expected = CreateTestArray(i => (char)i, 50); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -564,7 +566,7 @@ public void ConnectionSerialisesAndDeserialisesStringArrayInput() { string[] expected = CreateTestArray(i => string.Format("Hello_{0}", i), 50); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -579,7 +581,7 @@ public void ConnectionSerialisesAndDeserialisesDateTimeArrayInput() { DateTime[] expected = CreateTestArray(i => new DateTime(2020, 11, 11, 0, 0, i, DateTimeKind.Utc), 50); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -594,7 +596,7 @@ public void ConnectionSerialisesAndDeserialisesMonthArrayInput() { c.Month[] expected = CreateTestArray(i => new c.Month(i), 50); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -609,7 +611,7 @@ public void ConnectionSerialisesAndDeserialisesDateArrayInput() { c.Date[] expected = CreateTestArray(i => new c.Date(new DateTime(2020, 11, 11, 0, 0, i, DateTimeKind.Utc)), 50); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -624,7 +626,7 @@ public void ConnectionSerialisesAndDeserialisesKTimeSpanArrayInput() { c.KTimespan[] expected = CreateTestArray(i => new c.KTimespan(i * 100), 50); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -639,7 +641,7 @@ public void ConnectionSerialisesAndDeserialisesMinuteArrayInput() { c.Minute[] expected = CreateTestArray(i => new c.Minute(i), 50); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -654,7 +656,7 @@ public void ConnectionSerialisesAndDeserialisesSecondArrayInput() { c.Second[] expected = CreateTestArray(i => new c.Second(i), 50); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -669,7 +671,7 @@ public void ConnectionSerialisesAndDeserialisesTimeSpanArrayInput() { TimeSpan[] expected = CreateTestArray(i => new TimeSpan(i * 10000), 50); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { byte[] serialisedData = connection.Serialize(1, expected); @@ -684,7 +686,7 @@ public void ConnectionSerialisesAndDeserialisesObjectArrayInputWithZipEnabled() { object[] expected = CreateTestArray(i => string.Format("Hello_{0}", i), 2000); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { connection.IsZipEnabled = true; @@ -701,7 +703,7 @@ public void ConnectionSerialisesAndDeserialisesBooleanArrayInputWithZipEnabled() { bool[] expected = CreateTestArray(i => i % 2 == 0, 2000); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { connection.IsZipEnabled = true; @@ -718,7 +720,7 @@ public void ConnectionSerialisesAndDeserialisesGuidArrayInputWithZipEnabled() { Guid[] expected = CreateTestArray(i => Guid.NewGuid(), 2000); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { connection.IsZipEnabled = true; @@ -735,7 +737,7 @@ public void ConnectionSerialisesAndDeserialisesByteArrayInputWithZipEnabled() { byte[] expected = CreateTestArray(i => (byte)i, 2000); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { connection.IsZipEnabled = true; @@ -752,7 +754,7 @@ public void ConnectionSerialisesAndDeserialisesShortArrayInputWithZipEnabled() { short[] expected = CreateTestArray(i => (short)i, 2000); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { connection.IsZipEnabled = true; @@ -769,7 +771,7 @@ public void ConnectionSerialisesAndDeserialisesIntArrayInputWithZipEnabled() { int[] expected = CreateTestArray(i => i, 2000); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { connection.IsZipEnabled = true; @@ -786,7 +788,7 @@ public void ConnectionSerialisesAndDeserialisesLongArrayInputWithZipEnabled() { long[] expected = CreateTestArray(i => (long)i, 2000); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { connection.IsZipEnabled = true; @@ -802,7 +804,7 @@ public void ConnectionSerialisesAndDeserialisesFloatArrayInputWithZipEnabled() { float[] expected = CreateTestArray(i => (float)i / 2, 50); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { connection.IsZipEnabled = true; @@ -819,7 +821,7 @@ public void ConnectionSerialisesAndDeserialisesDoubleArrayInputWithZipEnabled() { double[] expected = CreateTestArray(i => (double)i / 2, 2000); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { connection.IsZipEnabled = true; @@ -840,7 +842,7 @@ public void ConnectionSerialisesAndDeserialisesCharArrayInputWithZipEnabled() return (char)Math.Abs(new Random(i).Next(0, 127)); }, 2000); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { connection.IsZipEnabled = true; @@ -856,7 +858,7 @@ public void ConnectionSerialisesAndDeserialisesStringArrayInputWithZipEnabled() { string[] expected = CreateTestArray(i => string.Format("Hello_{0}", i), 2000); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { connection.IsZipEnabled = true; @@ -873,7 +875,7 @@ public void ConnectionSerialisesAndDeserialisesDateTimeArrayInputWithZipEnabled( { DateTime[] expected = CreateTestArray(i => new DateTime(2020, 11, 11, 0, 0, i % 60, DateTimeKind.Utc), 2000); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { connection.IsZipEnabled = true; @@ -890,7 +892,7 @@ public void ConnectionSerialisesAndDeserialisesMonthArrayInputWithZipEnabled() { c.Month[] expected = CreateTestArray(i => new c.Month(i), 2000); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { connection.IsZipEnabled = true; @@ -907,7 +909,7 @@ public void ConnectionSerialisesAndDeserialisesDateArrayInputWithZipEnabled() { c.Date[] expected = CreateTestArray(i => new c.Date(new DateTime(2020, 11, 11, 0, 0, i % 60, DateTimeKind.Utc)), 2000); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { connection.IsZipEnabled = true; @@ -924,7 +926,7 @@ public void ConnectionSerialisesAndDeserialisesKTimeSpanArrayInputWithZipEnabled { c.KTimespan[] expected = CreateTestArray(i => new c.KTimespan(i * 100), 2000); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { connection.IsZipEnabled = true; @@ -941,7 +943,7 @@ public void ConnectionSerialisesAndDeserialisesMinuteArrayInputWithZipEnabled() { c.Minute[] expected = CreateTestArray(i => new c.Minute(i), 2000); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { connection.IsZipEnabled = true; @@ -958,7 +960,7 @@ public void ConnectionSerialisesAndDeserialisesSecondArrayInputWithZipEnabled() { c.Second[] expected = CreateTestArray(i => new c.Second(i), 2000); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { connection.IsZipEnabled = true; @@ -975,7 +977,7 @@ public void ConnectionSerialisesAndDeserialisesTimeSpanArrayInputWithZipEnabled( { TimeSpan[] expected = CreateTestArray(i => new TimeSpan(i * 10000), 2000); - using (var connection = new c()) + using (var connection = new c(_testVersionNumber)) { connection.IsZipEnabled = true; diff --git a/kx.Test/Connection/ConnectionSyncWriteTests.cs b/kx.Test/Connection/ConnectionSyncWriteTests.cs new file mode 100644 index 0000000..baa2728 --- /dev/null +++ b/kx.Test/Connection/ConnectionSyncWriteTests.cs @@ -0,0 +1,165 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Moq; +using NUnit.Framework; + +namespace kx.Test.Connection +{ + [TestFixture] + public class ConnectionSyncWriteTests + { + [Test] + public void ConnectionWritesExpectedObjectParameterToClientStreamSynchronously() + { + object expected = "param1"; + + List bytesWritten = new List(); + + Mock testStream = CreateTestStream(bytesWritten); + + using (var connection = new c(testStream.Object)) + { + connection.k(expected); + + object result = connection.Deserialize(bytesWritten.ToArray()); + + Assert.AreEqual(expected, result); + } + } + + [Test] + public void ConnectionWritesExpectedStringExpressionToClientStreamSynchronously() + { + const string expression = "test_expression"; + + List bytesWritten = new List(); + + Mock testStream = CreateTestStream(bytesWritten); + + using (var connection = new c(testStream.Object)) + { + connection.k(expression); + + object result = connection.Deserialize(bytesWritten.ToArray()); + + Assert.AreEqual(expression, result); + } + } + + [Test] + public void ConnectionWritesExpectedStringExpressionAndObjectParameterToClientStreamSynchronously() + { + const string expression = "test_expression"; + object parameter1 = 1; + + List bytesWritten = new List(); + + Mock testStream = CreateTestStream(bytesWritten); + + using (var connection = new c(testStream.Object)) + { + connection.k(expression, parameter1); + + object[] result = connection.Deserialize(bytesWritten.ToArray()) as object[]; + + Assert.IsNotNull(result); + Assert.AreEqual(2, result.Length); + Assert.AreEqual(expression, new string(result[0] as char[])); + Assert.AreEqual(parameter1, result[1]); + } + } + + [Test] + public void ConnectionWritesExpectedStringExpressionAndTwoObjectParametersToClientStreamSynchronously() + { + const string expression = "test_expression"; + object parameter1 = 1; + object parameter2 = 2; + + List bytesWritten = new List(); + + Mock testStream = CreateTestStream(bytesWritten); + + using (var connection = new c(testStream.Object)) + { + connection.k(expression, parameter1, parameter2); + + object[] result = connection.Deserialize(bytesWritten.ToArray()) as object[]; + + Assert.IsNotNull(result); + Assert.AreEqual(3, result.Length); + Assert.AreEqual(expression, new string(result[0] as char[])); + Assert.AreEqual(parameter1, result[1]); + Assert.AreEqual(parameter2, result[2]); + } + } + + [Test] + public void ConnectionWritesExpectedStringExpressionAndThreeObjectParametersToClientStreamSynchronously() + { + const string expression = "test_expression"; + object parameter1 = 1; + object parameter2 = 2; + object parameter3 = 3; + + List bytesWritten = new List(); + + Mock testStream = CreateTestStream(bytesWritten); + + using (var connection = new c(testStream.Object)) + { + connection.k(expression, parameter1, parameter2, parameter3); + + object[] result = connection.Deserialize(bytesWritten.ToArray()) as object[]; + + Assert.IsNotNull(result); + Assert.AreEqual(4, result.Length); + Assert.AreEqual(expression, new string(result[0] as char[])); + Assert.AreEqual(parameter1, result[1]); + Assert.AreEqual(parameter2, result[2]); + Assert.AreEqual(parameter3, result[3]); + } + } + + private Mock CreateTestStream(List bytesWritten) + { + Mock testStream = new Mock(); + + //simulate sync response read + testStream.Setup(s => s.Read(It.IsAny(), It.IsAny(), It.IsAny())) + .Returns((a, b, c) => + { + //simulate header read + if (a.Length == 8) + { + a[0] = 1; + a[1] = 2; + a[2] = 0; + a[3] = 0; + a[4] = 10; + } + //simulate empty string response + if (a.Length == 2) + { + a[0] = 245; + } + return a.Length; + }); + + //record bytes written to test-stream. + testStream.Setup(s => s.Write(It.IsAny(), It.IsAny(), It.IsAny())) + .Callback((a, b, c) => + { + var content = a + .Skip(b) + .Take(c) + .ToArray(); + + bytesWritten.AddRange(content); + }); + + return testStream; + } + } +} \ No newline at end of file diff --git a/kx.Test/Connection/ConnectionTests.cs b/kx.Test/Connection/ConnectionTests.cs index c801eed..363de65 100644 --- a/kx.Test/Connection/ConnectionTests.cs +++ b/kx.Test/Connection/ConnectionTests.cs @@ -20,7 +20,7 @@ public void ConnectionInitialises() [Test] public void ConnectionThrowsIfHostIsNull() { - Assert.Throws(() => new c(null, 8080)); + Assert.Throws(() => new c(null as string, 8080)); } [Test] diff --git a/kx.Test/kx.Test.csproj b/kx.Test/kx.Test.csproj index f649734..b42f6ab 100644 --- a/kx.Test/kx.Test.csproj +++ b/kx.Test/kx.Test.csproj @@ -25,6 +25,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/kx/c.cs b/kx/c.cs index dc4167a..9d7e77d 100644 --- a/kx/c.cs +++ b/kx/c.cs @@ -214,11 +214,11 @@ public c(string host, /// Initialises a new instance of /// /// - /// Parameterless constructor intended for unit-testing only, keep internal. + /// Parameterless constructor intended for unit-testing only and access to global members. /// - internal c() - : this(3) + protected c() { + _versionNumber = 3; } /// @@ -234,6 +234,63 @@ internal c(int versionNumber) _versionNumber = versionNumber; } + /// + /// Initialises a new instance of with a specified + /// client-stream. + /// + /// The client-stream. + /// + /// Test constructor intended for unit-testing only, keep internal. + /// + internal c(Stream clientStream) + : this(clientStream, 3) + { + + } + + /// + /// Initialises a new instance of with a specified + /// client-stream and version-number. + /// + /// The client-stream. + /// The KDB+ version number to use for testing. + /// + /// Test constructor intended for unit-testing only, keep internal. + /// + internal c(Stream clientStream, int versionNumber) + { + _clientStream = clientStream; + _versionNumber = versionNumber; + } + + + /// + /// Gets or sets whether or not the resulting byte array is compressed. + /// + public bool IsCompressed + { + get; + private set; + } + + /// + /// Gets or sets whether or not the resulting byte array is sync. + /// + public bool IsSync + { + get; + private set; + } + + /// + /// Gets or sets whether or not the resulting byte array is a response. + /// + public bool IsResponse + { + get; + private set; + } + /// /// Gets or sets whether zip compression is enabled. /// @@ -243,6 +300,42 @@ public bool IsZipEnabled set; } + /// + /// Gets the buffer used to store the incoming message bytes from the remote prior + /// to de-serialisation. + /// + protected byte[] ReadBuffer + { + get { return _readBuffer; } + } + + /// + /// Gets or sets the current postion of the de-serialiser with the read buffer. + /// + /// Read-Position cannot be less than zero. + protected int ReadPosition + { + get { return _readPosition; } + set + { + if (value < 0) + { + throw new ArgumentOutOfRangeException(nameof(value), + $"Unable to set Read-Position. Value must be greater than 0 but was {value}"); + } + _readPosition = value; + } + } + + /// + /// Gets a boolean flag indicating the Endianness of a message; + /// true indicates little-Endian, false indicatse big-Endian. + /// + protected bool IsLittleEndian + { + get { return _isLittleEndian; } + } + /// /// Gets or sets character encoding for serialising/deserialising strings, /// default is . @@ -270,29 +363,7 @@ public bool IsZipEnabled /// public object k() { - _readBuffer = new byte[8]; - read(_readBuffer); - - _isLittleEndian = _readBuffer[0] == 1; - bool num = _readBuffer[2] == 1; - _readPosition = 4; - - _readBuffer = new byte[ri() - 8]; - read(_readBuffer); - - if (num) - { - UnCompress(); - } - else - { - _readPosition = 0; - } - if (_readBuffer[0] == 128) - { - _readPosition = 1; - throw new KException(rs()); - } + k0(); return r(); } @@ -388,6 +459,39 @@ public object k(string s, object x, object y, object z) return k(array); } + /// + /// Waits for an async message and read header. + /// + public void k0() + { + _readBuffer = new byte[8]; + read(_readBuffer); + + ParseHeader(); + _readPosition = 4; + _readBuffer = new byte[ri() - 8]; + read(_readBuffer); + + if (IsCompressed) + { + UnCompress(); + } + else + { + _readPosition = 0; + } + ParseException(); + } + + /// + /// Sends an async message to the remote KDB+ process with a specified object parameter. + /// + /// The object parameter. + public void ks(object x) + { + w(0, x); + } + /// /// Sends an async message to the remote KDB+ process with a specified expression. /// @@ -433,6 +537,27 @@ public void ks(string s, object x, object y) w(0, array); } + /// + /// Sends an async message to the remote KDB+ process with a specified object parameter. + /// + /// The object parameter. + public void kn(object x) + { + w(1, x); + } + + /// + /// Sends a response message to the remote KDB+ process. + /// + /// The response message to send. + /// + /// This should be called only during processing of an incoming sync message. + /// + public void kr(object x) + { + w(2, x); + } + /// /// Serialises a specified object as a byte-array /// @@ -501,16 +626,14 @@ public object Deserialize(byte[] buffer) } _readBuffer = buffer; - _isLittleEndian = _readBuffer[0] == 1; - - bool isCompressed = _readBuffer[2] == 1; + ParseHeader(); int responseLength = buffer.Length - 8; _readBuffer = new byte[responseLength]; Array.Copy(buffer, 8, _readBuffer, 0, responseLength); - if (isCompressed) + if (IsCompressed) { UnCompress(); } @@ -526,19 +649,52 @@ public object Deserialize(byte[] buffer) return r(); } + /// + /// Reads the contents of the incoming message from the client + /// read-buffer. + /// + /// + /// The deserialised contents from the read-buffer. + /// + protected object ReadObject() + { + return r(); + } + + /// + /// Reads an from the client read-buffer. + /// + /// + /// A deserialised int from the client read-buffer. + /// + protected int ReadInt32() + { + return ri(); + } + + /// + /// Writes a specified byte array directly to the underlying client stream. + /// + /// The byte array to be writtern to the client stream. + /// The number of bytes to be written to the client stream. + protected void Write(byte[] bytes, int number) + { + _clientStream.Write(bytes, 0, number); + } + /// /// Gets the null object for the specified . /// - /// The .NET type. + /// The .NET type. /// /// Instance of null object of specified KDB+ type. /// - public static object NULL(Type t) + public static object NULL(Type type) { for (int i = 0; i < KNullValues.Length; i++) { if (KNullValues[i] != null && - t == KNullValues[i].GetType()) + KNullValues[i].GetType() == type) { return KNullValues[i]; } @@ -547,7 +703,7 @@ public static object NULL(Type t) } /// - /// Gest the null object for the specified id. + /// Gets the null object for the specified id. /// /// The character id. /// @@ -661,6 +817,23 @@ public static object at(object x, int i) return null; } + private void ParseHeader() + { + _isLittleEndian = (_readBuffer[0] == 1); + IsCompressed = (_readBuffer[2] == 1); + IsSync = (_readBuffer[1] == 1); + IsResponse = (_readBuffer[1] == 2); + } + + private void ParseException() + { + if (_readBuffer[0] == 128) + { + _readPosition = 1; + throw new KException(rs()); + } + } + [System.Diagnostics.CodeAnalysis.SuppressMessage("csharpsquid", "S1121: Keep assignment of p and h for backwards compatability")] private void Compress() { @@ -1530,14 +1703,8 @@ private object r() private void w(int i, object x) { - int j = nx(x) + 8; - _writeBuffer = new byte[j]; - _writeBuffer[0] = 1; - _writeBuffer[1] = (byte)i; - _writePosition = 4; - w(j); - w(x); - _clientStream.Write(_writeBuffer, 0, j); + byte[] buffer = Serialize(i, x); + _clientStream.Write(buffer, 0, buffer.Length); } private void read(byte[] b) @@ -1558,7 +1725,7 @@ private void read(byte[] b) } return; } - throw new Exception("read"); + throw new KException("read"); } private static int ns(string s)