Created by: Andrew
This commit is contained in:
Andrew
2016-08-27 22:43:23 -04:00
commit 6d74d5b59e
31 changed files with 3002 additions and 0 deletions

View File

@@ -0,0 +1,151 @@
using System;
using System.IO;
namespace KartExtreme.IO.Packet
{
public class InPacket : PacketBase
{
private readonly byte[] _buffer;
private int _index;
public override int Position
{
get { return _index; }
}
public override int Length
{
get { return _buffer.Length; }
}
public int Available
{
get
{
return _buffer.Length - _index;
}
}
public InPacket(byte[] packet)
{
_buffer = packet;
_index = 0;
}
private void CheckLength(int length)
{
if (_index + length > _buffer.Length || length < 0)
throw new PacketReadException("Not enough space");
}
public bool ReadBool()
{
return ReadByte() == 1;
}
public byte ReadByte()
{
CheckLength(1);
return _buffer[_index++];
}
public sbyte ReadSByte()
{
return (sbyte)ReadByte();
}
public byte[] ReadBytes(int count)
{
CheckLength(count);
var temp = new byte[count];
Buffer.BlockCopy(_buffer, _index, temp, 0, count);
_index += count;
return temp;
}
public unsafe short ReadShort()
{
CheckLength(2);
short value;
fixed (byte* ptr = _buffer)
{
value = *(short*)(ptr + _index);
}
_index += 2;
return value;
}
public ushort ReadUShort()
{
return (ushort)ReadShort();
}
public unsafe int ReadInt()
{
CheckLength(4);
int value;
fixed (byte* ptr = _buffer)
{
value = *(int*)(ptr + _index);
}
_index += 4;
return value;
}
public uint ReadUInt()
{
return (uint)ReadInt();
}
public unsafe long ReadLong()
{
CheckLength(8);
long value;
fixed (byte* ptr = _buffer)
{
value = *(long*)(ptr + _index);
}
_index += 8;
return value;
}
public ulong ReadULong()
{
return (ulong)ReadLong();
}
public string ReadString(int count)
{
CheckLength(count);
char[] final = new char[count];
for (int i = 0; i < count; i++)
{
final[i] = (char)ReadByte();
}
return new string(final);
}
public void Skip(int count)
{
CheckLength(count);
_index += count;
}
public override byte[] ToArray()
{
var final = new byte[_buffer.Length];
Buffer.BlockCopy(_buffer, 0, final, 0, _buffer.Length);
return final;
}
}
}

View File

@@ -0,0 +1,167 @@
using System;
using System.Globalization;
using System.IO;
using System.Text;
namespace KartExtreme.IO.Packet
{
public class OutPacket : PacketBase, IDisposable
{
private MemoryStream m_stream;
private bool m_disposed;
public override int Length
{
get { return (int)m_stream.Position; }
}
public override int Position
{
get { return (int)m_stream.Position; }
}
public bool Disposed
{
get
{
return m_disposed;
}
}
public OutPacket(int size = 64)
{
m_stream = new MemoryStream(size);
m_disposed = false;
}
//From LittleEndianByteConverter by Shoftee
private void Append(long value, int byteCount)
{
for (int i = 0; i < byteCount; i++)
{
m_stream.WriteByte((byte)value);
value >>= 8;
}
}
public void WriteBool(bool value)
{
ThrowIfDisposed();
WriteByte(value ? (byte)1 : (byte)0);
}
public void WriteByte(byte value = 0)
{
ThrowIfDisposed();
m_stream.WriteByte(value);
}
public void WriteSByte(sbyte value = 0)
{
WriteByte((byte)value);
}
public void WriteBytes(params byte[] value)
{
ThrowIfDisposed();
m_stream.Write(value, 0, value.Length);
}
public void WriteShort(short value = 0)
{
ThrowIfDisposed();
Append(value, 2);
}
public void WriteUShort(ushort value = 0)
{
WriteShort((short)value);
}
public void WriteInt(int value = 0)
{
ThrowIfDisposed();
Append(value, 4);
}
public void WriteUInt(uint value = 0)
{
WriteInt((int)value);
}
public void WriteLong(long value = 0)
{
ThrowIfDisposed();
Append(value, 8);
}
public void WriteULong(ulong value = 0)
{
WriteLong((long)value);
}
public void WriteString(string value)
{
if (value == null)
throw new ArgumentNullException("value");
WriteInt(value.Length);
WriteString(value, value.Length);
}
public void WriteString(string value, int length)
{
if (value == null ||
length < 1 ||
length > value.Length)
throw new ArgumentNullException("value");
var bytes = Encoding.Unicode.GetBytes(value);
var i = 0;
for (; i < value.Length & i < length; i++)
{
var offset = i * 2;
this.WriteByte(bytes[offset]);
this.WriteByte(bytes[offset + 1]);
}
for (; i < length; i++)
{
this.WriteShort();
}
}
public void WriteHexString(string value)
{
if (value == null)
throw new ArgumentNullException("value");
value = value.Replace(" ", "");
for (int i = 0; i < value.Length; i += 2)
{
WriteByte(byte.Parse(value.Substring(i, 2), NumberStyles.HexNumber));
}
}
private void ThrowIfDisposed()
{
if (m_disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
}
public override byte[] ToArray()
{
ThrowIfDisposed();
return m_stream.ToArray();
}
public void Dispose()
{
m_disposed = true;
if (m_stream != null)
m_stream.Dispose();
m_stream = null;
}
}
}

View File

@@ -0,0 +1,25 @@
using System.IO;
using System.Text;
namespace KartExtreme.IO.Packet
{
public abstract class PacketBase
{
public abstract int Length { get; }
public abstract int Position { get; }
public abstract byte[] ToArray();
public override string ToString()
{
var sb = new StringBuilder();
foreach (byte b in this.ToArray())
{
sb.AppendFormat("{0:X2} ", b);
}
return sb.ToString();
}
}
}

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KartExtreme.IO.Packet
{
public sealed class PacketReadException : Exception
{
public PacketReadException(string message)
: base(message)
{
}
}
}