monotone

monotone Mtn Source Tree

Root/netxx_pipe.hh

1// -*- mode: C++; c-file-style: "gnu"; indent-tabs-mode: nil -*-
2// copyright (C) 2005 Christof Petig <christof@petig-baender.de>
3// all rights reserved.
4// licensed to the public under the terms of the GNU GPL (>= 2)
5// see the file COPYING for details
6
7#include <string>
8#include <vector>
9#include <netxx/socket.h>
10#include <netxx/streambase.h>
11#ifdef WIN32
12# include <windows.h>
13#endif
14
15/* What is this all for?
16
17If you want to transparently handle a pipe and a socket on unix and windows
18you have to abstract some difficulties:
19
20 - sockets have a single filedescriptor for reading and writing
21 pipes usually come in pairs (one for reading and one for writing)
22
23 - process creation is different on unix and windows
24
25 => so Netxx::PipeStream is a Netxx::StreamBase which abstracts two pipes to
26 and from an external command
27
28 - windows can select on a socket but not on a pipe
29
30 => so Netxx::PipeCompatibleProbe is a Netxx::Probe like class which
31 _can_ handle pipes on windows (emulating select is difficult at best!)
32 (on unix Probe and PipeCompatibleProbe are nearly identical: with pipes
33 you should not select for both read and write on the same descriptor)
34
35*/
36
37namespace Netxx
38 {
39 class PipeCompatibleProbe;
40 class StreamServer;
41
42 class PipeStream : public StreamBase
43 {
44 int readfd, writefd;
45 int child;
46#ifdef WIN32
47
48 char readbuf[1024];
49 unsigned bytes_available;
50 OVERLAPPED overlap;
51
52 friend class PipeCompatibleProbe;
53#endif
54
55 public:
56 // do we need Timeout for symmetry with Stream?
57 explicit PipeStream (int readfd, int writefd);
58 explicit PipeStream (const std::string &cmd, const std::vector<std::string> &args);
59 virtual signed_size_type read (void *buffer, size_type length);
60 virtual signed_size_type write (const void *buffer, size_type length);
61 virtual void close (void);
62 virtual socket_type get_socketfd (void) const;
63 virtual const ProbeInfo* get_probe_info (void) const;
64 int get_readfd(void) const
65 {
66 return readfd;
67 }
68 int get_writefd(void) const
69 {
70 return writefd;
71 }
72 };
73
74#ifdef WIN32
75
76 // this probe can either handle _one_ PipeStream or several network Streams
77 // so if !is_pipe this acts like a Probe
78 class PipeCompatibleProbe : public Probe
79 {
80 bool is_pipe;
81 // only meaningful if is_pipe is true
82 PipeStream *pipe;
83 ready_type ready_t;
84 public:
85 PipeCompatibleProbe() : is_pipe(), pipe(), ready_t()
86 {}
87 void clear()
88 {
89 if (is_pipe)
90 {
91 pipe=0;
92 is_pipe=false;
93 }
94 else
95 Probe::clear();
96 }
97 // this function does all the hard work (emulating a select)
98 result_type ready(const Timeout &timeout=Timeout(), ready_type rt=ready_none);
99 void add(PipeStream &ps, ready_type rt=ready_none);
100 void add(const StreamBase &sb, ready_type rt=ready_none);
101 void add(const StreamServer &ss, ready_type rt=ready_none);
102 void remove(const PipeStream &ps);
103 };
104#else
105
106 // we only act specially if a PipeStream is added (directly or via the
107 // StreamBase parent reference)
108 struct PipeCompatibleProbe : Probe
109 {
110 void add(PipeStream &ps, ready_type rt=ready_none);
111 void add(const StreamBase &sb, ready_type rt=ready_none);
112 void add(const StreamServer &ss, ready_type rt=ready_none);
113 };
114#endif
115
116}

Archive Download this file

Branches

Tags

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