Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Packet helpers
- #region To Byte Array
- /// <summary>
- /// Converts a byte value to a byte array
- /// </summary>
- /// <param name="b">input value</param>
- /// <returns>byte array output</returns>
- public static byte[] MakeByte(byte b)
- {
- return new byte[] { b };
- }
- /// <summary>
- /// Converts a short value to a byte array
- /// </summary>
- /// <param name="s">input value</param>
- /// <returns>byte array output</returns>
- public static byte[] MakeShort(short s)
- {
- return BitConverter.GetBytes(s);
- }
- /// <summary>
- /// Converts a ushort value to a byte array
- /// </summary>
- /// <param name="s">input value</param>
- /// <returns>byte array output</returns>
- public static byte[] MakeUshort(ushort s)
- {
- return BitConverter.GetBytes(s);
- }
- /// <summary>
- /// Converts a int value to a byte array
- /// </summary>
- /// <param name="i">input value</param>
- /// <returns>byte array output</returns>
- public static byte[] MakeInt(int i)
- {
- return BitConverter.GetBytes(i);
- }
- /// <summary>
- /// Converts a long value to a byte array
- /// </summary>
- /// <param name="l">input value</param>
- /// <returns>byte array output</returns>
- public static byte[] MakeLong(long l)
- {
- return BitConverter.GetBytes(l);
- }
- /// <summary>
- /// Converts a float value to a byte array
- /// </summary>
- /// <param name="f">input value</param>
- /// <returns>byte array output</returns>
- public static byte[] MakeFloat(float f)
- {
- return BitConverter.GetBytes(f);
- }
- /// <summary>
- /// Converts a double value to a byte array
- /// </summary>
- /// <param name="d">input value</param>
- /// <returns>byte array output</returns>
- public static byte[] MakeDouble(double d)
- {
- return BitConverter.GetBytes(d);
- }
- /// <summary>
- /// Converts a string value to a byte array ( adds an integer header for it's length )
- /// Uses Unicode
- /// </summary>
- /// <param name="s">input value</param>
- /// <returns>byte array output</returns>
- public static byte[] MakeString(string s)
- {
- List<byte> data = new List<byte>();
- data.AddRange(MakeInt(Encoding.Unicode.GetByteCount(s)));
- data.AddRange(Encoding.Unicode.GetBytes(s));
- return data.ToArray();
- }
- /// <summary>
- /// Converts a boolean value to a byte array
- /// </summary>
- /// <param name="b">input value</param>
- /// <returns>byte array output</returns>
- public static byte[] MakeBoolean(bool b)
- {
- return (b) ? new byte[] { 0x01 } : new byte[] { 0x00 };
- }
- /// <summary>
- /// Converts a byte array to a better byte array... deal with it
- /// </summary>
- /// <param name="b">input array</param>
- /// <returns>byte array output ( this time with size-int prefix )</returns>
- public static byte[] MakeByteArray(byte[] b)
- {
- List<byte> data = new List<byte>();
- data.AddRange(MakeInt(b.Length));
- data.AddRange(b);
- return data.ToArray();
- }
- #endregion
- #region From Byte Array
- /// <summary>
- /// Reads a byte value from the client's stream
- /// </summary>
- /// <param name="client">Networking client</param>
- /// <returns>byte value</returns>
- public static byte ReadByte(TcpClient client)
- {
- byte[] data = new byte[1];
- client.GetStream().Read(data, 0, 1);
- return data[0];
- }
- /// <summary>
- /// Reads a short value from the client's stream
- /// </summary>
- /// <param name="client">Networking client</param>
- /// <returns>short vlaue</returns>
- public static short ReadShort(TcpClient client)
- {
- byte[] data = new byte[2];
- client.GetStream().Read(data, 0, 2);
- return BitConverter.ToInt16(data, 0);
- }
- /// <summary>
- /// Reads a ushort value from the client's stream
- /// </summary>
- /// <param name="client">Networking client</param>
- /// <returns>ushort vlaue</returns>
- public static ushort ReadUshort(TcpClient client)
- {
- byte[] data = new byte[2];
- client.GetStream().Read(data, 0, 2);
- return BitConverter.ToUInt16(data, 0);
- }
- /// <summary>
- /// Reads an integer value from the client's stream
- /// </summary>
- /// <param name="client">Networking client</param>
- /// <returns>int value</returns>
- public static int ReadInt(TcpClient client)
- {
- byte[] data = new byte[4];
- client.GetStream().Read(data, 0, 4);
- return BitConverter.ToInt32(data, 0);
- }
- /// <summary>
- /// Reads a long value from the client's stream
- /// </summary>
- /// <param name="client">Networking client</param>
- /// <returns>long value</returns>
- public static long ReadLong(TcpClient client)
- {
- byte[] data = new byte[8];
- client.GetStream().Read(data, 0, 8);
- return BitConverter.ToInt64(data, 0);
- }
- /// <summary>
- /// Reads a float value from the client's stream
- /// </summary>
- /// <param name="client">Networking client</param>
- /// <returns>float value</returns>
- public static float ReadFloat(TcpClient client)
- {
- byte[] data = new byte[4];
- client.GetStream().Read(data, 0, 4);
- return BitConverter.ToSingle(data, 0);
- }
- /// <summary>
- /// Reads a double value from the client's stream
- /// </summary>
- /// <param name="client">Networking client</param>
- /// <returns>double value</returns>
- public static double ReadDouble(TcpClient client)
- {
- byte[] data = new byte[8];
- client.GetStream().Read(data, 0, 8);
- return BitConverter.ToDouble(data, 0);
- }
- /// <summary>
- /// Reads a string value from the client's stream
- /// </summary>
- /// <param name="client">Networking client</param>
- /// <returns>string value</returns>
- public static string ReadString(TcpClient client)
- {
- byte[] data = new byte[ReadInt(client)];
- client.GetStream().Read(data, 0, data.Length);
- return Encoding.Unicode.GetString(data);
- }
- /// <summary>
- /// Reads a boolean from the client's stream
- /// </summary>
- /// <param name="client">Networking client</param>
- /// <returns>boolean value</returns>
- public static bool ReadBool(TcpClient client)
- {
- byte[] data = new byte[1];
- client.GetStream().Read(data, 0, 1);
- if (data[0] == 0x01 || data[0] == 0x00)
- {
- return (data[0] == 0x01);
- }
- else
- {
- throw new Exception("invalid boolean value for parsing!");
- }
- }
- /// <summary>
- /// Reads a byte array from the client's stream
- /// </summary>
- /// <param name="client">Networking client</param>
- /// <returns>byte array value</returns>
- public static byte[] ReadByteArray(TcpClient client)
- {
- byte[] data = new byte[ReadInt(client)];
- client.GetStream().Read(data, 0, data.Length);
- return data;
- }
- #endregion
- #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement