Пример #1
0
		private static void Unmask(byte[] message)
		{
			var wsHeader = new WebSocketHeader()
			{
				Mask = true,
				MaskingKey0 = 0x12,
				MaskingKey1 = 0x34,
				MaskingKey2 = 0x56,
				MaskingKey3 = 0x78,
			};

			wsHeader.MaskData(message, 0, message.Length);
		}
Пример #2
0
		private void SendWebsocket(WebSocketHeader header, ArraySegment<byte> content)
		{
			int headerLength = header.GetHeaderLength();

			var r = EventArgsManager.Get();
			r.CopyAddressesFrom(connection);
			r.Count = headerLength + content.Count;
			r.AllocateBuffer();

			header.GenerateHeader(r.OutgoingData);
			Buffer.BlockCopy(content.Array, content.Offset, r.Buffer, r.Offset + headerLength, content.Count);

			SendAsyncSip(r);
		}
Пример #3
0
		private static byte[] PrepareForWebsocket(byte[] message, Opcodes opcode)
		{
			var wsHeader = new WebSocketHeader()
			{
				Fin = true,
				Opcode = opcode,
				PayloadLength = message.Length,
				Mask = true,
				MaskingKey0 = 0x12,
				MaskingKey1 = 0x34,
				MaskingKey2 = 0x56,
				MaskingKey3 = 0x78,
			};

			var extra = new byte[wsHeader.GetHeaderLength()];
			wsHeader.GenerateHeader(new ArraySegment<byte>(extra));

			if (wsHeader.Mask)
				wsHeader.MaskData(message, 0, message.Length);

			return extra;
		}
Пример #4
0
		private void ServersManager_BeforeSend(ServersManager<Connection> s, Connection c, ServerAsyncEventArgs e)
		{
			if (c != null && c.Mode == Connection.Modes.WebSocket)
			{
				//c.BeforeSend(e);

				var header = new WebSocketHeader()
				{
					Fin = true,
					Opcode = websocketOpcode.HasValue ? websocketOpcode.Value : c.WebSocketHeader.Opcode,
					PayloadLength = e.Count,
				};

				int headerLength = header.GetHeaderLength();

				if (e.OffsetOffset < headerLength)
					throw new InvalidProgramException(@"TransportLayer.ServersManager_BeforeSend no reserved space for WebSocket header");

				e.OffsetOffset -= headerLength;
				e.Count += headerLength;

				header.GenerateHeader(e.OutgoingData);
			}
		}