You've already forked lidgren-network-gen3
mirror of
https://github.com/lidgren/lidgren-network-gen3.git
synced 2026-05-16 07:06:30 +09:00
NetIncomingMessage and NetOutgoingMessage common parts unified in NetBuffer; which is now a base class for the former
This commit is contained in:
103
Lidgren.Network/NetBuffer.cs
Normal file
103
Lidgren.Network/NetBuffer.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Lidgren.Network
|
||||
{
|
||||
public partial class NetBuffer
|
||||
{
|
||||
// @TODO Add ReadTime() to incomingmessage class
|
||||
private const int c_overAllocateAmount = 4;
|
||||
|
||||
private static readonly Dictionary<Type, MethodInfo> s_readMethods;
|
||||
private static readonly Dictionary<Type, MethodInfo> s_writeMethods;
|
||||
|
||||
internal byte[] m_data;
|
||||
internal int m_bitLength;
|
||||
internal int m_readPosition;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the internal data buffer
|
||||
/// </summary>
|
||||
public byte[] Data
|
||||
{
|
||||
get { return m_data; }
|
||||
set { m_data = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the length of the used portion of the buffer in bytes
|
||||
/// </summary>
|
||||
public int LengthBytes
|
||||
{
|
||||
get { return ((m_bitLength + 7) >> 3); }
|
||||
set
|
||||
{
|
||||
m_bitLength = value * 8;
|
||||
InternalEnsureBufferSize(m_bitLength);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the length of the used portion of the buffer in bits
|
||||
/// </summary>
|
||||
public int LengthBits
|
||||
{
|
||||
get { return m_bitLength; }
|
||||
set
|
||||
{
|
||||
m_bitLength = value;
|
||||
InternalEnsureBufferSize(m_bitLength);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the read position in the buffer, in bits (not bytes)
|
||||
/// </summary>
|
||||
public long Position
|
||||
{
|
||||
get { return (long)m_readPosition; }
|
||||
set { m_readPosition = (int)value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the position in the buffer in bytes; note that the bits of the first returned byte may already have been read - check the Position property to make sure.
|
||||
/// </summary>
|
||||
public int PositionInBytes
|
||||
{
|
||||
get { return (int)(m_readPosition / 8); }
|
||||
}
|
||||
|
||||
static NetBuffer()
|
||||
{
|
||||
Type[] integralTypes = typeof(Byte).Assembly.GetTypes();
|
||||
|
||||
s_readMethods = new Dictionary<Type, MethodInfo>();
|
||||
MethodInfo[] methods = typeof(NetIncomingMessage).GetMethods(BindingFlags.Instance | BindingFlags.Public);
|
||||
foreach (MethodInfo mi in methods)
|
||||
{
|
||||
if (mi.GetParameters().Length == 0 && mi.Name.StartsWith("Read", StringComparison.InvariantCulture))
|
||||
{
|
||||
string n = mi.Name.Substring(4);
|
||||
foreach (Type it in integralTypes)
|
||||
{
|
||||
if (it.Name == n)
|
||||
s_readMethods[it] = mi;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s_writeMethods = new Dictionary<Type, MethodInfo>();
|
||||
methods = typeof(NetOutgoingMessage).GetMethods(BindingFlags.Instance | BindingFlags.Public);
|
||||
foreach (MethodInfo mi in methods)
|
||||
{
|
||||
if (mi.Name.Equals("Write", StringComparison.InvariantCulture))
|
||||
{
|
||||
ParameterInfo[] pis = mi.GetParameters();
|
||||
if (pis.Length == 1)
|
||||
s_writeMethods[pis[0].ParameterType] = mi;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user