Voici le code complet fonctionnel. Il est par contre assez lent je trouve, mais je craint que cela soit la seule maniere d'eviter les erreurs.
Code : Tout sélectionner
; si fichier existe deja
Enumeration
#fichier_log = 10
#Window_transfert
#ListView_trans_pclocal
#Text_trans_pclocal
#Text_trans_pcdistant
#ListView_trans_pcdistant
#Text_trans_message
#ProgressBar_trans_fichier
#ProgressBar_trans_total
#Button_trans_envoi_fichier
#Button_trans_reception_fichier
#Button_stop_transfert
#Text_repertoire_distant
EndEnumeration
CreateFile(#fichier_log,"c:\log.txt")
If InitNetwork() = 0
MessageRequester("Error", "Can't initialize the network !", 0)
End
EndIf
Port = 6832
Global taille_buffer.l = 32384*10
Global *Buffer = AllocateMemory(taille_buffer)
Global *BufferTemporaire = AllocateMemory(taille_buffer)
Global fichier_a_recuperer.b
Global Dim nom_du_fichier_a_recuperer.s(99999)
Global recuperation_fini.b = 1
Declare envoi_message(id_du_serveur.l,message$)
Procedure.l reception_de_donne(identifiant_server.l)
Protected taille_buffer_recu.l = 0
Protected memoire_recu.l = 0
FillMemory(*BufferTemporaire,taille_buffer)
Repeat
FillMemory(*Buffer,taille_buffer)
taille_buffer_recu = ReceiveNetworkData(identifiant_server, *Buffer, taille_buffer)
If taille_buffer_recu = -1
Goto fin_boucle_attente2
EndIf
CopyMemory(*Buffer,*BufferTemporaire + memoire_recu,taille_buffer_recu)
memoire_recu + taille_buffer_recu
fin_boucle_attente2:
Until memoire_recu = taille_buffer
envoi_message(identifiant_server,"<bloc_recu><bloc_recu>")
FillMemory(*Buffer,taille_buffer)
CopyMemory(*BufferTemporaire,*Buffer,taille_buffer)
WriteData(#fichier_log,*buffer,taille_buffer)
WriteStringN(#fichier_log,"")
ProcedureReturn memoire_recu
EndProcedure
Procedure OpenWindow_Window_transfert()
If OpenWindow(#Window_transfert, 507, 146, 819, 525, "Transferts", #PB_Window_TitleBar|#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
If UseGadgetList(WindowID(#Window_transfert))
ExplorerListGadget(#ListView_trans_pclocal, 0, 45, 330, 415, "", #PB_Explorer_MultiSelect)
TextGadget(#Text_trans_pclocal, 120, 0, 100, 20, "PC LOCAL")
TextGadget(#Text_trans_pcdistant, 585, 0, 120, 20, "PC DISTANT")
ListIconGadget(#ListView_trans_pcdistant, 490, 40, 325, 415, "", 150, #PB_ListIcon_MultiSelect)
TextGadget(#Text_trans_message, 0, 460, 815, 20, "", #PB_Text_Border|#PB_Text_Center)
ProgressBarGadget(#ProgressBar_trans_fichier, 0, 480, 815, 20, 0, 100, #PB_ProgressBar_Smooth)
ProgressBarGadget(#ProgressBar_trans_total, -5, 500, 820, 20, 0, 100, #PB_ProgressBar_Smooth)
ButtonGadget(#Button_trans_envoi_fichier, 340, 155, 140, 25, "Envoi des fichiers >>>")
ButtonGadget(#Button_trans_reception_fichier, 340, 220, 140, 25, "<<< Reception des fichiers")
ButtonGadget(#Button_stop_transfert, 340, 400, 140, 25, "STOP TRANSFERT")
TextGadget(#Text_repertoire_distant, 490, 20, 325, 15, "", #PB_Text_Border|#PB_Text_Center)
EndIf
EndIf
AddGadgetColumn(#ListView_trans_pcdistant,1,"Taille",100)
SetGadgetItemAttribute(#ListView_trans_pcdistant,0,#PB_Explorer_ColumnWidth,220,0)
SetGadgetItemAttribute(#ListView_trans_pclocal,0,#PB_Explorer_ColumnWidth,220,0)
SetGadgetItemAttribute(#ListView_trans_pclocal,0,#PB_Explorer_ColumnWidth,100,1)
SetGadgetItemAttribute(#ListView_trans_pclocal,0,#PB_Explorer_ColumnWidth,0,2)
SetGadgetItemAttribute(#ListView_trans_pclocal,0,#PB_Explorer_ColumnWidth,0,3)
SetClassLong_(WindowID(#Window_transfert), #GCL_STYLE, GetClassLong_(WindowID(#Window_transfert), #GCL_STYLE) | #CS_DBLCLKS)
EndProcedure
Procedure.b attente_reponse(identifiant_server.l,reponse.s)
Repeat
SEvent = NetworkClientEvent(identifiant_server)
If SEvent = #PB_NetworkEvent_Data
FillMemory(*buffer,taille_buffer)
reception_de_donne(identifiant_server)
If FindString(PeekS(*buffer+8) , reponse)
ProcedureReturn 1
EndIf
FillMemory(*buffer,taille_buffer)
EndIf
ForEver
EndProcedure
Procedure lecteurs_initiaux(recup_init.s)
ClearGadgetItems(#ListView_trans_pcdistant)
drive.s
For i=2 To 26
drive = StringField(recup_init,i,"|")
If drive <> ""
AddGadgetItem(#ListView_trans_pcdistant,-1,drive)
EndIf
Next i
EndProcedure
Procedure envoi_message(id_du_serveur.l,message$)
PokeQ(*Buffer, Len(message$))
PokeS(*Buffer + 8, message$)
SendNetworkData(id_du_serveur, *buffer,taille_buffer)
FillMemory(*buffer,taille_buffer)
Delay(100)
EndProcedure
Global Dim type_fichier.b(200000)
OpenWindow_Window_transfert()
ServerID = OpenNetworkConnection("127.0.0.1", Port)
If ServerID
envoi_message(ServerID,"<liste_initiale><liste_initiale>")
Else
MessageRequester("PureBasic - Client", "Serveur introuvable.", 0)
End
EndIf
Repeat
event = WaitWindowEvent(5)
Gevent = EventGadget()
; ******************************************************************************
; ******************************** RESEAU ************************************
; ******************************************************************************
SEvent = NetworkClientEvent(ServerID)
If SEvent
Select SEvent
Case #PB_NetworkEvent_Data
taille_recu.l = reception_de_donne(ServerID)
If taille_recu = -1
Goto fin_boucle_infinie
EndIf
If FindString(PeekS(*buffer+8),"<numero_client>")
Debug "je suis le numero " + StringField(PeekS(*buffer+8),2,"<numero_client>")
Delay(100)
Goto fin_boucle_infinie
EndIf
If FindString(PeekS(*buffer+8),"<liste_initiale>")
lecteurs_initiaux(StringField(PeekS(*buffer+8),2,"<liste_initiale>"))
EndIf
If FindString(PeekS(*buffer+8),"<renvoi_repertoire>")
ClearGadgetItems(#ListView_trans_pcdistant)
string_taille.l = PeekQ(*buffer)
string_tempo.s = PeekS(*buffer+8,taille_recu-8)
retour_repertoire:
If Len(string_tempo) < string_taille
FillMemory(*buffer,taille_buffer)
taille_recu = reception_de_donne(ServerID)
If taille_recu = -1
Delay(100)
Goto retour_repertoire
EndIf
string_tempo + PeekS(*buffer,taille_recu)
Goto retour_repertoire
EndIf
Dim type_fichier(200000)
For i = 2 To 200000
If StringField(string_tempo,i,"?") <> ""
If Mid( StringField(string_tempo,i,"?") , 1,1) = "d"
AddGadgetItem(#ListView_trans_pcdistant,-1,Mid( StringField(string_tempo,i,"?") , 2, Len( StringField(string_tempo,i,"?") )-1 ) );,ImageID(CatchImage(#PB_Any, ?Image_ButtonImage_dossier)))
EndIf
If Mid( StringField(string_tempo,i,"?") , 1,1) = "f"
type_fichier(i-2) = 1
; image retiré (fait planter)
AddGadgetItem(#ListView_trans_pcdistant,-1,StringField(Mid( StringField(string_tempo,i,"?") , 2, Len( StringField(string_tempo,i,"?") )-1 ),1,"|") + Chr(10) + StringField(Mid( StringField(string_tempo,i,"?") , 2, Len( StringField(string_tempo,i,"?") )-1 ),2,"|"));,imageid_fichier)
EndIf
Else
Goto fin_next_DD
EndIf
Next i
fin_next_DD:
; retire double ".."
If GetGadgetItemText(#ListView_trans_pcdistant,0) = ".." And GetGadgetItemText(#ListView_trans_pcdistant,1) = ".."
RemoveGadgetItem(#ListView_trans_pcdistant,0)
For i = 1 To CountGadgetItems(#ListView_trans_pcdistant)+1
type_fichier(i-1) = type_fichier(i)
Next i
EndIf
EndIf
If FindString(PeekS(*buffer+8),"<FILE>")
CopyMemory(*Buffer+8,*Buffer,taille_recu-8)
CreateFile(0,GetGadgetText(#ListView_trans_pclocal) + GetFilePart(StringField(PeekS(*buffer),2,"<FILE>")) )
taille_a_recevoir = Val(StringField(PeekS(*buffer),2,"<SIZE>"))
taille_recu2.l = FindString( PeekS(*buffer) , "<SIZE>", FindString( PeekS(*buffer),"<SIZE>") + 4) + 5
taille_buffer_recu = taille_recu
If taille_a_recevoir < (taille_buffer_recu - taille_recu2 - 8)
WriteData(0,*buffer + taille_recu2,taille_a_recevoir)
FillMemory(*buffer,taille_buffer)
envoi_message(ServerID,"<FIN>fichier<FIN>")
recuperation_fini = 1
CloseFile(0)
SetGadgetState(#ProgressBar_trans_fichier,0)
Goto quittage
Else
WriteData(0,*buffer + taille_recu2,taille_buffer_recu - taille_recu2 - 8)
EndIf
taille_finale = taille_buffer_recu - taille_recu2 - 8
FillMemory(*buffer,taille_buffer)
SetGadgetText(#Text_trans_message,GetFilePart(StringField(PeekS(*buffer),2,"<FILE>")) )
SetGadgetAttribute(#ProgressBar_trans_fichier,#PB_ProgressBar_Maximum,taille_a_recevoir)
compteur_erreur = 0
boucle_fichier:
taille_buffer_recu = reception_de_donne(ServerID)
compteur_erreur = 0
If taille_buffer_recu + taille_finale >= taille_a_recevoir
WriteData(0,*buffer,taille_a_recevoir - taille_finale)
FillMemory(*buffer,taille_buffer)
envoi_message(ServerID,"<FIN>fichier<FIN>")
recuperation_fini = 1
CloseFile(0)
SetGadgetState(#ProgressBar_trans_fichier,0)
Goto quittage
Else
WriteData(0,*buffer,taille_buffer_recu)
taille_finale + taille_buffer_recu
SetGadgetState(#ProgressBar_trans_fichier,taille_finale)
EndIf
FillMemory(*buffer,taille_buffer)
Goto boucle_fichier
quittage:
EndIf
FillMemory(*buffer,taille_buffer)
EndSelect
EndIf
fin_boucle_infinie:
If event = #PB_Event_CloseWindow
EventWindow = EventWindow()
If EventWindow = #Window_transfert
CloseWindow(#Window_transfert)
Quit = 1
EndIf
EndIf
; ******************************************************************************
; ******************************** FENETRE ***********************************
; ******************************************************************************
If (event = #WM_LBUTTONDBLCLK And Gevent = #ListView_trans_pcdistant) Or ( event = #PB_Event_Gadget And Gevent = #Button_trans_reception_fichier ); si double clique dans pc distant
If GetGadgetState(#ListView_trans_pcdistant) <> -1
compteur.l = 0
Dim multi_fichier_a_recuperer.s(200000)
compteur_multi_fichier_a_recuperer = 0
For i = 0 To CountGadgetItems(#ListView_trans_pcdistant)
If GetGadgetItemState(#ListView_trans_pcdistant,i) = 1
If type_fichier(i) = 1
multi_fichier_a_recuperer(compteur_multi_fichier_a_recuperer) = GetGadgetItemText(#ListView_trans_pcdistant,i)
compteur_multi_fichier_a_recuperer + 1
EndIf
compteur + 1
EndIf
Next i
If type_fichier(GetGadgetState(#ListView_trans_pcdistant)) = 0 ; si double click sur repertoire
repertoire_de_depart.s
If GetGadgetItemText(#ListView_trans_pcdistant,GetGadgetState(#ListView_trans_pcdistant)) <> ".."
repertoire_de_depart = repertoire_de_depart + GetGadgetItemText(#ListView_trans_pcdistant,GetGadgetState(#ListView_trans_pcdistant)) + "\"
Else
repertoire_de_depart2.s = ""
For x_list = 1 To 255
If StringField(repertoire_de_depart,x_list,"\") = ""
x_list - 2
Goto suite_x_list_repertoire
EndIf
Next x_list
suite_x_list_repertoire:
For y_list = 1 To x_list
repertoire_de_depart2 + StringField(repertoire_de_depart,y_list,"\") + "\"
Next y_list
repertoire_de_depart = repertoire_de_depart2
EndIf
If repertoire_de_depart <> ""
envoi_message(ServerID,"<liste_repertoire>" + repertoire_de_depart + "<liste_repertoire>")
Else
envoi_message(ServerID,"<liste_initiale><liste_initiale>")
EndIf
ElseIf type_fichier(GetGadgetState(#ListView_trans_pcdistant)) = 1 And GetGadgetText(#ListView_trans_pclocal) <> "" ; si double click sur fichier / SERVEUR -> CLIENT
If compteur_multi_fichier_a_recuperer = 1
envoi_message(ServerID,"<Je veux le fichier>" + repertoire_de_depart + GetGadgetItemText(#ListView_trans_pcdistant,GetGadgetState(#ListView_trans_pcdistant)) + "<Je veux le fichier>")
Else
y_list = 0
For x_list = 0 To CountGadgetItems(#ListView_trans_pcdistant)
If GetGadgetItemState(#ListView_trans_pcdistant,x_list) <> 0
fichier_a_recuperer = 1
nom_du_fichier_a_recuperer(y_list) = GetGadgetItemText(#ListView_trans_pcdistant,x_list)
y_list + 1
EndIf
Next x_list
EndIf
EndIf
EndIf
ElseIf (event = #WM_LBUTTONDBLCLK And Gevent = #ListView_trans_pclocal) Or ( event = #PB_Event_Gadget And Gevent = #Button_trans_envoi_fichier )
If GetGadgetItemState(#ListView_trans_pclocal,GetGadgetState(#ListView_trans_pclocal)) = 5 And repertoire_de_depart <> ""
taille_complete_fichier.l = Len("<FILE>" + repertoire_de_depart + GetGadgetItemText(#ListView_trans_pclocal,GetGadgetState(#ListView_trans_pclocal)) + "<FILE><SIZE>" + Str(FileSize(GetGadgetText(#ListView_trans_pclocal) + GetGadgetItemText(#ListView_trans_pclocal,GetGadgetState(#ListView_trans_pclocal)))) + "<SIZE>") + (FileSize(GetGadgetText(#ListView_trans_pclocal) + GetGadgetItemText(#ListView_trans_pclocal,GetGadgetState(#ListView_trans_pclocal))))
longueur_entete.l = Len("<FILE>" + repertoire_de_depart + GetGadgetItemText(#ListView_trans_pclocal,GetGadgetState(#ListView_trans_pclocal)) + "<FILE><SIZE>" + Str(FileSize(GetGadgetText(#ListView_trans_pclocal) + GetGadgetItemText(#ListView_trans_pclocal,GetGadgetState(#ListView_trans_pclocal)))) + "<SIZE>")
ReadFile(0,GetGadgetText(#ListView_trans_pclocal) + GetGadgetItemText(#ListView_trans_pclocal,GetGadgetState(#ListView_trans_pclocal)))
SetGadgetText(#Text_trans_message,GetGadgetItemText(#ListView_trans_pclocal,GetGadgetState(#ListView_trans_pclocal)))
SetGadgetAttribute(#ProgressBar_trans_fichier,#PB_ProgressBar_Maximum,taille_complete_fichier/taille_buffer)
For i = 0 To taille_complete_fichier/taille_buffer
*Buffer = AllocateMemory(taille_buffer)
If i = 0
PokeQ(*buffer,longueur_entete )
PokeS(*buffer + 8,"<FILE>" + repertoire_de_depart + GetGadgetItemText(#ListView_trans_pclocal,GetGadgetState(#ListView_trans_pclocal)) + "<FILE><SIZE>" + Str(FileSize(GetGadgetText(#ListView_trans_pclocal) + GetGadgetItemText(#ListView_trans_pclocal,GetGadgetState(#ListView_trans_pclocal)))) + "<SIZE>",longueur_entete)
ReadData(0, *Buffer + longueur_entete + 8, taille_buffer - longueur_entete - 8)
Else
ReadData(0, *Buffer,taille_buffer)
EndIf
SendNetworkData(ServerID, *buffer,taille_buffer)
FillMemory(*buffer,taille_buffer)
SetGadgetState(#ProgressBar_trans_fichier,i)
Delay(10) ; tester ou remettre 100
Next i
CloseFile(0)
Else
Debug "repertoire"
EndIf
EndIf
If fichier_a_recuperer = 1 And recuperation_fini = 1
recuperation_fini = 0
Delay(100)
envoi_message(ServerID,"<Je veux le fichier>" + repertoire_de_depart + nom_du_fichier_a_recuperer(0) + "<Je veux le fichier>")
Delay(100)
If nom_du_fichier_a_recuperer(1) <> ""
x_list = 1
Repeat
nom_du_fichier_a_recuperer(x_list-1) = nom_du_fichier_a_recuperer(x_list)
x_list + 1
Until nom_du_fichier_a_recuperer(x_list) = ""
nom_du_fichier_a_recuperer(x_list - 1) = ""
Else
fichier_a_recuperer = 0
nom_du_fichier_a_recuperer(0) = ""
EndIf
EndIf
ForEver
Code : Tout sélectionner
;repertoires impossible a ouvrir
Enumeration
#fichier_a_lire
#repertoire
#fichier_log
EndEnumeration
CreateFile(#fichier_log,"c:\log serveur.txt")
Global Dim numero_du_client.l(1000)
Global numero_connexion.l = 0
If InitNetwork() = 0
MessageRequester("Erreur", "Initialisation du réseau impossible.", 0)
End
EndIf
Port = 6832
Global taille_buffer.l = 32384*10
Global *Buffer = AllocateMemory(taille_buffer)
Global *BufferTemporaire = AllocateMemory(taille_buffer)
Global SEvent.l
Global Dim client_en_cours.b(99999999)
; getalldrive
Global Dim Drive_Nom.s(25),nombre_disque
Declare.b attente_reponse(identifiant_server.l,reponse.s)
Procedure.l reception_de_donne(identifiant_server.l)
Protected taille_buffer_recu.l = 0
Protected memoire_recu.l = 0
Protected compteur.l = 0
FillMemory(*BufferTemporaire,taille_buffer)
Repeat
FillMemory(*Buffer,taille_buffer)
taille_buffer_recu = ReceiveNetworkData(identifiant_server, *Buffer, taille_buffer)
If taille_buffer_recu = -1
compteur + 1
Delay(10)
If compteur > 100
ProcedureReturn -1
EndIf
Goto fin_boucle_attente2
EndIf
WriteData(#fichier_log,*buffer,taille_buffer_recu)
WriteStringN(#fichier_log,"")
CopyMemory(*Buffer,*BufferTemporaire + memoire_recu,taille_buffer_recu)
memoire_recu + taille_buffer_recu
fin_boucle_attente2:
Until memoire_recu = taille_buffer
FillMemory(*Buffer,taille_buffer)
CopyMemory(*BufferTemporaire,*Buffer,taille_buffer)
ProcedureReturn memoire_recu
EndProcedure
Procedure envoi_message(id_du_serveur.l,message$)
nombre_multiplication.l = Len(message$) / taille_buffer
If Len(message$) % taille_buffer = 0
nombre_multiplication - 1
EndIf
For i_envoi.l = 0 To nombre_multiplication
FillMemory(*Buffer,taille_buffer)
If i_envoi = 0
PokeQ(*Buffer, Len(message$))
PokeS(*Buffer + 8, Mid(message$,1,taille_buffer-8) )
Else
PokeS(*Buffer, Mid(message$,(i_envoi * taille_buffer) - 7,taille_buffer) )
EndIf
WriteData(#fichier_log,*buffer,taille_buffer)
WriteStringN(#fichier_log,"")
SendNetworkData(id_du_serveur, *buffer,taille_buffer)
attente_reponse(id_du_serveur,"<bloc_recu><bloc_recu>")
Next i_envoi
FillMemory(*buffer,taille_buffer)
EndProcedure
Procedure GetAllDrives()
MaskDrive = GetLogicalDrives_()
Mask = 1
lpFileSystemNameBuffer.s = Space(256)
nombre_disque=0
For i.l = 65 To 97
If MaskDrive & Mask
wNom$=Chr(i)+":\"
wNom2$ = Chr(i) + ":"
wType=GetDriveType_(WNom$)
If wtype=#DRIVE_FIXED
nombre_disque=nombre_disque+1
Drive_Nom(nombre_disque)= wNom2$
EndIf
EndIf
Mask << 1
Next i
EndProcedure
Procedure.s enumere_repertoire(numero_client,Directory$)
Protected Dim fichiers_dans_repertoire.s(10000)
Protected i_enumere.l = 0
Protected string_renvoyer.s = "<renvoi_repertoire>"
FillMemory(*buffer,taille_buffer)
start_enumere_repertoire:
If ExamineDirectory(#repertoire, Directory$, "*.*")
While NextDirectoryEntry(#repertoire)
If DirectoryEntryType(#repertoire) = #PB_DirectoryEntry_File ; si fichier
fichiers_dans_repertoire(i_enumere) = DirectoryEntryName(#repertoire)
i_enumere + 1
Else ; si repertoire
If chiffre_depart = 0 And DirectoryEntryName(#repertoire) <> "."
string_renvoyer + "?d.."
chiffre_depart = 1
EndIf
If DirectoryEntryName(#repertoire) <> "."
string_renvoyer + "?d" + DirectoryEntryName(#repertoire)
EndIf
EndIf
Wend
FinishDirectory(#repertoire)
For x_enumere = 0 To i_enumere-1
string_renvoyer + "?f" + fichiers_dans_repertoire(x_enumere) + "|" + Str(FileSize(Directory$ + fichiers_dans_repertoire(x_enumere)))
Next x_enumere
envoi_message(numero_client,string_renvoyer)
ProcedureReturn string_renvoyer
Else ; impossible de charger le repertoire
ProcedureReturn string_renvoyer
EndIf
EndProcedure
Procedure.b attente_reponse(identifiant_server.l,reponse.s)
compteur_interne.l = 0
Repeat
Delay(10)
SEvent = NetworkServerEvent()
If SEvent = #PB_NetworkEvent_Data
FillMemory(*buffer,taille_buffer)
reception_de_donne(identifiant_server.l)
If FindString(PeekS(*buffer+8) , reponse)
ProcedureReturn 1
EndIf
FillMemory(*buffer,taille_buffer)
EndIf
compteur_interne + 1
If compteur_interne = 100
ProcedureReturn 0
EndIf
ForEver
EndProcedure
Procedure reception_donnee(numero_client.l)
Protected numero_fichier.l
Protected compteur_erreur.l = 0
taille_actuelle.l = 0
affiche_string.b = 0
fichier_en_approche.b = 0
taille_a_recevoir.l = 0
taille_recu.l = 0
taille_buffer_recu.l = 0
taille_finale.l
; prepare si fichier a receptionner
For reception_donnee_i = 0 To 1000
If Not IsFile(reception_donnee_i)
numero_fichier = reception_donnee_i
Goto suite_reception_donnee
EndIf
Next reception_donnee_i
compteur_erreur= 0
suite_reception_donnee:
taille_buffer_recu = reception_de_donne(numero_client)
Delay(100)
If compteur_erreur > 10
If IsFile(numero_fichier)
CloseFile(numero_fichier)
EndIf
ProcedureReturn
EndIf
If taille_buffer_recu = -1
FillMemory(*buffer,taille_buffer)
Delay(100)
compteur_erreur + 1
Goto suite_reception_donnee
EndIf
If taille_actuelle = 0
taille_a_recevoir = PeekQ(*Buffer)
CopyMemory(*Buffer+8,*Buffer,taille_buffer_recu-8)
; si <STRING>
If FindString(PeekS(*buffer) , "<STRING>",1)
affiche_string = 1
CopyMemory(*Buffer+8,*Buffer,taille_buffer_recu-Len("<STRING>") )
taille_recu = taille_buffer_recu-Len("<STRING>")
FillMemory(*buffer,taille_buffer)
; si <FILE>
ElseIf FindString(PeekS(*buffer) , "<FILE>",1)
fichier_en_approche = 1
CreateFile(numero_fichier,StringField(PeekS(*buffer),2,"<FILE>") )
taille_a_recevoir = Val(StringField(PeekS(*buffer),2,"<SIZE>"))
taille_recu = FindString( PeekS(*buffer) , "<SIZE>", FindString( PeekS(*buffer),"<SIZE>") + 4) + 5
WriteData(numero_fichier,*buffer + taille_recu,taille_buffer_recu - taille_recu - 8)
taille_finale = taille_buffer_recu - taille_recu - 8
FillMemory(*buffer,taille_buffer)
compteur_erreur = 0
boucle_fichier:
taille_buffer_recu = ReceiveNetworkData(numero_client, *Buffer, taille_buffer)
If taille_buffer_recu = -1
compteur_erreur + 1
Delay(100)
If compteur_erreur > 10
If IsFile(numero_fichier)
CloseFile(numero_fichier)
EndIf
ProcedureReturn
EndIf
Goto boucle_fichier
EndIf
compteur_erreur = 0
If taille_buffer_recu + taille_finale > taille_a_recevoir
WriteData(numero_fichier,*buffer,taille_a_recevoir - taille_finale)
FillMemory(*buffer,taille_buffer)
envoi_message(numero_client,"<FIN>fichier<FIN>")
Goto quittage
Else
WriteData(numero_fichier,*buffer,taille_buffer_recu)
taille_finale + taille_buffer_recu
EndIf
FillMemory(*buffer,taille_buffer)
Goto boucle_fichier
; si <Je veux le fichier> envoi d'un fichier vers le client
ElseIf FindString(PeekS(*buffer) , "<Je veux le fichier>",1)
ClearDebugOutput()
fichier_a_recuperer.s = StringField(PeekS(*buffer),2,"<Je veux le fichier>")
taille_fichier_a_recuperer.l = FileSize(StringField(PeekS(*buffer),2,"<Je veux le fichier>"))
message$ = "<FILE>" + fichier_a_recuperer + "<FILE><SIZE>" + Str(taille_fichier_a_recuperer) + "<SIZE>"
taille_complete_fichier = Len(message$) + taille_fichier_a_recuperer
longueur_entete = Len(message$)
ReadFile(#fichier_a_lire,fichier_a_recuperer)
For i = 0 To (taille_complete_fichier/taille_buffer)
FillMemory(*buffer,taille_buffer)
If i = 0
PokeQ(*buffer,longueur_entete )
PokeS(*buffer + 8,"<FILE>" + fichier_a_recuperer + "<FILE><SIZE>" + Str(taille_fichier_a_recuperer) + "<SIZE>",longueur_entete)
ReadData(#fichier_a_lire, *Buffer + longueur_entete + 8, taille_buffer - longueur_entete - 8)
Else
ReadData(#fichier_a_lire, *Buffer,taille_buffer)
EndIf
SendNetworkData(numero_client, *buffer,taille_buffer)
FillMemory(*buffer,taille_buffer)
attente_reponse(numero_client,"<bloc_recu><bloc_recu>")
Next i
CloseFile(#fichier_a_lire)
If attente_reponse(numero_client,"<FIN>fichier<FIN>") = 1
envoi_message(numero_client,"<FIN>fichier<FIN>")
EndIf
; liste drives
ElseIf FindString(PeekS(*buffer) , "<liste_initiale>",1)
GetAllDrives()
drive_tempo.s = "<liste_initiale>"
For i=1 To nombre_disque
drive_tempo = drive_tempo + "|" + Drive_Nom(i)
Next i
envoi_message(numero_client,drive_tempo)
; liste repertoire
ElseIf FindString(PeekS(*buffer) , "<liste_repertoire>",1)
enumere_repertoire(numero_client, StringField(PeekS(*buffer),2,"<liste_repertoire>"))
FillMemory(*buffer,taille_buffer)
EndIf
EndIf
quittage:
If IsFile(numero_fichier)
CloseFile(numero_fichier)
EndIf
client_en_cours(numero_client) = 0
EndProcedure
If CreateNetworkServer(0, Port)
Repeat
Delay(5)
SEvent = NetworkServerEvent()
If SEvent
ClientID = EventClient()
If client_en_cours(ClientID) = 1
Goto fin_boucle
EndIf
If SEvent = #PB_NetworkEvent_Connect
PokeQ(*buffer,Len("<numero_client>" + Str(ClientID) + "<numero_client>"))
PokeS(*buffer+8,"<numero_client>" + Str(ClientID) + "<numero_client>")
SendNetworkData(ClientID,*buffer,taille_buffer)
FillMemory(*buffer,taille_buffer)
ElseIf SEvent = #PB_NetworkEvent_Data
client_en_cours(ClientID) = 1
CreateThread(@reception_donnee(),ClientID)
ElseIf SEvent = #PB_NetworkEvent_Disconnect
Debug "fin de connexion de " + Str(ClientID)
EndIf
EndIf
fin_boucle:
Until Quit = 1
CloseNetworkServer(0)
Else
MessageRequester("Error", "Can't create the server (port in use ?).", 0)
EndIf