JohnPony Posted December 25, 2012 Author Share Posted December 25, 2012 This works in a very similar way to how mirage's original byte array packets were shipped, it packs data into a small byte array and ships it to where you need it to be, at which point you can unpack it.I'm not sure if anyone else can find use of this, but if so you can download the library extension here:[http://www.mmorpgcre…BufferSharp.dll](http://www.mmorpgcreation.com/BufferSharp.dll)Regards,GP Link to comment Share on other sites More sharing options...
JohnPony Posted December 26, 2012 Author Share Posted December 26, 2012 - Snip, old code. Link to comment Share on other sites More sharing options...
Mal Posted December 26, 2012 Share Posted December 26, 2012 Yeah, I'm not too sure what the purpose of this is. You can use a binary or stream reader/writer in its place, and I'm more than certain it'd be more efficient. I'd only write my own stuff if it were doing bitwise operations (take a look at Lidgren's; talking about complex). Link to comment Share on other sites More sharing options...
JohnPony Posted December 26, 2012 Author Share Posted December 26, 2012 Updated the code with a better resize method. Link to comment Share on other sites More sharing options...
Godlord Posted December 26, 2012 Share Posted December 26, 2012 I'm wondering why you are allocating the size you need (linear allocation), rather than the next power of two for that size (exponential allocation). Especially, since allocation and copying memory are slow operations.Yours faithfully,S.J.R. van Schaik. Link to comment Share on other sites More sharing options...
JohnPony Posted December 27, 2012 Author Share Posted December 27, 2012 > I'm wondering why you are allocating the size you need (linear allocation), rather than the next power of two for that size (exponential allocation). Especially, since allocation and copying memory are slow operations.> > Yours faithfully,> > S.J.R. van Schaik.~~I'm sorry but could you provide a short summery or example? I'm still learning and every bit of information helps.~~Genusis gave me a bit more information, but he told be to wait for you to respond as well.Regards,GP Link to comment Share on other sites More sharing options...
Godlord Posted December 27, 2012 Share Posted December 27, 2012 At this moment you are just allocating a buffer that is just large enough to store your data in. The problem with that is that this linear behaviour (as you actually increment the size with one byte for every byte you add) causes the buffer to be re-allocated every time data is being added to the buffer. Rather than re-allocating the buffer in a linear fashion, you should re-allocate it in an exponential fashion (i.e. twice the previous size, or the next power of two, as your data may not fit). That way those additional re-allocations are being prevented. Furthermore, preserving just the right amount of storage, prevents re-allocations from occurring in general.Yours faithfully,S.J.R. van Schaik. Link to comment Share on other sites More sharing options...
JohnPony Posted December 27, 2012 Author Share Posted December 27, 2012 Thanks Stephan, I'll try this out right away. I'm guessing I'd just removed those empty bytes of data when I'm prepared to transfer the buffer.Regards,GP Link to comment Share on other sites More sharing options...
Godlord Posted December 27, 2012 Share Posted December 27, 2012 > Thanks Stephan, I'll try this out right away. I'm guessing I'd just removed those empty bytes of data when I'm prepared to transfer the buffer.> > Regards,> > GPAren't you able to just tell how many bytes you want to send?Yours faithfully,S.J.R. van Schaik. Link to comment Share on other sites More sharing options...
JohnPony Posted December 27, 2012 Author Share Posted December 27, 2012 > Aren't you able to just tell how many bytes you want to send?> > Yours faithfully,> > S.J.R. van Schaik.Ah yes, point made, that's pretty much what I was saying. Link to comment Share on other sites More sharing options...
JohnPony Posted December 29, 2012 Author Share Posted December 29, 2012 ```using System;using System.Text;namespace BufferSharp{ public class BufferData { private byte[] _buffer; //Holds our total amount of bytes. private int _offset; // Holds our current offset in the byte array. public BufferData() { _buffer = new byte[0]; } /// /// Writes A Byte Into The Buffer /// /// Byte Value To Write Into The Buffer. public void WriteByte(byte value) { byte[] src = BitConverter.GetBytes(value); byte[] tmparray = new byte[_buffer.Length + src.Length]; Buffer.BlockCopy(_buffer, 0, tmparray, 0, _buffer.Length); Buffer.BlockCopy(src, 0, tmparray, tmparray.Length - src.Length, src.Length); _buffer = tmparray; } /// /// Writes A 64 Bit Integer Into The Buffer. /// /// Long Value To Write Into The Buffer. public void WriteLong(long value) { byte[] src = BitConverter.GetBytes(value); byte[] tmparray = new byte[_buffer.Length + src.Length]; Buffer.BlockCopy(_buffer, 0, tmparray, 0, _buffer.Length); Buffer.BlockCopy(src, 0, tmparray, tmparray.Length - src.Length, src.Length); _buffer = tmparray; } /// /// Writes A 16 Bit Integer Into The Buffer. /// /// Int Value To Write Into The Buffer. public void WriteInt16(Int16 value) { byte[] src = BitConverter.GetBytes(value); byte[] tmparray = new byte[_buffer.Length + src.Length]; Buffer.BlockCopy(_buffer, 0, tmparray, 0, _buffer.Length); Buffer.BlockCopy(src, 0, tmparray, tmparray.Length - src.Length, src.Length); _buffer = tmparray; } /// /// Writes A String Into The Buffer. /// /// String Value To Write Into The Buffer. public void WriteString(string value) { byte[] src = ASCIIEncoding.ASCII.GetBytes(value); WriteByte((byte)src.Length); byte[] tmpdata = new byte[_buffer.Length + src.Length]; Buffer.BlockCopy(_buffer, 0, tmpdata, 0, _buffer.Length); Buffer.BlockCopy(src, 0, tmpdata, tmpdata.Length - src.Length, src.Length); _buffer = tmpdata; } /// /// Writes A 32 Bit Integer Into The Buffer. /// /// Int Value To Write Into The Buffer public void WriteInt32(Int32 value) { byte[] src = BitConverter.GetBytes(value); byte[] tmparray = new byte[_buffer.Length + src.Length]; Buffer.BlockCopy(_buffer, 0, tmparray, 0, _buffer.Length); Buffer.BlockCopy(src, 0, tmparray, tmparray.Length - src.Length, src.Length); _buffer = tmparray; } /// /// Grabs The Buffer For Usage. /// /// public byte[] GrabBuffer() { return _buffer; } /// /// Prepares The Buffer For Reading. /// /// Byte Array To Begin Reading From. public void BeginRead(byte[] buffer) { _buffer = buffer; _offset = 0; } /// /// Reads An Int From The Buffer. /// /// A 16 Bit Int Value. public Int16 ReadInt16() { byte[] tmpdata = new byte[3]; System.Buffer.BlockCopy(_buffer, _offset, tmpdata, 0, 2); _offset = _offset + 2; return BitConverter.ToInt16(tmpdata, 0); } /// /// Reads An Int From The Buffer. /// /// A 32 Bit Int Value public Int32 ReadInt32() { byte[] tmpdata = new byte[5]; System.Buffer.BlockCopy(_buffer, _offset, tmpdata, 0, 4); _offset = _offset + 4; return BitConverter.ToInt32(tmpdata, 0); } /// /// Reads A Byte From The Buffer. /// /// A 8 Bit Byte Value. public byte ReadByte() { byte[] tmpdata = new byte[2]; System.Buffer.BlockCopy(_buffer, _offset, tmpdata, 0, 1); _offset = _offset + 2; return Convert.ToByte(BitConverter.ToInt16(tmpdata, 0)); } /// /// Reads A String From The Buffer. /// /// String Read From The Buffer. public string ReadString() { byte size = ReadByte(); byte[] tmpdata = new byte[size + 1]; System.Buffer.BlockCopy(_buffer, _offset, tmpdata, 0, size); _offset = _offset + size; return System.Text.ASCIIEncoding.ASCII.GetString(tmpdata); } /// /// Reads A Long Value From The Buffer /// /// A 64 Bit Int Value Read From The Buffer. public long ReadLong() { byte[] tmpdata = new byte[9]; System.Buffer.BlockCopy(_buffer, _offset, tmpdata, 0, 8); _offset = _offset + 8; return BitConverter.ToInt64(tmpdata, 0); } }}```Updated the code. Link to comment Share on other sites More sharing options...
Godlord Posted December 31, 2012 Share Posted December 31, 2012 When sending data across the wire, or when writing data to a disk, I'd also keep endianness and portability (although, it doesn't really matter much for C#, as the language is proprietary) in mind. Except for some proprietary and mostly exotic protocols the internet generally uses big endian, rather than little endian. On top of that, I'd personally avoid the terms **short**, **int** and **long** as the bit-widths are implicit, even though they have been standardised in C#. Therefore, they are only useful when referring to variables on the host, and not when dealing with portable data. However, whether you actually do this is up to you. Since you are using C# anyway, it doesn't really matter (unless you are expecting that your data is going to be used elsewhere).Yours faithfully,S.J.R. van Schaik. 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