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

Winsock Byte Array


MarkMorris
 Share

Recommended Posts

I'm a little confused, after comparing MS4(Blank Source) With Origins(Blank Source) adding a log system that tracks packets and writes them out as strings, Origins is laggy as hell. After reveiwing what was going on, I realised that origins write all its data as longs(4 bytes) rather than Integer(2 Bytes) or Bytes(1 Byte).

I'm not big on TCP and Winsock but sending alot of data this can be a problem, I've also notice notice a few change in HandleData Example Below.

Eclipes Origins:
```
CallWindowProc HandleDataSub(MsgType), Index, Buffer.ReadBytes(Buffer.Length - 4 + 1), 0, 0
```
Mirage Source
```
CallWindowProc HandleDataSub(MsgType), 1, Buffer.ReadBytes(Buffer.Length), 0, 0
```
Whats with the - 4 +1 haha, Can someone explain that to me,

Packet Headers are also Longs, Mirage uses Integer Saving 2 bytes just for the packet headers.

I want to convert this to Integer or is it even possible to covert it to using bytes?

Can anyone share some light cause I want to write a solid TCP system, I've also notice even if you prealocate, it still uses more bytes than it should using more bandwidth.

These test was all done on blank copies :)

* UPDATE *
Even if you use Longs as your packet Header it only uses the first Byte to store info leaving the remaining 3 Blank.
Link to comment
Share on other sites

You could probably convert the packet name to using bytes, but you'd have to change Sub HandleData to read the MsgType as a byte. I believe that Robin once stated that he was lazy and wrote all integral data types as Longs into the buffer, but you could fix that up so that everything is written according to its correct data type. Convert Booleans to integers and enter them into the buffer as an integer, and multiply Doubles by 10 and write them as either Longs or Integers and then divide them by 10 when your receive them from the buffer. I've explained this because there is no option to write booleans or doubles to the buffer. I hope this helps!
Link to comment
Share on other sites

@Kimimaru:

> You could probably convert the packet name to using bytes, but you'd have to change Sub HandleData to read the MsgType as a byte. I believe that Robin once stated that he was lazy and wrote all integral data types as Longs into the buffer, but you could fix that up so that everything is written according to its correct data type. Convert Booleans to integers and enter them into the buffer as an integer, and multiply Doubles by 10 and write them as either Longs or Integers and then divide them by 10 when your receive them from the buffer. I've explained this because there is no option to write booleans or doubles to the buffer. I hope this helps!

Thanks i didnt realise this, Well would it would be best to do them as byte or integers? Mirage Source 4 Uses Integer as the packet headers. Also any light on why robin has changed the HandleData sub with the - 4 +1 bullshit, its anoying me to why its there.
Link to comment
Share on other sites

I have converted all the packets, and i have change handledata to readbyte.

Im still having some problems though. I can log in ect, but it wont receive SMapdone Packet, I'm pretty sure its somwthing to do with the packet being below 4 something to do with incoming data.

Any help
Link to comment
Share on other sites

I don't know why the -4 + 1 is there, to be honest. Your **IncomingData Subs** are probably still formatted to receive packets as Longs. Try changing your Client Sub IncomingData to this, **but please backup your current sub first**:

```
Public Sub IncomingData(ByVal DataLength As Long)
    Dim Buffer() As Byte
    Dim pLength As Long
    Dim Data() As Byte
    frmMainGame.Socket.GetData Buffer, vbUnicode, DataLength
    PlayerBuffer.WriteBytes Buffer()

    If PlayerBuffer.Length >= 1 Then
        pLength = PlayerBuffer.ReadByte(False)
    End If

    Do While pLength > 0 And pLength <= PlayerBuffer.Length - 1

        'make sure we have the right plength and pbuffer
        If pLength <= PlayerBuffer.Length - 1 Then
            PlayerBuffer.ReadByte
            Data() = PlayerBuffer.ReadBytes(pLength + 1)
            HandleData Data()
        End If

        pLength = 0

        If PlayerBuffer.Length >= 1 Then
            pLength = PlayerBuffer.ReadByte(False)
        End If

    Loop

    ' Check if the playbuffer is empty
    If PlayerBuffer.Length <= 1 Then PlayerBuffer.Flush
End Sub
```
Now try changing your Server **Sub IncomingData** to this:

