[Updated] SendNetworkFileEx - replaces native commands

Share your advanced PureBasic knowledge/code with the community.
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

[Updated] SendNetworkFileEx - replaces native commands

Post by Inf0Byt3 »

Code updated for 5.20+

I felt i have to share this with you :D. If someone finds a method to send multiple files at the same time, please please share it.

Enjoy!!

Code: Select all

;Original author: Benubi
;Modified by Inf0Byt3
;http://www.purebasic-lounge.de/viewtopic.php?t=2685

#PB_SendFile_ID="PBMGSFLE"
Structure PB_SendFileHeader
      id.b[8]
      dwlength.l
      filename.s{256}
EndStructure
DataSection
    PB_SendFile_ID:
    Data.s #PB_SendFile_ID
    Data.b 0,0,0
EndDataSection


Procedure Callback(dwCookie,tlen,dwlength)
 
 ProcedureReturn 1
 
EndProcedure

Procedure SendNetworkFileEx(ConnectionID,File$,dwCallBack,dwCookie,dwBuffer)     
  
 Protected TEMP.PB_SendFileHeader,tlen,rlen,result,fh,dwLength
 Protected Dim BUFFER.B(0)

 CopyMemory(?PB_SendFile_ID,@TEMP, SizeOf(PB_SendFileHeader))
 fh = ReadFile(#PB_Any,File$)
 If fh<>0
  If dwBuffer <= 0
   dwBuffer = 1500
  EndIf
     
  Dim BUFFER.B(dwBuffer)
  dwLength = Lof(fh)
  TEMP\dwlength = dwLength
  TEMP\filename = GetFilePart(File$)
  If dwLength >= 0
   SendNetworkData(ConnectionID,@TEMP,SizeOf(PB_SendFileHeader))
   
    While tlen < TEMP\dwlength
     If CallFunctionFast(dwCallBack,dwCookie,tlen,TEMP\dwlength) = #False
       CloseFile(fh)
       Dim BUFFER.B(0)
       ProcedureReturn #False
     EndIf
     rlen = dwBuffer
     If rlen + tlen > TEMP\dwLength
      rlen = TEMP\dwLength - tlen
     EndIf
     FileSeek(fh,tlen)
     ReadData(fh,@BUFFER(0),rlen)
     rlen = SendNetworkData(ConnectionID,@BUFFER(0),rlen)
     If rlen > 0
      tlen + rlen
      result = tlen
     Else
      SleepEx_(1,1)               
     EndIf
    Wend
   
   EndIf
   If IsFile(fh)
    CloseFile(fh)
   EndIf
   CallFunctionFast(dwCallBack,dwCookie,tlen,TEMP\dwlength)
  EndIf
 
  Dim BUFFER.b(0)
  ProcedureReturn result
 
EndProcedure

Procedure ReceiveNetworkFileEx(ConnectionID, Path.s, dwCallBack,dwCookie,dwBuffer)
 
 Protected TEMP.PB_SendFileHeader,result
 Protected tlen,fh,rlen
 Protected Dim BUFFER.B(0)
     
 If Path = ""
  Path = GetCurrentDirectory()
 EndIf
 If Right(Path,1) <> "\"
  Path + "\"
 EndIf
 If dwBuffer<=0
   dwBuffer=1500
 EndIf
 Dim BUFFER.B(dwBuffer)
 
 If ReceiveNetworkData(ConnectionID,@TEMP,SizeOf(PB_SendFileHeader))=SizeOf(PB_SendFileHeader)
  If CompareMemory(@TEMP,?PB_SendFile_ID,Len(#PB_SendFile_ID))             
   fh=CreateFile(#PB_Any,Path+TEMP\filename)
   If fh
   
    While tlen<TEMP\dwlength
     If CallFunctionFast(dwCallBack,dwCookie,Loc(fh),TEMP\dwlength)=#False
      Debug "CALLBACK: CANCEL ReceiveNetworkFileEx"
      Break
     EndIf
     rlen=dwBuffer
     If rlen + tlen > TEMP\dwLength
      rlen = TEMP\dwLength - tlen
     EndIf
     rlen=ReceiveNetworkData(ConnectionID,@BUFFER(0),rlen)
     If rlen>0
      tlen + rlen
      WriteData(fh,@BUFFER(0),rlen)
      FlushFileBuffers(fh)
      result = tlen
     Else
      SleepEx_(1,1)
     EndIf
    Wend
   
   Else
    Debug "Bad id!"
   EndIf
  Else
   Debug "error, bad size"
  EndIf
  If IsFile(fh)
   CloseFile(fh)
  EndIf
  CallFunctionFast(dwCallBack,dwCookie,TEMP\dwlength,TEMP\dwlength)
 Else
  Debug "Error, cannot open file"
 EndIf
 
 Dim BUFFER.b(0)
 ProcedureReturn result
 
EndProcedure
Last edited by Inf0Byt3 on Sat Nov 11, 2006 12:09 pm, edited 2 times in total.
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
Xombie
Addict
Addict
Posts: 898
Joined: Thu Jul 01, 2004 2:51 am
Location: Tacoma, WA
Contact:

Post by Xombie »

I don't yet have a use for this but it seems pretty nifty.

However, and I really really don't want to sound like I'm being picky but could we please not use all of the [!!!] type things in the topics here? I get enough of that from spam mail and usenet postings :? It really distracts from the topic and all other topics and implies that your code is more important than the rest of the code in the forum. Which could lead to escalation by some of the other members and before you know it, we'll have all capital letters with "AAAAAAA" or "!!!!!!!!!!!!!!!!!", etc... in the topics.

Again, I'm not trying to pick on you but I've seen this kind of thing happen in the past to other forums.

Having said all of that, thank you very much for the code 8)
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

No problem, i edited the title...
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
kinglestat
Enthusiast
Enthusiast
Posts: 746
Joined: Fri Jul 14, 2006 8:53 pm
Location: Malta
Contact:

Post by kinglestat »

this seems to be an interesting post
but how do you use it ?
can you provide a simple example ?

cheers

KingLestat
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

Yup, here it is... The include and examples included :). Here it is:

File:1->SendNetworkFileEX.7z
Image

Enjoy!

[BTW] Run the server first ;).
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

just came across this, nice..

- np
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

Oh, thank you. Although, like the original PB command, it has a problem. When you are sending a file, it creates a loop that 'freezes' the main window, as you don't call WindowEvent while sending. I'll try this days to make it a bit smarter, i mean to make it support multiple transfers in the same time. I am thinking to make something like TransferEvent(). When this is called in the main program loop, it just sends a little part of the file and then returns to the event handling part, allowing you to handle the window events correctly.
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
Post Reply