Valhalla Legends Forums Archive | .NET Platform | [C#] Writing a buffer of sorts

AuthorMessageTime
JoeTheOdd
Bare with me through the next few days/weeks. I'm writing a project with knowledge of the syntax and not the language. :).

Anyhow, for my latest toy, a library implementing YMSG (the Yahoo! Messenger protocol), I'll be needing, of all things, a buffer! I'm going to split the project into YSharp.Sockets for Buffer and YmsgBuffer (implementing AddHeader, etc) plus perhaps a polling socket interface and YSharp.Packets for creation of packets (IE, YSharp.Packets.LogonSequence().CreateGreeting(String username) or something like that).

Anyhow, I'm starting to write the buffer class. I'm not sure where to go with this, so here's what I have.

[code]using System;
using System.Collections.Generic;
using System.Text;

namespace YSharp.Sockets
{
    public class Buffer
    {
        System.Buffer myData;

        public Buffer(byte[] data)
        {
            myData += data.ToString();
        }

        public Buffer()
        {
            // nothing to do
        }

    }
}[/code]

Naturally I want to add addDword, addWord, etc, but I guess I'm just looking for critique. Is there a better underlying data structure to be building my packts upon, like a byte array? Etc.
September 7, 2006, 10:45 PM
Myndfyr
The System.Buffer class isn't appropriate for actually storing data.  It's used to manipulate arrays of primitive types (for instance, fast-copying int arrays).  It's a static class and does not define any instance methods, so there's not much to do with an instance of it.

The MBNCSUtil DataBuffer class is a generalized buffer for storing outgoing data.  I prefer the MemoryStream class because it dynamically grows (you don't need to worry about reallocating a byte[] and then copying it yourself), although some of the functionality for it has to be custom-implemented (for instance, writing a string to a DataBuffer in .NET using, say, BinaryWriter, would not produce the desired result for, say, Battle.net).

Because MBNCSUtil is designed as a utility library, it might implement more functionality than you're looking for.  A good place to start might be to remove all of the functions decorated with [CLSCompliant(false)], as well as all of the Insert(blah) functions (as opposed to InsertByte, InsertInt16, etc), and maybe even the entire "Operator overloads" region.  They're functionally equivalent, and provided for convenience only, with the exception of the CLSCompliant(false) functions, which operate on unsigned (or in the case of the sbyte, signed) types not supported by all CLS languages.

Never use a string as a buffer in .NET  (like I said here).  Strings aren't stored as simple arrays of bytes anymore; .NET stores them as Unicode character arrays (2 bytes per character), so data will be misinterpreted when you try to use a string as a buffer.

The alternative is to use a byte[] and dynamically resize it as needed.  The common name for this type of operation is EnsureCapacity(int size).

I highly recommend against using an ArrayList or List<byte> for this type of operation.  Both will be highly inefficient in terms of memory and speed (the List<byte> class will likely require 8 bytes per 1 byte of data, and the ArrayList will likely require 12). 
September 8, 2006, 12:57 AM
JoeTheOdd
Would you mind if I went through the MBNCSUtil source code and used (large) bits and pieces of it's non-Battle.net-dependant code in my project? You'll definately get credit, with or without (you're going to be a huge help, obviously, with replies like that :P).
September 8, 2006, 2:58 AM
Myndfyr
MBNCSUtil is licensed under a modified form of the BSD license - in other words, you can use it for whatever you like, commercial or noncommercial, as long as you reproduce the copyright notice for the modified code (if you only distribute it in binary form, then it should be included in any kind of "About" dialog or whatever that it contains code that is copyright me).
September 8, 2006, 3:07 AM

Search