Reading/writing data from a pipe

Just starting out? Need help? Post your questions and find answers here.
localmotion34
Enthusiast
Enthusiast
Posts: 665
Joined: Fri Sep 12, 2003 10:40 pm
Location: Tallahassee, Florida

Reading/writing data from a pipe

Post by localmotion34 »

I am looking to include an image file as a binary, and then be able to decode it.

What i have come up with is using named pipes. However, i can't seem to get the data in the memory buffer written to the pipe.

Code: Select all

ImageSize = ?giffileend-?giffile
          pipe = CreateNamedPipe_("\\.\pipe\ImageDataPipe",#PIPE_ACCESS_INBOUND|#FILE_FLAG_OVERLAPPED,#PIPE_TYPE_BYTE|#PIPE_READMODE_BYTE|#PIPE_NOWAIT,1,ImageSize,ImageSize,#NMPWAIT_USE_DEFAULT_WAIT,#Null)
          Debug pipe 
          OpenFile(0,"\\.\pipe\ImageDataPipe")
          WriteData(0,?giffile,ImageSize)
          CloseFile(0)
          Debug ReadFile(0,"ImageDataPipe")
          Debug ReadByte(0)
          CloseFile(0)
          CloseHandle_(ImageDataPipe)
What i need to do is create a pipe, spit out the binary data, and then be able to read it just like i would any other file, and decode the GIf image.

Any ideas on how to do this?

Code: Select all

!.WHILE status != dwPassedOut
! Invoke AllocateDrink, dwBeerAmount
!MOV Mug, Beer
!Invoke Drink, Mug, dwBeerAmount
!.endw
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Here's a quick one I hacked up. Not sure if it'll be any use:

Code: Select all

ImageSize = ?giffileend-?giffile 
pipe = CreateNamedPipe_("\\.\pipe\ImageDataPipe",#PIPE_ACCESS_DUPLEX,#PIPE_TYPE_BYTE|#PIPE_READMODE_BYTE|#PIPE_NOWAIT,1,ImageSize,ImageSize,#NMPWAIT_USE_DEFAULT_WAIT,#Null) 
file=CreateFile_("\\.\pipe\ImageDataPipe", #GENERIC_WRITE|#GENERIC_READ, 0, 0, #OPEN_EXISTING	, #FILE_ATTRIBUTE_NORMAL, 0)
WriteFile_(file, ?giffile, ImageSize, @byteswritten, 0)
CloseHandle_(file)

For i = 0 To GetFileSize_(pipe, 0)-1
  ReadFile_(pipe, @temp, 1, @bytesread, 0)
  Debug "Byte " + Str(i) + " = " + Str(temp)
Next
CloseHandle_(ImageDataPipe) 
End

DataSection
giffile:
IncludeBinary "bullseye.ico"
giffileend:
I may look like a mule, but I'm not a complete ass.
localmotion34
Enthusiast
Enthusiast
Posts: 665
Joined: Fri Sep 12, 2003 10:40 pm
Location: Tallahassee, Florida

Post by localmotion34 »

After creating the pipe, is it possible to use the PB file functions like readfile() and readbyte() on the file inside the pipe?

Code: Select all

ImageSize = ?giffileend-?giffile
          pipe = CreateNamedPipe_("\\.\pipe\ImageDataPipe",#PIPE_ACCESS_INBOUND|#FILE_FLAG_OVERLAPPED,#PIPE_TYPE_BYTE|#PIPE_READMODE_BYTE|#PIPE_NOWAIT,1,ImageSize,ImageSize,#NMPWAIT_USE_DEFAULT_WAIT,#Null)
          Debug pipe 
          file=CreateFile_("\\.\pipe\ImageDataPipe", #GENERIC_WRITE|#GENERIC_READ, 0, 0, #OPEN_EXISTING   , #FILE_ATTRIBUTE_NORMAL, 0)
          WriteFile_(file, ?giffile, ImageSize, @byteswritten, 0)
          CloseHandle_(file) 
          ;OpenFile(0,"\\.\pipe\ImageDataPipe")
          ;WriteData(0,?giffile,ImageSize)
          
          Debug ReadFile(0,"ImageDataPipe")
          Debug ReadByte(0)
          CloseFile(0)

i get an error saying the file object was not initialized.

Code: Select all

!.WHILE status != dwPassedOut
! Invoke AllocateDrink, dwBeerAmount
!MOV Mug, Beer
!Invoke Drink, Mug, dwBeerAmount
!.endw
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Unsure.

I don't really know enough about pipes to answer that one I'm afraid.
I may look like a mule, but I'm not a complete ass.
Nico
Enthusiast
Enthusiast
Posts: 274
Joined: Sun Jan 11, 2004 11:34 am
Location: France

Post by Nico »

An Example:

The server

Code: Select all

#PIPE_ACCES_DUPLEX=3
#Gadget=0

Procedure Thread_Read_Write(Hpipe.l)
  While 1=1
  fConnected = ConnectNamedPipe_(Hpipe, #Null)
  If fConnected
    AddGadgetItem(#Gadget, -1, "Un client est connecté, lecture du fichier")
  
    ;Pensez à modifier le Buffer_Lecture pour qu'il soit de taille aussi grand
    ; que les données à recevoir
    Buffer_Lecture.s=Space(100)
    ReadFile_(Hpipe, @Buffer_Lecture, Len(Buffer_Lecture), @recu, 0)
    AddGadgetItem(#Gadget, -1, Buffer_Lecture)
   
    FlushFileBuffers_(Hpipe)
   
    Buffer_Ecriture.s=Space(100)
    Buffer_Ecriture="Fin de lecture du message, le serveur vous a déconnecté"
    WriteFile_(Hpipe, Buffer_Ecriture, Len(Buffer_Ecriture), @envoye, 0)

    If DisconnectNamedPipe_(Hpipe)
      AddGadgetItem(#Gadget, -1, "Déconnexion du client, le serveur est en attente de nouvelle connexion client")
    Else
      CloseHandle_(Hpipe)
      MessageRequester("Info","La connexion à échoué, le canal de communication est détruit")
    EndIf
  EndIf
  Delay(1000)
  Wend
EndProcedure


If OpenWindow(0,0,0,400,400,"Serveur Pipe Nommé",#PB_Window_SystemMenu|#PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0))
  EditorGadget (#Gadget,10,40,380,350,#PB_Container_Raised)
  StringGadget(1,10,10,200,20,"\\.\pipe\NomDePipe")
  ButtonGadget(2,240,10,100,20,"Lancer le Serveur")
 
  Repeat
    EventID.l = WaitWindowEvent()
    Select EventID
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 2
            NamePipe.s=GetGadgetText(1)
            Hpipe=CreateNamedPipe_(NamePipe, #PIPE_ACCES_DUPLEX, #PIPE_TYPE_MESSAGE | #PIPE_READMODE_MESSAGE, 1, 100, 100, 3000, #Null)
            If Hpipe
              AddGadgetItem(#Gadget, -1, "Le server est en attente de connexion client")
              Thread=CreateThread(@Thread_Read_Write(),Hpipe)
            EndIf
        EndSelect
       
      Case #PB_Event_CloseWindow
        KillThread(Thread)
        CloseHandle_(Hpipe)
      Quit = 1
    EndSelect
   
  Until Quit = 1
EndIf

The client

Code: Select all

#Gadget=0

Procedure AppelServer(lpszPipename.s)

ImageSize = ?giffileend-?giffile 
 
result=WaitNamedPipe_(lpszPipename, #Null)
If result
    File = CreateFile_( lpszPipename, #GENERIC_READ |#GENERIC_WRITE, 0, #Null, #OPEN_EXISTING,0, #Null)
    
    If File 
      WriteFile_(File , ?giffile, ImageSize, @envoye, 0)
      
      FlushFileBuffers_(File )
     
      ;Pensez à modifier le Buffer_Lecture pour qu'il soit de taille aussi grand
      ; que les données à recevoir
      Buffer_Lecture.s=Space(100)
      ReadFile_(File , @Buffer_Lecture, Len(Buffer_Lecture), @recu, 0)
      AddGadgetItem(#Gadget, -1, Buffer_Lecture) 
      
      CloseHandle_(File)
    EndIf
     
Else
    AddGadgetItem(#Gadget, -1, "Aucun serveur détecté, lancer le serveur et recommencer")
EndIf 

EndProcedure

If OpenWindow(0,0,0,400,400,"Client Pipe Nommé",#PB_Window_SystemMenu|#PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0))
  EditorGadget (#Gadget,10,70,380,320,#PB_Container_Raised)
  ButtonGadget(1, 50, 40, 300, 20, "Envoyer le fichier inclu dans les DataSection")
  StringGadget(2,10, 10,200,20,"\\.\pipe\NomDePipe")

    Repeat
      EventID.l = WaitWindowEvent()
      Select EventID
        Case #PB_Event_Gadget
          Select EventGadget()
            Case 1
              AppelServer(GetGadgetText(2))
          EndSelect
         
        Case #PB_Event_CloseWindow
          Quit = 1
      EndSelect
 
    Until Quit = 1
EndIf

DataSection
    giffile:
    Data.s "J'ai l'honneur de vous transmettre ce message"
    giffileend: 
EndDataSection
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Nice one Nico. 8)
I may look like a mule, but I'm not a complete ass.
Post Reply