Esempio n. 1
0
		/// <summary>
		/// Tries to parse data that has been received.
		/// </summary>
		/// <param name="numBytes">the number of bytes waiting to be read</param>
		protected override bool OnReceive(BufferSegment segment)
		{
			var packet = new AuthPacketIn(segment, 0, _remainingLength);
			segment.IncrementUsage();

			Console.WriteLine("S <- C: " + packet.PacketId);
			AuthPacketManager.Instance.HandlePacket(this, packet);
			_remainingLength = 0;
			return true;
		}
Esempio n. 2
0
        /// <summary>
        /// </summary>
        /// <param name="buffer">
        /// </param>
        /// <returns>
        /// </returns>
        protected override bool OnReceive(BufferSegment buffer)
        {
            Message message = null;

            var packet = new byte[this._remainingLength];
            Array.Copy(buffer.SegmentData, packet, this._remainingLength);

            LogUtil.Debug(
                DebugInfoDetail.Network,
                "Offset: " + buffer.Offset.ToString() + " -- RemainingLength: " + this._remainingLength);
            LogUtil.Debug(DebugInfoDetail.Network, HexOutput.Output(packet));

            this._remainingLength = 0;
            try
            {
                message = this.messageSerializer.Deserialize(packet);
            }
            catch (Exception)
            {
                uint messageNumber = this.GetMessageNumber(packet);
                this.Server.Warning(
                    this,
                    "Client sent malformed message {0}",
                    messageNumber.ToString(CultureInfo.InvariantCulture));
                LogUtil.Debug(DebugInfoDetail.Error, HexOutput.Output(packet));
                return false;
            }

            buffer.IncrementUsage();

            if (message == null)
            {
                uint messageNumber = this.GetMessageNumber(packet);
                this.Server.Warning(
                    this,
                    "Client sent unknown message {0}",
                    messageNumber.ToString(CultureInfo.InvariantCulture));
                return false;
            }

            this.bus.Publish(new MessageReceivedEvent(this, message));

            return true;
        }
Esempio n. 3
0
        /// <summary>
        /// </summary>
        /// <param name="buffer">
        /// </param>
        /// <returns>
        /// </returns>
        /// <exception cref="NotImplementedException">
        /// </exception>
        protected override bool OnReceive(BufferSegment buffer)
        {
            Message message = null;

            var packet = new byte[this._remainingLength];
            Array.Copy(buffer.SegmentData, packet, this._remainingLength);

            LogUtil.Debug(DebugInfoDetail.Network, "\r\nReceived: \r\n" + HexOutput.Output(packet));

            this._remainingLength = 0;
            try
            {
                message = this.messageSerializer.Deserialize(packet);
            }
            catch (Exception)
            {
                uint messageNumber = this.GetMessageNumber(packet);
                this.Server.Warning(
                    this,
                    "Client sent malformed message {0}",
                    messageNumber.ToString(CultureInfo.InvariantCulture));
                LogUtil.Debug(DebugInfoDetail.Error, HexOutput.Output(packet));
                return false;
            }

            buffer.IncrementUsage();

            if (message == null)
            {
                uint messageNumber = this.GetMessageNumber(packet);
                this.Server.Warning(
                    this,
                    "Client sent unknown message {0}",
                    messageNumber.ToString(CultureInfo.InvariantCulture));
                return false;
            }

            // FUUUUUGLY

            Type wrapperType = typeof(MessageWrapper<>);
            Type genericWrapperType = wrapperType.MakeGenericType(message.Body.GetType());

            object wrapped = Activator.CreateInstance(genericWrapperType);
            wrapped.GetType().GetProperty("Client").SetValue(wrapped, (IZoneClient)this, null);
            wrapped.GetType().GetProperty("Message").SetValue(wrapped, message, null);
            wrapped.GetType().GetProperty("MessageBody").SetValue(wrapped, message.Body, null);

            this.bus.Publish(wrapped);

            return true;
        }
