using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
namespace LibCommon
{
public class PacketBase
{
public EPacketDefs packetDefs = EPacketDefs.EInvalid;
protected Dictionary< int , PropertyInfo> dProperties = new Dictionary< int , PropertyInfo>();
public PacketBase()
{
var targetType = GetType();
var apropty = targetType.GetProperties();
foreach (PropertyInfo pi in apropty)
{
var attr = pi.GetCustomAttributes<ProtoMemberAttribute>();
foreach (ProtoMemberAttribute proto in attr)
{
int index = proto.Tag;
RegistProperty(index, pi);
}
}
}
private void RegistProperty( int index, PropertyInfo pi)
{
dProperties.Add(index, pi);
}
public void DoRead( byte [] bytes)
{
MemoryStream ms = new MemoryStream(bytes);
BinaryReader br = new BinaryReader(ms);
ms.Position = 0;
while (ms.Position < ms.Length)
{
int index = br.ReadInt32();
if (index < 1)
{
break ;
}
if (!dProperties.ContainsKey(index))
{
continue ;
}
if (dProperties[index].PropertyType == typeof ( string ))
{
dProperties[index].SetValue( this , br.ReadString());
}
else if (dProperties[index].PropertyType == typeof ( int ))
{
dProperties[index].SetValue( this , br.ReadInt32());
}
else if (dProperties[index].PropertyType == typeof ( long ))
{
dProperties[index].SetValue( this , br.ReadInt64());
}
else if (dProperties[index].PropertyType == typeof ( float ))
{
dProperties[index].SetValue( this , br.ReadSingle());
}
else if (dProperties[index].PropertyType == typeof ( double ))
{
dProperties[index].SetValue( this , br.ReadDouble());
}
else if (dProperties[index].PropertyType == typeof (List< string >))
{
List< string > lString = new List< string >();
int count = br.ReadInt32();
for ( int i = 0; i < count; i++)
{
lString.Add(br.ReadString());
}
dProperties[index].SetValue( this , lString);
}
else if (dProperties[index].PropertyType == typeof (List< int >))
{
List< int > lValue = new List< int >();
int count = br.ReadInt32();
for ( int i = 0; i < count; i++)
{
lValue.Add(br.ReadInt32());
}
dProperties[index].SetValue( this , lValue);
}
else if (dProperties[index].PropertyType == typeof (List< long >))
{
List<Int64> lValue = new List<Int64>();
int count = br.ReadInt32();
for ( int i = 0; i < count; i++)
{
lValue.Add(br.ReadInt64());
}
dProperties[index].SetValue( this , lValue);
}
else if (dProperties[index].PropertyType == typeof (List< float >))
{
List< float > lValue = new List< float >();
int count = br.ReadInt32();
for ( int i = 0; i < count; i++)
{
lValue.Add(br.ReadSingle());
}
dProperties[index].SetValue( this , lValue);
}
else if (dProperties[index].PropertyType == typeof (List< double >))
{
List< double > lValue = new List< double >();
int count = br.ReadInt32();
for ( int i = 0; i < count; i++)
{
lValue.Add(br.ReadDouble());
}
dProperties[index].SetValue( this , lValue);
}
}
}
public byte [] DoWrite()
{
MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);
var targetType = GetType();
var apropty = targetType.GetProperties();
foreach (PropertyInfo pi in apropty)
{
var attr = pi.GetCustomAttributes<ProtoMemberAttribute>();
foreach (ProtoMemberAttribute proto in attr)
{
int index = proto.Tag;
if (dProperties.ContainsKey(index))
{
bw.Write(index);
if (dProperties[index].PropertyType == typeof ( string ))
{
bw.Write(( string )dProperties[index].GetValue( this ));
}
else if (dProperties[index].PropertyType == typeof ( int ))
{
bw.Write(( int )dProperties[index].GetValue( this ));
}
else if (dProperties[index].PropertyType == typeof ( long ))
{
bw.Write(( long )dProperties[index].GetValue( this ));
}
else if (dProperties[index].PropertyType == typeof ( float ))
{
bw.Write(( float )dProperties[index].GetValue( this ));
}
else if (dProperties[index].PropertyType == typeof ( double ))
{
bw.Write(( double )dProperties[index].GetValue( this ));
}
else if (dProperties[index].PropertyType == typeof (List< string >))
{
List< string > lValue = dProperties[index].GetValue( this ) as List< string >;
int count = lValue.Count;
bw.Write(count);
for ( int i = 0; i < count; i++)
{
bw.Write(lValue);
}
}
else if (dProperties[index].PropertyType == typeof (List< int >))
{
List< int > lValue = dProperties[index].GetValue( this ) as List< int >;
int count = lValue.Count;
bw.Write(count);
for ( int i = 0; i < count; i++)
{
bw.Write(lValue);
}
}
else if (dProperties[index].PropertyType == typeof (List< long >))
{
List< long > lValue = dProperties[index].GetValue( this ) as List< long >;
int count = lValue.Count;
bw.Write(count);
for ( int i = 0; i < count; i++)
{
bw.Write(lValue);
}
}
else if (dProperties[index].PropertyType == typeof (List< float >))
{
List< float > lValue = dProperties[index].GetValue( this ) as List< float >;
int count = lValue.Count;
bw.Write(count);
for ( int i = 0; i < count; i++)
{
bw.Write(lValue);
}
}
else if (dProperties[index].PropertyType == typeof (List< double >))
{
List< double > lValue = dProperties[index].GetValue( this ) as List< double >;
int count = lValue.Count;
bw.Write(count);
for ( int i = 0; i < count; i++)
{
bw.Write(lValue);
}
}
}
}
}
byte [] result = new byte [ms.Position];
Array.ConstrainedCopy(ms.GetBuffer(), 0, result, 0, ( int )ms.Position);
return result;
}
}
}
|