/// <summary> /// Validates buffer capacity before writing into it. /// </summary> /// <param name="nextValueLength">Length of next bytes sequence to write into buffer.</param> private void ValidateBufferSize(int nextValueLength) { if (m_Offset + nextValueLength > m_Buffer.Length) { L2Buffer.Extend(ref m_Buffer, nextValueLength + m_DefaultOverflowValue); } }
/// <summary> /// Writes array of <see cref="byte"/> into packet buffer. /// </summary> /// <param name="v">Array of <see cref="byte"/> values.</param> public unsafe void WriteBytesArray(byte[] v) { int length = v.Length; ValidateBufferSize(length); L2Buffer.Copy(v, 0, m_Buffer, m_Offset, length); m_Offset += length; }
/// <summary> /// Returns string representation of current packet. /// </summary> /// <returns>String representation of current packet.</returns> public override string ToString() { //System.Text.StringBuilder sb = new System.Text.StringBuilder(); //sb.AppendLine("Packet dump:"); //sb.AppendFormat("1s op: {0}{2}2d op: {1}{2}", FirstOpcode, SecondOpcode, Environment.NewLine); //sb.Append(L2Buffer.ToString(m_Buffer)); //return sb.ToString(); return(L2Buffer.ToString(m_Buffer)); }
/// <summary> /// Reads array of <see cref="byte"/> values from packet buffer. /// </summary> /// <param name="length">Length of array to read.</param> /// <returns>Array of <see cref="byte"/> values.</returns> public unsafe byte[] ReadBytesArray(int length) { byte[] dest = new byte[length]; fixed(byte *buf = m_Buffer, dst = dest) L2Buffer.Copy(buf, length, dst, ref m_Offset); return(dest); }
/// <summary> /// Writes array of <see cref="long"/> values into packet buffer. /// </summary> /// <param name="v">Array of <see cref="long"/> values.</param> public unsafe void WriteLong(params long[] v) { int length = v.Length * sizeof(long); ValidateBufferSize(length); fixed(byte *buf = m_Buffer) fixed(long *w = v) L2Buffer.UnsafeCopy(w, length, buf, ref m_Offset); }
/// <summary> /// Writes array of <see cref="double"/> values into packet buffer. /// </summary> /// <param name="v">Array of <see cref="double"/> values.</param> public unsafe void WriteDouble(params double[] v) { int length = v.Length * sizeof(double); ValidateBufferSize(length); fixed(byte *buf = m_Buffer) fixed(double *w = v) L2Buffer.UnsafeCopy(w, length, buf, ref m_Offset); }
/// <summary> /// Writes array of <see cref="int"/> values into packet buffer. /// </summary> /// <param name="v">Array of <see cref="int"/> values.</param> public unsafe void WriteInt(params int[] v) { int length = v.Length * sizeof(int); ValidateBufferSize(Length); fixed(byte *buf = m_Buffer) fixed(int *w = v) L2Buffer.UnsafeCopy(w, length, buf, ref m_Offset); }
/// <summary> /// Writes <see cref="string"/> object into packet buffer. /// </summary> /// <param name="s"><see cref="string"/> value.</param> public unsafe void WriteString(string s) { s += '\0'; int length = s.Length * sizeof(char); ValidateBufferSize(length); fixed(byte *buf = m_Buffer) fixed(char *w = s) L2Buffer.UnsafeCopy(w, length, buf, ref m_Offset); }
/// <summary> /// Writes array of <see cref="string"/> values to packet buffer. /// </summary> /// <param name="s">Array of <see cref="string"/> values.</param> public unsafe void WriteString(params string[] s) { string v = String.Empty; for (int i = 0; i < s.Length; i++) { v += s[i] + '\0'; } int length = v.Length * sizeof(char); ValidateBufferSize(length); fixed(byte *buf = m_Buffer) fixed(char *w = v) L2Buffer.UnsafeCopy(w, length, buf, ref m_Offset); }
/// <summary> /// Resizes <see cref="Packet"/> buffer to it's actual capacity and appends buffer length to the beginning of <see cref="Packet"/> buffer. /// </summary> /// <param name="headerSize"><see cref="Packet"/> header (opcodes) capacity.</param> public unsafe void Prepare(int headerSize) { m_Offset += headerSize; L2Buffer.Extend(ref m_Buffer, headerSize, m_Offset); fixed(byte *buf = m_Buffer) { if (headerSize == sizeof(short)) { *(( short * )(buf)) = ( short )m_Offset; } else { *(( int * )(buf)) = m_Offset; } } }
/// <summary> /// Returns packet buffer. /// </summary> /// <param name="skipFirstBytesCount">Amount of first bytes to skip.</param> /// <returns>Buffer without provided amount of first bytes.</returns> public byte[] GetBuffer(int skipFirstBytesCount) { return(L2Buffer.Copy(m_Buffer, skipFirstBytesCount, new byte[m_Buffer.Length - skipFirstBytesCount], 0, m_Buffer.Length - skipFirstBytesCount)); }
/// <summary> /// Reads <see cref="string"/> value from packet buffer. /// </summary> /// <returns><see cref="string"/> value.</returns> public unsafe string ReadString() { fixed(byte *buf = m_Buffer) return(L2Buffer.GetTrimmedString(buf, ref m_Offset, m_Buffer.Length)); }