Example #1
0
		public void ProcessIncomingRequest(Connection c)
		{
			if (c.HttpReader.HasUpgarde(Upgrades.Websocket))
			{
				// c.HttpReader.SecWebSocketProtocol

				var key = c.HttpReader.SecWebSocketKey.ToString();
				byte[] accept;
				lock (sha1)
					accept = sha1.ComputeHash(Encoding.ASCII.GetBytes(key + guid));

				using (var writer = new HttpMessageWriter())
				{
					writer.WriteStatusLine(StatusCodes.SwitchingProtocols);
					writer.WriteConnectionUpgrade();
					writer.WriteUpgradeWebsocket();
					writer.WriteSecWebSocketAccept(accept);
					writer.WriteSecWebSocketProtocol(sip);
					writer.WriteContentLength(0);
					writer.WriteCRLF();

					SendResponse(c, writer, InvalidAgentIndex);
				}

				c.UpgradeToWebsocket();
			}
			else
			{
				base.ProcessIncomingRequest(c);
			}
		}
Example #2
0
		private void ValidateWebsocket(IEnumerable<int> splitSizes, int messagesCount)
		{
			var ping1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, };
			var ping2 = new byte[] { 8, 9, 10 };
			var ping3 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18 };
			var message = Encoding.UTF8.GetBytes("REGISTER sip:officesip.local SIP/2.0\r\n\r\n");

			var stream = CreateStream(
				messagesCount,
				PrepareForWebsocket(ping1, Opcodes.Ping), ping1,
				PrepareForWebsocket(ping2, Opcodes.Ping), ping2,
				PrepareForWebsocket(ping3, Opcodes.Ping), ping3,
				PrepareForWebsocket(message, Opcodes.Binary), message
			);

			Unmask(ping1);
			Unmask(ping2);
			Unmask(ping3);
			Unmask(message);

			ServerAsyncEventArgs e = null;
			using (var connection = new Connection())
			{
				connection.UpgradeToWebsocket();

				foreach (int splitSize in splitSizes)
				{
					int realMessageCount = 0;

					for (int offset = 0; offset < stream.Length; offset += splitSize)
					{
						var info = "Split by: " + splitSize + "; Message #: " + realMessageCount;

						if (e == null)
							e = EventArgsManager.Get();

						e.BytesTransferred = Math.Min(splitSize, stream.Length - offset);
						Buffer.BlockCopy(stream, offset, e.Buffer, e.Offset, e.BytesTransferred);

						bool closeConnection;
						for (bool repeat = true; repeat; )
						{
							repeat = connection.Proccess(ref e, out closeConnection);

							Assert.IsFalse(closeConnection, info);

							if (connection.IsMessageReady)
							{
								switch (realMessageCount % 4)
								{
									case 0:
										Assert.AreEqual(Opcodes.Ping, connection.WebSocketHeader.Opcode);
										AreEqual(ping1, connection.Content);
										break;
									case 1:
										Assert.AreEqual(Opcodes.Ping, connection.WebSocketHeader.Opcode);
										AreEqual(ping2, connection.Content);
										break;
									case 2:
										Assert.AreEqual(Opcodes.Ping, connection.WebSocketHeader.Opcode);
										AreEqual(ping3, connection.Content);
										break;
									case 3:
										Assert.AreEqual(Opcodes.Binary, connection.WebSocketHeader.Opcode);
										AreEqual(message, connection.Header);
										break;
								}

								connection.ResetState();
								realMessageCount++;
							}
						}
					}

					EventArgsManager.Put(ref e);

					Assert.AreEqual(messagesCount * 4, realMessageCount);
				}
			}
		}
Example #3
0
			private void ValidateSip(Connection connection)
			{
				Assert.AreEqual(sipExpected.Method, connection.Reader.Method);
				Assert.AreEqual(sipExpected.RequestUri.Value.ToString(), connection.Reader.RequestUri.Value.ToString());

				Assert.AreEqual(sipHeader.Length, connection.Header.Count);
				Assert.AreEqual(sipHeader, Encoding.UTF8.GetString(connection.Header.Array, connection.Header.Offset, connection.Header.Count));

				Assert.AreEqual(sipExpected.ContentLength, connection.Content.Count);
				if (sipExpected.ContentLength > 0)
					Assert.AreEqual(sipContent, Encoding.UTF8.GetString(connection.Content.Array, connection.Content.Offset, connection.Content.Count));
			}
Example #4
0
			private void ValidateHttp(Connection connection, string details)
			{
				Assert.AreEqual(httpExpected.Method, connection.HttpReader.Method, details);
				Assert.AreEqual(httpExpected.RequestUri.ToString(), connection.HttpReader.RequestUri.ToString(), details);

				if (connection.Mode != Connection.Modes.Ajax)
				{
					Assert.AreEqual(httpHeader.Length, connection.Header.Count, details);
					Assert.AreEqual(httpHeader, Encoding.UTF8.GetString(connection.Header.Array, connection.Header.Offset, connection.Header.Count), details);

					Assert.AreEqual(httpExpected.ContentLength, connection.Content.Count);
					if (httpExpected.ContentLength > 0)
						Assert.AreEqual(httpContent, Encoding.UTF8.GetString(connection.Content.Array, connection.Content.Offset, connection.Content.Count));
				}
			}
