VB/VB.Net Envoyer un paquet avec un networkstream

  • Auteur de la discussion Anonymous
  • Date de début
A

Anonymous

Invité
#1
Bonsoir,

j'utilise la class dofusStream des sources de ToOnS:
Imports System.Net.Sockets


Namespace Dofus
Public Class DofusReader
Inherits IO.BinaryReader


Sub New(ByVal input As IO.Stream)
MyBase.New(input)
End Sub

Sub New(ByVal input As IO.Stream, ByVal encoding As System.Text.Encoding)
MyBase.New(input, encoding)
End Sub
Public Function readUnsignedByte()
Return ReadByte()
End Function
Public Overrides Function ReadByte() As Byte
Return MyBase.ReadByte()
End Function

Public Function ReadUnSignedshort()
Return ReadUInt16()
End Function

Public Overrides Function ReadUInt16() As UShort
Return (CUShort(ReadByte()) << 8) + ReadByte()
End Function

Public Overrides Function ReadBoolean() As Boolean
Return ReadByte() = 1
End Function

Public Function ReadShort()
Return ReadInt16()
End Function

Public Overrides Function ReadInt16() As Short
Dim Value As UShort = ReadUInt16()
If Value > Short.MaxValue Then
Dim Value2 As Short = -(UShort.MaxValue - Value) - 1
Return Value2
End If
Return Value
End Function

Public Function ReadInt()
Return ReadInt32()
End Function

Public Overrides Function ReadInt32() As Integer
Dim Value As UInteger = ReadUInt32()
If Value > Integer.MaxValue Then
Dim Value2 As Integer = -(UInteger.MaxValue - Value) - 1
Return Value2
End If
Return Value
End Function

Public Overrides Function ReadUInt32() As UInteger
Return (CUInt(ReadByte()) << 24) + (CUInt(ReadByte()) << 16) + (CUInt(ReadByte()) << 8) + ReadByte()
End Function

Public Overrides Function ReadDouble() As Double
Dim Bytes() As Byte = ReadBytes(8)
Array.Reverse(Bytes)
Return BitConverter.ToDouble(Bytes, 0)
End Function

Public Function ReadUTF()
Return ReadString()
End Function

Public Overrides Function ReadString() As String
Dim ByteArray() As Byte = MyBase.ReadBytes(ReadUInt16())
Return System.Text.Encoding.UTF8.GetString(ByteArray)
End Function

End Class
Public Class DofusWriter
Shared m_Bytes As New List(Of Byte)

Private Shared Function ComputeStaticHeader(ByVal PacketId As Integer, ByVal MessageLenghtType As Integer) As Short
Return (PacketId << 2) Or MessageLenghtType
End Function

Private Shared Function ComputeTypeLen(ByVal MessageLenght As Integer) As Short
Select Case MessageLenght
Case Is > UShort.MaxValue
Return 3
Case Is > Byte.MaxValue
Return 2
Case Is > 0
Return 1
Case Else
Return 0
End Select
End Function

Friend Shared Sub Send(ByVal PacketID As Integer, ByVal sock As NetworkStream)
Dim MessageLenghtType As Integer = ComputeTypeLen(m_Bytes.Count)
Dim Header As Short = ComputeStaticHeader(PacketID, MessageLenghtType)

Dim BytesToWrite(2 + MessageLenghtType + m_Bytes.Count - 1) As Byte
BytesToWrite(0) = Header >> 8
BytesToWrite(1) = Header - 256 * BytesToWrite(0)

Dim index As Integer

Select Case MessageLenghtType
Case 1
BytesToWrite(2) = m_Bytes.Count
index = 3
Case 2
BytesToWrite(2) = m_Bytes.Count >> 8
BytesToWrite(3) = m_Bytes.Count - 256 * BytesToWrite(2)
index = 4
Case 3
BytesToWrite(2) = m_Bytes.Count >> 16
BytesToWrite(3) = m_Bytes.Count >> 8
BytesToWrite(4) = m_Bytes.Count - 256 * BytesToWrite(3) - 256 * 256 * BytesToWrite(2)
index = 5
End Select
Dim identificator As New PacketIdentificator
identificator.GetClasseName(PacketID)
Console.WriteLine("envoie : id = " & PacketID & ", Class : " & identificator.ClasseName & ", taille = " & m_Bytes.Count) ' on ecrit l'ID et la taille
Console.Write("0x")
For i As Integer = index To m_Bytes.Count + index - 1
BytesToWrite(i) = m_Bytes(i - index)
Console.Write(BytesToWrite(i).ToString("X2"))
Next
Console.WriteLine()
sock.send(BytesToWrite)

m_Bytes.Clear()

End Sub

Public Sub WriteByte(ByVal Value As Byte)
m_Bytes.Add(Value)
End Sub


