November 28, 2020, 11:12:34 AM

Author Topic: [bmx] fmc.ProcessStream by Fabian. [ 1+ years ago ]  (Read 1619 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] fmc.ProcessStream by Fabian. [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : fmc.ProcessStream
Author : Fabian.
Posted : 1+ years ago

Description : Since 2006-11-06 my modules stored in the code archives are out of synchronization. To have the latest version you need to use the bmk/syncmods utility or the MaxIDE.

You need to add "webspace100.we.funpic.de/root/mod" as a new line to "cfg/modservers.cfg" in your BlitzMax installation directory.

You can't synchronize with the default BlitzMax 1.22 IDE. If you don't have another IDE, you need to download a <a href="http://webspace100.we.funpic.de/root/programs/MaxIDE.exe" target="_blank">tweaked MaxIDE[/url]. You need to specify "fmc" as additional module scope.

Alternative you can synchronize with the bmk tool: "bmk syncmods fmc" (<-- doesn't seem to work with bmk, so use "syncmods fmc" instead)

For a better description see my <a href="../Community/posts37e4.html?topic=69294" target="_blank">thread[/url] in the programming forum introducing these modules.

Needed modules:
<a href="codearcsb7bf.html?code=1786" >fmc.Development[/url]
<a href="codearcs6306.html?code=1876" >fmc.ObjectTool[/url]
<a href="codearcsffc9.html?code=1780" >fmc.IOStream[/url]


Code :
Code: BlitzMax
  1. Strict
  2. Rem
  3. bbdoc:ProcessStream
  4. EndRem
  5. Module fmc.ProcessStream
  6.  
  7. ModuleInfo "Version: 0.01"
  8. ModuleInfo "Modserver: Fabian"
  9.  
  10. Import pub.freeprocess
  11. Import fmc.Development
  12. Import fmc.ObjectTool
  13. Import fmc.IOStream
  14.  
  15. Rem
  16. bbdoc:Create a new process stream
  17. returns:A new process stream
  18. about:
  19. This function creates a new process and its stream.
  20. Closing the process stream will only close the stream to the process and not terminate the process itself.
  21. Note that processes created with this command are not terminated at the end of the program.
  22. To terminate the process of a process stream you can get the process stream's process using #ProcessStreamProcess
  23. and terminate it using @pub.freeprocess.TerminateProcess or you can just call #TerminateProcessStreamProcess.
  24. To create a process stream you also can call #brl.stream.OpenStream with "execute::" and the process name
  25. as parameter.
  26. EndRem
  27. Function CreateProcessStream:TStream ( Name$ )
  28.   Return TProcessStream.Create ( Name )
  29. EndFunction
  30.  
  31. Rem
  32. bbdoc:Get a process stream's process
  33. returns:A process stream's process
  34. about:
  35. This function returns a process stream's process.
  36. EndRem
  37. Function ProcessStreamProcess:TProcess ( Stream:TStream )
  38.   Local ProcessStream:TProcessStream = TProcessStream ( Stream )
  39.   If ProcessStream
  40.     Return ProcessStream.Proc
  41.   EndIf
  42. EndFunction
  43.  
  44. Rem
  45. bbdoc:Terminate a process stream's process and close the stream itself
  46. about:
  47. This function terminates the process stream's process and than closes the process stream
  48. EndRem
  49. Function TerminateProcessStreamProcess ( Stream:TStream )
  50.   Local ProcessStream:TProcessStream = TProcessStream ( Stream )
  51.   If ProcessStream And ProcessStream.Proc
  52.     ProcessStream.Proc.Terminate
  53.     ProcessStream.Close
  54.   EndIf
  55. EndFunction
  56.  
  57. Private
  58.  
  59. New TProcessStreamFactory
  60. ?Win32
  61.  
  62. Function Proc ( Context )
  63.   Local ProcessStream:TProcessStream = TProcessStream ( ObjectForHandle ( ( Int Ptr Context ) [ 0 ] ) )
  64.   TEvent.Create ( EVENT_READAVAIL , ProcessStream , ( Int Ptr Context ) [ 1 ] ).Emit
  65.   MemFree Byte Ptr Context
  66. EndFunction
  67.  
  68. Extern "Win32"
  69.   Function PeekNamedPipe ( Pipe , Buffer:Byte Ptr , Size , Read Ptr , Avail Ptr , BLTM Ptr )
  70.   Function ReadFile ( File , Buffer:Byte Ptr , Size , Read Ptr , O:Byte Ptr )
  71.   Function WriteFile ( File , Buffer:Byte Ptr , Size , Written Ptr , O:Byte Ptr )
  72.   Function CreatePipe ( In Ptr , Out Ptr , PA:Byte Ptr , Size )
  73.   Function CloseHandle ( Handle )
  74. EndExtern
  75. ?
  76.  
  77. Type TProcessStreamFactory Extends TStreamFactory
  78.   Method CreateStream:TStream ( url:Object , proto$ , path$ , readable , writeable )
  79.     If proto = "execute"
  80.       Return TProcessStream.Create ( path )
  81.     EndIf
  82.   EndMethod
  83. EndType
  84.  
  85. Type TProcessStream Extends TStream
  86.   Field Proc:TProcess
  87. ?Win32
  88.   Field PipeI
  89.   Field PipeO
  90.   Field PipeT
  91.   Field BufferLen
  92.   Field Buffer:Byte Ptr
  93.   Field Sync:TSync
  94.   Field Thread:TThread
  95.   Field Closed
  96. ?
  97.  
  98.   Function Create:TProcessStream ( Name$ )
  99.     Local ProcessStream:TProcessStream = New TProcessStream
  100.     ProcessStream.Proc = TProcess.Create ( Name , HIDECONSOLE )
  101.     If ProcessStream.Proc
  102.       TProcess.ProcessList.Remove ProcessStream.Proc
  103. ?Win32
  104.       CreatePipe Varptr ProcessStream.PipeI , Varptr ProcessStream.PipeO , Null , 0
  105.       ProcessStream.PipeT = ProcessStream.Proc.pipe.readhandle
  106.       ProcessStream.BufferLen = 1024
  107.       ProcessStream.Buffer = MemAlloc ( ProcessStream.BufferLen )
  108.       ProcessStream.Sync = New TSync
  109.       ProcessStream.Sync.Sync
  110.       ProcessStream.Thread = TThread.Create ( Func , OpenHandle ( ProcessStream ) )
  111.  
  112.       Function Func ( Context )
  113.         Local ProcessStream:TProcessStream = TProcessStream ( ObjectForHandle ( Context ) )
  114.         Local ThreadOut = ProcessStream.PipeO
  115.         Local ThreadIn = ProcessStream.PipeT
  116.         Local BufferLen = ProcessStream.BufferLen
  117.         Local Buffer:Byte Ptr = ProcessStream.Buffer
  118.         Local Sync:TSync = ProcessStream.Sync
  119.         Repeat
  120.           Local All
  121.           Local Cur
  122.           Sync.EndSync
  123.           If Not ReadFile ( ThreadIn , Buffer , BufferLen , Varptr All , Null )
  124.             Sync.Sync
  125.             CloseHandle ThreadOut
  126.             ProcessStream.Closed = True
  127.             Sync.EndSync
  128.             Return
  129.           EndIf
  130.           Sync.Sync
  131.           While All > Cur
  132.             Local N
  133.             WriteFile ThreadOut , Buffer + Cur , All - Cur , Varptr N , Null
  134.             Cur :+ N
  135.           Wend
  136.           If All
  137.             Local Mem Ptr = Int Ptr MemAlloc ( 8 )
  138.             Mem [ 0 ] = Context
  139.             Mem [ 1 ] = All
  140.             CallbackMain Proc , Int Mem
  141.           EndIf
  142.         Forever
  143.       EndFunction
  144.  
  145. ?
  146.       Return ProcessStream
  147.     EndIf
  148.   EndFunction
  149.  
  150.   Method Eof ( )
  151.     If Not Proc
  152.       Return True
  153.     EndIf
  154.     If Not ( Proc.Status ( ) Or Size ( ) )
  155.       Close
  156.       Return True
  157.     EndIf
  158.   EndMethod
  159.  
  160.   Method Pos ( )
  161.     Return -1
  162.   EndMethod
  163.  
  164.   Method Size ( )
  165.     If Proc
  166. ?Win32
  167.       Local Avail
  168.       PeekNamedPipe PipeI , Null , 0 , Null , Varptr Avail , Null
  169.       Return Avail
  170. ?Linux | MacOS
  171.       Return Proc.pipe.ReadAvail ( )
  172. ?
  173.     EndIf
  174.   EndMethod
  175.  
  176.   Method Seek ( pos )
  177.     If pos = -1
  178. ?Win32
  179.       Return -3
  180. ?Linux | MacOS
  181.       Return -2
  182. ?
  183.     EndIf
  184.     Return -1
  185.   EndMethod
  186.  
  187.   Method Flush ( )
  188.   EndMethod
  189.  
  190.   Method Close ( )
  191.     If Proc
  192. ?Win32
  193.       Sync.Sync
  194.       Thread.Terminate
  195.       MemFree Buffer
  196.       CloseHandle PipeI
  197.       If Not Closed
  198.         CloseHandle PipeO
  199.       EndIf
  200.       ReleaseObject Self
  201. ?
  202.       Proc.Close
  203.       Proc = Null
  204.     EndIf
  205.   EndMethod
  206.  
  207.   Method Read ( buf:Byte Ptr , count )
  208.     If Proc
  209. ?Win32
  210.       ReadFile PipeI , buf , count , Varptr count , Null
  211.       Return count
  212. ?Linux | MacOS
  213.       Return Proc.pipe.Read ( buf , count )
  214. ?
  215.     EndIf
  216.   EndMethod
  217.  
  218.   Method Write ( buf:Byte Ptr , count )
  219.     If Proc
  220.       Return Proc.pipe.Write ( buf , count )
  221.     EndIf
  222.   EndMethod
  223. EndType


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal