annotate Net/WebSocketPacketStream.cs @ 54:ba4e2cb031e0

Added general purpose tar archive reader class
author Ivo Smits <Ivo@UCIS.nl>
date Wed, 02 Oct 2013 21:17:30 +0200
parents 2e3f57f326b3
children 50d4aed66c67
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
1 ???using System;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
2 using System.IO;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
3 using System.Security.Cryptography;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
4 using System.Text;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
5 using System.Threading;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
6 using UCIS.Util;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
7
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
8 namespace UCIS.Net.HTTP {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
9 public class WebSocketPacketStream : PacketStream {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
10 Stream baseStream;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
11 Boolean negotiationDone = false;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
12 Boolean closed = false;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
13 ManualResetEvent negotiationEvent = new ManualResetEvent(false);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
14 Boolean binaryProtocol = false;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
15 int wsProtocol = -1;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
16
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
17 public WebSocketPacketStream(HTTPContext context) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
18 try {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
19 String ConnectionHeader = context.GetRequestHeader("Connection"); //can be comma-separated list
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
20 Boolean ConnectionUpgrade = ConnectionHeader != null && ConnectionHeader.Contains("Upgrade");
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
21 Boolean UpgradeWebsocket = "WebSocket".Equals(context.GetRequestHeader("Upgrade"), StringComparison.OrdinalIgnoreCase);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
22 String SecWebSocketKey = context.GetRequestHeader("Sec-WebSocket-Key");
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
23 String SecWebSocketKey1 = context.GetRequestHeader("Sec-WebSocket-Key1");
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
24 String SecWebSocketKey2 = context.GetRequestHeader("Sec-WebSocket-Key2");
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
25 String SecWebSocketProtocol = context.GetRequestHeader("Sec-WebSocket-Protocol");
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
26 String[] SecWebSocketProtocols = SecWebSocketProtocol == null ? null : SecWebSocketProtocol.Split(new String[] { ", " }, StringSplitOptions.None);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
27 if (!ConnectionUpgrade || !UpgradeWebsocket || SecWebSocketProtocols == null || SecWebSocketProtocols.Length == 0) goto Failure;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
28 binaryProtocol = SecWebSocketProtocol != null && Array.IndexOf(SecWebSocketProtocols, "binary") != -1;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
29 if (SecWebSocketKey != null) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
30 wsProtocol = 13;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
31 String hashedKey;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
32 using (SHA1 sha1 = new SHA1Managed()) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
33 Byte[] hashable = Encoding.ASCII.GetBytes(SecWebSocketKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
34 Byte[] hash = sha1.ComputeHash(hashable);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
35 hashedKey = Convert.ToBase64String(hash, Base64FormattingOptions.None);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
36 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
37 context.SuppressStandardHeaders = true;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
38 context.SendStatus(101);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
39 context.SendHeader("Connection", "Upgrade");
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
40 context.SendHeader("Upgrade", "websocket");
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
41 context.SendHeader("Sec-WebSocket-Accept", hashedKey);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
42 context.SendHeader("Sec-WebSocket-Protocol", binaryProtocol ? "binary" : "base64");
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
43 baseStream = context.GetResponseStream();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
44 } else if (SecWebSocketKey1 != null && SecWebSocketKey2 != null) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
45 wsProtocol = 100;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
46 Byte[] key = new Byte[4 + 4 + 8];
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
47 CalculateHybi00MagicNumber(SecWebSocketKey1, key, 0);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
48 CalculateHybi00MagicNumber(SecWebSocketKey2, key, 4);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
49 context.SuppressStandardHeaders = true;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
50 context.SendStatus(101);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
51 context.SendHeader("Connection", "Upgrade");
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
52 context.SendHeader("Upgrade", "websocket");
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
53 context.SendHeader("Sec-WebSocket-Protocol", binaryProtocol ? "binary" : "base64");
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
54 context.SendHeader("Sec-WebSocket-Origin", context.GetRequestHeader("Origin"));
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
55 context.SendHeader("Sec-WebSocket-Location", "ws://" + context.GetRequestHeader("Host") + context.RequestPath);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
56 baseStream = context.GetResponseStream();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
57 ReadAllBytes(key, 8, 8);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
58 using (MD5 md5 = new MD5CryptoServiceProvider()) key = md5.ComputeHash(key);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
59 baseStream.Write(key, 0, key.Length);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
60 } else {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
61 goto Failure;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
62 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
63 if (closed) baseStream.Close();
37
2e3f57f326b3 Fixed some compiler warnings
Ivo Smits <Ivo@UCIS.nl>
parents: 12
diff changeset
64 } catch (Exception) {
12
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
65 closed = true;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
66 context.Close();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
67 } finally {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
68 negotiationDone = true;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
69 negotiationEvent.Set();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
70 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
71 return;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
72 Failure:
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
73 closed = true;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
74 context.SendErrorAndClose(400);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
75 return;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
76 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
77
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
78 private void CalculateHybi00MagicNumber(String s, Byte[] obuf, int opos) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
79 long number = 0;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
80 long spaces = 0;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
81 foreach (Char c in s) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
82 if (c == ' ') {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
83 spaces++;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
84 } else if (c >= '0' && c <= '9') {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
85 number = number * 10 + (c - '0');
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
86 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
87 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
88 number /= spaces;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
89 obuf[opos++] = (Byte)(number >> 24);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
90 obuf[opos++] = (Byte)(number >> 16);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
91 obuf[opos++] = (Byte)(number >> 8);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
92 obuf[opos++] = (Byte)(number >> 0);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
93 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
94
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
95 public override bool CanRead { get { return negotiationDone && !closed; } }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
96 public override bool CanSeek { get { return false; } }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
97 public override bool CanWrite { get { return negotiationDone && !closed; } }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
98 public override void Flush() { }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
99
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
100 public override void Close() {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
101 closed = true;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
102 base.Close();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
103 if (baseStream != null) baseStream.Close();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
104 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
105 public override bool CanTimeout { get { return baseStream.CanTimeout; } }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
106 public override int ReadTimeout {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
107 get { return baseStream.ReadTimeout; }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
108 set { baseStream.ReadTimeout = value; }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
109 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
110 public override int WriteTimeout {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
111 get { return baseStream.WriteTimeout; }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
112 set { baseStream.WriteTimeout = value; }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
113 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
114
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
115 public override long Length { get { throw new NotSupportedException(); } }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
116 public override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
117 public override void SetLength(long value) { throw new NotSupportedException(); }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
118 public override long Position {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
119 get { throw new NotSupportedException(); }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
120 set { throw new NotSupportedException(); }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
121 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
122
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
123 private void ReadAllBytes(Byte[] buffer, int offset, int count) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
124 while (count > 0) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
125 int l = baseStream.Read(buffer, offset, count);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
126 if (l <= 0) throw new EndOfStreamException();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
127 offset += l;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
128 count -= l;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
129 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
130 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
131
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
132 Byte[] leftOver = null;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
133 public override int Read(byte[] buffer, int offset, int count) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
134 Byte[] packet = leftOver;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
135 leftOver = null;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
136 if (packet == null) packet = ReadPacket();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
137 if (packet == null) return 0;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
138 if (count > packet.Length) count = packet.Length;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
139 Buffer.BlockCopy(packet, 0, buffer, offset, count);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
140 if (packet.Length > count) leftOver = ArrayUtil.Slice(packet, count);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
141 return count;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
142 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
143 public override byte[] ReadPacket() {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
144 if (leftOver != null) throw new InvalidOperationException("There is remaining data from a partial read");
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
145 negotiationEvent.WaitOne();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
146 if (closed) throw new ObjectDisposedException("WebSocketPacketStream");
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
147 try {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
148 if (wsProtocol == 13) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
149 Byte[] multipartbuffer = null;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
150 int multipartopcode = -1;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
151 while (true) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
152 int flags = baseStream.ReadByte();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
153 if (flags == -1) throw new EndOfStreamException();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
154 UInt64 pllen = (byte)baseStream.ReadByte();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
155 Boolean masked = (pllen & 128) != 0;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
156 pllen &= 127;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
157 if (pllen == 126) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
158 pllen = (uint)baseStream.ReadByte() << 8;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
159 pllen |= (uint)baseStream.ReadByte();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
160 } else if (pllen == 127) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
161 pllen = (ulong)baseStream.ReadByte() << 56;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
162 pllen |= (ulong)baseStream.ReadByte() << 48;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
163 pllen |= (ulong)baseStream.ReadByte() << 40;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
164 pllen |= (ulong)baseStream.ReadByte() << 32;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
165 pllen |= (uint)baseStream.ReadByte() << 24;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
166 pllen |= (uint)baseStream.ReadByte() << 16;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
167 pllen |= (uint)baseStream.ReadByte() << 8;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
168 pllen |= (uint)baseStream.ReadByte();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
169 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
170 Byte[] mask = new Byte[4];
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
171 if (masked) ReadAllBytes(mask, 0, mask.Length);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
172 //Console.WriteLine("Read flags={0} masked={1} mask={2} len={3}", flags, masked, mask, pllen);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
173 Byte[] payload = new Byte[pllen]; // + (4 - (pllen % 4))];
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
174 ReadAllBytes(payload, 0, (int)pllen);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
175 if (masked) for (int i = 0; i < (int)pllen; i++) payload[i] ^= mask[i % 4];
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
176 int opcode = flags & 0x0f;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
177 Boolean fin = (flags & 0x80) != 0;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
178 if (opcode == 0) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
179 //Console.WriteLine("WebSocket received continuation frame type {0}!", multipartopcode);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
180 Array.Resize(ref multipartbuffer, multipartbuffer.Length + payload.Length);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
181 payload.CopyTo(multipartbuffer, multipartbuffer.Length - payload.Length);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
182 opcode = -1;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
183 if (fin) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
184 payload = multipartbuffer;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
185 opcode = multipartopcode;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
186 multipartbuffer = null;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
187 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
188 } else if (!fin) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
189 //Console.WriteLine("WebSocket received non-fin frame type {0}!", opcode);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
190 multipartbuffer = payload;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
191 multipartopcode = opcode;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
192 opcode = -1;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
193 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
194 if (opcode == -1) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
195 } else if (opcode == 0) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
196 throw new NotSupportedException("WebSocket opcode 0 is not supported");
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
197 } else if (opcode == 1) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
198 String text = Encoding.UTF8.GetString(payload); //, 0, pllen);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
199 return Convert.FromBase64String(text);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
200 } else if (opcode == 2) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
201 return payload; // ArrayUtil.Slice(payload, 0, pllen);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
202 } else if (opcode == 8) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
203 return null;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
204 } else if (opcode == 9) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
205 //Console.WriteLine("WebSocket PING");
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
206 WriteFrame(10, payload, 0, (int)pllen);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
207 } else if (opcode == 10) { //PONG
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
208 } else {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
209 //Console.WriteLine("WebSocket UNKNOWN OPCODE {0}", opcode);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
210 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
211 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
212 } else if (wsProtocol == 100) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
213 int frameType = baseStream.ReadByte();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
214 if (frameType == -1) throw new EndOfStreamException();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
215 if ((frameType & 0x80) != 0) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
216 int length = 0;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
217 while (true) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
218 int b = baseStream.ReadByte();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
219 if (b == -1) throw new EndOfStreamException();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
220 length = (length << 7) | (b & 0x7f);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
221 if ((b & 0x80) == 0) break;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
222 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
223 Byte[] buffer = new Byte[length];
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
224 ReadAllBytes(buffer, 0, length);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
225 if (frameType == 0xff && length == 0) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
226 return null;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
227 } else {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
228 throw new InvalidOperationException();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
229 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
230 } else {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
231 using (MemoryStream ms = new MemoryStream()) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
232 while (true) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
233 int b = baseStream.ReadByte();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
234 if (b == -1) throw new EndOfStreamException();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
235 if (b == 0xff) break;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
236 ms.WriteByte((Byte)b);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
237 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
238 if (frameType == 0x00) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
239 ms.Seek(0, SeekOrigin.Begin);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
240 StreamReader reader = new StreamReader(ms, Encoding.UTF8, false);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
241 return Convert.FromBase64String(reader.ReadToEnd());
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
242 } else {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
243 throw new InvalidOperationException();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
244 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
245 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
246 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
247 } else {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
248 throw new InvalidOperationException();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
249 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
250 } catch (Exception ex) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
251 Console.WriteLine(ex);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
252 throw;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
253 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
254 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
255 private delegate Byte[] ReadPacketDelegate();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
256 ReadPacketDelegate readPacketDelegate;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
257 public override IAsyncResult BeginReadPacket(AsyncCallback callback, object state) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
258 if (readPacketDelegate == null) readPacketDelegate = ReadPacket;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
259 return readPacketDelegate.BeginInvoke(callback, state);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
260 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
261 public override byte[] EndReadPacket(IAsyncResult asyncResult) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
262 return readPacketDelegate.EndInvoke(asyncResult);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
263 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
264 public override void Write(byte[] buffer, int offset, int count) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
265 negotiationEvent.WaitOne();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
266 if (closed) throw new ObjectDisposedException("WebSocketPacketStream");
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
267 if (!binaryProtocol) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
268 String encoded = Convert.ToBase64String(buffer, offset, count, Base64FormattingOptions.None);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
269 buffer = Encoding.ASCII.GetBytes(encoded);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
270 offset = 0;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
271 count = buffer.Length;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
272 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
273 if (wsProtocol == 13) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
274 WriteFrame(binaryProtocol ? (Byte)0x2 : (Byte)0x1, buffer, offset, count);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
275 } else if (wsProtocol == 100) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
276 Byte[] bytes = new Byte[2 + count];
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
277 bytes[0] = 0x00;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
278 Buffer.BlockCopy(buffer, offset, bytes, 1, count);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
279 bytes[1 + count] = 0xff;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
280 baseStream.Write(bytes, 0, bytes.Length);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
281 } else {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
282 throw new InvalidOperationException();
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
283 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
284 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
285 private void WriteFrame(Byte opcode, Byte[] buffer, int offset, int count) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
286 int pllen = count;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
287 int hlen = 2;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
288 if (pllen > 0xffff) hlen += 8;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
289 else if (pllen > 125) hlen += 2;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
290 Byte[] wbuf = new Byte[count + hlen];
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
291 wbuf[0] = (Byte)(opcode | 0x80);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
292 if (pllen > 0xffff) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
293 wbuf[1] = 127;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
294 wbuf[2] = 0;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
295 wbuf[3] = 0;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
296 wbuf[4] = 0;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
297 wbuf[5] = 0;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
298 wbuf[6] = (Byte)(pllen >> 24);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
299 wbuf[7] = (Byte)(pllen >> 16);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
300 wbuf[8] = (Byte)(pllen >> 8);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
301 wbuf[9] = (Byte)(pllen >> 0);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
302 } else if (pllen > 125) {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
303 wbuf[1] = 126;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
304 wbuf[2] = (Byte)(pllen >> 8);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
305 wbuf[3] = (Byte)(pllen >> 0);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
306 } else {
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
307 wbuf[1] = (Byte)pllen;
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
308 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
309 Buffer.BlockCopy(buffer, offset, wbuf, hlen, count);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
310 baseStream.Write(wbuf, 0, wbuf.Length);
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
311 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
312 }
ba8f94212c6e Added WebSocket support
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
313 }