Public Sub WriteBoolean(ByVal Value As Boolean)
If Value Then
WriteByte(1)
Else
WriteByte(0) ' ton cryptpasse et pas bon c'est pas le bon projet que t'a lancé
End If
End Sub
Public Sub WriteShort(ByVal Value As Short)
WriteByte(CByte(Value >> 8))
WriteByte(CByte(Value))
End Sub
Public Sub WriteUShort(ByVal Value As UShort)
WriteByte(CByte(Value >> 8))
WriteByte(CByte(Value And 255))
End Sub
Public Sub WriteUInt32(ByVal Value As UInteger)
WriteByte(CByte(Value >> 24))
Value -= (Value >> 24) << 24
WriteByte(CByte(Value >> 16))
Value -= (Value >> 16) << 16
WriteByte(CByte(Value >> 8))
Value -= (Value >> 8) << 8
WriteByte(CByte(Value))
End Sub
Public Sub WriteDouble(ByVal Value As UInteger)
WriteByte(CByte(Value >> 56))
Value -= (Value >> 56) << 56
WriteByte(CByte(Value >> 48))
Value -= (Value >> 48) << 48
WriteByte(CByte(Value >> 40))
Value -= (Value >> 40) << 40
WriteByte(CByte(Value >> 32))
Value -= (Value >> 32) << 32
WriteByte(CByte(Value >> 24))
Value -= (Value >> 24) << 24
WriteByte(CByte(Value >> 16))
Value -= (Value >> 16) << 16
WriteByte(CByte(Value >> 8))
Value -= (Value >> 8) << 8
WriteByte(CByte(Value))
End Sub
Public Sub WriteString(ByVal Value As String)
WriteUTF(Value)
End Sub
Public Sub WriteUTF(ByVal Value As String)
Dim BytesToWrite() As Byte = System.Text.Encoding.UTF8.GetBytes(Value)
WriteUShort(BytesToWrite.Length)
For Each Bit As Byte In BytesToWrite
WriteByte(Bit)
Next
End Sub

Sub WriteInt32(ByVal Target As Integer)
Dim arr As Byte()
arr = BitConverter.GetBytes(Target)
WriteByte(arr(3))
WriteByte(arr(2))
WriteByte(arr(1))
WriteByte(arr(0))
End Sub

Sub WriteInt16(ByVal Target As Integer)
Dim arr As Byte()
arr = BitConverter.GetBytes(Target)
WriteByte(arr(1))
WriteByte(arr(0))
End Sub

Sub WriteSingle(ByVal Target As Double)
Dim arr As Byte()
arr = BitConverter.GetBytes(Target)
For Each bytte As Byte In arr
WriteByte(bytte)
Next
End Sub

End Class
End Namespace

Voilà ce que j'ai modifié:
Friend Shared Sub Send(ByVal PacketID As Integer, ByVal sock As NetworkStream)
J'ai remplacé le byval sock as socket en NetworkStream, j'essaye d'envoyer le paquet : 0.
Mais le problème vient d'ici:
sock.send(BytesToWrite)
On n'utilise pas send pour un NetworkStream.
Comment faire ? m'y suis-je bien pris ?

Voilà mon bout de code ou j'utilise dofusStream:
requestCount = requestCount + 1
Dim networkStream As NetworkStream = _
clientSocket.GetStream()
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
Dofus.DofusWriter.Send(0, networkStream)
 

ToOnS

Membre Actif
Inscrit
8 Avril 2009
Messages
974
Reactions
0
#2
essais en remplacant .send par .write
 
A

Anonymous

Invité
#3
Effectivement on dirais que c'est sa mais y me demande plusieurs argument:
Buffer as byte, offset as integer, size as integer

C'est quoi les autre arguments ?
Ou est-ce que je met mes bytes ?!?

Buffer c'est le nombre d'octect soit 1 puisque je n'envoi que l'id 0 soit 1 octect.
 
A

Anonymous

Invité
#4
J'ai trouvé ceci sur le net:
sendBytes = Encoding.ASCII.GetBytes(serverResponse)
networkStream.Write(sendBytes, 0, sendBytes.Length)

Voilà ce que sa donne chez moi:
Dim sendbytes = Encoding.ASCII.GetBytes(BytesToWrite)
sock.Write(sendbytes, 0, sendbytes.length)

mais j'ai une erreur à l'encodage:

Erreur 5 La résolution de surcharge a échoué, car aucun 'GetBytes' accessible ne peut être appelé avec ces arguments :
'Public Overridable Function GetBytes(s As String) As Byte()' : Impossible de convertir une valeur de type 'Tableau à 1 dimension(s) de Byte' en 'String'.
'Public Overridable Function GetBytes(chars() As Char) As Byte()' : Impossible de convertir une valeur de type 'Tableau à 1 dimension(s) de Byte' en 'Tableau à 1 dimension(s) de Char', car 'Byte' n'est pas dérivé de 'Char'. C:\Users\frydman\Desktop\Emulateur\Emulateur\dofusStream.vb 135 29 Emulateur
 

ToOnS

Membre Actif
Inscrit
8 Avril 2009
Messages
974
Reactions
0
#5
Encoding.ASCII.GetBytes ca c'est pour transformer les bytes en une chaine donc ca te sert a rien , on envoit des bytes directement , on est plus en 1.29 :

sock.Write(bytestowrite, 0, bytestowrite.length)
 
A

Anonymous

Invité
#6
Merci ToOnS, je vais tout recoder c'est du n'importe quoi

Bonne soirée
 
A

Anonymous

Invité
#7
Bonjour,

J'ai reposté ici pour ne pas creer un new topic.
Alors j'ai ma cle public et ma cle prive.
J'envoi au client ma cle public = salt
Mais la cle prive s'envoi en tableau de integer.... Ma cle prive est en string.
Quel conversion faire ? Faut t'il integrer dans l'envoi au client les caractere de ce genre ?
<q> <key>
 
Inscrit
29 Septembre 2011
Messages
393
Reactions
3
#8
Mais Lolo pourquoi tu dit que ta clef public = salt rien avoir.
 
Haut Bas