forked from KxSystems/csharpkdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc.cs
More file actions
3266 lines (3026 loc) · 112 KB
/
Copy pathc.cs
File metadata and controls
3266 lines (3026 loc) · 112 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace kx
{
/// <summary>
/// An extended class of <see cref="TcpClient"/> that serves as a connector class for
/// interfacing with a KDB+ process.
/// </summary>
/// <remarks>
/// This class is essentially a serializer/deserializer of .NET types
/// to/from the KDB+ IPC wire format, enabling remote method invocation in KDB+ via TCP/IP.
/// </remarks>
public class c : IDisposable
{
private readonly Socket _socket;
private readonly Stream _clientStream;
private bool disposed;
private const int DefaultMaxBufferSize = 65536;
private const long Year2000Ticks = 630822816000000000L;
private const int KMinInt32 = int.MinValue;
private const long KMinInt64 = long.MinValue;
private const double NullDouble = double.NaN;
private static readonly DateTime KMinDateTime = DateTime.MinValue.AddTicks(1L);
private static readonly DateTime KMaxDateTime = DateTime.MaxValue;
private static readonly object[] KNullValues = new object[20]
{
null,
false,
default(Guid),
null,
(byte)0,
short.MinValue,
KMinInt32,
KMinInt64,
(float)NullDouble,
NullDouble,
' ',
"",
new DateTime(0L,DateTimeKind.Unspecified),
new Month(KMinInt32),
new Date(KMinInt32),
new DateTime(0L,DateTimeKind.Unspecified),
new KTimespan(KMinInt64),
new Minute(KMinInt32),
new Second(KMinInt32),
new TimeSpan(KMinInt64)
};
private static readonly char[] KNullCharIds = " bg xhijefcspmdznuvt"
.ToCharArray();
private static readonly int[] NumberOfBytesForType = new int[20]
{
0,
1,
16,
0,
1,
2,
4,
8,
4,
8,
1,
0,
8,
4,
4,
8,
8,
4,
4,
4
};
private readonly int _maxBufferSize;
private readonly int _versionNumber;
private readonly bool _isLoopback;
/// <summary>
/// The buffer used to store the incoming message bytes from the remote prior to de-serialization.
/// </summary>
private byte[] _readBuffer;
/// <summary>
/// The buffer used to store the outgoing message bytes when serializing an object.
/// </summary>
private byte[] _writeBuffer;
/// <summary>
/// The current position of the de-serializer within the read buffer.
/// </summary>
private int _readPosition;
/// <summary>
/// The current position of the serialiser within the write buffer.
/// </summary>
private int _writePosition;
/// <summary>
/// A boolean flag indicating the Endianness of a message.
/// </summary>
private bool _isLittleEndian;
/// <summary>
/// Specifies the IP address family preference to use when establishing a TCP connection.
/// </summary>
/// <remarks>
/// Use this option to control whether a connection should be attempted using IPv4,
/// IPv6, or a dual-mode socket that can support both IPv4 and IPv6 where the
/// operating system and network configuration allow it.
/// </remarks>
public enum IpVersionPreference
{
/// <summary>
/// Use IPv4 only.
/// </summary>
/// <remarks>
/// Creates or selects a socket that uses <see cref="System.Net.Sockets.AddressFamily.InterNetwork"/>.
/// </remarks>
IPv4,
/// <summary>
/// Use IPv6 only.
/// </summary>
/// <remarks>
/// Creates or selects a socket that uses <see cref="System.Net.Sockets.AddressFamily.InterNetworkV6"/>
/// without accepting IPv4-mapped IPv6 addresses.
/// </remarks>
IPv6,
/// <summary>
/// Use a dual-mode socket that can support both IPv4 and IPv6.
/// </summary>
/// <remarks>
/// Creates or selects an IPv6 socket with <see cref="System.Net.Sockets.Socket.DualMode"/>
/// enabled, allowing both IPv6 addresses and IPv4-mapped IPv6 addresses where supported.
/// </remarks>
DualStack
}
/// <summary>
/// Initialises a new instance of <see cref="c" /> with a specified host and port
/// to connect to.
/// </summary>
/// <param name="host">The host to connect to.</param>
/// <param name="port">The port to connect to.</param>
/// <exception cref="ArgumentNullException"><paramref name="host" /> was null.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> must be between <see cref="System.Net.IPEndPoint.MinPort" /> and <see cref="System.Net.IPEndPoint.MaxPort" /></exception>
/// <exception cref="KException">Unable to connect to KDB+ process, access denied or process unavailable.</exception>
public c(string host, int port) : this(host, port, Environment.UserName) { }
private c(
string host,
int port,
string userPassword,
int maxBufferSize,
KdbTlsOptions tlsOptions,
bool uds,
IpVersionPreference ipVersionPreference)
{
if (host == null)
{
throw new ArgumentNullException(nameof(host),
$"Unable to initialise c. {nameof(host)} parameter cannot be null");
}
if (!uds && (port < 0 || port > 65535))
{
throw new ArgumentOutOfRangeException(nameof(port),
$"Unable to initialise c. {nameof(port)} parameter must be between MinPort and MaxPort");
}
if (userPassword == null)
{
throw new ArgumentNullException(nameof(userPassword),
$"Unable to initialise c. {nameof(userPassword)} parameter cannot be null");
}
_maxBufferSize = maxBufferSize;
if(uds)
{
_socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP);
_socket.Connect(new UnixDomainSocketEndPoint(host));
_isLoopback = true;
}
else
{
switch (ipVersionPreference)
{
case (IpVersionPreference.IPv4):
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
break;
case (IpVersionPreference.IPv6):
_socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
break;
case (IpVersionPreference.DualStack):
_socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
break;
}
_socket.Connect(host, port);
_isLoopback = _socket.RemoteEndPoint is IPEndPoint &&
IPAddress.IsLoopback((_socket.RemoteEndPoint as IPEndPoint).Address);
}
_clientStream = new NetworkStream(_socket);
if (tlsOptions != null && tlsOptions.Enabled)
{
var sslStream = new SslStream(
_clientStream,
leaveInnerStreamOpen: false,
tlsOptions.RemoteCertificateValidationCallback,
tlsOptions.LocalCertificateSelectionCallback);
var targetHost = tlsOptions.TargetHost ?? host;
var clientCertificates = tlsOptions.ClientCertificates;
var enabledSslProtocols = tlsOptions.EnabledSslProtocols ?? SslProtocols.None;
var checkCertificateRevocation =
tlsOptions.CertificateRevocationCheckMode.HasValue &&
tlsOptions.CertificateRevocationCheckMode.Value != X509RevocationMode.NoCheck;
sslStream.AuthenticateAsClient(
targetHost,
clientCertificates,
enabledSslProtocols,
checkCertificateRevocation);
_clientStream = sslStream;
}
_writeBuffer = new byte[2 + userPassword.Length];
_writePosition = 0;
w(userPassword + "\u0003");
_clientStream.Write(_writeBuffer, 0, _writePosition);
if (_clientStream.Read(_writeBuffer, 0, 1) != 1)
{
throw new KException("access");
}
_versionNumber = Math.Min(_writeBuffer[0], (byte)3);
}
/// <summary>
/// Initialises a new instance of <see cref="c" /> with a specified host and port
/// to connect to, a username/password for authentication, an optional maximum buffersize and
/// an optional flag indicating whether to use TLS.
/// The maximum transmissible message size is 2GB due to a limitation with the maximum array size in C sharp,
/// therefore <a href="https://code.kx.com/q/basics/ipc/#handshake">capability 3</a> will be used within the kdb+ handshake.
/// </summary>
/// <param name="host">The host to connect to.</param>
/// <param name="port">The port to connect to.</param>
/// <param name="userPassword">The username and passsword, as "username:password" for remote authorisation.</param>
/// <param name="maxBufferSize">The maximum buffer size, default is 65536.</param>
/// <param name="useTLS">A boolean flag indicating whether or not TLS authentication is enabled, default is false.</param>
/// <param name="ipVersionPreference">The IP address family preference to use when establishing a TCP connection.</param>
/// <exception cref="ArgumentNullException"><paramref name="host" /> or <paramref name="userPassword" /> was null.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> must be between <see cref="System.Net.IPEndPoint.MinPort" /> and <see cref="System.Net.IPEndPoint.MaxPort" /></exception>
/// <exception cref="KException">Unable to connect to KDB+ process, access denied or process unavailable.</exception>
public c(
string host,
int port,
string userPassword,
int maxBufferSize = DefaultMaxBufferSize,
bool useTLS = false,
IpVersionPreference ipVersionPreference = IpVersionPreference.IPv4)
: this(host, port, userPassword, maxBufferSize, useTLS ? KdbTls.Default(host) : KdbTlsOptions.Disabled, false, ipVersionPreference)
{
}
/// <param name="host">The hostname or IP address of the KDB+ server.</param>
/// <param name="port">The TCP port of the KDB+ server.</param>
/// <param name="userPassword">The username/password string used for authentication.</param>
/// <param name="maxBufferSize">The maximum buffer size used for IPC messages.</param>
/// <param name="tlsOptions">The TLS options to use for the connection.</param>
/// <param name="ipVersionPreference">The IP address family preference to use when establishing a TCP connection.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="host"/> or <paramref name="userPassword"/> was null.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="port"/> must be between <see cref="IPEndPoint.MinPort"/> and
/// <see cref="IPEndPoint.MaxPort"/>.
/// </exception>
/// <exception cref="KException">
/// Unable to connect to the KDB+ process, access denied, or process unavailable.
/// </exception>
public c(
string host,
int port,
string userPassword,
int maxBufferSize,
KdbTlsOptions tlsOptions,
IpVersionPreference ipVersionPreference = IpVersionPreference.IPv4)
: this(host,port,userPassword,maxBufferSize,tlsOptions ?? KdbTlsOptions.Disabled,false,ipVersionPreference)
{
}
/// <summary>
/// Initialises a new instance of <see cref="c" /> using a Unix Domain Socket connection.
/// The maximum transmissible message size is 2GB due to a limitation with the maximum array size in C sharp,
/// therefore <a href="https://code.kx.com/q/basics/ipc/#handshake">capability 3</a> will be used within the kdb+ handshake.
/// </summary>
/// <param name="file">uds file e.g. "/tmp/kx.5010" is the default with kdb+ listening on port 5010</param>
/// <param name="userPassword">The username and passsword, as "username:password" for remote authorisation.</param>
/// <param name="maxBufferSize">The maximum buffer size, default is 65536.</param>
/// <param name="ipVersionPreference">The IP address family preference to use when establishing a TCP connection.</param>
/// <exception cref="ArgumentNullException"><paramref name="file" /> or <paramref name="userPassword" /> was null.</exception>
/// <exception cref="KException">Unable to connect to KDB+ process, access denied or process unavailable.</exception>
/// <exception cref="PlatformNotSupportedException">The current OS does not support Unix Domain Sockets</exception>
public c(
string file,
string userPassword,
int maxBufferSize = DefaultMaxBufferSize,
IpVersionPreference ipVersionPreference = IpVersionPreference.IPv4)
: this(file, 0, userPassword, maxBufferSize, KdbTlsOptions.Disabled, true, ipVersionPreference)
{
}
/// <summary>
/// Initialises a new instance of <see cref="c"/> using a Unix Domain Socket connection,
/// a username/password for authentication, a maximum buffer size, and explicit TLS options.
/// </summary>
/// <param name="file">The Unix Domain Socket file path.</param>
/// <param name="userPassword">The username/password string used for authentication.</param>
/// <param name="maxBufferSize">The maximum buffer size used for IPC messages.</param>
/// <param name="tlsOptions">The TLS options to use for the connection.</param>
/// <param name="ipVersionPreference">The IP address family preference to use when establishing a TCP connection.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="file"/> or <paramref name="userPassword"/> was null.
/// </exception>
/// <exception cref="KException">
/// Unable to connect to the KDB+ process, access denied, or process unavailable.
/// </exception>
/// <exception cref="PlatformNotSupportedException">
/// The current operating system does not support Unix Domain Sockets.
/// </exception>
public c(
string file,
string userPassword,
int maxBufferSize,
KdbTlsOptions tlsOptions,
IpVersionPreference ipVersionPreference = IpVersionPreference.IPv4)
: this(file, 0, userPassword, maxBufferSize, tlsOptions ?? KdbTlsOptions.Disabled, true, ipVersionPreference)
{
}
/// <summary>
/// Initialises a new instance of <see cref="c"/>
/// </summary>
/// <remarks>
/// Parameterless constructor intended for unit-testing only and access to global members.
/// </remarks>
protected c()
{
_versionNumber = 3;
}
/// <summary>
/// Initialises a new instance of <see cref="c"/> with a specified
/// KDB+ version number.
/// </summary>
/// <param name="versionNumber">The KDB+ version number to use for testing.</param>
/// <remarks>
/// Test constructor intended for unit-testing only, keep internal.
/// </remarks>
internal c(int versionNumber)
{
_versionNumber = versionNumber;
}
/// <summary>
/// Initialises a new instance of <see cref="c"/> with a specified
/// client-stream.
/// </summary>
/// <param name="clientStream">The client-stream.</param>
/// <remarks>
/// Test constructor intended for unit-testing only, keep internal.
/// </remarks>
internal c(Stream clientStream)
: this(clientStream, 3)
{
}
/// <summary>
/// Initialises a new instance of <see cref="c"/> with a specified
/// client-stream and version-number.
/// </summary>
/// <param name="clientStream">The client-stream.</param>
/// <param name="versionNumber">The KDB+ version number to use for testing.</param>
/// <remarks>
/// Test constructor intended for unit-testing only, keep internal.
/// </remarks>
internal c(Stream clientStream, int versionNumber)
{
_clientStream = clientStream;
_versionNumber = versionNumber;
}
/// <summary>
/// Gets or sets whether or not the resulting byte array is compressed.
/// </summary>
public bool IsCompressed
{
get;
private set;
}
/// <summary>
/// Gets or sets whether or not the resulting byte array is sync.
/// </summary>
public bool IsSync
{
get;
private set;
}
/// <summary>
/// Gets or sets whether or not the resulting byte array is a response.
/// </summary>
public bool IsResponse
{
get;
private set;
}
/// <summary>
/// Gets or sets whether zip compression is enabled.
/// </summary>
public bool IsZipEnabled
{
get;
set;
}
#pragma warning disable CA1819
/// <summary>
/// Gets the buffer used to store the incoming message bytes from the remote prior
/// to de-serialisation.
/// </summary>
protected byte[] ReadBuffer
{
get { return _readBuffer; }
}
#pragma warning restore CA1819
/// <summary>
/// Gets or sets the current postion of the de-serialiser with the read buffer.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">Read-Position cannot be less than zero.</exception>
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;
}
}
/// <summary>
/// Gets a boolean flag indicating the Endianness of a message;
/// <c>true</c> indicates little-Endian, <c>false</c> indicatse big-Endian.
/// </summary>
protected bool IsLittleEndian
{
get { return _isLittleEndian; }
}
/// <summary>
/// Gets or sets character encoding for serialising/deserialising strings,
/// default is <see cref="Encoding.ASCII"/>.
/// </summary>
public static Encoding e { get; set; } = Encoding.ASCII;
/// <summary>
/// Requests that the underlying stream and TCP connection be closed.
/// </summary>
public void Close()
{
if (_clientStream != null)
{
_clientStream.Close();
}
_socket.Close();
}
/// <summary>
/// Destructor
/// </summary>
~c()
{
Dispose(false);
}
/// <summary>
/// Releases unmanaged resources and performs other cleanup operations.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and optionally managed resources.
/// </summary>
/// <param name="disposing">
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
/// </param>
protected virtual void Dispose(bool disposing){
if (disposed)
{
return;
}
if(disposing)
{
if (_clientStream != null){
_clientStream.Close();
_clientStream.Dispose();
}
if (_socket != null){
_socket.Close();
_socket.Dispose();
}
}
disposed = true;
}
/// <summary>
/// Reads an incoming message from the remote KDB+ process async.
/// </summary>
/// <returns>
/// Deserialised response to request.
/// </returns>
public async Task<object> kAsync()
{
await k0Async().ConfigureAwait(false);
return r();
}
/// <summary>
/// Reads an incoming message from the remote KDB+ process.
/// </summary>
/// <returns>
/// Deserialised response to request.
/// </returns>
public object k()
{
k0();
return r();
}
/// <summary>
/// Sends a sync message request to the remote KDB+ process.
/// </summary>
/// <param name="x">The object parameter to send.</param>
/// <returns>
/// Deserialised response to request.
/// </returns>
public object k(object x)
{
w(1, x);
return k();
}
/// <summary>
/// Sends a sync message request to the remote KDB+ process with a
/// specified expression.
/// </summary>
/// <param name="s">The expression to send.</param>
/// <returns>
/// Deserialised response to request.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="s"/> parameter was null.</exception>
public object k(string s)
{
if (s == null)
{
throw new ArgumentNullException(nameof(s));
}
return k(s.ToCharArray());
}
/// <summary>
/// Sends a sync message request to the remote KDB+ process with a
/// specified expression and request object.
/// </summary>
/// <param name="s">The expression to send.</param>
/// <param name="x">The object parameter to send.</param>
/// <returns>
/// Deserialised response to request.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="s"/> parameter was null.</exception>
public object k(string s, object x)
{
if (s == null)
{
throw new ArgumentNullException(nameof(s));
}
object[] array = new object[]
{
s.ToCharArray(),
x
};
return k(array);
}
/// <summary>
/// Sends a sync message request to the remote KDB+ process with a
/// specified expression and request objects.
/// </summary>
/// <param name="s">The expression to send.</param>
/// <param name="x">The first object parameter to send.</param>
/// <param name="y">The second object parameter to send.</param>
/// <returns>
/// Deserialised response to request.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="s"/> parameter was null.</exception>
public object k(string s, object x, object y)
{
if (s == null)
{
throw new ArgumentNullException(nameof(s));
}
object[] array = new object[]
{
s.ToCharArray(),
x,
y
};
return k(array);
}
/// <summary>
/// Sends a sync message request to the remote KDB+ process with a
/// specified expression and request objects.
/// </summary>
/// <param name="s">The expression to send.</param>
/// <param name="x">The first object parameter to send.</param>
/// <param name="y">The second object parameter to send.</param>
/// <param name="z">The third object parameter to send.</param>
/// <returns>
/// Deserialised response to request.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="s"/> parameter was null.</exception>
public object k(string s, object x, object y, object z)
{
if (s == null)
{
throw new ArgumentNullException(nameof(s));
}
object[] array = new object[]
{
s.ToCharArray(),
x,
y,
z
};
return k(array);
}
/// <summary>
/// Waits for an async message and read header.
/// </summary>
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();
}
/// <summary>
/// Waits for an async message and read header.
/// </summary>
public async Task k0Async()
{
_readBuffer = new byte[8];
await ReadAsync(_readBuffer).ConfigureAwait(false);
ParseHeader();
_readPosition = 4;
_readBuffer = new byte[ri() - 8];
await ReadAsync(_readBuffer).ConfigureAwait(false);
if (IsCompressed)
{
UnCompress();
}
else
{
_readPosition = 0;
}
ParseException();
}
/// <summary>
/// Sends an async message to the remote KDB+ process with a specified object parameter.
/// </summary>
/// <param name="x">The object parameter.</param>
public async Task ksAsync(object x)
{
await wAsync(0, x).ConfigureAwait(false);
}
/// <summary>
/// Sends an async message to the remote KDB+ process with a specified expression.
/// </summary>
/// <param name="s">The expression to send.</param>
/// <exception cref="ArgumentNullException"><paramref name="s"/> parameter was null.</exception>
public async Task ksAsync(string s)
{
if (s == null)
{
throw new ArgumentNullException(nameof(s));
}
await wAsync(0, s.ToCharArray()).ConfigureAwait(false);
}
/// <summary>
/// Sends an async message to the remote KDB+ process with a specified expression
/// and object parameter.
/// </summary>
/// <param name="s">The expression to send.</param>
/// <param name="x">The object parameter to send.</param>
/// <exception cref="ArgumentNullException"><paramref name="s"/> parameter was null.</exception>
public async Task ksAsync(string s, object x)
{
if (s == null)
{
throw new ArgumentNullException(nameof(s));
}
object[] array = new object[]
{
s.ToCharArray(),
x
};
await wAsync(0, array).ConfigureAwait(false);
}
/// <summary>
/// Sends an async message to the remote KDB+ process with a specified expression
/// and object parameters.
/// </summary>
/// <param name="s">The expression to send.</param>
/// <param name="x">The first object parameter to send.</param>
/// <param name="y">The second object parameter to send.</param>
/// <exception cref="ArgumentNullException"><paramref name="s"/> parameter was null.</exception>
public async Task ksAsync(string s, object x, object y)
{
if (s == null)
{
throw new ArgumentNullException(nameof(s));
}
object[] array = new object[]
{
s.ToCharArray(),
x,
y
};
await wAsync(0, array).ConfigureAwait(false);
}
/// <summary>
/// Sends an async message to the remote KDB+ process with a specified object parameter.
/// </summary>
/// <param name="x">The object parameter.</param>
public void ks(object x)
{
w(0, x);
}
/// <summary>
/// Sends an async message to the remote KDB+ process with a specified expression.
/// </summary>
/// <param name="s">The expression to send.</param>
/// <exception cref="ArgumentNullException"><paramref name="s"/> parameter was null.</exception>
public void ks(string s)
{
if (s == null)
{
throw new ArgumentNullException(nameof(s));
}
w(0, s.ToCharArray());
}
/// <summary>
/// Sends an async message to the remote KDB+ process with a specified expression
/// and object parameter.
/// </summary>
/// <param name="s">The expression to send.</param>
/// <param name="x">The object parameter to send.</param>
/// <exception cref="ArgumentNullException"><paramref name="s"/> parameter was null.</exception>
public void ks(string s, object x)
{
if (s == null)
{
throw new ArgumentNullException(nameof(s));
}
object[] array = new object[]
{
s.ToCharArray(),
x
};
w(0, array);
}
/// <summary>
/// Sends an async message to the remote KDB+ process with a specified expression
/// and object parameters.
/// </summary>
/// <param name="s">The expression to send.</param>
/// <param name="x">The first object parameter to send.</param>
/// <param name="y">The second object parameter to send.</param>
/// <exception cref="ArgumentNullException"><paramref name="s"/> parameter was null.</exception>
public void ks(string s, object x, object y)
{
if (s == null)
{
throw new ArgumentNullException(nameof(s));
}
object[] array = new object[]
{
s.ToCharArray(),
x,
y
};
w(0, array);
}
/// <summary>
/// Sends an async message to the remote KDB+ process with a specified object parameter.
/// </summary>
/// <param name="x">The object parameter.</param>
public void kn(object x)
{
w(1, x);
}
/// <summary>
/// Sends an async message to the remote KDB+ process with a specified object parameter.
/// </summary>
/// <param name="x">The object parameter.</param>
public async Task knAsync(object x)
{
await wAsync(1, x).ConfigureAwait(false);
}
/// <summary>
/// Sends a response message to the remote KDB+ process.
/// </summary>
/// <param name="x">The response message to send.</param>
/// <remarks>
/// This should be called only during processing of an incoming sync message.
/// </remarks>
public void kr(object x)
{
w(2, x);
}
/// <summary>
/// Sends a response message to the remote KDB+ process.
/// </summary>
/// <param name="x">The response message to send.</param>
/// <remarks>
/// This should be called only during processing of an incoming sync message.
/// </remarks>
public async Task krAsync(object x)
{
await wAsync(2, x).ConfigureAwait(false);
}
/// <summary>
/// Serialises a specified object as a byte-array
/// </summary>
/// <param name="messageType">The type of object to be serialised.</param>
/// <param name="x">The object to be serialised.</param>
/// <returns>
/// A byte-array containing the serialised object data.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="x"/> parameter was null.</exception>
public byte[] Serialize(int messageType, object x)
{
return Serialize(messageType, x, IsZipEnabled);
}
/// <summary>
/// Serialises a specified object as a byte-array
/// </summary>
/// <param name="messageType">The type of object to be serialised.</param>
/// <param name="x">The object to be serialised.</param>
/// <param name="zip">A boolean flag indicating whether or not zip compression is enabled.</param>
/// <returns>
/// A byte-array containing the serialised object data.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="x"/> parameter was null.</exception>
public byte[] Serialize(int messageType, object x, bool zip)
{
if (x == null)
{
throw new ArgumentNullException(nameof(x),
$"Unable to serialize data. {nameof(x)} parameter cannot be null");
}
try
{
int length = nx(x) + 8;
_writeBuffer = new byte[length];
_writeBuffer[0] = 1;
_writeBuffer[1] = (byte)messageType;
_writePosition = 4;
w(length);
w(x);
if (zip &&
_writePosition > 2000 &&
!_isLoopback)
{
Compress();
}
return _writeBuffer;
}
catch (Exception ex)
{
throw new KException("Error occurred while trying to serialize object.", ex);
}
}
/// <summary>
/// Deserialises a specified byte-array to an object.
/// </summary>
/// <param name="buffer">The byte-array to be deserialised.</param>
/// <returns>
/// The deserialised object.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="buffer"/> parameter was null.</exception>
/// <exception cref="KException">Error occurred during de-serialisation.</exception>
public object Deserialize(byte[] buffer)
{
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer),
$"Unable to deserialize data. {nameof(buffer)} parameter cannot be null");
}
_readBuffer = buffer;
ParseHeader();
if (IsCompressed)
{
UnCompress(8);
}
else
{
_readPosition = 8;
}
if (_readBuffer[_readPosition] == 128)
{
++_readPosition;
throw new KException(rs());
}