Public Class GameSock
Public WithEvents MySock As New BazSocket.BazSocket
Public WithEvents ServerSock As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Public MyLog As LogBot
Public ConsoleLog As LogConsole
Public Main As MainForm
Public FormBot As FormBot
Private ListenServerSock As New Thread(New ThreadStart(AddressOf ListenServerd))
Public IsDisconnectWant As Boolean = False
Private buffer(8191) As Byte
Private Data_Out(0) As Byte
Private Waiting As Integer
Private Packet As Integer = 0
'#############Socket##############
Public Sub Initialize(ByVal Ip As String, ByVal port As Integer)
ListenServerSock = New Thread(New ThreadStart(AddressOf ListenServerd))
ServerSock = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
ServerSock.Connect(Ip, port)
ListenServerSock.Start()
End Sub
Public Sub Close()
Try
ServerSock.Close()
ListenServerSock.Abort()
ListenServerSock.Join()
IsDisconnectWant = True
MySock.Disconnect()
MySock.Close()
Catch exe As Exception
ConsoleLog.WriteError(exe.Message)
End Try
MyLog.WriteError("Client déconnecté du socket de jeu")
End Sub
Private Sub Client_Close(ByVal sender As Object, ByVal e As System.EventArgs) Handles MySock.Closed
If IsDisconnectWant = False Then
FormBot.Close()
End If
End Sub
Private Sub Client_DataArrival(ByVal sender As Object, ByVal data() As Byte) Handles MySock.DataArrival
ServerSock.Send(data)
parsing(data, False)
End Sub
Private Sub ListenServerd()
While ServerSock.Connected ' boucle infinie tant que connecté
Dim i = ServerSock.Receive(buffer) ' on met dans i le nombre d'octets recu
Dim data(i - 1) As Byte ' on créé un tableau de bytes du nombre de bytes recus
Array.Copy(buffer, data, i) ' on copie buffer dans data
MySock.Send(data)
parsing(data, True)
If i = 0 Then
MyLog.WriteError("Fermeture de la connection innopinée")
End If
End While
End Sub
Public Sub parsing(ByVal data() As Byte, ByVal IsServer As Boolean)
Dim packetid As UShort
Dim index As Integer ' pour savoir ou on est
Dim id_and_length As UShort ' les 2 premiers octets (16 bits)
Dim packet_id As UShort ' les 14 premiers bits des 16
Dim packet_length_of As Byte ' les 2 derniers bits des 16
Dim packet_length As Integer ' la longueur du packet
Dim Packet_Start As Integer
Dim Packet_End As Integer
If Waiting > 1 Then ' le buffer etait trop petit ?
Dim data_temps(data.Length + Data_Out.Length - 1) As Byte ' on créé un tableau de byte temporaire
Array.Copy(Data_Out, 0, data_temps, 0, Data_Out.Length) ' on met le debut du paquet trop long dans le tableau temporaire
Array.Copy(data, 0, data_temps, Data_Out.Length, data.Length) ' on met la reception a la suite
data = data_temps ' on met le tableau temporaire dans le tableau de travail
End If
Do Until index = data.Length ' on traite jusque la fin
Packet_Start = index
id_and_length = data(index) * 256 + data(index + 1) ' les 2 premiers octets
packet_length_of = id_and_length And 3 ' on veut les 2 derniers bits donc on masque (and) avec 11 en binaire (3 en decimal)
packet_id = id_and_length >> 2 ' on veut les 14 premiers bits donc on decale les 16 bits de 2 bits vers la droite
index += 2 + packet_length_of ' on avance des 2 octets de id_and_length + du nombre d'octets de la taille de taille
Select Case packet_length_of ' on lit le bon nombre d'octet pour connaitre la taille des données
Case 0
packet_length = 0
Case 1
packet_length = data(index - 1)
Case 2
packet_length = 256 * data(index - 2) + data(index - 1)
Case 3
packet_length = 65536 * data(index - 3) + 256 * data(index - 2) + data(index - 1)
End Select
If index + packet_length > data.Length Then ' buffer trop petit ?
Waiting = packet_length + index - Packet_Start ' alors on le signale
ReDim Data_Out(data.Length - Packet_Start - 1) ' on redimensionne le tableau de debut du paquet trop long
Array.Copy(data, Packet_Start, Data_Out, 0, data.Length - Packet_Start) ' on copie le debut du paquet trop long
Exit Sub ' on sort
End If
Dim packet(0) As Byte ' on prepare le paquet
If packet_length > 0 Then ' si sa taille est plus grande que 0 on redimensionne
ReDim packet(packet_length - 1)
Array.Copy(data, index, packet, 0, packet_length) ' et on copie les donnée
End If
If IsServer = True Then
If packet_id = Nothing Then
MsgBox("problème")
Else
MyLog.WriteLog("Packet : ")
End If
End If
index += packet_length ' on met l'index a jour
Packet_End = index
If Packet_End = data.Length Then ' si ca tombe pile poil alors le buffer etait assez grand
Waiting = 0 ' on reset
ReDim Data_Out(0) ' on reset
End If
Loop
End Sub
'#############Paquets##############
Private Sub ReceiveId(ByVal ID As Integer, ByVal PacketData As BigEndianReader)
Packet = ID
Select Case ID
Case Network.Game.Character.Select.CharacterSelectedSuccessMessage.protocolId
Dim CSSM As New Network.Game.Character.Select.CharacterSelectedSuccessMessage
CSSM.deserialize(PacketData)
Dim Sexe As String = ""
Dim Classe As String = ""
If CSSM.infos.sex = False Then 'Le Sexe
Sexe = "Mâle"
ElseIf CSSM.infos.sex = True Then
Sexe = "Femelle"
End If
Select Case CSSM.infos.breed 'La classe
Case 1
Classe = "Féca"
Case 2
Classe = "Osamodas"
Case 3
Classe = "Enutrof"
Case 4
Classe = "Sram"
Case 5
Classe = "Xélor"
Case 6
Classe = "Ecaflip"
Case 7
Classe = "Eniripsa"
Case 8
Classe = "Iop"
Case 9
Classe = "Crâ"
Case 10
Classe = "Sadida"
Case 11
Classe = "Sacrieur"
Case 12
Classe = "Pandawa"
Case 13
Classe = "Roublard"
Case 14
Classe = "Zobal"
End Select
MyLog.WriteLog("Selection du personnage " & CSSM.infos.name)
' Case Network.Game.Chat.TextInformationMessage.protocolId
' Dim TIM As New Network.Game.Chat.TextInformationMessage
' TIM.deserialize(PacketData)
' If TIM.msgId = 193 Then
' MyLog.WriteLog("Vous êtes connectés")
' End If
End Select
End Sub
End Class