当前位置: 首页>>代码示例>>C#>>正文


C# GSPacketIn.Load方法代码示例

本文整理汇总了C#中GSPacketIn.Load方法的典型用法代码示例。如果您正苦于以下问题:C# GSPacketIn.Load方法的具体用法?C# GSPacketIn.Load怎么用?C# GSPacketIn.Load使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在GSPacketIn的用法示例。


在下文中一共展示了GSPacketIn.Load方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ReceiveBytes

		/// <summary>
		/// Called when the client receives bytes
		/// </summary>
		/// <param name="numBytes">The number of bytes received</param>
		public void ReceiveBytes(int numBytes)
		{
			lock (m_SyncLock)
			{
				byte[] buffer = m_client.ReceiveBuffer;

				//End Offset of buffer
				int bufferSize = m_client.ReceiveBufferOffset + numBytes;

				//Size < minimum
				if (bufferSize < GSPacketIn.HDR_SIZE)
				{
					m_client.ReceiveBufferOffset = bufferSize; // undo buffer read
					return;
				}

				//Reset the offset
				m_client.ReceiveBufferOffset = 0;

				//Current offset into the buffer
				int curOffset = 0;

				do
				{
					int packetLength = (buffer[curOffset] << 8) + buffer[curOffset + 1] + GSPacketIn.HDR_SIZE;
					int dataLeft = bufferSize - curOffset;

					if (dataLeft < packetLength)
					{
						Buffer.BlockCopy(buffer, curOffset, buffer, 0, dataLeft);
						m_client.ReceiveBufferOffset = dataLeft;
						break;
					}

					// ** commented out because this hasn't been used in forever and crutching
					// ** to it only hurts performance in a design that needs to be reworked
					// ** anyways.                                               
					// **                                                               - tobz
					//var curPacket = new byte[packetLength];
					//Buffer.BlockCopy(buffer, curOffset, curPacket, 0, packetLength);
					//curPacket = m_encoding.DecryptPacket(buffer, false);

					int packetEnd = curOffset + packetLength;

					int calcCheck = CalculateChecksum(buffer, curOffset, packetLength - 2);
					int pakCheck = (buffer[packetEnd - 2] << 8) | (buffer[packetEnd - 1]);

					if (pakCheck != calcCheck)
					{
						if (log.IsWarnEnabled)
							log.WarnFormat(
								"Bad TCP packet checksum (packet:0x{0:X4} calculated:0x{1:X4}) -> disconnecting\nclient: {2}\ncurOffset={3}; packetLength={4}",
								pakCheck, calcCheck, m_client.ToString(), curOffset, packetLength);

						if (log.IsInfoEnabled)
						{
							log.Info("Last client sent/received packets (from older to newer):");

							foreach (IPacket prevPak in GetLastPackets())
							{
								log.Info(prevPak.ToHumanReadable());
							}
							
							log.Info(Marshal.ToHexDump("Last Received Bytes : ", buffer));
						}

						m_client.Disconnect();
						return;
					}

					var pak = new GSPacketIn(packetLength - GSPacketIn.HDR_SIZE);
					pak.Load(buffer, curOffset, packetLength);

					try
					{
						HandlePacket(pak);
					}
					catch (Exception e)
					{
						if (log.IsErrorEnabled)
							log.Error("HandlePacket(pak)", e);
					}

					curOffset += packetLength;
				} while (bufferSize - 1 > curOffset);

				if (bufferSize - 1 == curOffset)
				{
					buffer[0] = buffer[curOffset];
					m_client.ReceiveBufferOffset = 1;
				}
			}
		}
开发者ID:mynew4,项目名称:DAoC,代码行数:97,代码来源:PacketProcessor.cs


注:本文中的GSPacketIn.Load方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。