Skip to content

Instantly share code, notes, and snippets.

@hugopeixoto
Created June 19, 2011 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugopeixoto/1034827 to your computer and use it in GitHub Desktop.
Save hugopeixoto/1034827 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ekaiyo.sources.networking
{
class Codec
{
#region Encoders
public static int Encode (byte[] a_buffer, int a_offset, byte a_value)
{
a_buffer[a_offset] = a_value;
return 1;
}
public static int Encode (byte[] a_buffer, int a_offset, float a_value)
{
BitConverter.GetBytes(a_value).CopyTo(a_buffer, a_offset);
return sizeof(float);
}
public static int Encode (byte[] a_buffer, int a_offset, bool a_value)
{
BitConverter.GetBytes(a_value).CopyTo(a_buffer, a_offset);
return sizeof(bool);
}
public static int Encode (byte[] a_buffer, int a_offset, string a_value)
{
Encoding.ASCII.GetBytes(a_value, 0, a_value.Length, a_buffer, a_offset);
return a_value.Length + 1;
}
public static int Encode (byte[] a_buffer, int a_offset, Microsoft.Xna.Framework.Matrix a_value)
{
BitConverter.GetBytes(a_value.M11).CopyTo(a_buffer, 0);
BitConverter.GetBytes(a_value.M12).CopyTo(a_buffer, 1 * sizeof(float));
BitConverter.GetBytes(a_value.M13).CopyTo(a_buffer, 2 * sizeof(float));
BitConverter.GetBytes(a_value.M14).CopyTo(a_buffer, 3 * sizeof(float));
BitConverter.GetBytes(a_value.M21).CopyTo(a_buffer, 4 * sizeof(float));
BitConverter.GetBytes(a_value.M22).CopyTo(a_buffer, 5 * sizeof(float));
BitConverter.GetBytes(a_value.M23).CopyTo(a_buffer, 6 * sizeof(float));
BitConverter.GetBytes(a_value.M24).CopyTo(a_buffer, 7 * sizeof(float));
BitConverter.GetBytes(a_value.M31).CopyTo(a_buffer, 8 * sizeof(float));
BitConverter.GetBytes(a_value.M32).CopyTo(a_buffer, 9 * sizeof(float));
BitConverter.GetBytes(a_value.M33).CopyTo(a_buffer, 10 * sizeof(float));
BitConverter.GetBytes(a_value.M34).CopyTo(a_buffer, 11 * sizeof(float));
BitConverter.GetBytes(a_value.M41).CopyTo(a_buffer, 12 * sizeof(float));
BitConverter.GetBytes(a_value.M42).CopyTo(a_buffer, 13 * sizeof(float));
BitConverter.GetBytes(a_value.M43).CopyTo(a_buffer, 14 * sizeof(float));
BitConverter.GetBytes(a_value.M44).CopyTo(a_buffer, 15 * sizeof(float));
return 16 * sizeof(float);
}
public static int Encode (byte[] a_buffer, int a_offset, Microsoft.Xna.Framework.Vector3 a_value)
{
BitConverter.GetBytes(a_value.X).CopyTo(a_buffer, 0);
BitConverter.GetBytes(a_value.Y).CopyTo(a_buffer, sizeof(float));
BitConverter.GetBytes(a_value.Z).CopyTo(a_buffer, 2 * sizeof(float));
return 3 * sizeof(float);
}
public static int Encode (byte[] a_buffer, int a_offset, UInt64 a_value)
{
BitConverter.GetBytes(a_value).CopyTo(a_buffer, a_offset);
return sizeof(UInt64);
}
public static int Encode (byte[] a_buffer, int a_offset, Int64 a_value)
{
BitConverter.GetBytes(a_value).CopyTo(a_buffer, a_offset);
return sizeof(Int64);
}
public static int Encode (byte[] a_buffer, int a_offset, UInt32 a_value)
{
BitConverter.GetBytes(a_value).CopyTo(a_buffer, a_offset);
return sizeof(UInt32);
}
public static int Encode (byte[] a_buffer, int a_offset, Int32 a_value)
{
BitConverter.GetBytes(a_value).CopyTo(a_buffer, a_offset);
return sizeof(Int32);
}
public static int Encode (byte[] a_buffer, int a_offset, UInt16 a_value)
{
BitConverter.GetBytes(a_value).CopyTo(a_buffer, a_offset);
return sizeof(UInt16);
}
public static int Encode (byte[] a_buffer, int a_offset, Int16 a_value)
{
BitConverter.GetBytes(a_value).CopyTo(a_buffer, a_offset);
return sizeof(Int16);
}
#endregion
#region Decoders
public static int Decode (byte[] a_buffer, int a_offset, ref byte a_value)
{
a_buffer[a_offset] = a_value;
return 1;
}
public static int Decode (byte[] a_buffer, int a_offset, ref byte a_value)
{
a_value = a_buffer[];
return 1;
}
public static int Decode (byte[] a_buffer, int a_offset, ref float a_value)
{
a_value = BitConverter.ToSingle(a_buffer, a_offset);
return sizeof(float);
}
public static int Decode (byte[] a_buffer, int a_offset, ref bool a_value)
{
a_value = BitConverter.ToBoolean(a_buffer, a_offset);
return sizeof(bool);
}
public static int Decode (byte[] a_buffer, int a_offset, ref string a_value)
{
int size = 0;
while (a_buffer[size++] != '\0') continue;
a_value = Encoding.ASCII.GetString(a_buffer, a_offset, size);
return size;
}
public static int Decode (byte[] a_buffer, int a_offset, ref Microsoft.Xna.Framework.Matrix a_value)
{
a_value = new Microsoft.Xna.Framework.Matrix(
BitConverter.ToSingle(a_buffer, a_offset + 0 * sizeof(float)),
BitConverter.ToSingle(a_buffer, a_offset + 1 * sizeof(float)),
BitConverter.ToSingle(a_buffer, a_offset + 2 * sizeof(float)),
BitConverter.ToSingle(a_buffer, a_offset + 3 * sizeof(float)),
BitConverter.ToSingle(a_buffer, a_offset + 4 * sizeof(float)),
BitConverter.ToSingle(a_buffer, a_offset + 5 * sizeof(float)),
BitConverter.ToSingle(a_buffer, a_offset + 6 * sizeof(float)),
BitConverter.ToSingle(a_buffer, a_offset + 7 * sizeof(float)),
BitConverter.ToSingle(a_buffer, a_offset + 8 * sizeof(float)),
BitConverter.ToSingle(a_buffer, a_offset + 9 * sizeof(float)),
BitConverter.ToSingle(a_buffer, a_offset + 10 * sizeof(float)),
BitConverter.ToSingle(a_buffer, a_offset + 11 * sizeof(float)),
BitConverter.ToSingle(a_buffer, a_offset + 12 * sizeof(float)),
BitConverter.ToSingle(a_buffer, a_offset + 13 * sizeof(float)),
BitConverter.ToSingle(a_buffer, a_offset + 14 * sizeof(float)),
BitConverter.ToSingle(a_buffer, a_offset + 15 * sizeof(float)));
return 16 * sizeof(float);
}
public static int Decode (byte[] a_buffer, int a_offset, ref Microsoft.Xna.Framework.Vector3 a_value)
{
a_value = new Microsoft.Xna.Framework.Vector3(
BitConverter.ToSingle(a_buffer, a_offset + 0 * sizeof(float)),
BitConverter.ToSingle(a_buffer, a_offset + 1 * sizeof(float)),
BitConverter.ToSingle(a_buffer, a_offset + 2 * sizeof(float)));
return 3 * sizeof(float);
}
public static int Decode (byte[] a_buffer, int a_offset, ref UInt64 a_value)
{
a_value = BitConverter.ToUInt64(a_buffer, a_offset);
return sizeof(UInt64);
}
public static int Decode (byte[] a_buffer, int a_offset, ref Int64 a_value)
{
a_value = BitConverter.ToInt64(a_buffer, a_offset);
return sizeof(Int64);
}
public static int Decode (byte[] a_buffer, int a_offset, ref UInt32 a_value)
{
a_value = BitConverter.ToUInt32(a_buffer, a_offset);
return sizeof(UInt32);
}
public static int Decode (byte[] a_buffer, int a_offset, ref Int32 a_value)
{
a_value = BitConverter.ToInt32(a_buffer, a_offset);
return sizeof(Int32);
}
public static int Decode (byte[] a_buffer, int a_offset, ref UInt16 a_value)
{
a_value = BitConverter.ToUInt16(a_buffer, a_offset);
return sizeof(UInt16);
}
public static int Decode (byte[] a_buffer, int a_offset, ref Int16 a_value)
{
a_value = BitConverter.ToInt16(a_buffer, a_offset);
return sizeof(Int16);
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment