Exemple #1
0
		/// <summary>
		/// 对客户端传过来的数据类型进行封包和解包, object支持unity特定的类型: Vector2, Vector3
		/// </summary>
		/// <param name="buffer">Buffer.</param>
		/// <param name="objs">Objects.</param>
		public void encode(TNBuffer buffer, params object[] objs)
		{
			if (objs == null || objs.Length == 0) return;
		
			BinaryWriter bw = buffer.BeginWriting(true);
			for (int b = 0, bmax = objs.Length; b < bmax; ++b)
			{
				object obj = objs[b];
				if (obj != null && !WriteObject(bw, obj))
				{
					Debug.LogError("Unable to write type " + obj.GetType());
				}
			}
		}
Exemple #2
0
		/// <summary>
		/// See if the received packet can be processed and split it up into different ones.
		/// </summary>
		
		bool ProcessBuffer (int bytes)
		{
			if (mReceiveBuffer == null)
			{
				// Create a new packet buffer
				mReceiveBuffer = TNBuffer.Create();
				mReceiveBuffer.BeginWriting(false).Write(mTemp, 0, bytes);
			}
			else
			{
				// Append this data to the end of the last used buffer
				mReceiveBuffer.BeginWriting(true).Write(mTemp, 0, bytes);
			}
			
			for (int available = mReceiveBuffer.size - mOffset; available >= 4; )
			{
				// Figure out the expected size of the packet
				if (mExpected == 0)
				{
					mExpected = mReceiveBuffer.PeekInt(mOffset);
					if (mExpected == -1) break;
					
					if (mExpected == 0)
					{
						Close(true);
						return false;
					}
				}
				
				// The first 4 bytes of any packet always contain the number of bytes in that packet
				available -= 4;
				
				// If the entire packet is present
				if (available == mExpected)
				{

					if (onDebug != null)
					{
						onDebug(this.serverName, "OnReceive, bytes: " + mExpected);
					}

					// Reset the position to the beginning of the packet
					mReceiveBuffer.BeginReading(mOffset + 4);
					
					// This packet is now ready to be processed
					lock (mIn) mIn.Enqueue(mReceiveBuffer);
					mReceiveBuffer = null;
					mExpected = 0;
					mOffset = 0;

					if (null != this.onReceivePack)
					{
						this.onReceivePack();
					}
					break;
				}
				else if (available > mExpected)
				{
					// There is more than one packet. Extract this packet fully.
					int realSize = mExpected + 4;
					TNBuffer temp = TNBuffer.Create();
					
					// Extract the packet and move past its size component
					temp.BeginWriting(false).Write(mReceiveBuffer.buffer, mOffset, realSize);
					temp.BeginReading(4);
					
					// This packet is now ready to be processed
					lock (mIn) mIn.Enqueue(temp);
					
					// Skip this packet
					available -= mExpected;
					mOffset += realSize;
					mExpected = 0;

					if (null != this.onReceivePack)
					{
						this.onReceivePack();
					}
				}
				else break;
			}
			return true;
		}
Exemple #3
0
	/// <summary>
	/// Copy the contents of this buffer into the target one, trimming away unused space.
	/// </summary>

	public void CopyTo (TNBuffer target)
	{
		BinaryWriter w = target.BeginWriting(false);
		int bytes = size;
		if (bytes > 0) w.Write(buffer, position, bytes);
		target.EndWriting();
	}