Esempio n. 4
0
		/// <summary>
		/// Pass recieved data into the packet buffer and try to parse.
		/// </summary>
		/// <param name="_remainingLength">number of bytes waiting to be read</param>
		/// <returns>false, if there is a part of a packet still remaining</returns>
		protected override bool OnReceive(BufferSegment segment)
		{
			var recvBuffer = segment.Buffer.Array;

			var i = 1;

			do
			{
				if (_remainingLength < RealmPacketIn.HEADER_SIZE)
				{
					return false;
				}

				RealmServerOpCode opcode;

				//var headerSize = GetContentInfo(recvBuffer, segment.Offset + _offset, out packetLength, out opcode);

				var offset = segment.Offset + _offset;
				int headerSize;
				bool isLargePacket;
				var packetLength = 0;
				if (IsEncrypted)
				{
					//headerSize = Decrypt(recvBuffer, offset, out packetLength, out opcode);
					var firstByte = GetDecryptedByte(recvBuffer, offset, 0);

					isLargePacket = (firstByte & 0x80) != 0;				// check for the big packet marker
					if (isLargePacket)
					{
						// packetLength has 23 bits
						if (_remainingLength < RealmPacketIn.LARGE_PACKET_HEADER_SIZE)
						{
							decryptUntil = 0;
							log.Warn("DecryptUntil: " + decryptUntil);
							return false;
						}

						packetLength = (firstByte & 0x7F) << 16;
						packetLength |= GetDecryptedByte(recvBuffer, offset, 1) << 8;
						packetLength |= GetDecryptedByte(recvBuffer, offset, 2);

						opcode = (RealmServerOpCode)GetDecryptedOpcode(recvBuffer, offset, 3);
						headerSize = RealmPacketIn.LARGE_PACKET_HEADER_SIZE;
					}
					else
					{
						// packetLength has 15 bits
						packetLength |= firstByte << 8;
						packetLength |= GetDecryptedByte(recvBuffer, offset, 1);

						opcode = (RealmServerOpCode)GetDecryptedOpcode(recvBuffer, offset, 2);
						headerSize = RealmPacketIn.HEADER_SIZE;
					}
				}
				else
				{
					packetLength = recvBuffer[offset] << 8 | recvBuffer[offset + 1];
					isLargePacket = false;

					// the opcode is actually 4 bytes, but can never go over 2, so we skip the last 2
					opcode = (RealmServerOpCode)(recvBuffer[offset + 2] | recvBuffer[offset + 3] << 8);
					headerSize = RealmPacketIn.HEADER_SIZE;
				}

				packetLength += (headerSize - 4);

				if (packetLength > BufferSize)
				{
					// packet is just too big
					var bytes = new byte[headerSize];
					Array.Copy(recvBuffer, offset, bytes, 0, headerSize);
					LogUtil.ErrorException("Client {0} sent corrupted packet (ID: {1}) with size {2} bytes, which exceeds maximum: " +
						"{3} (packet #{4}, segment #{5}, LargePacket: {6}, Remaining: {7}, Header: {8} ({9}))",
							  this, opcode, packetLength, BufferSize, i, segment.Number,
							  isLargePacket,
							  _remainingLength,
							  bytes.ToString(" ", b => string.Format("{0:X2}", b)),
								Encoding.ASCII.GetString(bytes));

					Disconnect();

					return false;
				}

				if (_remainingLength < packetLength)
				{
					// packet incomplete
					if (IsEncrypted)
					{
						decryptUntil = headerSize;
						log.Warn("DecryptUntil: {0}, HeaderSize: {1}, Packet: {2}", decryptUntil, headerSize, opcode);
					}
					return false;
				}

				var pkt = new RealmPacketIn(segment, _offset, packetLength, opcode, headerSize);
				segment.IncrementUsage();

				//.UpdatePacketCounters(pkt.PacketId, fullPacketSize);

				PerformanceCounters.PacketsReceivedPerSecond.Increment();
				PerformanceCounters.TotalBytesReceived.IncrementBy(packetLength);

				RealmPacketMgr.Instance.HandlePacket(this, pkt);

				_remainingLength -= packetLength;
				_offset += packetLength;
				decryptUntil = -1;
				i++;
			} while (_remainingLength > 0);

			return true;
		}