Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Eclipse's clsBuffer rewritten for VB.NET.


ghost1
 Share

Recommended Posts

So, while I was writing the networking system for my game, I obviously came across the packet system. The way I wanted to do it was by using byte arrays, and seeing as how Eclipse uses a perfect class for building up byte arrays, I decided to rewrite it to work with VB.NET, and decided to share it in here.

```
Imports System.Text

Public Class clsBuffer

' ********************

' ** Main Variables **

' ********************

Private Buffer() As Byte

Private BufferSize As Integer

Private WriteHead As Integer

Private ReadHead As Integer

' *****************

' ** Constructor **

' *****************

Public Sub New()

Flush()

End Sub

Public Sub PreAllocate(ByVal Length As Integer)

WriteHead = 0

ReadHead = 0

BufferSize = Length - 1

ReDim Buffer(0 To BufferSize)

End Sub

Public Sub Allocate(ByVal Length As Integer)

If BufferSize = 0 And Length > 1 Then Length -= 1

BufferSize += Length

ReDim Preserve Buffer(0 To BufferSize)

End Sub

Public Sub Flush()

ReDim Buffer(0)

BufferSize = 0

WriteHead = 0

ReadHead = 0

End Sub

Public Sub Trim()

If ReadHead >= Count() Then

Flush()

End If

End Sub

Public Sub WriteByte(ByVal nByte As Byte)

If WriteHead >= BufferSize Then Allocate(1)

Buffer(WriteHead) = nByte

WriteHead += 1

End Sub

Public Sub WriteBytes(ByVal nBytes() As Byte)

Dim nLength As Integer = nBytes.Length

If WriteHead + nLength - 1 > BufferSize Then Allocate(nLength)

For i As Integer = WriteHead To nLength - 1

Buffer(i) = nBytes(i)

Next

WriteHead += nLength

End Sub

Public Sub WriteShort(ByVal nShort As Short)

If WriteHead + 1 > BufferSize Then Allocate(2)

BitConverter.GetBytes(nShort).CopyTo(Buffer, WriteHead)

WriteHead += 2

End Sub

Public Sub WriteInteger(ByVal nInt As Integer)

If WriteHead + 3 > BufferSize Then Allocate(4)

BitConverter.GetBytes(nInt).CopyTo(Buffer, WriteHead)

WriteHead += 4

End Sub

Public Sub WriteLong(ByVal nLong As Long)

If WriteHead + 7 > BufferSize Then Allocate(8)

BitConverter.GetBytes(nLong).CopyTo(Buffer, WriteHead)

WriteHead += 8

End Sub

Public Sub WriteString(ByVal nString As String)

WriteInteger(nString.Length)

If WriteHead + nString.Length - 1 > BufferSize Then Allocate(nString.Length)

Encoding.ASCII.GetBytes(nString).CopyTo(Buffer, WriteHead)

WriteHead += nString.Length

End Sub

Public Function ReadByte(Optional ByVal MoveReadHead As Boolean = True) As Byte

If ReadHead > BufferSize Then Return 0

Dim val As Byte = Buffer(ReadHead)

If MoveReadHead Then ReadHead += 1

Return val

End Function

Public Function ReadBytes(ByVal nLength As Integer, Optional ByVal MoveReadHead As Boolean = True) As Byte()

Dim Data() As Byte

If nLength = 0 Then Return Nothing

If ReadHead + nLength > BufferSize Then Return Nothing

ReDim Data(nLength)

For i As Integer = 0 To ReadHead + nLength - 1

Data(i) = Buffer(i)

Next

If MoveReadHead Then ReadHead += nLength

Return Data

End Function

Public Function ReadShort(Optional ByVal MoveReadHead As Boolean = True) As Short

If ReadHead + 1 > BufferSize Then Return 0

Dim val As Short = BitConverter.ToInt16(Buffer, ReadHead)

If MoveReadHead Then ReadHead += 2

Return val

End Function

Public Function ReadInteger(Optional ByVal MoveReadHead As Boolean = True) As Integer

If ReadHead + 3 > BufferSize Then Return 0

Dim val As Integer = BitConverter.ToInt32(Buffer, ReadHead)

If MoveReadHead Then ReadHead += 4

Return val

End Function

Public Function ReadLong(Optional ByVal MoveReadHead As Boolean = True) As Long

If ReadHead + 7 > BufferSize Then Return 0

Dim val As Long = BitConverter.ToInt64(Buffer, ReadHead)

If MoveReadHead Then ReadHead += 8

Return val

End Function

Public Function ReadString(Optional ByVal MoveReadHead As Boolean = True) As String

Dim sLength As Integer = ReadInteger()

If sLength <= 0 Then Return vbNullString

Dim val As String = Encoding.ASCII.GetString(Buffer, ReadHead, sLength)

If MoveReadHead Then ReadHead += sLength

Return val

End Function

Public Function Count() As Integer

Return Buffer.Length

End Function

Public Function Length() As Integer

Return Count() - ReadHead

End Function

Public Function ToArray() As Byte()

Return Buffer

End Function

Public Overrides Function ToString() As String

Return Buffer.ToString()

End Function

End Class
```

If you happen to run into any errors, please post here so I can go and fix them.

Credits are appreciated, but not needed.

Original class credits go to Jacob from MS4.
Link to comment
Share on other sites

If I had to guess, I'd say a little better performance using BinaryReader/BinaryWriter. But it's a shot in the dark. Like Jeff said if this works for you, and you're comfortable with it; use it. You'll understand your own code better than anyone else ![;)](http://www.touchofdeathforums.com/community/public/style_emoticons/<#EMO_DIR#>/wink.png)
Link to comment
Share on other sites

Rule #-insertrandomnumberhere- of programming: creating redundant code that operates exactly the same as or that otherwise does the same job as professional written, supported, and documented existing libraries is bad. Why? Because they're clean and fast. Some C++ nuts will tell you that they're bloated, slow, and don't handle as well as something they may write.

What is the difference between a C++ guru and you? Years of experience and the fact that you're using VB.NET. In VB.NET, you don't have the control necessary to perform something more optimally than what has already been done without writing twice to three times as many lines of code as what it took to make the managed library that's already out there.

Anyway, and to the point: I agree with Jeff; use the .NET libraries. I have a tutorial on serializing and deserializing structures and writing/reading them to a memorystream here under "Files": [http://sites.google…./revelationorpg](http://sites.google.com/site/revelationorpg)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...