Author Topic: [C#] Packet Buffer & Parser  (Read 11129 times)

0 Members and 1 Guest are viewing this topic.

Offline Smarter

  • Newbie
  • *
  • Posts: 11
    • View Profile
[C#] Packet Buffer & Parser
« on: December 06, 2006, 07:49:20 pm »
I'm attempting to convert:
Code: [Select]
Static strBuffer As String
Dim strTemp As String, lngLen As Long
Winsock1.GetData strTemp, vbString
strBuffer = strBuffer & strTemp
While Len(strBuffer) > 4
    lngLen = Val("&H" & Login.StrToHex(StrReverse(Mid(strBuffer, 3, 2))))
    If Len(strBuffer) < lngLen Then Exit Sub
    Login.ParsePacket Left(strBuffer, lngLen)
    strBuffer = Mid(strBuffer, lngLen + 1)
Wend

and
Code: [Select]
Private Buffer As String

Public Function InsertDWORDArray(Data() As Long)
Dim i As Integer
For i = LBound(Data) To UBound(Data) Step 1
    Buffer = Buffer & MakeDWORD(Data(i))
Next i
End Function

Public Function InsertDWORD(Data As Long)
Buffer = Buffer & MakeDWORD(Data)
End Function

Public Function InsertData(Data As String)
Buffer = Buffer & Data
End Function

Public Function InsertWORD(Data As Integer)
Buffer = Buffer & MakeWORD(Data)
End Function

Public Function InsertBYTE(Data As Integer)
Buffer = Buffer & Chr(Data)
End Function

Public Sub InsertBytes(Data As String)
Dim i As Long, Enqueueer As String
For i = 1 To Len(Data) Step 3
    Enqueueer = Enqueueer & Chr(Val("&h0" & Mid(Data, i, 2)))
Next i
Buffer = Buffer & Enqueueer
End Sub

Public Function InsertNTString(Data As String)
Buffer = Buffer & Data & Chr(0)
End Function

Public Function InsertNonNTString(Data As String)
Buffer = Buffer & Data
End Function

Function MakeDWORD(Value As Long) As String
Dim Result As String * 4
CopyMemory ByVal Result, Value, 4
MakeDWORD = Result
End Function

Function MakeWORD(Value As Integer) As String
Dim Result As String * 2
CopyMemory ByVal Result, Value, 2
MakeWORD = Result
End Function

Public Function Clear()
Buffer = ""
End Function

Public Function SendPacket(PacketID As Byte)
Dim PacketData As String, Packet As String
If Form1.Winsock1.State <> sckConnected Then: Exit Function
Form1.Winsock1.SendData Chr(&HFF) & Chr(PacketID) & MakeWORD(Len(Buffer) + 4) & Buffer
Clear
End Function





From VB > C#, It's proving quite the challange for me, as I am still learning C#, any help would be greatly appreciated.

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: [C#] Packet Buffer & Parser
« Reply #1 on: December 06, 2006, 07:52:59 pm »
Maybe if you just used the original one...

Offline Smarter

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: [C#] Packet Buffer & Parser
« Reply #2 on: December 06, 2006, 08:01:23 pm »
The originall what?

Offline Chavo

  • x86
  • Hero Member
  • *****
  • Posts: 2219
  • no u
    • View Profile
    • Chavoland
Re: [C#] Packet Buffer & Parser
« Reply #3 on: December 06, 2006, 08:17:26 pm »
He means there are other C# Packet Buffer's already around.  I wouldn't recommend copying them though, you are more likely to learn about them if you port it from another language that forces you to learn it.  The best solution would be to write it from scratch, but something tells me thats not the solution you want if you are trying to convert VB to C# ;)

Offline Smarter

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: [C#] Packet Buffer & Parser
« Reply #4 on: December 06, 2006, 08:21:30 pm »
Well, both actually, I want to learn through conversion, and i just "want it to work" heh. Could you link me ot the C# pbuffer you speak of?

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: [C#] Packet Buffer & Parser
« Reply #5 on: December 06, 2006, 10:01:16 pm »
http://www.jinxbot.net/mbncsutil/ is the link to the full documentation.  MBNCSUtil is licensed under the modified BSD license.

Because MBNCSUtil is also designed to be a generalized networking library, there are several classes of interest.  The most basic class is the DataBuffer, and there is a specialized Battle.net version of it called BncsPacket.

For reading incoming data, there is a generalized DataReader packet, and a specialized Battle.net-aware version called BncsReader.

You can try to reach me on AIM for support of this, but I don't have time to write your code for you.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline Smarter

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: [C#] Packet Buffer & Parser
« Reply #6 on: December 07, 2006, 11:14:24 am »
I've had MBNCSUtil, since I started learning C#, and it seems and I was told that it is hard to use.

Offline Smarter

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: [C#] Packet Buffer & Parser
« Reply #7 on: December 07, 2006, 12:57:43 pm »
Code: [Select]
        private void sckBNET_DataArrival(long bytesTotal, object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
        {
            BncsPacket BNCSPacket = new BncsPacket();
            BncsReader BNCSRead = new BncsReader();
            BNCSPacket.Count();
        }
After playing with it a bit, it seems to make some sense, but how do you get the data arriving into the buffer, and use it, etc. Is there a C# Bot source out that I could learn this from?

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: [C#] Packet Buffer & Parser
« Reply #8 on: December 07, 2006, 08:12:30 pm »
Well, first of all, if you've ever seen my talks about Battle.net programming and .NET network programming in general, you'll know that I do not advocate the use of the ActiveX Winsock control in .NET programs.  With that said, I'm not going to correct the "code sample" you've provided.

I will point out that if you're using C#, the code you provided will cause 3 compiler errors: the BncsPacket() and BncsReader() default constructors are marked Obsolete.  The third is that Count is a property, not a method, and should not be referenced with trailing parentheses (it is referred to as BNCSPacket.Count, not BNCSPacket.Count()).

MBNCSUtil is organized much in the same way that the .NET Framework is, and uses many of the same naming conventions, so anyone familiar with the .NET Framework Class Library should generally understand what functions mean.

Because I chose to follow the .NET Framework's naming conventions, though, some of the Battle.net naming conventions are lost.  For example, what is commonly thought of as "AddDWORD()" or "ReadDWORD()" would be AddInt32() in BncsPacket, or ReadInt32() in BncsReader.

For instance, when you're reading data from the server:
Code: [Select]
sckBnet.Read(buf, 0, 4, SocketFlags.None);
int len = BitConverter.ToInt16(buf, 2) - 4;
byte[] data = new byte[len];
if (len < 0)
{
  sckBnet.Read(data, 0, len, SocketFlags.None);
}
HandlePacket(buf[1], data, len);
That code reads one packet of data from a Battle.net connection (assuming you have a Socket named sckBnet and a byte[] named buf of arbitrary length >= 4), and then calls HandlePacket with the packet ID, length, and any data that may have been included.

HandlePacket demonstrates how easy it is to use a DataReader.
Code: [Select]
void HandlePacket(byte id, byte[] data, int len)
{
  DataReader rdr = new DataReader(data);
  switch (id)
  {
    case 0x50: // SID_AUTH_INFO
      int logonType =  rdr.ReadInt32();
      int serverToken = rdr.ReadInt32();
      int udpToken = rdr.ReadInt32();
      DateTime mpqTime = DateTime.FromFileTime(rdr.ReadInt64());
      string verFileName = rdr.ReadCString();
      string vercheckStr = rdr.ReadCString();
  // do something with all of that.
      break;
  }
}
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline Smarter

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: [C#] Packet Buffer & Parser
« Reply #9 on: December 08, 2006, 09:09:17 am »
Did I ever mention I love you, and about 20 minutes after I posted that I started talking with a C# developer friend of mine who told me the only help he would give me is to use TCPClient();, but Socket you say to use, so i'll be sure to use that! Thanks so much, if i ever actually finish my program, you will be credited everywhere possible! Ok, and one other thing, so MBNCSUtil, is just a buffer, so I still have to make a Parser? Or does that have a sexy part for that too, cause in VB that was the part I really blew with, parsing the data... I could make the packets up, etc, but couldn't parse it.. always had to get help.

Offline Smarter

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: [C#] Packet Buffer & Parser
« Reply #10 on: December 08, 2006, 09:35:32 am »
Gah see, i always don't know the simplest thing, i've been at it for 2 days now, cannot figure out how to get the data into the Buffer array.. >.<.... also Socket doesn't have a .Read method ...? I must be doing something wrong here...! >.<

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: [C#] Packet Buffer & Parser
« Reply #11 on: December 08, 2006, 01:27:39 pm »
Gah see, i always don't know the simplest thing, i've been at it for 2 days now, cannot figure out how to get the data into the Buffer array.. >.<.... also Socket doesn't have a .Read method ...? I must be doing something wrong here...! >.<
My bad, I was thinking of a NetworkStream.  Socket's equivalent call is Receive where I wrote Read.

Socket, NetworkStream, and TcpClient are all valid classes to use.

DataReader/BncsReader is the parser class.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline Smarter

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: [C#] Packet Buffer & Parser
« Reply #12 on: December 08, 2006, 01:30:18 pm »
Well, i figured all that out, and made the socket use a ASync connection, but now my problem is implementing BNLS into my project... it confuses me... I got the data to buffer and all that great stuff, the project just needs it's 0x51 C>S ... using BNLS, any help on that? lol I hate asking for help >.<.

Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: [C#] Packet Buffer & Parser
« Reply #13 on: December 08, 2006, 04:07:21 pm »
Interesting topic, comes at a good time when I'm working on a C# Battle.net client.
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Offline Smarter

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: [C#] Packet Buffer & Parser
« Reply #14 on: December 08, 2006, 07:01:26 pm »
Code: [Select]
public void OnDataReceived(IAsyncResult asyn)
        {
            int len = BitConverter.ToInt16(buffer, 2) - 4;
            byte[] data = new byte[len];
            if (len < 0)
            {
                // sckBnet.Read(data, 0, len, SocketFlags.None);
                sckBNET.Receive(data, 0, len, SocketFlags.None);
            }
            HandlePacket(buffer[1], data, len);

            //end receive...
            int iRx = 0;
            iRx = sckBNET.EndReceive(asyn);

            WaitForData();
        }

I get an error stating: byte[] data = new byte[len]; - Arithmetic operation resulted in an overflow., help! lol