forked from rickparrish/RMLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTcpConnection.cs
More file actions
1047 lines (943 loc) · 35.8 KB
/
TcpConnection.cs
File metadata and controls
1047 lines (943 loc) · 35.8 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
/*
RMLib: Nonvisual support classes used by multiple R&M Software programs
Copyright (C) Rick Parrish, R&M Software
This file is part of RMLib.
RMLib is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
RMLib is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with RMLib. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
// If linux needs to be pinvoked, here's a sample:
//[DllImport("libc")]
//private extern static int send(IntPtr sock, byte[] buf, int count, SocketFlags flags);
namespace RandM.RMLib
{
public class TcpConnection : IDisposable
{
// Events
public event EventHandler<StringEventArgs> ReadEvent = null;
// Read buffer size
protected const int READ_BUFFER_SIZE = 64 * 1024; // 64k input buffer
// Protected variables
protected string _LocalHost = "";
protected string _LocalIP = "";
protected Int32 _LocalPort = 0;
protected Queue<byte> _OutputBuffer = new Queue<byte>();
protected string _RemoteHost = "";
protected string _RemoteIP = "";
protected Int32 _RemotePort = 0;
protected Socket _Socket = null;
protected Stream _Stream = null;
// Private variables
private IntPtr _DuplicateHandle = IntPtr.Zero;
private bool _Disposed = false;
private Queue<byte> _InputBuffer = new Queue<byte>();
private byte _LastByte = 0;
// Public properties
public string LineEnding { get; set; }
public bool ReadTimedOut { get; private set; }
public bool StripLF { get; set; }
public bool StripNull { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public TcpConnection()
{
// Reset the socket
InitSocket();
}
~TcpConnection()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if (!_Disposed)
{
// If disposing equals true, dispose all managed
// and unmanaged resources.
if (disposing)
{
// Dispose managed resources.
Close();
}
// Call the appropriate methods to clean up
// unmanaged resources here.
// If disposing is false,
// only the following code is executed.
// Note disposing has been done.
_Disposed = true;
}
}
public Socket Accept()
{
try
{
return _Socket.Accept();
}
catch (SocketException sex)
{
RMLog.DebugException(sex, "SocketException in TcpConnection::Accept(). ErrorCode=" + sex.ErrorCode.ToString());
return null;
}
catch (Exception ex)
{
RMLog.DebugException(ex, "Exception in TcpConnection::Accept()");
return null;
}
}
public TcpConnection AcceptTCP()
{
try
{
Socket NewSocket = _Socket.Accept();
TcpConnection NewConnection = new TcpConnection();
NewConnection.Open(NewSocket);
return NewConnection;
}
catch (SocketException sex)
{
RMLog.DebugException(sex, "SocketException in TcpConnection::AcceptTCP(). ErrorCode=" + sex.ErrorCode.ToString());
return null;
}
catch (Exception ex)
{
RMLog.DebugException(ex, "Exception in TcpConnection::AcceptTCP()");
return null;
}
}
protected void AddToInputQueue(byte data)
{
var ReadHandler = ReadEvent;
if (ReadHandler != null)
{
if ((data >= 33) && (data <= 126))
{
ReadHandler(this, new StringEventArgs("Char " + ((char)data).ToString() + " (0x" + data.ToString("X2") + ")"));
}
else
{
ReadHandler(this, new StringEventArgs("Byte " + data.ToString() + " (0x" + data.ToString("X2") + ")"));
}
}
if (StripLF && (_LastByte == 0x0D) && (data == 0x0A))
{
// Ignore LF following CR
}
else if (StripNull && (_LastByte == 0x0D) && (data == 0x00))
{
// Ignore NULL following CR
}
else
{
_InputBuffer.Enqueue(data);
}
_LastByte = data;
}
public bool CanAccept()
{
return CanAccept(0);
}
public bool CanAccept(int milliseconds)
{
try
{
return (_Socket == null) ? false : _Socket.Poll(milliseconds * 1000, SelectMode.SelectRead);
}
catch (SocketException sex)
{
RMLog.DebugException(sex, "SocketException in TcpConnection::CanAccept(). ErrorCode=" + sex.ErrorCode.ToString());
return false;
}
catch (Exception ex)
{
RMLog.DebugException(ex, "Exception in TcpConnection::CanAccept()");
return false;
}
}
public bool CanRead()
{
return CanRead(1);
}
public bool CanRead(int milliseconds)
{
// Check if buffer is empty
if (_InputBuffer.Count == 0) ReceiveData(milliseconds);
return (_InputBuffer.Count > 0);
}
public void Close()
{
if (Connected)
{
try
{
if (ShutdownOnClose) _Socket.Shutdown(SocketShutdown.Both);
}
catch (SocketException sex)
{
switch (sex.ErrorCode)
{
case 10057: break; // The socket is not connected
default: RMLog.DebugException(sex, "SocketException in TcpConnection::Close(). ErrorCode=" + sex.ErrorCode.ToString()); break;
}
}
catch (Exception ex)
{
RMLog.DebugException(ex, "Exception in TcpConnection::Close()");
}
if (Connected)
{
_Stream.Close();
_Socket.Close();
}
}
InitSocket();
}
public bool Connect(string hostName, Int32 port)
{
Close();
try
{
// Get the ip addresses for the give hostname, and then convert from ipv4 to ipv6 if necessary
var HostAddresses = new List<IPAddress>();
if (!Socket.OSSupportsIPv6)
{
HostAddresses.AddRange(Dns.GetHostAddresses(hostName));
_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
else
{
foreach (var IPA in Dns.GetHostAddresses(hostName))
{
if (IPA.AddressFamily == AddressFamily.InterNetwork)
{
HostAddresses.Add(IPAddress.Parse("::ffff:" + IPA.ToString()));
}
else
{
HostAddresses.Add(IPA);
}
}
_Socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
// Only do the Mono check here because the XP check is done above
if (!ProcessUtils.IsRunningOnMono) // From: https://github.com/statianzo/Fleck/blob/master/src/Fleck/WebSocketServer.cs
{
#if __MonoCS__
#else
// Windows default to IPv6Only=true, so we call this to allow connections to both IPv4 and IPv6
// Mono will throw an exception, but that's OK because it defaults to allowing both IPv4 and IPv6
_Socket.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)27, false); // 27 = SocketOptionName.IPv6Only
#endif
}
}
_Socket.Blocking = true;
_Socket.Connect(HostAddresses.ToArray(), port);
if (_Socket.Connected)
{
_LocalIP = ((IPEndPoint)_Socket.LocalEndPoint).Address.ToString();
_LocalPort = ((IPEndPoint)_Socket.LocalEndPoint).Port;
_RemoteIP = ((IPEndPoint)_Socket.RemoteEndPoint).Address.ToString();
_RemotePort = ((IPEndPoint)_Socket.RemoteEndPoint).Port;
_Stream = new NetworkStream(_Socket);
if (_RemoteIP.ToLower().StartsWith("::ffff:"))
{
_RemoteIP = _RemoteIP.Substring(7); // Trim leading ::ffff:
}
else if (((IPEndPoint)_Socket.RemoteEndPoint).Address.AddressFamily == AddressFamily.InterNetworkV6)
{
_RemoteIP = "[" + _RemoteIP + "]"; // Wrap IPv6 in []
}
}
return _Socket.Connected;
}
catch (SocketException sex)
{
switch (sex.ErrorCode)
{
case 10060: break; // Connection timed out
case 10061: break; // Connection refused
case 10065: break; // No route to host
default: RMLog.DebugException(sex, "SocketException in TcpConnection::Connect(). ErrorCode=" + sex.ErrorCode.ToString()); break;
}
return false;
}
catch (Exception ex)
{
RMLog.DebugException(ex, "Exception in TcpConnection::Connect()");
return false;
}
}
public bool Connected
{
get { return ((_Socket != null) && (_Socket.Connected)); }
}
public static IPAddress GetAddrByName(string hostName)
{
try
{
IPAddress[] Addresses = Dns.GetHostAddresses(hostName);
return (Addresses.Length > 0) ? Addresses[0] : IPAddress.None;
}
catch (SocketException sex)
{
RMLog.DebugException(sex, "SocketException in TcpConnection::GetAddrByName(). ErrorCode=" + sex.ErrorCode.ToString());
return IPAddress.None;
}
catch (Exception ex)
{
RMLog.DebugException(ex, "Exception in TcpConnection::GetAddrByName()");
return IPAddress.None;
}
}
public static string GetHostByIP(string ipAddress)
{
try
{
return Dns.GetHostEntry(ipAddress).HostName.ToString();
}
catch (SocketException sex)
{
RMLog.DebugException(sex, "SocketException in TcpConnection::GetHostByIP(). ErrorCode=" + sex.ErrorCode.ToString());
return "";
}
catch (Exception ex)
{
RMLog.DebugException(ex, "Exception in TcpConnection::GetHostByIP()");
return "";
}
}
public static string GetIPByName(string hostName)
{
try
{
return Dns.GetHostEntry(hostName).AddressList[0].ToString();
}
catch (SocketException sex)
{
RMLog.DebugException(sex, "SocketException in TcpConnection::GetIPByName(). ErrorCode=" + sex.ErrorCode.ToString());
return "";
}
catch (Exception ex)
{
RMLog.DebugException(ex, "Exception in TcpConnection::GetIPByName()");
return "";
}
}
public string GetLocalHost()
{
if (string.IsNullOrWhiteSpace(_LocalHost)) { _LocalHost = GetHostByIP(_LocalIP); }
return _LocalHost;
}
public string GetLocalIP()
{
return _LocalIP;
}
public static string GetLocalIPs()
{
try
{
StringBuilder Result = new StringBuilder();
IPAddress[] Addresses = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress Address in Addresses)
{
if (Result.Length > 0) { Result.Append(","); }
Result.Append(Address.ToString());
}
return Result.ToString();
}
catch (SocketException sex)
{
RMLog.DebugException(sex, "SocketException in TcpConnection::GetLocalIPs(). ErrorCode=" + sex.ErrorCode.ToString());
return "";
}
catch (Exception ex)
{
RMLog.DebugException(ex, "Exception in TcpConnection::GetLocalIPs()");
return "";
}
}
public Int32 GetLocalPort()
{
return _LocalPort;
}
public string GetRemoteHost()
{
if (string.IsNullOrWhiteSpace(_RemoteHost)) { _RemoteHost = GetHostByIP(_RemoteIP); }
return _RemoteHost;
}
public string GetRemoteIP()
{
return _RemoteIP;
}
public Int32 GetRemotePort()
{
return _RemotePort;
}
public Socket GetSocket()
{
return _Socket;
}
public Stream GetStream()
{
return _Stream;
}
public IntPtr Handle
{
get
{
if (Environment.OSVersion.Platform == PlatformID.Win32Windows)
{
if (_DuplicateHandle == IntPtr.Zero)
{
NativeMethods.DuplicateHandle(Process.GetCurrentProcess().Handle, _Socket.Handle, Process.GetCurrentProcess().Handle, out _DuplicateHandle, 0, true, (uint)NativeMethods.DuplicateOptions.DUPLICATE_SAME_ACCESS);
}
return _DuplicateHandle;
}
else
{
return _Socket.Handle;
}
}
}
protected virtual void InitSocket()
{
_Socket = null;
_Stream = null;
_InputBuffer.Clear();
_OutputBuffer.Clear();
LineEnding = "\r\n";
_LocalHost = "";
_LocalIP = "";
_LocalPort = 0;
ReadTimedOut = false;
_RemoteHost = "";
_RemoteIP = "";
_RemotePort = 0;
ShutdownOnClose = true;
StripLF = false;
}
public bool Listen(string ipAddress, Int32 port)
{
Close();
try
{
IPAddress IPA = ParseIPAddress(ipAddress);
_Socket = new Socket(IPA.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
if (!ProcessUtils.IsRunningOnMono && Socket.OSSupportsIPv6) // From: https://github.com/statianzo/Fleck/blob/master/src/Fleck/WebSocketServer.cs
{
#if __MonoCS__
#else
// Windows default to IPv6Only=true, so we call this to allow connections to both IPv4 and IPv6
// Mono will throw an exception, but that's OK because it defaults to allowing both IPv4 and IPv6
_Socket.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)27, false); // 27 = SocketOptionName.IPv6Only
#endif
}
_Socket.Blocking = true;
_Socket.Bind(new IPEndPoint(IPA, port));
_Socket.Listen(5);
return true;
}
catch (SocketException sex)
{
RMLog.DebugException(sex, "SocketException in TcpConnection::Listen(). ErrorCode=" + sex.ErrorCode.ToString());
return false;
}
catch (Exception ex)
{
RMLog.DebugException(ex, "Exception in TcpConnection::Listen()");
return false;
}
}
protected virtual void NegotiateInbound(byte[] data)
{
NegotiateInbound(data, data.Length);
}
protected virtual void NegotiateInbound(byte[] data, int numberOfBytes)
{
for (int i = 0; i < numberOfBytes; i++)
{
AddToInputQueue(data[i]);
}
}
protected virtual void NegotiateOutbound(byte[] data)
{
NegotiateOutbound(data, data.Length);
}
protected virtual void NegotiateOutbound(byte[] data, int numberOfBytes)
{
for (int i = 0; i < numberOfBytes; i++)
{
_OutputBuffer.Enqueue(data[i]);
}
}
public bool Open(int ASocketHandle)
{
if (OSUtils.IsWindows)
{
try
{
NativeMethods.WSAData WSA = new NativeMethods.WSAData();
SocketError Result = NativeMethods.WSAStartup((short)0x0202, out WSA);
if (Result != SocketError.Success) throw new SocketException(NativeMethods.WSAGetLastError());
SocketPermission SP = new SocketPermission(System.Security.Permissions.PermissionState.Unrestricted);
SP.Demand();
SocketInformation SI = new SocketInformation() {
Options = SocketInformationOptions.Connected,
ProtocolInformation = new byte[Marshal.SizeOf(typeof(NativeMethods.WSAPROTOCOL_INFO))],
};
Result = SocketError.Success;
unsafe
{
fixed (byte* pinnedBuffer = SI.ProtocolInformation)
{
Result = NativeMethods.WSADuplicateSocket(new IntPtr(ASocketHandle), (uint)Process.GetCurrentProcess().Id, pinnedBuffer);
}
}
if (Result != SocketError.Success) throw new SocketException(NativeMethods.WSAGetLastError());
return Open(new Socket(SI));
}
catch (SocketException sex)
{
RMLog.DebugException(sex, "SocketException in TcpConnection::Open(). ErrorCode=" + sex.ErrorCode.ToString());
return false;
}
catch (Exception ex)
{
RMLog.DebugException(ex, "Exception in TcpConnection::Open()");
return false;
}
}
else
{
RMLog.Debug("MONO cannot open an existing socket handle");
return false;
//try
//{
// SocketInformation SI = new SocketInformation();
// SI.Options = SocketInformationOptions.Connected;
// SI.ProtocolInformation = new byte[24];
// // From Mono's Socket.cs DuplicateAndClose() SI.ProtocolInformation = Mono.DataConverter.Pack("iiiil", (int)address_family, (int)socket_type, (int)protocol_type, isbound ? 1 : 0, (long)socket);
// byte[] B1 = BitConverter.GetBytes((int)AddressFamily.InterNetwork);
// byte[] B2 = BitConverter.GetBytes((int)SocketType.Stream);
// byte[] B3 = BitConverter.GetBytes((int)ProtocolType.Tcp);
// byte[] B4 = BitConverter.GetBytes((int)1);
// byte[] B5 = BitConverter.GetBytes((long)ASocketHandle);
// Array.Copy(B1, 0, SI.ProtocolInformation, 0, B1.Length);
// Array.Copy(B2, 0, SI.ProtocolInformation, 4, B2.Length);
// Array.Copy(B3, 0, SI.ProtocolInformation, 8, B3.Length);
// Array.Copy(B4, 0, SI.ProtocolInformation, 12, B4.Length);
// Array.Copy(B5, 0, SI.ProtocolInformation, 16, B5.Length);
// return Open(new Socket(SI));
//}
//catch (SocketException sex)
//{
// RMLog.DebugException(sex, "SocketException in TcpConnection::Open(). ErrorCode=" + sex.ErrorCode.ToString());
// return false;
//}
//catch (Exception ex)
//{
// RMLog.DebugException(ex, "Exception in TcpConnection::Open()");
// return false;
//}
}
}
public bool Open(byte[] socketInformationBytes)
{
try
{
SocketInformation SI = new SocketInformation() {
ProtocolInformation = socketInformationBytes,
Options = SocketInformationOptions.Connected,
};
_Socket = new Socket(SI) {
Blocking = true,
};
if (_Socket.Connected) _Stream = new NetworkStream(_Socket);
return _Socket.Connected;
}
catch (SocketException sex)
{
RMLog.DebugException(sex, "SocketException in TcpConnection::Open(). ErrorCode=" + sex.ErrorCode.ToString());
return false;
}
catch (Exception ex)
{
RMLog.DebugException(ex, "Exception in TcpConnection::Open()");
return false;
}
}
public virtual bool Open(Socket socket)
{
Close();
try
{
_Socket = socket;
_Socket.Blocking = true;
if (_Socket.Connected)
{
_LocalIP = ((IPEndPoint)_Socket.LocalEndPoint).Address.ToString();
_LocalPort = ((IPEndPoint)_Socket.LocalEndPoint).Port;
_RemoteIP = ((IPEndPoint)_Socket.RemoteEndPoint).Address.ToString();
_RemotePort = ((IPEndPoint)_Socket.RemoteEndPoint).Port;
_Stream = new NetworkStream(_Socket);
if (_RemoteIP.ToLower().StartsWith("::ffff:"))
{
_RemoteIP = _RemoteIP.Substring(7); // Trim leading ::ffff:
}
else if (((IPEndPoint)_Socket.RemoteEndPoint).Address.AddressFamily == AddressFamily.InterNetworkV6)
{
_RemoteIP = "[" + _RemoteIP + "]"; // Wrap IPv6 in []
}
}
return _Socket.Connected;
}
catch (SocketException sex)
{
RMLog.DebugException(sex, "SocketException in TcpConnection::Open(). ErrorCode=" + sex.ErrorCode.ToString());
return false;
}
catch (Exception ex)
{
RMLog.DebugException(ex, "Exception in TcpConnection::Open()");
return false;
}
}
// Modified version of https://github.com/statianzo/Fleck/blob/master/src/Fleck/WebSocketServer.cs
private IPAddress ParseIPAddress(string ipAddress)
{
string[] BindToAll = new string[]
{
"",
"0.0.0.0",
"::",
"[::]",
"0000:0000:0000:0000:0000:0000:0000:0000",
"[0000:0000:0000:0000:0000:0000:0000:0000]"
};
if (BindToAll.Contains(ipAddress))
{
// We want to bind to all addresses, so return IPAddress.Any or IPv6Any based on the OS we're running on
return Socket.OSSupportsIPv6 ? IPAddress.IPv6Any : IPAddress.Any;
}
else
{
try
{
return IPAddress.Parse(ipAddress);
}
catch (Exception ex)
{
throw new FormatException("Failed to parse the IP address. Please make sure you specify a valid IP address. Use 0.0.0.0 or [::] to listen on all interfaces.", ex);
}
}
}
public byte[] PeekBytes()
{
return _InputBuffer.ToArray();
}
public char? PeekChar()
{
if (_InputBuffer.Count == 0)
{
return null;
}
else
{
return (char)_InputBuffer.Peek();
}
}
public string PeekString()
{
return RMEncoding.Ansi.GetString(_InputBuffer.ToArray());
}
public byte[] ReadBytes()
{
return ReadBytes(_InputBuffer.Count);
}
public byte[] ReadBytes(int bytesToRead)
{
if (bytesToRead > _InputBuffer.Count) bytesToRead = _InputBuffer.Count;
byte[] Result = new byte[bytesToRead];
for (int i = 0; i < bytesToRead; i++)
{
Result[i] = _InputBuffer.Dequeue();
}
return Result;
}
public char? ReadChar(Int32 timeOut)
{
DateTime StartTime = DateTime.Now;
ReadTimedOut = false;
while (true)
{
if (!Connected) return null;
if (CanRead())
{
return (char)_InputBuffer.Dequeue();
}
if ((timeOut != 0) && (DateTime.Now.Subtract(StartTime).TotalMilliseconds > timeOut))
{
ReadTimedOut = true;
return null;
}
}
}
public string ReadLn(int timeOut)
{
return ReadLn(new string[] { LineEnding }, true, '\0', timeOut);
}
public string ReadLn(string terminator, int timeOut)
{
return ReadLn(new string[] { terminator }, false, '\0', timeOut);
}
public string ReadLn(bool echo, int timeOut)
{
return ReadLn(new string[] { LineEnding }, echo, '\0', timeOut);
}
public string ReadLn(char passwordCharacter, int timeOut)
{
return ReadLn(new string[] { LineEnding }, true, passwordCharacter, timeOut);
}
public string ReadLn(string terminator, bool echo, char passwordCharacter, int timeOut)
{
return ReadLn(new string[] { terminator }, echo, passwordCharacter, timeOut);
}
public string ReadLn(string[] terminators, bool echo, char passwordCharacter, int timeOut)
{
DateTime StartTime = DateTime.Now;
char? Ch = null;
bool NeedTerminator = true;
string Result = "";
string TerminatorCharacters = string.Join("", terminators);
while (NeedTerminator)
{
if (!Connected) break;
Ch = ReadChar((timeOut == 0) ? 0 : timeOut - (int)DateTime.Now.Subtract(StartTime).TotalMilliseconds);
if (Ch != null)
{
if (echo)
{
if ((Ch == '\x08') || (Ch == '\x7F'))
{
// Delete, check if we have a character to remove
if (Result.Length > 0)
{
// Determine if character we're trimming is printable, and if so, backspace it off the screen
Ch = Result[Result.Length - 1];
if (Ch >= ' ') Write("\x08 \x08");
// Remove the last character from the input string
Result = Result.Substring(0, Result.Length - 1);
}
}
else
{
// Not a delete, so add it to the string and echo it if its printable
if ((Ch >= ' '))
{
Result += Ch;
Write((passwordCharacter == '\0') ? Ch.ToString() : passwordCharacter.ToString());
}
// Also add it to the string (but no echo) if its part of a non-printable terminator
else if (TerminatorCharacters.Contains(Ch.ToString()))
{
Result += Ch;
}
}
}
else
{
Result += Ch;
}
}
else if (ReadTimedOut)
{
// Return what we have so far
return Result;
}
foreach (string Terminator in terminators)
{
if (Result.EndsWith(Terminator))
{
NeedTerminator = false;
break;
}
}
}
// Find the terminator and remove it
foreach (string Terminator in terminators)
{
if (Result.EndsWith(Terminator))
{
Result = Result.Substring(0, Result.Length - Terminator.Length);
break;
}
}
if (echo) { WriteLn(); }
return Result;
}
/// <summary>
/// Reads the entire buffer into a string variable
/// </summary>
/// <returns></returns>
public string ReadString()
{
string Result = RMEncoding.Ansi.GetString(_InputBuffer.ToArray());
_InputBuffer.Clear();
return Result;
}
public void ReceiveData(int milliseconds)
{
try
{
if ((Connected) && (_Socket.Poll(milliseconds * 1000, SelectMode.SelectRead)))
{
byte[] Buffer = new byte[READ_BUFFER_SIZE];
int BytesRead = _Stream.Read(Buffer, 0, READ_BUFFER_SIZE);
if (BytesRead == 0)
{
// No bytes read means we have a disconnect
_Stream.Close();
_Socket.Close();
}
else
{
// If we have bytes, add them to the buffer
NegotiateInbound(Buffer, BytesRead);
}
}
}
catch (IOException ioex)
{
if (ioex.Message == "Read failure")
{
// Apparently we have a disconnect
}
else
{
RMLog.DebugException(ioex, "IOException in TcpConnection::ReceiveData()");
}
_Stream.Close();
_Socket.Close();
}
catch (SocketException sex)
{
if (sex.Message == "Connection reset by peer")
{
// Apparently we have a disconnect
}
else
{
RMLog.DebugException(sex, "SocketException in TcpConnection::ReceiveData(). ErrorCode=" + sex.ErrorCode.ToString());
}
_Stream.Close();
_Socket.Close();
}
catch (Exception ex)
{
RMLog.DebugException(ex, "Exception in TcpConnection::ReceiveData()");
_Stream.Close();
_Socket.Close();
}
}
public void SetBlocking(bool block)
{
if (Connected)
{
_Socket.Blocking = block;
}
}
public bool ShutdownOnClose { get; set; }
public virtual void Write(string text)
{
WriteBytes(RMEncoding.Ansi.GetBytes(text));
}
public void WriteBytes(byte[] data)
{
WriteBytes(data, data.Length);
}
public void WriteBytes(byte[] data, int numberOfBytes)
{
NegotiateOutbound(data, numberOfBytes);
WriteRaw(_OutputBuffer.ToArray());
_OutputBuffer.Clear();
}
public void WriteLn()
{
Write("\r\n");
}
public void WriteLn(string text)
{
Write(text + "\r\n");
}
public void WriteRaw(byte[] data)
{