First, stop handling packets like strings, that's outdated vb6.
I had this source laying around, it's kinda old tho but should work fine too.
Code:
Public Class phPacket
Implements IDisposable
#Region "-----Field/Propertys-----"
''' <summary>
''' The Opcode as HexWord
''' </summary>
Public Property OpcodeHEX() As String
Get
Return m_Opcode.ToString("X2")
End Get
Set
m_Opcode = Convert.ToUInt16(value, 16)
End Set
End Property
''' <summary>
''' The Opcode as Ushort
''' </summary>
Public Property Opcode() As UShort
Get
Return m_Opcode
End Get
Set
m_Opcode = value
End Set
End Property
Private m_Opcode As UShort
Private m_SecurityCount As Byte
Public Property SecurityCount() As Byte
Get
Return m_SecurityCount
End Get
Set
m_SecurityCount = value
End Set
End Property
Private m_SecurityCrc As Byte
Public Property SecurityCrc() As Byte
Get
Return m_SecurityCrc
End Get
Set
m_SecurityCrc = value
End Set
End Property
''' <summary>
''' Current pointer position
''' </summary>
Public Property Pointer() As Integer
Get
Return m_Pointer
End Get
Set
m_Pointer = value
End Set
End Property
Private m_Pointer As Integer
''' <summary>
''' Packet Length
''' </summary>
Public Property Length() As Integer
Get
Return m_Length
End Get
Set
m_Length = value
End Set
End Property
Private m_Length As Integer
''' <summary>
''' BufferSize : Default 8192
''' </summary>
Public Property BufferSize() As Integer
Get
Return m_BufferSize
End Get
Set
m_BufferSize = value
Array.Resize(Of Byte)(m_Buffer, value)
End Set
End Property
Private m_BufferSize As Integer = 8192
'Silkroad Default: 8192
''' <summary>
''' Data Buffer
''' </summary>
Public Property Buffer() As Byte()
Get
Return m_Buffer
End Get
Set
m_Buffer = value
End Set
End Property
Private m_Buffer As Byte()
#End Region
#Region "-----Constructor-----"
''' <summary>
''' Creates an instance of cPacket using only the received bytes from client.
''' </summary>
''' <param name="buffer">The Bytearray received from client.</param>
Public Sub New(buffer As Byte())
m_Length = BitConverter.ToUInt16(buffer, 0)
m_Opcode = BitConverter.ToUInt16(buffer, 2)
m_SecurityCount = buffer(4)
m_SecurityCrc = buffer(5)
m_Buffer = New [Byte](m_BufferSize - 1) {}
If m_Length > 0 Then
Array.ConstrainedCopy(buffer, 6, m_Buffer, 0, m_Length)
End If
End Sub
''' <summary>
''' Creates an empty instance of cPacket using only the Opcode.
''' </summary>
''' <param name="Opcode">Opcode as ushort.</param>
Public Sub New(Opcode As UShort)
m_Opcode = Opcode
m_Buffer = New [Byte](m_BufferSize - 1) {}
End Sub
''' <summary>
''' Creates an empty instance of cPacket using only the Opcode.
''' </summary>
''' <param name="Opcode">Opcode as string.</param>
Public Sub New(Opcode As String)
m_Opcode = Convert.ToUInt16(Opcode, 16)
m_Buffer = New [Byte](m_BufferSize - 1) {}
End Sub
''' <summary>
''' Creates an intance of cPacket using the Opcode and Data(Buffer)
''' </summary>
''' <param name="Opcode">Opcode as ushort.</param>
''' <param name="Buffer">Data(Buffer) as Bytearray.</param>
Public Sub New(Opcode As UShort, Buffer As [Byte]())
m_Opcode = Opcode
m_Buffer = Buffer
End Sub
''' <summary>
''' Creates an intance of cPacket using the Opcode and Data(Buffer)
''' </summary>
''' <param name="Opcode">Opcode as ushort.</param>
''' <param name="Buffer">Data(Buffer) as HexString.</param>
Public Sub New(Opcode As UShort, Buffer As String)
m_Opcode = Opcode
m_Buffer = New [Byte](m_BufferSize - 1) {}
AddHex(Buffer)
End Sub
''' <summary>
''' Creates an intance of cPacket using the Opcode and Data(Buffer)
''' </summary>
''' <param name="Opcode">Opcode as string.</param>
''' <param name="Buffer">Data(Buffer) as Bytearray.</param>
Public Sub New(Opcode As String, Buffer As [Byte]())
m_Opcode = Convert.ToUInt16(Opcode, 16)
m_Buffer = Buffer
End Sub
''' <summary>
''' Creates an intance of cPacket using the Opcode and Data(Buffer)
''' </summary>
''' <param name="Opcode">Opcode as string.</param>
''' <param name="Buffer">Data(Buffer) as HexString.</param>
Public Sub New(Opcode As String, Buffer As String)
m_Opcode = Convert.ToUInt16(Opcode, 16)
m_Buffer = New [Byte](m_BufferSize - 1) {}
AddHex(Buffer)
End Sub
#End Region
#Region "-----Read-----"
''' <summary>
''' Reads a boolean value from buffer and increments the Pointer by 1.
''' </summary>
''' <returns>Returns the read byte as boolean.</returns>
Public Function ReadBool() As Boolean
'Increase Pointer
m_Pointer += 1
Return Convert.ToBoolean(m_Buffer(m_Pointer - 1))
End Function
''' <summary>
''' Reads a char value from buffer and increments the Pointer by 1.
''' </summary>
''' <returns>Returns the read byte as char.</returns>
Public Function ReadChar() As Char
'Increase Pointer
m_Pointer += 1
Return Convert.ToChar(m_Buffer(m_Pointer - 1))
End Function
''' <summary>
''' Reads a byte value from buffer and increments the Pointer by 1.
''' </summary>
''' <returns>the read byte.</returns>
Public Function ReadByte() As Byte
'Increase Pointer
m_Pointer += 1
Return m_Buffer(m_Pointer - 1)
End Function
''' <summary>
''' Reads a bytearray value from buffer and increments the Pointer by count.
''' </summary>
''' <param name="count">Number of bytes to read.</param>
''' <returns>the read bytearray.</returns>
Public Function ReadByte(count As Integer) As Byte()
'Increase Pointer
m_Pointer += count
'Copy to tempBuffer
Dim tempBuffer As Byte() = New Byte(count - 1) {}
Array.ConstrainedCopy(m_Buffer, m_Pointer - count, tempBuffer, 0, count)
Return tempBuffer
End Function
''' <summary>
''' Reads an ushort from buffer and increments the Pointer by 2.
''' </summary>
''' <returns>the read ushort.</returns>
Public Function ReadUShort() As UShort
'Increase Pointer
m_Pointer += 2
Return BitConverter.ToUInt16(m_Buffer, m_Pointer - 2)
End Function
''' <summary>
''' Reads a short from buffer and increments the Pointer by 2.
''' </summary>
''' <returns>the read short.</returns>
Public Function ReadShort() As Short
'Increase Pointer
m_Pointer += 2
Return BitConverter.ToInt16(m_Buffer, m_Pointer - 2)
End Function
''' <summary>
''' Reads an uinteger from buffer and increments the Pointer by 4.
''' </summary>
''' <returns>the read uinteger.</returns>
Public Function ReadUInteger() As UInteger
'Increase Pointer
m_Pointer += 4
Return BitConverter.ToUInt32(m_Buffer, m_Pointer - 4)
End Function
''' <summary>
''' Reads an integer from buffer and increments the Pointer by 4.
''' </summary>
''' <returns>the read integer.</returns>
Public Function ReadInteger() As Integer
'Increase Pointer
m_Pointer += 4
Return BitConverter.ToInt32(m_Buffer, m_Pointer - 4)
End Function
''' <summary>
''' Reads an ulong from buffer and increments the Pointer by 8.
''' </summary>
''' <returns>the read ulong.</returns>
Public Function ReadULong() As ULong
'Increase Pointer
m_Pointer += 8
Return BitConverter.ToUInt64(m_Buffer, m_Pointer - 8)
End Function
''' <summary>
''' Reads a long from buffer and increments the Pointer by 8.
''' </summary>
''' <returns>the read long.</returns>
Public Function ReadLong() As Long
'Increase Pointer
m_Pointer += 8
Return BitConverter.ToInt64(m_Buffer, m_Pointer - 8)
End Function
''' <summary>
''' Reads a float from buffer and increments the Pointer by 4.
''' </summary>
''' <returns>the read float.</returns>
Public Function ReadFloat() As Single
'Increase Pointer
m_Pointer += 4
Return BitConverter.ToSingle(m_Buffer, m_Pointer - 4)
End Function
''' <summary>
''' Reads the stringlength + ansi-string from buffer and increments the Pointer by 4 + stringlength.
''' </summary>
''' <returns>the read ANSI-String.</returns>
Public Function ReadString() As String
Dim length As Integer = ReadUShort()
'Increase Pointer
m_Pointer += length
Return Encoding.[Default].GetString(m_Buffer, m_Pointer - length, length)
End Function
''' <summary>
''' Reads an ansi-string from buffer using the given length and increments the Pointer by length.
''' </summary>
''' <returns>the read ANSI-String.</returns>
Public Function ReadString(length As Integer) As String
'Increase Pointer
m_Pointer += length
Return Encoding.[Default].GetString(m_Buffer, m_Pointer - length, length)
End Function
''' <summary>
''' Reads the stringlength + unicode string from buffer and increments the Pointer by 4 + stringlength.
''' </summary>
''' <returns>the read Unicode-String.</returns>
Public Function ReadUnicodeString() As String
Dim length As Integer = ReadUShort()
'Increase Pointer
m_Pointer += length
Return Encoding.Unicode.GetString(m_Buffer, m_Pointer - length, length)
End Function
''' <summary>
''' Reads an Unicode-string from buffer using the given length and increments the Pointer by length.
''' </summary>
''' <returns>the read Unicode-String.</returns>
Public Function ReadUnicodeString(length As Integer) As String
length *= 2
'Increase Pointer
m_Pointer += length
Return Encoding.Unicode.GetString(m_Buffer, m_Pointer - length, length)
End Function
#End Region
#Region "-----Add-----"
''' <summary>
''' Adds a boolean to the buffer and increments the length by 1.
''' </summary>
''' <param name="value">Boolean to add.</param>
Public Sub AddBool(value As Boolean)
m_Length += 1
m_Buffer(m_Length - 1) = Convert.ToByte(value)
End Sub
Public Sub AddBool(value As Object)
m_Length += 1
m_Buffer(m_Length - 1) = Convert.ToByte(value)
End Sub
''' <summary>
''' Adds a char to the buffer and increments the length by 1.
''' </summary>
''' <param name="value">Char to add.</param>
Public Sub AddChar(value As Char)
m_Buffer(m_Length) = Convert.ToByte(value)
m_Length += 1
End Sub
Public Sub AddChar(value As Object)
m_Buffer(m_Length) = Convert.ToByte(value)
m_Length += 1
End Sub
''' <summary>
''' Adds a byte to the buffer and increments the length by 1.
''' </summary>
''' <param name="value">Byte to add.</param>
Public Sub AddByte(value As Byte)
m_Buffer(m_Length) = value
m_Length += 1
End Sub
Public Sub AddByte(value As Object)
m_Buffer(m_Length) = Convert.ToByte(value)
m_Length += 1
End Sub
''' <summary>
''' Adds a Bytearray to the buffer and increments the length by Bytearray length.
''' </summary>
''' <param name="value">Bytearray to add.</param>
Public Sub AddByte(value As Byte())
'? What is faster CopyTo or ConstrainedCopy ?
'! Test done, result ConstrainedCopy
Array.ConstrainedCopy(value, 0, m_Buffer, m_Length, value.Length)
'value.CopyTo(pBuffer, pLength);
m_Length += value.Length
End Sub
''' <summary>
''' Adds an ushort to the buffer and increments the length by 2.
''' </summary>
''' <param name="value">Ushort to add.</param>
Public Sub AddUShort(value As UShort)
Array.ConstrainedCopy(BitConverter.GetBytes(value), 0, m_Buffer, m_Length, 2)
m_Length += 2
End Sub
Public Sub AddUShort(value As Object)
Array.ConstrainedCopy(BitConverter.GetBytes(Convert.ToUInt16(value)), 0, m_Buffer, m_Length, 2)
m_Length += 2
End Sub
''' <summary>
''' Adds a short to the buffer and increments the length by 2.
''' </summary>
''' <param name="value">Short to add.</param>
Public Sub AddShort(value As Short)
Array.ConstrainedCopy(BitConverter.GetBytes(value), 0, m_Buffer, m_Length, 2)
m_Length += 2
End Sub
Public Sub AddShort(value As Object)
Array.ConstrainedCopy(BitConverter.GetBytes(Convert.ToInt32(value)), 0, m_Buffer, m_Length, 2)
m_Length += 2
End Sub
''' <summary>
''' Adds an uinteger to the buffer and increments the length by 4.
''' </summary>
''' <param name="value">Uinteger to add.</param>
Public Sub AddUInteger(value As UInteger)
Array.ConstrainedCopy(BitConverter.GetBytes(value), 0, m_Buffer, m_Length, 4)
m_Length += 4
End Sub
Public Sub AddUInteger(value As Object)
Array.ConstrainedCopy(BitConverter.GetBytes(Convert.ToUInt32(value)), 0, m_Buffer, m_Length, 4)
m_Length += 4
End Sub
''' <summary>
''' Adds an integer to the buffer and increments the length by 4.
''' </summary>
''' <param name="value">Integer to add.</param>
Public Sub AddInteger(value As Integer)
Array.ConstrainedCopy(BitConverter.GetBytes(value), 0, m_Buffer, m_Length, 4)
m_Length += 4
End Sub
Public Sub AddInteger(value As Object)
Array.ConstrainedCopy(BitConverter.GetBytes(Convert.ToInt32(value)), 0, m_Buffer, m_Length, 4)
m_Length += 4
End Sub
''' <summary>
''' Adds an ulong to the buffer and increments the length by 8.
''' </summary>
''' <param name="value">Ulong to add.</param>
Public Sub AddULong(value As ULong)
Array.ConstrainedCopy(BitConverter.GetBytes(value), 0, m_Buffer, m_Length, 8)
m_Length += 8
End Sub
Public Sub AddULong(value As Object)
Array.ConstrainedCopy(BitConverter.GetBytes(Convert.ToUInt64(value)), 0, m_Buffer, m_Length, 8)
m_Length += 8
End Sub
''' <summary>
''' Adds an long to the buffer and increments the length by 8.
''' </summary>
''' <param name="value">Long to add.</param>
Public Sub AddLong(value As Long)
Array.ConstrainedCopy(BitConverter.GetBytes(value), 0, m_Buffer, m_Length, 8)
m_Length += 8
End Sub
Public Sub AddLong(value As Object)
Array.ConstrainedCopy(BitConverter.GetBytes(Convert.ToInt64(value)), 0, m_Buffer, m_Length, 8)
m_Length += 8
End Sub
''' <summary>
''' Adds a float to the buffer and increments the length by 4.
''' </summary>
''' <param name="value">Float to add.</param>
Public Sub AddFloat(value As Single)
Array.ConstrainedCopy(BitConverter.GetBytes(value), 0, m_Buffer, m_Length, 4)
m_Length += 4
End Sub
Public Sub AddFloat(value As Object)
Array.ConstrainedCopy(BitConverter.GetBytes(Convert.ToSingle(value)), 0, m_Buffer, m_Length, 4)
m_Length += 4
End Sub
''' <summary>
''' Adds an ANSI-String to the buffer and increments the length by stringlength.
''' </summary>
''' <param name="value">ANSI-String to add.</param>
Public Sub AddString(value As String)
Dim size As UShort = Convert.ToUInt16(value.Length)
'Add StringLength
AddUShort(size)
'Get Bytes & Copy to pBuffer
Dim tempBuffer As Byte() = Encoding.[Default].GetBytes(value)
Array.ConstrainedCopy(tempBuffer, 0, m_Buffer, m_Length, size)
'Incease Length
m_Length += size
End Sub
Public Sub AddString(value As Object)
Dim size As UShort = Convert.ToUInt16(DirectCast(value, String).Length)
'Add StringLength
AddUShort(size)
'Get Bytes & Copy to pBuffer
Dim tempBuffer As Byte() = Encoding.[Default].GetBytes(DirectCast(value, String))
Array.ConstrainedCopy(tempBuffer, 0, m_Buffer, m_Length, size)
'Incease Length
m_Length += size
End Sub
''' <summary>
''' Adds an Unicode-String to the buffer and increments the length by stringlength.
''' </summary>
''' <param name="value">Unicode-String to add.</param>
Public Sub AddUString(value As String)
Dim size As UShort = Convert.ToUInt16(value.Length)
'Add StringLength
AddUShort(size)
'Get Bytes & Copy to pBuffer
Dim tempBuffer As Byte() = Encoding.Unicode.GetBytes(value)
Array.ConstrainedCopy(tempBuffer, 0, m_Buffer, m_Length, size * 2)
'Incease Length
m_Length += (size * 2)
End Sub
Public Sub AddUString(value As Object)
Dim size As UShort = Convert.ToUInt16(DirectCast(value, String).Length)
'Add StringLength
AddUShort(size)
'Get Bytes & Copy to pBuffer
Dim tempBuffer As Byte() = Encoding.Unicode.GetBytes(DirectCast(value, String))
Array.ConstrainedCopy(tempBuffer, 0, m_Buffer, m_Length, size * 2)
'Incease Length
m_Length += (size * 2)
End Sub
Public Sub AddDate(value As DateTime)
AddUShort(value.Year)
AddUShort(value.Month)
AddUShort(value.Day)
AddUShort(value.Hour)
AddUShort(value.Minute)
AddUShort(value.Second)
AddUInteger(value.Millisecond)
End Sub
Public Sub AddDate(value As Object)
Dim [date] As DateTime = DirectCast(value, DateTime)
AddUShort([date].Year)
AddUShort([date].Month)
AddUShort([date].Day)
AddUShort([date].Hour)
AddUShort([date].Minute)
AddUShort([date].Second)
AddUInteger([date].Millisecond)
End Sub
''' <summary>
''' Adds a HexString to the buffer and increments the length by HexStringlength / 2.
''' </summary>
''' <param name="Hex">HexString to add.</param>
Public Sub AddHex(Hex As String)
Hex = Hex.Replace(" ", "")
Hex = Hex.Replace("-", "")
AddByte(ToByteArray(Hex))
End Sub
#End Region
''' <summary>
''' Resets the Pointer to 0.
''' </summary>
Public Sub ResetPointer()
m_Pointer = 0
End Sub
''' <summary>
''' Resets the Length to 0.
''' </summary>
Public Sub ResetLength()
m_Length = 0
End Sub
Private Sub ResetBuffer()
m_Buffer = New Byte(m_BufferSize - 1) {}
End Sub
''' <summary>
''' Clears the Buffer and resets the Pointer and Length.
''' </summary>
Public Sub Reset()
ResetBuffer()
ResetPointer()
ResetLength()
End Sub
''' <summary>
''' Converts the cPacket instance to Bytearray.
''' </summary>
''' <returns>the cPacket instance as Bytearray.</returns>
Public Function ToByteArray() As Byte()
Try
Dim packet As Byte() = New Byte(m_Length + 5) {}
BitConverter.GetBytes(m_Length).CopyTo(packet, 0)
BitConverter.GetBytes(m_Opcode).CopyTo(packet, 2)
Array.Resize(Of Byte)(m_Buffer, m_Length)
m_Buffer.CopyTo(packet, 6)
'pBuffer = new byte[pBufferSize];
Return packet
Catch ex As Exception
Throw ex
End Try
End Function
''' <summary>
''' Converts the Buffer to String.
''' </summary>
''' <returns>the buffer as String.</returns>
Public Overrides Function ToString() As String
If m_Length <> 0 Then
Dim sData As String = BitConverter.ToString(m_Buffer, 0, m_Length).Replace("-", "")
If m_Pointer > 0 Then
sData = sData.Insert(m_Pointer * 2, "|")
End If
Return sData
Else
Return "Empty"
End If
End Function
Public Sub Dispose()
Array.Clear(m_Buffer, 0, m_Buffer.Length)
m_Buffer = Nothing
End Sub
Private Function ToByteArray(HexString As String) As Byte()
Dim bytes As [Byte]() = New [Byte](HexString.Length \ 2 - 1) {}
For i As Integer = 0 To HexString.Length - 1 Step 2
bytes(i \ 2) = Convert.ToByte(HexString.Substring(i, 2), 16)
Next
Return bytes
End Function
End Class