File: FXPipe.cpp

package info (click to toggle)
gogglesmm 1.2.5-6
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 16,812 kB
  • sloc: cpp: 231,960; ansic: 893; xml: 222; makefile: 33
file content (220 lines) | stat: -rw-r--r-- 5,825 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/********************************************************************************
*                                                                               *
*                             P i p e   C l a s s                               *
*                                                                               *
*********************************************************************************
* Copyright (C) 2005,2022 by Jeroen van der Zijp.   All Rights Reserved.        *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify          *
* it under the terms of the GNU Lesser General Public License as published by   *
* the Free Software Foundation; either version 3 of the License, or             *
* (at your option) any later version.                                           *
*                                                                               *
* This library is distributed in the hope that it will be useful,               *
* but WITHOUT ANY WARRANTY; without even the implied warranty of                *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
* GNU Lesser General Public License for more details.                           *
*                                                                               *
* You should have received a copy of the GNU Lesser General Public License      *
* along with this program.  If not, see <http://www.gnu.org/licenses/>          *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "fxascii.h"
#include "FXArray.h"
#include "FXHash.h"
#include "FXStream.h"
#include "FXString.h"
#include "FXPath.h"
#include "FXIO.h"
#include "FXIODevice.h"
#include "FXPipe.h"



/*
  Notes:

  - Create a pipe for reading or writing; if created for reading,
    the "other" is the write-side.  If created for writing, the
    "other" is the read-side.

  - The pipe may be inherited by a sub-process if so indicated.

  - On Linux, we prefer to use pipe2(), elsewhere we use pipe()
    and set the flags properly afterwards, using fcntl().

  - Both our end the other end FXPipe instances should not be open
    yet prior to a call to open().

  - FIXME Probably need special class to implement unix socket
    or named pipe, this will be just for anonymous pipes.
*/

// Bad handle value
#ifdef WIN32
#define BadHandle INVALID_HANDLE_VALUE
#else
#define BadHandle -1
#endif

using namespace FX;

/*******************************************************************************/

namespace FX {


// Construct and open pipes with access mode m for this one and the reverse for the other
FXPipe::FXPipe(FXPipe& other,FXuint m){
  open(other,m);
  }


// Construct file and attach existing handle h
FXPipe::FXPipe(FXInputHandle h){
  attach(h);
  }


#if defined(WIN32)

// Open pipes with access mode m for this one and the reverse for the other
FXbool FXPipe::open(FXPipe& other,FXuint m){
  if(device==BadHandle && other.device==BadHandle){
    HANDLE hnd[2];

    // Inheritable
    SECURITY_ATTRIBUTES security;
    security.nLength=sizeof(SECURITY_ATTRIBUTES);
    security.bInheritHandle=((m&FXIO::Inheritable)==0);
    security.lpSecurityDescriptor=nullptr;

    // Create connected pipe
    if(CreatePipe(&hnd[0],&hnd[1],&security,0)!=0){

      // Who's is the read-side?
      if(m&FXIO::ReadOnly){
        device=hnd[0];
        other.device=hnd[1];
        }
      else{
        device=hnd[1];
        other.device=hnd[0];
        }
      return true;
      }
    }
  return false;
  }


#else


// Open pipes with access mode m for this one and the reverse for the other
FXbool FXPipe::open(FXPipe& other,FXuint m){
  if(device==BadHandle && other.device==BadHandle){
    FXint hnd[2];
#if defined(HAVE_PIPE2)
    FXint flags=0;

    // Non-blocking
    if(m&FXIO::NonBlocking){ flags|=O_NONBLOCK; }

    // Inheritable
#if defined(O_CLOEXEC)
    if(!(m&FXIO::Inheritable)){ flags|=O_CLOEXEC; }
#endif

    // Create pipe
    if(pipe2(hnd,flags)==0){

      // Who's is the read-side?
      if(m&FXIO::ReadOnly){
        device=hnd[0];
        other.device=hnd[1];
        }
      else{
        device=hnd[1];
        other.device=hnd[0];
        }
      return true;
      }

#else

    // Create pipe
    if(pipe(hnd)==0){

      // Who's is the read-side?
      if(m&FXIO::ReadOnly){
        device=hnd[0];
        other.device=hnd[1];
        }
      else{
        device=hnd[1];
        other.device=hnd[0];
        }

      // Non-blocking
      if(m&FXIO::NonBlocking){
        fcntl(device,F_SETFL,O_NONBLOCK);
        }

      // Inheritable
#if defined(O_CLOEXEC)
      if(!(m&FXIO::Inheritable)){
        fcntl(device,F_SETFD,FD_CLOEXEC);
        }
#endif
      return true;
      }
#endif
    }
  return false;
  }

#endif

/*******************************************************************************/

// Create a named pipe (not available on Windows)
FXbool FXPipe::create(const FXString& file,FXuint perm){
#if !defined(WIN32)
  if(!file.empty()){
    return ::mkfifo(file.text(),perm&0777)==0;
    }
#endif
  return false;
  }


// Remove a named pipe (not available on Windows)
FXbool FXPipe::remove(const FXString& file){
#if !defined(WIN32)
  if(!file.empty()){
    return ::unlink(file.text())==0;
    }
#endif
  return false;
  }

/*

// FIXME move elsewhere
HANDLE WINAPI CreateNamedPipe(
  __in      LPCTSTR lpName,
  __in      DWORD dwOpenMode,
  __in      DWORD dwPipeMode,
  __in      DWORD nMaxInstances,
  __in      DWORD nOutBufferSize,
  __in      DWORD nInBufferSize,
  __in      DWORD nDefaultTimeOut,
  __in_opt  LPSECURITY_ATTRIBUTES lpSecurityAttributes
);
*/

}