Example #5
0
			public ValidateClass(string httpMessage, string sipMessage, int messagesCount, IEnumerable<int> splitSizes)
			{
				if (httpMessage != null)
					ParseHtppMessage(httpMessage);
				if (sipMessage != null)
					ParseSipMessage(sipMessage);

				var stream = CreateStream(messagesCount, httpMessage, sipMessage);

				int oldUsedBuffers = EventArgsManager.Created - EventArgsManager.Queued;

				ServerAsyncEventArgs e = null;
				using (var connection = new Connection())
				{
					foreach (int splitSize in splitSizes)
					{
						int realMessageCount = 0;

						for (int offset = 0; offset < stream.Length; offset += splitSize)
						{
							var details = string.Format("Split by: {0}; Message #: {1}", splitSize, realMessageCount);

							if (e == null)
								e = EventArgsManager.Get();

							e.BytesTransferred = Math.Min(splitSize, stream.Length - offset);
							Buffer.BlockCopy(stream, offset, e.Buffer, e.Offset, e.BytesTransferred);

							bool closeConnection;
							for (bool repeat = true; repeat; )
							{
								repeat = connection.Proccess(ref e, out closeConnection);

								Assert.IsFalse(closeConnection, details);

								if (connection.IsMessageReady)
								{
									if (httpMessage != null)
										ValidateHttp(connection, details);
									if (sipMessage != null)
										ValidateSip(connection);

									connection.ResetState();
									realMessageCount++;
								}
							}
						}

						EventArgsManager.Put(ref e);

						Assert.AreEqual(messagesCount, realMessageCount);
					}
				}

				Assert.AreEqual(oldUsedBuffers, EventArgsManager.Created - EventArgsManager.Queued);
			}
Example #6
0
		private void Validate(string messageText, bool addKeepAlive, int messagesCount, IEnumerable<int> splitSizes, bool detachBuffer, bool websocket)
		{
			var message = Encoding.UTF8.GetBytes(messageText);

			var expected = new SipMessageReader();
			expected.SetDefaultValue();
			int parsed = expected.Parse(message, 0, message.Length);
			expected.SetArray(message);
			if (expected.ContentLength < 0)
				expected.ContentLength = 0;
			var expectedUri = expected.RequestUri.Value.ToString();
			var content = Encoding.UTF8.GetString(message, parsed, message.Length - parsed);

			if (addKeepAlive)
				message = Encoding.UTF8.GetBytes("\r\n\r\n" + messageText + "\r\n\r\n\r\n\r\n\r\n\r\n");

			var extra = new byte[0];
			if (websocket)
				extra = PrepareForWebsocket(message);

			var stream = CreateStream(messagesCount, extra, message);
			int headersLength = messageText.IndexOf("\r\n\r\n") + 4;
			var headersText = messageText.Substring(0, headersLength);

			int oldUsedBuffers = EventArgsManager.Created - EventArgsManager.Queued;

			ServerAsyncEventArgs e = null;
			using (var connection = new Connection())
			using (var threads = new Threads(connection))
			{
				if (websocket)
					connection.UpgradeToWebsocket();

				foreach (int splitSize in splitSizes)
				{
					int realMessageCount = 0;

					for (int offset = 0; offset < stream.Length; offset += splitSize)
					{
						var info = "Split by: " + splitSize + "; Message #: " + realMessageCount +
							"\r\nMessage Sizes: " + message.Length + " " + (message.Length - content.Length) + " " + content.Length;

						if (e == null)
							e = EventArgsManager.Get();

						e.BytesTransferred = Math.Min(splitSize, stream.Length - offset);
						Buffer.BlockCopy(stream, offset, e.Buffer, e.Offset, e.BytesTransferred);

						bool closeConnection;
						for (bool repeat = true; repeat; )
						{
							//repeat = connection.Proccess(ref e, out closeConnection);
							repeat = threads.Proccess(ref e, out closeConnection);

							Assert.IsFalse(closeConnection, info);

							if (connection.IsMessageReady)
							{
								var actualHeaders = Encoding.UTF8.GetString(connection.Header.Array, connection.Header.Offset, connection.Header.Count);

								Assert.AreEqual(expected.Method, connection.Reader.Method, info);
								Assert.AreEqual(expectedUri, connection.Reader.RequestUri.Value.ToString(), info);

								Assert.AreEqual(headersLength, connection.Header.Count);
								Assert.AreEqual(headersText, actualHeaders);

								Assert.AreEqual(expected.ContentLength, connection.Content.Count);
								if (expected.ContentLength > 0)
									Assert.AreEqual(content, Encoding.UTF8.GetString(connection.Content.Array, connection.Content.Offset, connection.Content.Count), info);

								BufferHandle handle = new BufferHandle();
								if (detachBuffer)
									handle = connection.Dettach(ref e);

								connection.ResetState();
								realMessageCount++;

								if (detachBuffer)
									handle.Free();
							}
						}
					}

					EventArgsManager.Put(ref e);

					Assert.AreEqual(messagesCount, realMessageCount);
				}
			}

			Assert.AreEqual(oldUsedBuffers, EventArgsManager.Created - EventArgsManager.Queued);
		}
Example #7
0
			public Threads(Connection connection)
			{
				this.connection = connection;
				this.done = new AutoResetEvent(false);

				this.release = new AutoResetEvent[maxThread];
				for (int i = 0; i < this.release.Length; i++)
				{
					this.release[i] = new AutoResetEvent(false);
					ThreadPool.QueueUserWorkItem((x) => { release[(int)x].WaitOne(); }, i);
				}
			}