Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

413 lines
11 KiB

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