• Welcome to Valhalla Legends Archive.
 

[C#] Writing a buffer of sorts

Started by Joe[x86], September 07, 2006, 05:45 PM

Previous topic - Next topic

Joe[x86]

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.

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
        }

    }
}


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.
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

MyndFyre

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). 
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Joe[x86]

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).
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

MyndFyre

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).
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.