B4A Library NamedPipe

This is a simple to use library that enables you to create a NamedPipe.
Your NamedPipe can be used to send and receive bytes from one process to another process.

NamedPipe
Version:
1.00
  • AutoCloseInputStream
    Methods:
    • MarkSupported As Boolean
      Tests if this input stream supports the mark and reset methods.
    • Mark (ReadLimit As Int)
      Marks the current position in this input stream.
      The Readlimit argument tells this input stream to allow that many bytes to be read before the mark position gets invalidated.
    • IsInitialized As Boolean
    • Read2 (Buffer() As Byte, Offset As Int, Length As Int) As Int
      Reads up to Length bytes of data at Offset from this input stream into Buffer.
      Less than Length bytes will be read if the end of the data stream is reached or if Length exceeds the buffer size.
    • Read As Int
      Reads the next byte of data or -1 if the end of the input stream is reached.
      The value byte is returned as an Int in the range 0 to 255.
    • Skip (Number As Long) As Long
    • Close
      Closes this input stream and releases any system resources associated with the stream.
    • Reset
    Properties:
    • Available As Int [read only]
      Returns an estimate of the number of bytes that can be read (or skipped over) from the current underlying input stream
      without blocking by the next invocation of a method for the current underlying input stream.
  • AutoCloseOutputStream
    Methods:
    • Write (Bytes() As Byte)
      Writes Bytes.Length bytes from the specified byte array to this output stream.
    • Flush
      Flushes this output stream and forces any buffered output bytes to be written out.
    • Write3 (Byte1 As Int)
      Writes Byte1 to this output stream.
    • Close
      Closes this output stream and releases any system resources associated with this stream.
    • IsInitialized As Boolean
    • Write2 (Bytes() As Byte, Offset As Int, Length As Int)
      Writes Length bytes from the specified byte array starting at Offset to this output stream.
  • NamedPipe
    Methods:
    • Close
      Closes both InputStream and OutputStream
    • IsInitialized As Boolean
    • Initialize
    Properties:
    • InputStream As AutoCloseInputStream [read only]
    • OutputStream As AutoCloseOutputStream [read only]
    • FdOutput As Int [read only]
      Returns the native file descriptor (integer) for this Pipe's Output.
    • FdInput As Int [read only]
      Returns the native file descriptor (integer) for this Pipe's Input.
 

Attachments

  • NamedPipe_v1.0.zip
    5.6 KB · Views: 282

warwound

Expert
Licensed User
Longtime User
A simple NamedPipe example which uses my ThreadExtras library.

B4X:
#Region  Service Attributes
   #StartAtBoot: False
#End Region

'   ignore warnings that Subs ReadPipe and WritePipe are not used
#IgnoreWarnings: 12

Sub Process_Globals
   Private NamedPipe1 As NamedPipe
   Private PipeReader As Runnable
   Private PipeWriter As Runnable
End Sub

Sub Service_Create
   NamedPipe1.Initialize
   
   PipeReader.Initialize("PipeReader", Me, "ReadPipe", Array As Object())
   PipeWriter.Initialize("PipeWriter", Me, "WritePipe", Array As Object())
   
   PipeReader.Start   '   ReadPipe will be executed in a background thread
   PipeWriter.Start   '   WritePipe will be executed in a background thread
End Sub

Sub Service_Start (StartingIntent As Intent)
   
End Sub

Sub Service_Destroy
   Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
   
   PipeReader.StopFlag=True
   
   PipeWriter.StopFlag=True
   PipeWriter.Interrupt
   
   NamedPipe1.Close
End Sub

Sub PipeReader_Complete(Result As Object)
   Log("PipeReader_Complete")
End Sub

Sub PipeWriter_Complete(Result As Object)
   Log("PipeWriter_Complete")
End Sub

Sub ReadPipe()
   Log("ReadPipeStarted")
   Dim AutoCloseInputStream1 As AutoCloseInputStream=NamedPipe1.InputStream
   
   '   a 32 byte buffer to read the pipe into
   Dim Bytes(32) As Byte   
   
   Do Until PipeReader.StopFlag Or PipeReader.IsInterrupted
       
       
       '   this method will block until the pipe contains bytes that it can read
       '   it's essential that you don't try to use this method on the UI thread
       '   your app would probably force close if you did so
       Dim BytesRead As Int=AutoCloseInputStream1.Read2(Bytes, 0, Bytes.Length)
       
       Log(BytesToString(Bytes, 0, BytesRead, "UTF8"))
   Loop
   AutoCloseInputStream1.Close
   
   Log("ReadPipeStopped")
End Sub

Sub WritePipe()
   Log("WritePipeStarted")
   Dim AutoCloseOutputStream1 As AutoCloseOutputStream=NamedPipe1.OutputStream
   Dim Thread1 As Thread
   Dim Bytes() As Byte="Hello from Sub WritePipe".GetBytes("UTF8")
   Do Until PipeWriter.StopFlag Or PipeWriter.IsInterrupted
       AutoCloseOutputStream1.Write2(Bytes, 0, Bytes.Length)
       Try
           Thread1.Sleep(500)
       Catch
           '   an InterruptedException will occur if PipeWriter.Interrupt is executed while Sleep is executing
           Log(LastException)
           PipeWriter.StopFlag=True
       End Try
       
   Loop
   AutoCloseOutputStream1.Close
   Log("WritePipeStopped")
End Sub

In this example i create and initialize a NamedPipe.
Then create and initialize two Runnables.
One Runnable will execute Sub ReadPipe in a background thread, the other will execute Sub WritePipe in a background thread.
Both Runnables are started and Sub WritePipe writes bytes to the named pipe while Sub ReadPipe reads and logs those bytes.

This code runs in a Service, when the Service is destroyed each Runnable's StopFlag property is set to True.
Additonally the Runnable method Interrupt is called on the PipeWriter Runnable.
This ensures that each background thread gracefully terminates.
 

Attachments

  • NamedPipeExample.zip
    8.3 KB · Views: 291
Cookies are required to use this site. You must accept them to continue using the site. Learn more…