monotone

monotone Mtn Source Tree

Root/netxx_pipe.hh

1#ifndef __NETXX_PIPE_HH__
2#define __NETXX_PIPE_HH__
3
4// Copyright (C) 2005 Christof Petig <christof@petig-baender.de>
5//
6// This program is made available under the GNU GPL version 2.0 or
7// greater. See the accompanying file COPYING for details.
8//
9// This program is distributed WITHOUT ANY WARRANTY; without even the
10// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11// PURPOSE.
12
13#include <vector>
14#include <netxx/socket.h>
15#include <netxx/streambase.h>
16#ifdef WIN32
17# include <windows.h>
18#endif
19
20/*
21 What is this all for?
22
23 If you want to transparently handle a pipe and a socket on unix and
24 windows you have to abstract some difficulties:
25
26 - sockets have a single filedescriptor for reading and writing
27 pipes usually come in pairs (one for reading and one for writing)
28
29 - process creation is different on unix and windows
30
31 => so Netxx::PipeStream is a Netxx::StreamBase which abstracts two pipes to
32 and from an external command
33
34 - windows can select on a socket but not on a pipe
35
36 => so Netxx::PipeCompatibleProbe is a Netxx::Probe like class which
37 _can_ handle pipes on windows (emulating select is difficult at best!)
38 (on unix Probe and PipeCompatibleProbe are nearly identical: with pipes
39 you should not select for both read and write on the same descriptor)
40
41*/
42
43namespace Netxx
44 {
45 class PipeCompatibleProbe;
46 class StreamServer;
47
48 class PipeStream : public StreamBase
49 {
50#ifdef WIN32
51 HANDLE named_pipe;
52 HANDLE child;
53 char readbuf[1024];
54 DWORD bytes_available;
55 bool read_in_progress;
56 OVERLAPPED overlap;
57 friend class PipeCompatibleProbe;
58#else
59 int readfd, writefd;
60 int child;
61#endif
62
63
64 public:
65 // do we need Timeout for symmetry with Stream?
66 explicit PipeStream (int readfd, int writefd);
67 explicit PipeStream (const std::string &cmd, const std::vector<std::string> &args);
68 virtual ~PipeStream() { close(); }
69 virtual signed_size_type read (void *buffer, size_type length);
70 virtual signed_size_type write (const void *buffer, size_type length);
71 virtual void close (void);
72 virtual socket_type get_socketfd (void) const;
73 virtual const ProbeInfo* get_probe_info (void) const;
74 int get_readfd(void) const
75 {
76#ifdef WIN32
77 return -1;
78#else
79 return readfd;
80#endif
81 }
82 int get_writefd(void) const
83 {
84#ifdef WIN32
85 return -1;
86#else
87 return writefd;
88#endif
89 }
90 };
91
92#ifdef WIN32
93
94 // This probe can either handle _one_ PipeStream or several network
95 // Streams so if !is_pipe this acts like a Probe.
96 class PipeCompatibleProbe : public Probe
97 {
98 bool is_pipe;
99 // only meaningful if is_pipe is true
100 PipeStream *pipe;
101 ready_type ready_t;
102 public:
103 PipeCompatibleProbe() : is_pipe(), pipe(), ready_t()
104 {}
105 void clear()
106 {
107 if (is_pipe)
108 {
109 pipe=0;
110 is_pipe=false;
111 }
112 else
113 Probe::clear();
114 }
115 // This function does all the hard work (emulating a select).
116 result_type ready(const Timeout &timeout=Timeout(), ready_type rt=ready_none);
117 void add(PipeStream &ps, ready_type rt=ready_none);
118 void add(const StreamBase &sb, ready_type rt=ready_none);
119 void add(const StreamServer &ss, ready_type rt=ready_none);
120 };
121#else
122
123 // We only act specially if a PipeStream is added (directly or via
124 // the StreamBase parent reference).
125 struct PipeCompatibleProbe : Probe
126 {
127 void add(PipeStream &ps, ready_type rt=ready_none);
128 void add(const StreamBase &sb, ready_type rt=ready_none);
129 void add(const StreamServer &ss, ready_type rt=ready_none);
130 };
131#endif
132
133}
134
135// Local Variables:
136// mode: C++
137// fill-column: 76
138// c-file-style: "gnu"
139// indent-tabs-mode: nil
140// End:
141// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
142
143#endif // __NETXX_PIPE_HH__
144

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status