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

Archive Download this file

Branches

Tags

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