ghost1 Posted September 18, 2012 Author Share Posted September 18, 2012 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.TextPublic Class clsBuffer' ********************' ** Main Variables **' ********************Private Buffer() As BytePrivate BufferSize As IntegerPrivate WriteHead As IntegerPrivate ReadHead As Integer' *****************' ** Constructor **' *****************Public Sub New() Flush()End SubPublic Sub PreAllocate(ByVal Length As Integer) WriteHead = 0 ReadHead = 0 BufferSize = Length - 1 ReDim Buffer(0 To BufferSize)End SubPublic Sub Allocate(ByVal Length As Integer) If BufferSize = 0 And Length > 1 Then Length -= 1 BufferSize += Length ReDim Preserve Buffer(0 To BufferSize)End SubPublic Sub Flush() ReDim Buffer(0) BufferSize = 0 WriteHead = 0 ReadHead = 0End SubPublic Sub Trim() If ReadHead >= Count() Then Flush() End IfEnd SubPublic Sub WriteByte(ByVal nByte As Byte) If WriteHead >= BufferSize Then Allocate(1) Buffer(WriteHead) = nByte WriteHead += 1End SubPublic 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 += nLengthEnd SubPublic Sub WriteShort(ByVal nShort As Short) If WriteHead + 1 > BufferSize Then Allocate(2) BitConverter.GetBytes(nShort).CopyTo(Buffer, WriteHead) WriteHead += 2End SubPublic Sub WriteInteger(ByVal nInt As Integer) If WriteHead + 3 > BufferSize Then Allocate(4) BitConverter.GetBytes(nInt).CopyTo(Buffer, WriteHead) WriteHead += 4End SubPublic Sub WriteLong(ByVal nLong As Long) If WriteHead + 7 > BufferSize Then Allocate(8) BitConverter.GetBytes(nLong).CopyTo(Buffer, WriteHead) WriteHead += 8End SubPublic 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.LengthEnd SubPublic 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 valEnd FunctionPublic 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 DataEnd FunctionPublic 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 valEnd FunctionPublic 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 valEnd FunctionPublic 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 valEnd FunctionPublic 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 valEnd FunctionPublic Function Count() As Integer Return Buffer.LengthEnd FunctionPublic Function Length() As Integer Return Count() - ReadHeadEnd FunctionPublic Function ToArray() As Byte() Return BufferEnd FunctionPublic Overrides Function ToString() As String Return Buffer.ToString()End FunctionEnd 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 More sharing options...
JeffSventora Posted September 18, 2012 Share Posted September 18, 2012 It's always good to write your own code, but as an alternative you can use the MemoryStream. BinaryWriter & BinaryReader classes to achieve the same goals. Nice though. Link to comment Share on other sites More sharing options...
Whackeddie99 Posted September 18, 2012 Share Posted September 18, 2012 Wow nice, thanks for sharing mate. Link to comment Share on other sites More sharing options...
ghost1 Posted September 18, 2012 Author Share Posted September 18, 2012 @Jeff; you think using those classes it'd be better? Link to comment Share on other sites More sharing options...
JeffSventora Posted September 18, 2012 Share Posted September 18, 2012 I guess the only way to know would be to do a speed comparison. Not really sure to be honest, but by all means if this works and you prefer to use your own class do it I just pointing out alternatives ![:P](http://www.touchofdeathforums.com/community/public/style_emoticons/<#EMO_DIR#>/tongue.png) Link to comment Share on other sites More sharing options...
Rob Janes Posted September 19, 2012 Share Posted September 19, 2012 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 More sharing options...
ghost1 Posted September 19, 2012 Author Share Posted September 19, 2012 Yeah, perhaps I'll give it a try to using the MemoryStream class afterwards.If I do I'll be sure to post the code here and tell you about the speed, as well. Link to comment Share on other sites More sharing options...
Mal Posted September 20, 2012 Share Posted September 20, 2012 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now