```
Sub IncomingData(ByVal Index As Long, ByVal DataLength As Long)
    Dim Buffer() As Byte
    Dim Data() As Byte
    Dim pLength As Long
    frmServer.Socket(Index).GetData Buffer(), vbUnicode, DataLength
    'IncomingBytes = IncomingBytes + DataLength
    TempPlayer(Index).Buffer.WriteBytes Buffer()

    'TempPlayer(Index).Buffer.DecompressBuffer
    If TempPlayer(Index).Buffer.Length >= 1 Then
        pLength = TempPlayer(Index).Buffer.ReadByte(False)

        If pLength < 0 Then
            Exit Sub
        End If
    End If

    Do While pLength > 0 And pLength <= TempPlayer(Index).Buffer.Length - 1

        If pLength <= TempPlayer(Index).Buffer.Length - 1 Then
            TempPlayer(Index).DataPackets = TempPlayer(Index).DataPackets + 1
            TempPlayer(Index).Buffer.ReadByte
            Data() = TempPlayer(Index).Buffer.ReadBytes(pLength + 1)
            'If EncryptPackets Then
            '    Encryption_XOR_DecryptByte Data(), PacketKeys(Player(Index).PacketInIndex)
            '    Player(Index).PacketInIndex = Player(Index).PacketInIndex + 1
            '    If Player(Index).PacketInIndex > PacketEncKeys - 1 Then Player(Index).PacketInIndex = 0
            'End If
            HandleData Index, Data()
        End If

        pLength = 0

        If TempPlayer(Index).Buffer.Length >= 1 Then
            pLength = TempPlayer(Index).Buffer.ReadByte(False)

            If pLength < 0 Then
                Exit Sub
            End If
        End If

    Loop

    If GetPlayerAccess(Index) <= 0 Then

        ' Check for data flooding
        If TempPlayer(Index).DataBytes > 1000 Then
            HackingAttempt Index, "Data Flooding"
            Exit Sub
        End If

        ' Check for packet flooding
        If TempPlayer(Index).DataPackets > 25 Then
            HackingAttempt Index, "Packet Flooding"
            Exit Sub
        End If
    End If

    ' Check if elapsed time has passed
    'Player(Index).DataBytes = Player(Index).DataBytes + DataLength
    If GetTickCount >= TempPlayer(Index).DataTimer Then
        TempPlayer(Index).DataTimer = GetTickCount + 1000
        TempPlayer(Index).DataBytes = 0
        TempPlayer(Index).DataPackets = 0
    End If

    If TempPlayer(Index).Buffer.Length <= 1 Then TempPlayer(Index).Buffer.Flush
End Sub
```
I haven't tested these subs, but give them a try, and again, remember to make a backup of your current Subs!
Link to comment
Share on other sites

@Kimimaru:

