wtt
2024-09-20 d9b200d424889c069953ec084ef9d4dd6c4ff66b
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
#region MIT License
/**
 * Ext.cs
 *  IsPredefinedScheme and MaybeUri methods derived from System.Uri.cs
 *  GetStatusDescription method derived from System.Net.HttpListenerResponse.cs
 *
 * The MIT License
 *
 * Copyright (c) 2010-2012 sta.blockhead
 *
 * System.Uri.cs
 * (C) 2001 Garrett Rooney
 * (C) 2003 Ian MacLean
 * (C) 2003 Ben Maurer
 * Copyright (C) 2003, 2005, 2009 Novell, Inc. (http://www.novell.com)
 * Copyright (c) 2009 Stephane Delcroix
 *
 * System.Net.HttpListenerResponse.cs
 * Copyright (C) 2003, 2005, 2009 Novell, Inc. (http://www.novell.com)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#endregion
 
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Net.Sockets;
using System.Text;
using WebSocketSharp.Net;
using WebSocketSharp.Net.Sockets;
 
namespace WebSocketSharp
{
    public delegate void Action();
    public delegate void Action<T1, T2>(T1 t1, T2 t2);
    public delegate void Action<T1, T2, T3>(T1 t1, T2 t2, T3 t3);
    public delegate void Action<T1, T2, T3, T4>(T1 t1, T2 t2, T3 t3, T4 t4);
    public delegate TResult Func<T1, TResult>(T1 t1);
    /// <summary>Provides a set of static methods for the websocket-sharp.
    /// </summary>
    public static class Ext
    {
        #region Private Methods
 
        private static void times(ulong n, Action act)
        {
            for (ulong i = 0; i < n; i++)
                act();
        }
 
        private static void times(ulong n, Action<ulong> act)
        {
            for (ulong i = 0; i < n; i++)
                act(i);
        }
 
        private static byte[] Reverse(byte[] array)
        {
            Array.Reverse(array);
            return array;
        }
 
        #endregion
 
        #region Public Methods
 
        /// <summary>
        /// Accept a WebSocket connection by the <see cref="TcpListener"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="WebSocketSharp.Net.Sockets.TcpListenerWebSocketContext"/> that contains a WebSocket connection.
        /// </returns>
        /// <param name="client">
        /// A <see cref="TcpClient"/> that contains a TCP connection to accept a WebSocket connection from.
        /// </param>
        /// <param name="secure">
        /// A <see cref="bool"/> that indicates a secure connection or not. (<c>true</c> indicates a secure connection.)
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Is thrown when the <paramref name="client"/> parameter passed to a method is invalid because it is <see langword="null"/>.
        /// </exception>
        public static TcpListenerWebSocketContext AcceptWebSocket(TcpClient client, bool secure)
        {
            if (client == null)
                throw new ArgumentNullException("client");
 
            return new TcpListenerWebSocketContext(client, secure);
        }
 
        /// <summary>
        /// Emit the specified <see cref="EventHandler"/> delegate if is not <see langword="null"/>.
        /// </summary>
        /// <param name="eventHandler">
        /// An <see cref="EventHandler"/> to emit.
        /// </param>
        /// <param name="sender">
        /// An <see cref="object"/> that emits the <paramref name="eventHandler"/>.
        /// </param>
        /// <param name="e">
        /// An <see cref="EventArgs"/> that contains no event data.
        /// </param>
        public static void Emit(
           EventHandler eventHandler, object sender, EventArgs e)
        {
            if (eventHandler != null)
                eventHandler(sender, e);
        }
 
        /// <summary>
        /// Emit the specified <see cref="EventHandler&lt;TEventArgs&gt;"/> delegate if is not <see langword="null"/>.
        /// </summary>
        /// <param name="eventHandler">
        /// An <see cref="EventHandler&lt;TEventArgs&gt;"/> to emit.
        /// </param>
        /// <param name="sender">
        /// An <see cref="object"/> that emits the <paramref name="eventHandler"/>.
        /// </param>
        /// <param name="e">
        /// An <see cref="EventArgs"/> that contains the event data.
        /// </param>
        /// <typeparam name="TEventArgs">
        /// The type of the event data generated by the event.
        /// </typeparam>
        public static void Emit<TEventArgs>(
           EventHandler<TEventArgs> eventHandler, object sender, TEventArgs e)
          where TEventArgs : EventArgs
        {
            if (eventHandler != null)
                eventHandler(sender, e);
        }
 
        /// <summary>
        /// Determines whether the specified <see cref="int"/> equals the specified <see cref="char"/> as <see cref="byte"/>.
        /// And save this specified <see cref="int"/> as <see cref="byte"/> to the specified <see cref="List&lt;byte&gt;"/>.
        /// </summary>
        /// <returns>
        /// <c>true</c> if the <paramref name="value"/> parameter equals the <paramref name="c"/> parameter as <see cref="byte"/>; otherwise, <c>false</c>.
        /// </returns>
        /// <param name="value">
        /// An <see cref="int"/> to compare.
        /// </param>
        /// <param name="c">
        /// A <see cref="char"/> to compare.
        /// </param>
        /// <param name="dest">
        /// A <see cref="List&lt;byte&gt;"/> to save the <paramref name="value"/> as <see cref="byte"/>.
        /// </param>
        /// <exception cref="ArgumentOutOfRangeException">
        /// Is thrown when the <paramref name="value"/> parameter passed to a method is invalid because it is outside the allowable range of values as <see cref="byte"/>.
        /// </exception>
        public static bool EqualsAndSaveTo(int value, char c, List<byte> dest)
        {
            if (value < 0 || value > 255)
                throw new ArgumentOutOfRangeException("value");
 
            var b = (byte)value;
            dest.Add(b);
 
            return b == Convert.ToByte(c);
        }
 
        /// <summary>
        /// Determines whether the entry with the specified key exists in the specified <see cref="NameValueCollection"/>.
        /// </summary>
        /// <returns>
        /// <c>true</c> if the entry with the <paramref name="name"/> exists in the <paramref name="collection"/>; otherwise, <c>false</c>.
        /// </returns>
        /// <param name="collection">
        /// A <see cref="NameValueCollection"/> that contains the entries.
        /// </param>
        /// <param name="name">
        /// A <see cref="string"/> that contains the key of the entry to find.
        /// </param>
        public static bool Exists(NameValueCollection collection, string name)
        {
            return collection == null
                   ? false
                   : collection[name] != null;
        }
 
        /// <summary>
        /// Determines whether the entry with the specified both key and value exists in the specified <see cref="NameValueCollection"/>.
        /// </summary>
        /// <returns>
        /// <c>true</c> if the entry with the both <paramref name="name"/> and <paramref name="value"/> exists in the <paramref name="collection"/>; otherwise, <c>false</c>.
        /// </returns>
        /// <param name="collection">
        /// A <see cref="NameValueCollection"/> that contains the entries.
        /// </param>
        /// <param name="name">
        /// A <see cref="string"/> that contains the key of the entry to find.
        /// </param>
        /// <param name="value">
        /// A <see cref="string"/> that contains the value of the entry to find.
        /// </param>
        public static bool Exists(NameValueCollection collection, string name, string value)
        {
            if (collection == null)
                return false;
 
            var values = collection[name];
            if (values == null)
                return false;
 
            foreach (string v in values.Split(','))
                if (String.Compare(v.Trim(), value, true) == 0)
                    return true;
 
            return false;
        }
 
        /// <summary>
        /// Gets the absolute path from the specified <see cref="Uri"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="string"/> that contains the absolute path if got successfully; otherwise, <see langword="null"/>.
        /// </returns>
        /// <param name="uri">
        /// A <see cref="Uri"/> that contains the URI to get the absolute path from.
        /// </param>
        public static string GetAbsolutePath(Uri uri)
        {
            if (uri == null)
                return null;
 
            if (uri.IsAbsoluteUri)
                return uri.AbsolutePath;
 
            var uriString = uri.OriginalString;
            var i = uriString.IndexOf('/');
            if (i != 0)
                return null;
 
            i = uriString.IndexOfAny(new[] { '?', '#' });
            return i > 0
                   ? uriString.Substring(0, i)
                   : uriString;
        }
 
        /// <summary>
        /// Gets the description of the HTTP status code using the specified code.
        /// </summary>
        /// <returns>
        /// A <see cref="string"/> that contains the description of the <paramref name="code"/>.
        /// </returns>
        /// <param name="code">
        /// One of <see cref="WebSocketSharp.Net.HttpStatusCode"/> values that contains the HTTP status code.
        /// </param>
        public static string GetDescription(HttpStatusCode code)
        {
            return GetStatusDescription((int)code);
        }
 
        /// <summary>
        /// Gets the name from the specified <see cref="string"/> that contains a pair of name and value are separated by a separator string.
        /// </summary>
        /// <returns>
        /// A <see cref="string"/> that contains the name if any; otherwise, <c>null</c>.
        /// </returns>
        /// <param name="nameAndValue">
        /// A <see cref="string"/> that contains a pair of name and value are separated by a separator string.
        /// </param>
        /// <param name="separator">
        /// A <see cref="string"/> that contains a separator string.
        /// </param>
        public static string GetName(string nameAndValue, string separator)
        {
            if (IsNullOrEmpty(nameAndValue))
                return null;
 
            if (IsNullOrEmpty(separator))
                return null;
 
            var i = nameAndValue.IndexOf(separator);
            return i > 0
                   ? nameAndValue.Substring(0, i).Trim()
                   : null;
        }
 
        /// <summary>
        /// Gets the name and value from the specified <see cref="string"/> that contains a pair of name and value are separated by a separator string.
        /// </summary>
        /// <returns>
        /// A <see cref="KeyValuePair&lt;string, string&gt;"/> that contains the name and value if any.
        /// </returns>
        /// <param name="nameAndValue">
        /// A <see cref="string"/> that contains a pair of name and value are separated by a separator string.
        /// </param>
        /// <param name="separator">
        /// A <see cref="string"/> that contains a separator string.
        /// </param>
        public static KeyValuePair<string, string> GetNameAndValue(string nameAndValue, string separator)
        {
            var name = GetName(nameAndValue, separator);
            var value = GetValue(nameAndValue, separator);
            return name != null
                   ? new KeyValuePair<string, string>(name, value)
                   : new KeyValuePair<string, string>(null, null);
        }
 
        /// <summary>
        /// Gets the description of the HTTP status code using the specified code.
        /// </summary>
        /// <returns>
        /// A <see cref="string"/> that contains the description of the <paramref name="code"/>.
        /// </returns>
        /// <param name="code">
        /// An <see cref="int"/> that contains the HTTP status code.
        /// </param>
        public static string GetStatusDescription(int code)
        {
            switch (code)
            {
                case 100: return "Continue";
                case 101: return "Switching Protocols";
                case 102: return "Processing";
                case 200: return "OK";
                case 201: return "Created";
                case 202: return "Accepted";
                case 203: return "Non-Authoritative Information";
                case 204: return "No Content";
                case 205: return "Reset Content";
                case 206: return "Partial Content";
                case 207: return "Multi-Status";
                case 300: return "Multiple Choices";
                case 301: return "Moved Permanently";
                case 302: return "Found";
                case 303: return "See Other";
                case 304: return "Not Modified";
                case 305: return "Use Proxy";
                case 307: return "Temporary Redirect";
                case 400: return "Bad Request";
                case 401: return "Unauthorized";
                case 402: return "Payment Required";
                case 403: return "Forbidden";
                case 404: return "Not Found";
                case 405: return "Method Not Allowed";
                case 406: return "Not Acceptable";
                case 407: return "Proxy Authentication Required";
                case 408: return "Request Timeout";
                case 409: return "Conflict";
                case 410: return "Gone";
                case 411: return "Length Required";
                case 412: return "Precondition Failed";
                case 413: return "Request Entity Too Large";
                case 414: return "Request-Uri Too Long";
                case 415: return "Unsupported Media Type";
                case 416: return "Requested Range Not Satisfiable";
                case 417: return "Expectation Failed";
                case 422: return "Unprocessable Entity";
                case 423: return "Locked";
                case 424: return "Failed Dependency";
                case 500: return "Internal Server Error";
                case 501: return "Not Implemented";
                case 502: return "Bad Gateway";
                case 503: return "Service Unavailable";
                case 504: return "Gateway Timeout";
                case 505: return "Http Version Not Supported";
                case 507: return "Insufficient Storage";
            }
 
            return String.Empty;
        }
 
        /// <summary>
        /// Gets the value from the specified <see cref="string"/> that contains a pair of name and value are separated by a separator string.
        /// </summary>
        /// <returns>
        /// A <see cref="string"/> that contains the value if any; otherwise, <c>null</c>.
        /// </returns>
        /// <param name="nameAndValue">
        /// A <see cref="string"/> that contains a pair of name and value are separated by a separator string.
        /// </param>
        /// <param name="separator">
        /// A <see cref="string"/> that contains a separator string.
        /// </param>
        public static string GetValue(string nameAndValue, string separator)
        {
            if (IsNullOrEmpty(nameAndValue))
                return null;
 
            if (IsNullOrEmpty(separator))
                return null;
 
            var i = nameAndValue.IndexOf(separator);
            return i >= 0 && i < nameAndValue.Length - 1
                   ? nameAndValue.Substring(i + 1).Trim()
                   : null;
        }
 
        /// <summary>
        /// Determines whether the specified <see cref="string"/> is a <see cref="String.Empty"/>.
        /// </summary>
        /// <returns>
        /// <c>true</c> if the <paramref name="value"/> parameter is a <see cref="String.Empty"/>; otherwise, <c>false</c>.
        /// </returns>
        /// <param name="value">
        /// A <see cref="string"/> to test.
        /// </param>
        public static bool IsEmpty(string value)
        {
            return value == String.Empty ? true : false;
        }
 
        /// <summary>
        /// Determines whether the specified <see cref="WebSocketSharp.ByteOrder"/> is host (this computer architecture) byte order.
        /// </summary>
        /// <returns>
        /// <c>true</c> if the <paramref name="order"/> parameter is host byte order; otherwise, <c>false</c>.
        /// </returns>
        /// <param name="order">
        /// A <see cref="WebSocketSharp.ByteOrder"/> to test.
        /// </param>
        public static bool IsHostOrder(ByteOrder order)
        {
            // true : !(true ^ true)  or !(false ^ false)
            // false: !(true ^ false) or !(false ^ true)
            return !(BitConverter.IsLittleEndian ^ (order == ByteOrder.LITTLE));
        }
 
        /// <summary>
        /// Determines whether the specified object is <see langword="null"/>.
        /// </summary>
        /// <returns>
        /// <c>true</c> if the <paramref name="obj"/> parameter is <see langword="null"/>; otherwise, <c>false</c>.
        /// </returns>
        /// <param name="obj">
        /// A <see cref="class"/> to test.
        /// </param>
        /// <typeparam name="T">
        /// The type of the <paramref name="obj"/> parameter.
        /// </typeparam>
        public static bool IsNull<T>(T obj)
          where T : class
        {
            return obj == null ? true : false;
        }
 
        /// <summary>
        /// Determines whether the specified object is <see langword="null"/>.
        /// And invokes the specified <see cref="Action"/> delegate if the specified object is <see langword="null"/>.
        /// </summary>
        /// <returns>
        /// <c>true</c> if the <paramref name="obj"/> parameter is <see langword="null"/>; otherwise, <c>false</c>.
        /// </returns>
        /// <param name="obj">
        /// A <see cref="class"/> to test.
        /// </param>
        /// <param name="act">
        /// An <see cref="Action"/> delegate that contains the method(s) called if the <paramref name="obj"/> is <see langword="null"/>.
        /// </param>
        /// <typeparam name="T">
        /// The type of the <paramref name="obj"/> parameter.
        /// </typeparam>
        public static bool IsNullDo<T>(T obj, Action act)
          where T : class
        {
            if (obj == null)
            {
                act();
                return true;
            }
 
            return false;
        }
 
        /// <summary>
        /// Determines whether the specified <see cref="string"/> is <see langword="null"/> or <see cref="String.Empty"/>.
        /// </summary>
        /// <returns>
        /// <c>true</c> if the <paramref name="value"/> parameter is <see langword="null"/> or <see cref="String.Empty"/>; otherwise, <c>false</c>.
        /// </returns>
        /// <param name="value">
        /// A <see cref="string"/> to test.
        /// </param>
        public static bool IsNullOrEmpty(string value)
        {
            return String.IsNullOrEmpty(value);
        }
 
        /// <summary>
        /// Determines whether the specified <see cref="string"/> is predefined scheme.
        /// </summary>
        /// <returns>
        /// <c>true</c> if the <paramref name="scheme"/> parameter is the predefined scheme; otherwise, <c>false</c>.
        /// </returns>
        /// <param name="scheme">
        /// A <see cref="string"/> to test.
        /// </param>
        public static bool IsPredefinedScheme(string scheme)
        {
            if (scheme == null && scheme.Length < 2)
                return false;
 
            char c = scheme[0];
            if (c == 'h')
                return (scheme == "http" || scheme == "https");
 
            if (c == 'f')
                return (scheme == "file" || scheme == "ftp");
 
            if (c == 'w')
                return (scheme == "ws" || scheme == "wss");
 
            if (c == 'n')
            {
                c = scheme[1];
                if (c == 'e')
                    return (scheme == "news" || scheme == "net.pipe" || scheme == "net.tcp");
 
                if (scheme == "nntp")
                    return true;
 
                return false;
            }
 
            if ((c == 'g' && scheme == "gopher") || (c == 'm' && scheme == "mailto"))
                return true;
 
            return false;
        }
 
        /// <summary>
        /// Determines whether the specified <see cref="string"/> is valid absolute path.
        /// </summary>
        /// <returns>
        /// <c>true</c> if the <paramref name="absPath"/> parameter is valid absolute path; otherwise, <c>false</c>.
        /// </returns>
        /// <param name="absPath">
        /// A <see cref="string"/> to test.
        /// </param>
        /// <param name="message">
        /// A <see cref="string"/> that receives a message if the <paramref name="absPath"/> is invalid.
        /// </param>
        public static bool IsValidAbsolutePath(string absPath, out string message)
        {
            if (IsNullOrEmpty(absPath))
            {
                message = "Must not be null or empty.";
                return false;
            }
 
            var i = absPath.IndexOf('/');
            if (i != 0)
            {
                message = "Not absolute path: " + absPath;
                return false;
            }
 
            i = absPath.IndexOfAny(new[] { '?', '#' });
            if (i != -1)
            {
                message = "Must not contain either or both query and fragment components: " + absPath;
                return false;
            }
 
            message = String.Empty;
            return true;
        }
 
        /// <summary>
        /// Determines whether the specified <see cref="string"/> is a URI string.
        /// </summary>
        /// <returns>
        /// <c>true</c> if the <paramref name="uriString"/> parameter is maybe a URI string; otherwise, <c>false</c>.
        /// </returns>
        /// <param name="uriString">
        /// A <see cref="string"/> to test.
        /// </param>
        public static bool MaybeUri(string uriString)
        {
            if (IsNullOrEmpty(uriString))
                return false;
 
            int p = uriString.IndexOf(':');
            if (p == -1)
                return false;
 
            if (p >= 10)
                return false;
 
            return IsPredefinedScheme(uriString.Substring(0, p));
        }
 
        /// <summary>
        /// Determines whether two specified <see cref="string"/> objects don't have the same value.
        /// </summary>
        /// <returns>
        /// <c>true</c> if the value of <paramref name="expected"/> parameter isn't the same as the value of <paramref name="actual"/> parameter; otherwise, <c>false</c>.
        /// </returns>
        /// <param name="expected">
        /// The first <see cref="string"/> to compare.
        /// </param>
        /// <param name="actual">
        /// The second <see cref="string"/> to compare.
        /// </param>
        /// <param name="ignoreCase">
        /// A <see cref="bool"/> that indicates a case-sensitive or insensitive comparison. (<c>true</c> indicates a case-insensitive comparison.)
        /// </param>
        public static bool NotEqual(string expected, string actual, bool ignoreCase)
        {
            return String.Compare(expected, actual, ignoreCase) != 0
                   ? true
                   : false;
        }
 
        /// <summary>
        /// Reads a block of bytes from the specified stream and returns the read data in an array of <see cref="byte"/>.
        /// </summary>
        /// <returns>
        /// An array of <see cref="byte"/> that receives the read data.
        /// </returns>
        /// <param name="stream">
        /// A <see cref="Stream"/> that contains the data to read.
        /// </param>
        /// <param name="length">
        /// An <see cref="int"/> that contains the number of bytes to read.
        /// </param>
        public static byte[] ReadBytes(Stream stream, int length)
        {
            if (stream == null || length <= 0)
                return new byte[] { };
 
            var buffer = new byte[length];
            var readLen = stream.Read(buffer, 0, length);
            var temp = 0;
            while (readLen < length)
            {
                temp = stream.Read(buffer, readLen, length - readLen);
                if (temp < 1)
                    break;
                readLen += temp;
            }
 
            return readLen == length
                   ? buffer
                   : readLen > 0
                     ? SubArray(buffer, 0, readLen)
                     : new byte[] { };
        }
 
        /// <summary>
        /// Reads a block of bytes from the specified stream and returns the read data in an array of <see cref="byte"/>.
        /// </summary>
        /// <returns>
        /// An array of <see cref="byte"/> that receives the read data.
        /// </returns>
        /// <param name="stream">
        /// A <see cref="Stream"/> that contains the data to read.
        /// </param>
        /// <param name="length">
        /// A <see cref="long"/> that contains the number of bytes to read.
        /// </param>
        public static byte[] ReadBytes(Stream stream, long length)
        {
            return ReadBytes(stream, length, 1024);
        }
 
        /// <summary>
        /// Reads a block of bytes from the specified stream and returns the read data in an array of <see cref="byte"/>.
        /// </summary>
        /// <returns>
        /// An array of <see cref="byte"/> that receives the read data.
        /// </returns>
        /// <param name="stream">
        /// A <see cref="Stream"/> that contains the data to read.
        /// </param>
        /// <param name="length">
        /// A <see cref="long"/> that contains the number of bytes to read.
        /// </param>
        /// <param name="bufferLength">
        /// An <see cref="int"/> that contains the buffer size in bytes of each internal read.
        /// </param>
        public static byte[] ReadBytes(Stream stream, long length, int bufferLength)
        {
            if (stream == null || length <= 0)
                return new byte[] { };
 
            if (bufferLength <= 0)
                bufferLength = 1024;
 
            var count = length / bufferLength;
            var rem = length % bufferLength;
            var readData = new List<byte>();
            var readBuffer = new byte[bufferLength];
            long readLen = 0;
            var tmpLen = 0;
 
            Action<byte[]> read = (buffer) =>
            {
                tmpLen = stream.Read(buffer, 0, buffer.Length);
                if (tmpLen > 0)
                {
                    readLen += tmpLen;
                    readData.AddRange(SubArray(buffer, 0, tmpLen));
                }
            };
 
            Times(count, () =>
            {
                read(readBuffer);
            });
 
            if (rem > 0)
                read(new byte[rem]);
 
            return readLen > 0
                   ? readData.ToArray()
                   : new byte[] { };
        }
 
        /// <summary>
        /// Retrieves a sub-array from the specified <paramref name="array"/>. A sub-array starts at the specified element position.
        /// </summary>
        /// <returns>
        /// An array of T that receives a sub-array, or an empty array of T if any problems with the parameters.
        /// </returns>
        /// <param name="array">
        /// An array of T that contains the data to retrieve a sub-array.
        /// </param>
        /// <param name="startIndex">
        /// An <see cref="int"/> that contains the zero-based starting position of a sub-array in the <paramref name="array"/>.
        /// </param>
        /// <param name="length">
        /// An <see cref="int"/> that contains the number of elements to retrieve a sub-array.
        /// </param>
        /// <typeparam name="T">
        /// The type of elements in the <paramref name="array"/>.
        /// </typeparam>
        public static T[] SubArray<T>(T[] array, int startIndex, int length)
        {
            if (array == null || array.Length == 0)
                return new T[] { };
 
            if (startIndex < 0 || length <= 0)
                return new T[] { };
 
            if (startIndex + length > array.Length)
                return new T[] { };
 
            if (startIndex == 0 && array.Length == length)
                return array;
 
            T[] subArray = new T[length];
            Array.Copy(array, startIndex, subArray, 0, length);
 
            return subArray;
        }
 
        /// <summary>
        /// Executes the specified <see cref="Action"/> delegate <paramref name="n"/> times.
        /// </summary>
        /// <param name="n">
        /// An <see cref="int"/> that contains the number of times to execute.
        /// </param>
        /// <param name="act">
        /// An <see cref="Action"/> delegate that contains the method(s) to execute.
        /// </param>
        public static void Times(int n, Action act)
        {
            if (n > 0 && act != null)
                times((ulong)n, act);
        }
 
        /// <summary>
        /// Executes the specified <see cref="Action"/> delegate <paramref name="n"/> times.
        /// </summary>
        /// <param name="n">
        /// A <see cref="long"/> that contains the number of times to execute.
        /// </param>
        /// <param name="act">
        /// An <see cref="Action"/> delegate that contains the method(s) to execute.
        /// </param>
        public static void Times(long n, Action act)
        {
            if (n > 0 && act != null)
                times((ulong)n, act);
        }
 
        /// <summary>
        /// Executes the specified <see cref="Action"/> delegate <paramref name="n"/> times.
        /// </summary>
        /// <param name="n">
        /// A <see cref="uint"/> that contains the number of times to execute.
        /// </param>
        /// <param name="act">
        /// An <see cref="Action"/> delegate that contains the method(s) to execute.
        /// </param>
        public static void Times(uint n, Action act)
        {
            if (n > 0 && act != null)
                times((ulong)n, act);
        }
 
        /// <summary>
        /// Executes the specified <see cref="Action"/> delegate <paramref name="n"/> times.
        /// </summary>
        /// <param name="n">
        /// A <see cref="ulong"/> that contains the number of times to execute.
        /// </param>
        /// <param name="act">
        /// An <see cref="Action"/> delegate that contains the method(s) to execute.
        /// </param>
        public static void Times(ulong n, Action act)
        {
            if (n > 0 && act != null)
                times((ulong)n, act);
        }
 
        /// <summary>
        /// Executes the specified <see cref="Action&lt;ulong&gt;"/> delegate <paramref name="n"/> times.
        /// </summary>
        /// <param name="n">
        /// An <see cref="int"/> that contains the number of times to execute.
        /// </param>
        /// <param name="act">
        /// An <see cref="Action&lt;ulong&gt;"/> delegate that contains the method(s) to execute.
        /// A <see cref="ulong"/> parameter to pass to this method(s) contains the zero-based count of iteration.
        /// </param>
        public static void Times(int n, Action<ulong> act)
        {
            if (n > 0 && act != null)
                times((ulong)n, act);
        }
 
        /// <summary>
        /// Executes the specified <see cref="Action&lt;ulong&gt;"/> delegate <paramref name="n"/> times.
        /// </summary>
        /// <param name="n">
        /// A <see cref="long"/> that contains the number of times to execute.
        /// </param>
        /// <param name="act">
        /// An <see cref="Action&lt;ulong&gt;"/> delegate that contains the method(s) to execute.
        /// A <see cref="ulong"/> parameter to pass to this method(s) contains the zero-based count of iteration.
        /// </param>
        public static void Times(long n, Action<ulong> act)
        {
            if (n > 0 && act != null)
                times((ulong)n, act);
        }
 
        /// <summary>
        /// Executes the specified <see cref="Action&lt;ulong&gt;"/> delegate <paramref name="n"/> times.
        /// </summary>
        /// <param name="n">
        /// A <see cref="uint"/> that contains the number of times to execute.
        /// </param>
        /// <param name="act">
        /// An <see cref="Action&lt;ulong&gt;"/> delegate that contains the method(s) to execute.
        /// A <see cref="ulong"/> parameter to pass to this method(s) contains the zero-based count of iteration.
        /// </param>
        public static void Times(uint n, Action<ulong> act)
        {
            if (n > 0 && act != null)
                times((ulong)n, act);
        }
 
        /// <summary>
        /// Executes the specified <see cref="Action&lt;ulong&gt;"/> delegate <paramref name="n"/> times.
        /// </summary>
        /// <param name="n">
        /// A <see cref="ulong"/> that contains the number of times to execute.
        /// </param>
        /// <param name="act">
        /// An <see cref="Action&lt;ulong&gt;"/> delegate that contains the method(s) to execute.
        /// A <see cref="ulong"/> parameter to pass to this method(s) contains the zero-based count of iteration.
        /// </param>
        public static void Times(ulong n, Action<ulong> act)
        {
            if (n > 0 && act != null)
                times((ulong)n, act);
        }
 
        /// <summary>
        /// Converts the specified array of <see cref="byte"/> to the specified type data.
        /// </summary>
        /// <returns>
        /// A T converted from the <paramref name="src"/>, or a default value of T
        /// if the <paramref name="src"/> is an empty array of <see cref="byte"/>
        /// or if the types of T aren't the <see cref="bool"/>, <see cref="char"/>, <see cref="double"/>,
        /// <see cref="float"/>, <see cref="int"/>, <see cref="long"/>, <see cref="short"/>,
        /// <see cref="uint"/>, <see cref="ulong"/>, <see cref="ushort"/>.
        /// </returns>
        /// <param name="src">
        /// An array of <see cref="byte"/> to convert.
        /// </param>
        /// <param name="srcOrder">
        /// A <see cref="WebSocketSharp.ByteOrder"/> that indicates the byte order of the <paramref name="src"/>.
        /// </param>
        /// <typeparam name="T">
        /// The type of the return value. The T must be a value type.
        /// </typeparam>
        /// <exception cref="ArgumentNullException">
        /// Is thrown when the <paramref name="src"/> parameter passed to a method is invalid because it is <see langword="null"/>.
        /// </exception>
        public static T To<T>(byte[] src, ByteOrder srcOrder)
          where T : struct
        {
            if (src == null)
                throw new ArgumentNullException("src");
 
            if (src.Length == 0)
                return default(T);
 
            var type = typeof(T);
            var buffer = ToHostOrder(src, srcOrder);
            if (type == typeof(Boolean))
                return (T)(object)BitConverter.ToBoolean(buffer, 0);
 
            if (type == typeof(Char))
                return (T)(object)BitConverter.ToChar(buffer, 0);
 
            if (type == typeof(Double))
                return (T)(object)BitConverter.ToDouble(buffer, 0);
 
            if (type == typeof(Int16))
                return (T)(object)BitConverter.ToInt16(buffer, 0);
 
            if (type == typeof(Int32))
                return (T)(object)BitConverter.ToInt32(buffer, 0);
 
            if (type == typeof(Int64))
                return (T)(object)BitConverter.ToInt64(buffer, 0);
 
            if (type == typeof(Single))
                return (T)(object)BitConverter.ToSingle(buffer, 0);
 
            if (type == typeof(UInt16))
                return (T)(object)BitConverter.ToUInt16(buffer, 0);
 
            if (type == typeof(UInt32))
                return (T)(object)BitConverter.ToUInt32(buffer, 0);
 
            if (type == typeof(UInt64))
                return (T)(object)BitConverter.ToUInt64(buffer, 0);
 
            return default(T);
        }
 
        /// <summary>
        /// Converts the specified data to an array of <see cref="byte"/>.
        /// </summary>
        /// <returns>
        /// An array of <see cref="byte"/> converted from the <paramref name="value"/>.
        /// </returns>
        /// <param name="value">
        /// A T to convert.
        /// </param>
        /// <param name="order">
        /// A <see cref="WebSocketSharp.ByteOrder"/> that indicates the byte order of the return.
        /// </param>
        /// <typeparam name="T">
        /// The type of the <paramref name="value"/>. The T must be a value type.
        /// </typeparam>
        public static byte[] ToBytes<T>(T value, ByteOrder order)
          where T : struct
        {
            var type = typeof(T);
            byte[] buffer;
            if (type == typeof(Boolean))
            {
                buffer = BitConverter.GetBytes((Boolean)(object)value);
            }
            else if (type == typeof(Char))
            {
                buffer = BitConverter.GetBytes((Char)(object)value);
            }
            else if (type == typeof(Double))
            {
                buffer = BitConverter.GetBytes((Double)(object)value);
            }
            else if (type == typeof(Int16))
            {
                buffer = BitConverter.GetBytes((Int16)(object)value);
            }
            else if (type == typeof(Int32))
            {
                buffer = BitConverter.GetBytes((Int32)(object)value);
            }
            else if (type == typeof(Int64))
            {
                buffer = BitConverter.GetBytes((Int64)(object)value);
            }
            else if (type == typeof(Single))
            {
                buffer = BitConverter.GetBytes((Single)(object)value);
            }
            else if (type == typeof(UInt16))
            {
                buffer = BitConverter.GetBytes((UInt16)(object)value);
            }
            else if (type == typeof(UInt32))
            {
                buffer = BitConverter.GetBytes((UInt32)(object)value);
            }
            else if (type == typeof(UInt64))
            {
                buffer = BitConverter.GetBytes((UInt64)(object)value);
            }
            else
            {
                buffer = new byte[] { };
            }
 
            return buffer.Length == 0 || IsHostOrder(order)
                   ? buffer
                   : Reverse(buffer);
        }
 
        /// <summary>
        /// Converts the order of the specified array of <see cref="byte"/> to the host byte order.
        /// </summary>
        /// <returns>
        /// An array of <see cref="byte"/> converted from the <paramref name="src"/>.
        /// </returns>
        /// <param name="src">
        /// An array of <see cref="byte"/> to convert.
        /// </param>
        /// <param name="srcOrder">
        /// A <see cref="WebSocketSharp.ByteOrder"/> that indicates the byte order of the <paramref name="src"/>.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Is thrown when the <paramref name="src"/> parameter passed to a method is invalid because it is <see langword="null"/>.
        /// </exception>
        public static byte[] ToHostOrder(byte[] src, ByteOrder srcOrder)
        {
            if (src == null)
                throw new ArgumentNullException("src");
 
            return src.Length == 0 || IsHostOrder(srcOrder)
                   ? src
                   : Reverse(src);
        }
 
        /// <summary>
        /// Converts the specified array to a <see cref="string"/> concatenated the specified separator string
        /// between each element of this array.
        /// </summary>
        /// <returns>
        /// A <see cref="string"/> converted from the <paramref name="array"/> parameter, or a <see cref="String.Empty"/>
        /// if the length of the <paramref name="array"/> is zero.
        /// </returns>
        /// <param name="array">
        /// An array of T to convert.
        /// </param>
        /// <param name="separator">
        /// A <see cref="string"/> that contains a separator string.
        /// </param>
        /// <typeparam name="T">
        /// The type of elements in the <paramref name="array"/>.
        /// </typeparam>
        /// <exception cref="ArgumentNullException">
        /// Is thrown when the <paramref name="array"/> parameter passed to a method is invalid because it is <see langword="null"/>.
        /// </exception>
        public static string ToString<T>(T[] array, string separator)
        {
            if (array == null)
                throw new ArgumentNullException("array");
 
            var len = array.Length;
            if (len == 0)
                return String.Empty;
 
            if (separator == null)
                separator = String.Empty;
 
            var sb = new StringBuilder();
            Times(len - 1, i =>
              sb.AppendFormat("{0}{1}", array[i].ToString(), separator)
            );
 
            sb.Append(array[len - 1].ToString());
            return sb.ToString();
        }
 
        /// <summary>
        /// Converts the specified <see cref="string"/> to a <see cref="Uri"/> object.
        /// </summary>
        /// <returns>
        /// A <see cref="Uri"/> converted from the <paramref name="uriString"/> parameter, or <see langword="null"/>
        /// if the <paramref name="uriString"/> is <see langword="null"/> or <see cref="String.Empty"/>.
        /// </returns>
        /// <param name="uriString">
        /// A <see cref="string"/> to convert.
        /// </param>
        public static Uri ToUri(string uriString)
        {
            return IsNullOrEmpty(uriString)
                   ? null
                   : MaybeUri(uriString)
                     ? new Uri(uriString)
                     : new Uri(uriString, UriKind.Relative);
        }
 
        /// <summary>
        /// Tries to create a new WebSocket <see cref="Uri"/> using the specified <paramref name="uriString"/>.
        /// </summary>
        /// <returns>
        /// <c>true</c> if the WebSocket <see cref="Uri"/> was successfully created; otherwise, <c>false</c>.
        /// </returns>
        /// <param name="uriString">
        /// A <see cref="string"/> that contains a WebSocket URI.
        /// </param>
        /// <param name="result">
        /// When this method returns, contains a created WebSocket <see cref="Uri"/> if the <paramref name="uriString"/> parameter is valid WebSocket URI; otherwise, <see langword="null"/>.
        /// </param>
        /// <param name="message">
        /// When this method returns, contains a error message <see cref="string"/> if the <paramref name="uriString"/> parameter is invalid WebSocket URI; otherwise, <c>String.Empty</c>.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Is thrown when the <paramref name="uriString"/> parameter passed to a method is invalid because it is <see langword="null"/>.
        /// </exception>
        public static bool TryCreateWebSocketUri(string uriString, out Uri result, out string message)
        {
            if (uriString == null)
                throw new ArgumentNullException("uriString");
 
            result = null;
            if (uriString == String.Empty)
            {
                message = "Must not be empty.";
                return false;
            }
 
            var uri = ToUri(uriString);
            if (!uri.IsAbsoluteUri)
            {
                message = "Not absolute URI: " + uriString;
                return false;
            }
 
            var scheme = uri.Scheme;
            if (scheme != "ws" && scheme != "wss")
            {
                message = "Unsupported scheme: " + scheme;
                return false;
            }
 
            var fragment = uri.Fragment;
            if (!String.IsNullOrEmpty(fragment))
            {
                message = "Must not contain the fragment component: " + uriString;
                return false;
            }
 
            var port = uri.Port;
            if (port > 0)
            {
                if ((scheme == "ws" && port == 443) ||
                    (scheme == "wss" && port == 80))
                {
                    message = String.Format(
                      "Invalid pair of scheme and port: {0}, {1}", scheme, port);
                    return false;
                }
            }
 
            result = uri;
            message = String.Empty;
 
            return true;
        }
 
        /// <summary>
        /// URL-decodes the specified <see cref="string"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="string"/> that receives a decoded string, or the <paramref name="s"/> parameter
        /// if the <paramref name="s"/> is <see langword="null"/> or <see cref="String.Empty"/>.
        /// </returns>
        /// <param name="s">
        /// A <see cref="string"/> to decode.
        /// </param>
        public static string UrlDecode(string s)
        {
            return IsNullOrEmpty(s)
                   ? s
                   : HttpUtility.UrlDecode(s);
        }
 
        /// <summary>
        /// URL-encodes the specified <see cref="string"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="string"/> that receives a encoded string, or the <paramref name="s"/> parameter
        /// if the <paramref name="s"/> is <see langword="null"/> or <see cref="String.Empty"/>.
        /// </returns>
        /// <param name="s">
        /// A <see cref="string"/> to encode.
        /// </param>
        public static string UrlEncode(string s)
        {
            return IsNullOrEmpty(s)
                   ? s
                   : HttpUtility.UrlEncode(s);
        }
 
        /// <summary>
        /// Writes the specified content data using the specified <see cref="WebSocketSharp.Net.HttpListenerResponse"/>.
        /// </summary>
        /// <param name="response">
        /// A <see cref="WebSocketSharp.Net.HttpListenerResponse"/> that contains a network stream to write a content data.
        /// </param>
        /// <param name="content">
        /// An array of <see cref="byte"/> that contains a content data to write.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Is thrown when the <paramref name="response"/> parameter passed to a method is invalid because it is <see langword="null"/>.
        /// </exception>
        public static void WriteContent(HttpListenerResponse response, byte[] content)
        {
            if (response == null)
                throw new ArgumentNullException("response");
 
            if (content == null || content.Length == 0)
                return;
 
            var output = response.OutputStream;
            response.ContentLength64 = content.Length;
            output.Write(content, 0, content.Length);
            output.Close();
        }
 
        #endregion
    }
}