home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _717e84b2e04e032145ff074a769710b1 < prev    next >
Text File  |  2004-06-01  |  11KB  |  414 lines

  1. package Win32::Pipe;
  2.  
  3. $VERSION = '0.022';
  4.  
  5. # Win32::Pipe.pm
  6. #       +==========================================================+
  7. #       |                                                          |
  8. #       |                     PIPE.PM package                      |
  9. #       |                     ---------------                      |
  10. #       |                    Release v96.05.11                     |
  11. #       |                                                          |
  12. #       |    Copyright (c) 1996 Dave Roth. All rights reserved.    |
  13. #       |   This program is free software; you can redistribute    |
  14. #       | it and/or modify it under the same terms as Perl itself. |
  15. #       |                                                          |
  16. #       +==========================================================+
  17. #
  18. #
  19. #    Use under GNU General Public License or Larry Wall's "Artistic License"
  20. #
  21. #    Check the README.TXT file that comes with this package for details about
  22. #    it's history.
  23. #
  24.  
  25. require Exporter;
  26. require DynaLoader;
  27.  
  28. @ISA= qw( Exporter DynaLoader );
  29.     # Items to export into callers namespace by default. Note: do not export
  30.     # names by default without a very good reason. Use EXPORT_OK instead.
  31.     # Do not simply export all your public functions/methods/constants.
  32. @EXPORT = qw();
  33.  
  34. $ErrorNum = 0;
  35. $ErrorText = "";
  36.  
  37. sub new
  38. {
  39.     my ($self, $Pipe);
  40.     my ($Type, $Name, $Time) = @_;
  41.  
  42.     if (! $Time){
  43.         $Time = DEFAULT_WAIT_TIME;
  44.     }
  45.     $Pipe = PipeCreate($Name, $Time);
  46.     if ($Pipe){
  47.         $self = bless {};
  48.         $self->{'Pipe'} = $Pipe;
  49.     }else{
  50.         ($ErrorNum, $ErrorText) = PipeError();
  51.         return undef;
  52.     }
  53.     $self;
  54. }
  55.  
  56. sub Write{
  57.     my($self, $Data) = @_;
  58.     $Data = PipeWrite($self->{'Pipe'}, $Data);
  59.     return $Data;
  60. }
  61.  
  62. sub Read{
  63.     my($self) = @_;
  64.     my($Data);
  65.     $Data = PipeRead($self->{'Pipe'});
  66.     return $Data;
  67. }
  68.  
  69. sub Error{
  70.     my($self) = @_;
  71.     my($MyError, $MyErrorText, $Temp);
  72.     if (! ref($self)){
  73.         undef $Temp;
  74.     }else{
  75.         $Temp = $self->{'Pipe'};
  76.     }
  77.     ($MyError, $MyErrorText) = PipeError($Temp);
  78.     return wantarray? ($MyError, $MyErrorText):"[$MyError] \"$MyErrorText\"";
  79. }
  80.  
  81.  
  82. sub Close{
  83.     my ($self) = shift;
  84.     PipeClose($self->{'Pipe'});
  85. }
  86.  
  87. sub Connect{
  88.     my ($self) = @_;
  89.     my ($Result);
  90.     $Result = PipeConnect($self->{'Pipe'});
  91.     return $Result;
  92. }
  93.  
  94. sub Disconnect{
  95.     my ($self, $iPurge) = @_;
  96.     my ($Result);
  97.     if (! $iPurge){
  98.         $iPurge = 1;
  99.     }
  100.     $Result = PipeDisconnect($self->{'Pipe'}, $iPurge);
  101.     return $Result;
  102. }
  103.  
  104. sub BufferSize{
  105.     my($self) = @_;
  106.     my($Result) =  PipeBufferSize($self->{'Pipe'});
  107.     return $Result;
  108. }
  109.  
  110. sub ResizeBuffer{
  111.     my($self, $Size) = @_;
  112.     my($Result) = PipeResizeBuffer($self->{'Pipe'}, $Size);
  113.     return $Result;
  114. }
  115.  
  116.  
  117. ####
  118. #   Auto-Kill an instance of this module
  119. ####
  120. sub DESTROY
  121. {
  122.     my ($self) = shift;
  123.     Close($self);
  124. }
  125.  
  126.  
  127. sub Credit{
  128.     my($Name, $Version, $Date, $Author, $CompileDate, $CompileTime, $Credits) = Win32::Pipe::Info();
  129.     my($Out, $iWidth);
  130.     $iWidth = 60;
  131.     $Out .=  "\n";
  132.     $Out .=  "  +". "=" x ($iWidth). "+\n";
  133.     $Out .=  "  |". Center("", $iWidth). "|\n";
  134.     $Out .=  "  |" . Center("", $iWidth). "|\n";
  135.     $Out .=  "  |". Center("$Name", $iWidth). "|\n";
  136.     $Out .=  "  |". Center("-" x length("$Name"), $iWidth). "|\n";
  137.     $Out .=  "  |". Center("", $iWidth). "|\n";
  138.  
  139.     $Out .=  "  |". Center("Version $Version ($Date)", $iWidth). "|\n";
  140.     $Out .=  "  |". Center("by $Author", $iWidth). "|\n";
  141.     $Out .=  "  |". Center("Compiled on $CompileDate at $CompileTime.", $iWidth). "|\n";
  142.     $Out .=  "  |". Center("", $iWidth). "|\n";
  143.     $Out .=  "  |". Center("Credits:", $iWidth). "|\n";
  144.     $Out .=  "  |". Center(("-" x length("Credits:")), $iWidth). "|\n";
  145.     foreach $Temp (split("\n", $Credits)){
  146.         $Out .=  "  |". Center("$Temp", $iWidth). "|\n";
  147.     }
  148.     $Out .=  "  |". Center("", $iWidth). "|\n";
  149.     $Out .=  "  +". "=" x ($iWidth). "+\n";
  150.     return $Out;
  151. }
  152.  
  153. sub Center{
  154.     local($Temp, $Width) = @_;
  155.     local($Len) = ($Width - length($Temp)) / 2;
  156.     return " " x int($Len) . $Temp . " " x (int($Len) + (($Len != int($Len))? 1:0));
  157. }
  158.  
  159. # ------------------ A U T O L O A D   F U N C T I O N ---------------------
  160.  
  161. sub AUTOLOAD {
  162.     # This AUTOLOAD is used to 'autoload' constants from the constant()
  163.     # XS function.  If a constant is not found then control is passed
  164.     # to the AUTOLOAD in AutoLoader.
  165.  
  166.     my($constname);
  167.     ($constname = $AUTOLOAD) =~ s/.*:://;
  168.     #reset $! to zero to reset any current errors.
  169.     local $! = 0;
  170.     $val = constant($constname, @_ ? $_[0] : 0);
  171.  
  172.     if ($! != 0) {
  173.     if ($! =~ /Invalid/) {
  174.         $AutoLoader::AUTOLOAD = $AUTOLOAD;
  175.         goto &AutoLoader::AUTOLOAD;
  176.     }
  177.     else {
  178.  
  179.             # Added by JOC 06-APR-96
  180.             # $pack = 0;
  181.         $pack = 0;
  182.         ($pack,$file,$line) = caller;
  183.             print "Your vendor has not defined Win32::Pipe macro $constname, used in $file at line $line.";
  184.     }
  185.     }
  186.     eval "sub $AUTOLOAD { $val }";
  187.     goto &$AUTOLOAD;
  188. }
  189.  
  190. bootstrap Win32::Pipe;
  191.  
  192. 1;
  193. __END__
  194.  
  195. =head1 NAME
  196.  
  197. Win32::Pipe - Win32 Named Pipe
  198.  
  199. =head1 SYNOPSIS
  200.  
  201. To use this extension, follow these basic steps. First, you need to
  202. 'use' the pipe extension:
  203.  
  204.     use Win32::Pipe;
  205.  
  206. Then you need to create a server side of a named pipe:
  207.  
  208.     $Pipe = new Win32::Pipe("My Pipe Name");
  209.  
  210. or if you are going to connect to pipe that has already been created:
  211.  
  212.     $Pipe = new Win32::Pipe("\\\\server\\pipe\\My Pipe Name");
  213.  
  214.     NOTE: The "\\\\server\\pipe\\" is necessary when connecting
  215.           to an existing pipe! If you are accessing the same
  216.           machine you could use "\\\\.\\pipe\\" but either way
  217.           works fine.
  218.  
  219. You should check to see if C<$Pipe> is indeed defined otherwise there
  220. has been an error.
  221.  
  222. Whichever end is the server, it must now wait for a connection...
  223.  
  224.     $Result = $Pipe->Connect();
  225.  
  226.     NOTE: The client end does not do this! When the client creates
  227.           the pipe it has already connected!
  228.  
  229. Now you can read and write data from either end of the pipe:
  230.  
  231.     $Data = $Pipe->Read();
  232.  
  233.     $Result = $Pipe->Write("Howdy! This is cool!");
  234.  
  235. When the server is finished it must disconnect:
  236.  
  237.     $Pipe->Disconnect();
  238.  
  239. Now the server could C<Connect> again (and wait for another client) or
  240. it could destroy the named pipe...
  241.  
  242.     $Data->Close();
  243.  
  244. The client should C<Close> in order to properly end the session.
  245.  
  246. =head1 DESCRIPTION
  247.  
  248. =head2 General Use
  249.  
  250. This extension gives Win32 Perl the ability to use Named Pipes. Why?
  251. Well considering that Win32 Perl does not (yet) have the ability to
  252. C<fork> I could not see what good the C<pipe(X,Y)> was. Besides, where
  253. I am as an admin I must have several perl daemons running on several
  254. NT Servers. It dawned on me one day that if I could pipe all these
  255. daemons' output to my workstation (across the net) then it would be
  256. much easier to monitor. This was the impetus for an extension using
  257. Named Pipes. I think that it is kinda cool. :)
  258.  
  259. =head2 Benefits
  260.  
  261. And what are the benefits of this module?
  262.  
  263. =over
  264.  
  265. =item *
  266.  
  267. You may create as many named pipes as you want (uh, well, as many as
  268. your resources will allow).
  269.  
  270. =item *
  271.  
  272. Currently there is a limit of 256 instances of a named pipe (once a
  273. pipe is created you can have 256 client/server connections to that
  274. name).
  275.  
  276. =item *
  277.  
  278. The default buffer size is 512 bytes; this can be altered by the
  279. C<ResizeBuffer> method.
  280.  
  281. =item *
  282.  
  283. All named pipes are byte streams. There is currently no way to alter a
  284. pipe to be message based.
  285.  
  286. =item *
  287.  
  288. Other things that I cannot think of right now... :)
  289.  
  290. =back
  291.  
  292. =head1 CONSTRUCTOR
  293.  
  294. =over
  295.  
  296. =item new ( NAME )
  297.  
  298. Creates a named pipe if used in server context or a connection to the
  299. specified named pipe if used in client context. Client context is
  300. determined by prepending $Name with "\\\\".
  301.  
  302. Returns I<true> on success, I<false> on failure.
  303.  
  304. =back
  305.  
  306. =head1 METHODS
  307.  
  308. =over
  309.  
  310. =item BufferSize ()
  311.  
  312. Returns the size of the instance of the buffer of the named pipe.
  313.  
  314. =item Connect ()
  315.  
  316. Tells the named pipe to create an instance of the named pipe and wait
  317. until a client connects. Returns I<true> on success, I<false> on
  318. failure.
  319.  
  320. =item Close ()
  321.  
  322. Closes the named pipe.
  323.  
  324. =item Disconnect ()
  325.  
  326. Disconnects (and destroys) the instance of the named pipe from the
  327. client. Returns I<true> on success, I<false> on failure.
  328.  
  329. =item Error ()
  330.  
  331. Returns the last error messages pertaining to the named pipe. If used
  332. in context to the package. Returns a list containing C<ERROR_NUMBER>
  333. and C<ERROR_TEXT>.
  334.  
  335. =item Read ()
  336.  
  337. Reads from the named pipe. Returns data read from the pipe on success,
  338. undef on failure.
  339.  
  340. =item ResizeBuffer ( SIZE )
  341.  
  342. Sets the size of the buffer of the instance of the named pipe to
  343. C<SIZE>. Returns the size of the buffer on success, I<false> on
  344. failure.
  345.  
  346. =item Write ( DATA )
  347.  
  348. Writes C<DATA> to the named pipe. Returns I<true> on success, I<false>
  349. on failure.
  350.  
  351. =back
  352.  
  353. =head1 LIMITATIONS
  354.  
  355. What known problems does this thing have?
  356.  
  357. =over
  358.  
  359. =item *
  360.  
  361. If someone is waiting on a C<Read> and the other end terminates then
  362. you will wait for one B<REALLY> long time! (If anyone has an idea on
  363. how I can detect the termination of the other end let me know!)
  364.  
  365. =item *
  366.  
  367. All pipes are blocking. I am considering using threads and callbacks
  368. into Perl to perform async IO but this may be too much for my time
  369. stress. ;)
  370.  
  371. =item *
  372.  
  373. There is no security placed on these pipes.
  374.  
  375. =item *
  376.  
  377. This module has neither been optimized for speed nor optimized for
  378. memory consumption. This may run into memory bloat.
  379.  
  380. =back
  381.  
  382. =head1 INSTALLATION NOTES
  383.  
  384. If you wish to use this module with a build of Perl other than
  385. ActivePerl, you may wish to fetch the source distribution for this
  386. module. The source is included as part of the C<libwin32> bundle,
  387. which you can find in any CPAN mirror here:
  388.  
  389.   modules/by-authors/Gurusamy_Sarathy/libwin32-0.151.tar.gz
  390.  
  391. The source distribution also contains a pair of sample client/server
  392. test scripts. For the latest information on this module, consult the
  393. following web site:
  394.  
  395.   http://www.roth.net/perl
  396.  
  397. =head1 AUTHOR
  398.  
  399. Dave Roth <rothd@roth.net>
  400.  
  401. =head1 DISCLAIMER
  402.  
  403. I do not guarantee B<ANYTHING> with this package. If you use it you
  404. are doing so B<AT YOUR OWN RISK>! I may or may not support this
  405. depending on my time schedule.
  406.  
  407. =head1 COPYRIGHT
  408.  
  409. Copyright (c) 1996 Dave Roth. All rights reserved.
  410. This program is free software; you can redistribute
  411. it and/or modify it under the same terms as Perl itself.
  412.  
  413. =cut
  414.