> I don't know why the -4 + 1 is there, to be honest. Your **IncomingData Subs** are probably still formatted to receive packets as Longs. Try changing your Client Sub IncomingData to this, **but please backup your current sub first**:
>
> ```
> Public Sub IncomingData(ByVal DataLength As Long)
>     Dim Buffer() As Byte
>     Dim pLength As Long
>     Dim Data() As Byte
>     frmMainGame.Socket.GetData Buffer, vbUnicode, DataLength
>     PlayerBuffer.WriteBytes Buffer()
>
>     If PlayerBuffer.Length >= 1 Then
>         pLength = PlayerBuffer.ReadByte(False)
>     End If
>
>     Do While pLength > 0 And pLength <= PlayerBuffer.Length - 1
>
>         'make sure we have the right plength and pbuffer
>         If pLength <= PlayerBuffer.Length - 1 Then
>             PlayerBuffer.ReadByte
>             Data() = PlayerBuffer.ReadBytes(pLength + 1)
>             HandleData Data()
>         End If
>
>         pLength = 0
>
>         If PlayerBuffer.Length >= 1 Then
>             pLength = PlayerBuffer.ReadByte(False)
>         End If
>
>     Loop
>
>     ' Check if the playbuffer is empty
>     If PlayerBuffer.Length <= 1 Then PlayerBuffer.Flush
> End Sub
> ```
> Now try changing your Server **Sub IncomingData** to this:
>
> ```
> Sub IncomingData(ByVal Index As Long, ByVal DataLength As Long)
>     Dim Buffer() As Byte
>     Dim Data() As Byte
>     Dim pLength As Long
>     frmServer.Socket(Index).GetData Buffer(), vbUnicode, DataLength
>     'IncomingBytes = IncomingBytes + DataLength
>     TempPlayer(Index).Buffer.WriteBytes Buffer()
>
>     'TempPlayer(Index).Buffer.DecompressBuffer
>     If TempPlayer(Index).Buffer.Length >= 1 Then
>         pLength = TempPlayer(Index).Buffer.ReadByte(False)
>
>         If pLength < 0 Then
>             Exit Sub
>         End If
>     End If
>
>     Do While pLength > 0 And pLength <= TempPlayer(Index).Buffer.Length - 1
>
>         If pLength <= TempPlayer(Index).Buffer.Length - 1 Then
>             TempPlayer(Index).DataPackets = TempPlayer(Index).DataPackets + 1
>             TempPlayer(Index).Buffer.ReadByte
>             Data() = TempPlayer(Index).Buffer.ReadBytes(pLength + 1)
>             'If EncryptPackets Then
>             '    Encryption_XOR_DecryptByte Data(), PacketKeys(Player(Index).PacketInIndex)
>             '    Player(Index).PacketInIndex = Player(Index).PacketInIndex + 1
>             '    If Player(Index).PacketInIndex > PacketEncKeys - 1 Then Player(Index).PacketInIndex = 0
>             'End If
>             HandleData Index, Data()
>         End If
>
>         pLength = 0
>
>         If TempPlayer(Index).Buffer.Length >= 1 Then
>             pLength = TempPlayer(Index).Buffer.ReadByte(False)
>
>             If pLength < 0 Then
>                 Exit Sub
>             End If
>         End If
>
>     Loop
>
>     If GetPlayerAccess(Index) <= 0 Then
>
>         ' Check for data flooding
>         If TempPlayer(Index).DataBytes > 1000 Then
>             HackingAttempt Index, "Data Flooding"
>             Exit Sub
>         End If
>
>         ' Check for packet flooding
>         If TempPlayer(Index).DataPackets > 25 Then
>             HackingAttempt Index, "Packet Flooding"
>             Exit Sub
>         End If
>     End If
>
>     ' Check if elapsed time has passed
>     'Player(Index).DataBytes = Player(Index).DataBytes + DataLength
>     If GetTickCount >= TempPlayer(Index).DataTimer Then
>         TempPlayer(Index).DataTimer = GetTickCount + 1000
>         TempPlayer(Index).DataBytes = 0
>         TempPlayer(Index).DataPackets = 0
>     End If
>
>     If TempPlayer(Index).Buffer.Length <= 1 Then TempPlayer(Index).Buffer.Flush
> End Sub
> ```
> I haven't tested these subs, but give them a try, and again, remember to make a backup of your current Subs!

I have tried something similar before and didnt work, I tried these had the same effect as before, but im not receiveing nothing at all now from server.

In SendData the data is wrote in longs anyway, so I still thing it need to read as long, im not sure.

@Robin:

> The packet system is a rushed mess, to be quite honest. Don't get me wrong, it's infinity better than the old Eclipse stuff, but I'm not happy with it.
>
> I'll probably be switching out the core procedures with the ones from MR.

I'll check out Mirage Realms and see what happends
Link to comment
Share on other sites

Ok I've masterd it, cant use Byte for sending data as its to small sending large strings your get overflow.

Mirage Uses Integer so I converted to that instead, I noticed that its not just the header being change its the way the packet is being wrote to the buffer inside SendDataTo

Normal it write all data to the buffer as a Long, reads the long the reads the packet header which is also a long.

I plan to make it all Integer, saving to bytes on each packet header, then also saving 2 bytes for the amount of packets your putting in the packet itsself.

****** UPDATE ******
I've managed to get it all fixed up everything sends useing Integer now, All the packets are now sending there proper data types. I just done a comparison of the packet sending to the old server and im saving around 250 Bytes just from logging in. Acira and I done some test and even in runtime his ping is 30 he lives in Bulgaria/Sofia Im from UK.

I'll tweak it speak to Robin, and hopefully he can get it in the latest Origins. :) Thanks all for your help.
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...