Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[EO] (hopefully) simple packet tutorial


iSKweek
 Share

Recommended Posts

I wrote this up for someone but I figured I might as well post it up, let me know if there are any errors, I sorta did it while coffee-deprived and half-asleep :P

This is really quite simple once you understand the basics :D

First of all, all packets need to have their name declared in modEnumerations. For packets sent from the client they need to be C(packetname) and for

packets sent from the server they need to be S(packetname).

For example, here is the code for talking in the map.

This is from modClientTCP (client side :P)
```
Public Sub SayMsg(ByVal text As String)
Dim Buffer As clsBuffer

    ' If debug mode, handle error then exit out
    If Options.Debug = 1 Then On Error GoTo errorhandler

    Set Buffer = New clsBuffer
    Buffer.WriteLong CSayMsg
    Buffer.WriteString text
    SendData Buffer.ToArray()
    Set Buffer = Nothing

    ' Error handler
    Exit Sub
errorhandler:
    HandleError "SayMsg", "modClientTCP", Err.Number, Err.Description, Err.Source, Err.HelpContext
    Err.Clear
    Exit Sub
End Sub
```
the main part you need to worry about is really```
Dim Buffer As clsBuffer

    Set Buffer = New clsBuffer
    Buffer.WriteLong CSayMsg
    Buffer.WriteString text
    SendData Buffer.ToArray()
    Set Buffer = Nothing
```
Basically this part is just a normal sub that you call with parameters. for example.

Call SayMsg(text that needs to be sent)

This is then automatically put in the variable "text" by the sub.

For the inner parts of the packet you will see the lines "WriteLong" and "WriteString". Each packet always has "buffer.writelong (packetname)" below the

line "Set buffer = new clsbuffer" This is mainly for identification purposes for the server I think. Each bit of information that needs to be sent needs to have

its own writelong or writestring.

For example, if I wanted to send the player name, player level and some text I would have:
```
Public Sub Blah(ByVal index As Long, ByVal level As Long, ByVal text As String)
Dim Buffer As clsBuffer

    ' If debug mode, handle error then exit out
    If Options.Debug = 1 Then On Error GoTo errorhandler

    Set Buffer = New clsBuffer
    Buffer.WriteLong CRandomPacket
    Buffer.WriteLong index
    Buffer.WriteLong level
    Buffer.WriteString text
    SendData Buffer.ToArray()
    Set Buffer = Nothing

    ' Error handler
    Exit Sub
errorhandler:
    HandleError "SayMsg", "modClientTCP", Err.Number, Err.Description, Err.Source, Err.HelpContext
    Err.Clear
    Exit Sub
End Sub
```
Which I would call with```
Call Blah(index, Player(index).level, "teaxt blah blah")
```
In terms of sending data to the server that is all that you need to do client side. Now we head to the server code, this is simple trust me :P

First of all we need to go to modEnumerations and make sure that we declare the packet name, in this example it is just CRandomPacket.
Next go to modHandleData and find the initMesssages sub. In it you will find a huge amount of lines like this```
HandleDataSub(CPartyLeave) =

GetAddress(AddressOf HandlePartyLeave)
```
We need to add a line like this for our packet. So keeping with the packet name so far just add```
HandleDataSub(CRandomPacket) = GetAddress

(AddressOf HandleRandomPacket)
```
Most of this is just the same for each packet, the only thing that changes is the packet name and also the sub that we call to handle the information that is

recieved, in this case "HandleRandomPacket".

Once you have added that line in (the lines must be in the same order that the packet names are in modEnumerations) scroll down to the bottom of

modHandleData so that we can add in the sub to deal with the information.

The sub is rather straight forward and pretty much upto whatever you put in it, for example, here is the sub handling messages:
```
Private Sub HandleSayMsg(ByVal index As Long, ByRef Data() As Byte, ByVal StartAddR As Long, ByVal ExtraVar As Long)
    Dim msg As String
    Dim i As Long
    Dim Buffer As clsBuffer
    Set Buffer = New clsBuffer
    Buffer.WriteBytes Data()
    msg = Buffer.ReadString

    ' Prevent hacking
    For i = 1 To Len(msg)
        ' limit the ASCII
        If AscW(Mid$(msg, i, 1)) < 32 Or AscW(Mid$(msg, i, 1)) > 126 Then
            ' limit the extended ASCII
            If AscW(Mid$(msg, i, 1)) < 128 Or AscW(Mid$(msg, i, 1)) > 168 Then
                ' limit the extended ASCII
                If AscW(Mid$(msg, i, 1)) < 224 Or AscW(Mid$(msg, i, 1)) > 253 Then
                    Mid$(msg, i, 1) = ""
                End If
            End If
        End If
    Next

    Call AddLog("Map #" & GetPlayerMap(index) & ": " & GetPlayerName(index) & " says, '" & msg & "'", PLAYER_LOG)
    Call SayMsg_Map(GetPlayerMap(index), index, msg, QBColor(White))

    Set Buffer = Nothing
End Sub
```
For our sub we need to have something like:

```
Private Sub HandleRandomPacket(ByVal index As Long, ByRef Data() As Byte, ByVal StartAddR As Long, ByVal ExtraVar As Long)

    ' the variables we store our information in
    Dim text As String
    Dim player As Long
    Dim playerlevel AS Long

    Dim Buffer As clsBuffer
    Set Buffer = New clsBuffer
    Buffer.WriteBytes Data()

    player = buffer.ReadLong
    playerlevel = buffer.ReadLong
    text = buffer.ReadString

    'code that does something with this information

    Set Buffer = Nothing
End Sub
```
Basically the sub must start with "Private Sub HandleRandomPacket(ByVal index As Long, ByRef Data() As Byte, ByVal StartAddR As Long, ByVal ExtraVar As Long)" and then declare some variables to hold our information. The```
Dim Buffer As clsBuffer
    Set Buffer = New clsBuffer
    Buffer.WriteBytes Data()

```part is declaring our buffer to read from.

You will notice the readlong/readstring lines, these just read the information, nice and simple. The information has to be read in the same order it was written, think of a first in first out kinda thing.

That is all I think, just for sending information to the server. It is pretty much the same for sending information to the client, you can look at how it is all handled in the server or just ask if you get stuck, I tried to make this as simple and readable as possible but I am rather tired so sorry if it just confuses you :P
Link to comment
Share on other sites

Well, it would seem like a piece was missing to some people, I'd say write it for the sake of convenience, but it's not needed desperately, since it's almost the same thing.
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...