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 ListenServer))
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 ListenServer))
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
Close()
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 ListenServer()
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
Close()
FormBot.Close()
Return
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 debug As String = "recu : id = " & packet_id & ", taille = " & packet_length & vbCrLf ' on ecrit l'ID et la taille
packetid = packet_id
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
Dim j As Integer = 1
For i = 0 To packet_length - 1 ' on fait defiler tout les octets recus
debug &= packet(i).ToString("X2") ' on ecrit l'octet proprement pour de bon ce coup ci
j += 1
If j = 16 Then
debug &= vbCrLf
j = 1
End If
Next ' au suivant
If IsServer = True Then
MyLog.WritePaquet(packet_id, packet_length, True)
Dim PI As New PacketIdentificator
MyLog.WriteLog("Reception id -> " & packet_id & ", Class Name -> " & PI.GetClasseName(packet_id) & ", Lenght -> " & packet_length)
'ReceiveId(packet_id, New BigEndianReader(New System.IO.MemoryStream(packet)))
Else
MyLog.WritePaquet(packet_id, packet_length, False)
Dim PI As New PacketIdentificator
MyLog.WriteLog("Envoi id -> " & packet_id & ", Class Name -> " & PI.GetClasseName(packet_id) & ", Lenght -> " & packet_length)
'ReceiveId(packet_id, New BigEndianReader(New System.IO.MemoryStream(packet)))
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
'End Select
' End Sub
End Class