summaryrefslogtreecommitdiff
path: root/sal/osl/w32/pipeimpl.cxx
blob: 9ea65f52242b91da7a07ea9d7406790a3a5f9454 (plain)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * 
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: pipeimpl.cxx,v $
 * $Revision: 1.4 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org 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 version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/


// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_sal.hxx"
#	include "pipeimpl.h"

#ifndef _INC_MALLOC
#	include <malloc.h>
#endif

#ifndef _INC_TCHAR
#	ifdef UNICODE
#		define _UNICODE
#	endif
#	include <tchar.h>
#endif

const TCHAR PIPE_NAME_PREFIX_MAPPING[] = TEXT("PIPE_FILE_MAPPING_");
const TCHAR PIPE_NAME_PREFIX_SYNCHRONIZE[] = TEXT("PIPE_SYNCHRONIZE_MUTEX_");
const TCHAR PIPE_NAME_PREFIX_CONNECTION[] = TEXT("PIPE_CONNECTION_SEMAPHORE_");

const DWORD PIPE_BUFFER_SIZE = 4096;


//============================================================================
//	PipeData
//============================================================================

struct PipeData
{
    DWORD	dwProcessId;
    HANDLE	hReadPipe;
    HANDLE	hWritePipe;
};

//============================================================================
//	Pipe
//============================================================================

#ifdef UNICODE
#define Pipe		PipeW
#define ClientPipe	ClientPipeW
#define ServerPipe	ServerPipeW
#else
#define Pipe		PipeA
#define ClientPipe	ClientPipeA
#define ServerPipe	ServerPipeA
#endif

class Pipe
{
protected:
    HANDLE	m_hReadPipe;	// Handle to use for reading
    HANDLE	m_hWritePipe;	// Handle to use for writing

    Pipe( HANDLE hReadPipe, HANDLE hWritePipe );

    static HANDLE CreatePipeDataMutex( LPCTSTR lpName, BOOL bInitialOwner );
    static HANDLE CreatePipeDataMapping( LPCTSTR lpName );
    static HANDLE OpenPipeDataMapping( LPCTSTR lpName );
    static HANDLE CreatePipeConnectionSemaphore( LPCTSTR lpName, LONG lInitialCount, LONG lMaximumcount );

public:
    Pipe( const Pipe& );
    const Pipe& operator = ( const Pipe& );
    virtual ~Pipe();

    virtual bool Close();
    virtual bool Write( LPCVOID lpBuffer, DWORD dwBytesToWrite, LPDWORD lpBytesWritten, bool bWait = true );
    virtual bool Read( LPVOID lpBuffer, DWORD dwBytesToRead, LPDWORD lpBytesRead, bool bWait = true );

    virtual Pipe *AcceptConnection()
    {
        SetLastError( ERROR_INVALID_HANDLE );
        return NULL;
    }

    void * operator new( size_t nBytes )
    {
        return HeapAlloc( GetProcessHeap(), 0, nBytes );
    }

    void operator delete( void *ptr )
    {
        HeapFree( GetProcessHeap(), 0, ptr );
    }

    bool is() const
    {
        return (FALSE != HeapValidate( GetProcessHeap(), 0, this ));
    }

};

//============================================================================
//	ClientPipe
//============================================================================

class ClientPipe : public Pipe
{
protected:
    ClientPipe( HANDLE hReadPipe, HANDLE hWritePipe );
public:
    static ClientPipe* Create( LPCTSTR lpName );
};

//============================================================================
//	ServerPipe
//============================================================================

class ServerPipe : public Pipe
{
protected:
    HANDLE	m_hMapping;
    HANDLE	m_hSynchronize;
    LPTSTR	m_lpName;

    ServerPipe( LPCTSTR lpName, HANDLE hMapping, HANDLE hSynchronize, HANDLE hReadPipe, HANDLE hWritePipe );
public:
    virtual ~ServerPipe();

    static ServerPipe *Create( LPCTSTR lpName );

    virtual Pipe *AcceptConnection();
};

//----------------------------------------------------------------------------
//	
//----------------------------------------------------------------------------

HANDLE	Pipe::CreatePipeDataMapping( LPCTSTR lpName )
{
    HANDLE	hMapping = NULL;
    LPTSTR	lpMappingName = (LPTSTR)alloca( _tcslen(lpName) * sizeof(TCHAR) + sizeof(PIPE_NAME_PREFIX_MAPPING) );

    if ( lpMappingName )
    {
        _tcscpy( lpMappingName, PIPE_NAME_PREFIX_MAPPING );
        _tcscat( lpMappingName, lpName );

        LPTSTR	lpMappingFileName = (LPTSTR)alloca( MAX_PATH * sizeof(TCHAR) );

        if ( lpMappingFileName )
        {
            DWORD	nChars = GetTempPath( MAX_PATH, lpMappingFileName );

            if ( MAX_PATH + _tcslen(lpName) < nChars + 1 )
            {
                lpMappingFileName = (LPTSTR)alloca( (nChars + 1 + _tcslen(lpName)) * sizeof(TCHAR) );
                if ( lpMappingFileName )
                    nChars = GetTempPath( nChars, lpMappingFileName );
                else
                {
                    nChars = 0;
                    SetLastError( ERROR_NOT_ENOUGH_MEMORY );
                }
            }

            if ( nChars )
            {
                _tcscat( lpMappingFileName, lpMappingName );

                HANDLE hFile = CreateFile( 
                    lpMappingFileName, 
                    GENERIC_READ | GENERIC_WRITE, 
                    FILE_SHARE_READ | FILE_SHARE_WRITE, 
                    NULL, 
                    OPEN_ALWAYS, 
                    FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, 
                    NULL );

                if ( IsValidHandle(hFile) )
                {
                    hMapping = CreateFileMapping(
                        (HANDLE)hFile,
                        (LPSECURITY_ATTRIBUTES)NULL,
                        PAGE_READWRITE,
                        0,
                        sizeof(PipeData),
                        lpMappingName );

                    CloseHandle( hFile );
                }
            }
        }
        else
            SetLastError( ERROR_NOT_ENOUGH_MEMORY );
    }

    return hMapping;
}

//----------------------------------------------------------------------------
//	
//----------------------------------------------------------------------------

HANDLE	Pipe::OpenPipeDataMapping( LPCTSTR lpName )
{
    HANDLE	hMapping = NULL;
    LPTSTR	lpMappingName = (LPTSTR)alloca( _tcslen(lpName) * sizeof(TCHAR) + sizeof(PIPE_NAME_PREFIX_MAPPING) );

    if ( lpMappingName )
    {
        _tcscpy( lpMappingName, PIPE_NAME_PREFIX_MAPPING );
        _tcscat( lpMappingName, lpName );

        hMapping = OpenFileMapping( FILE_MAP_ALL_ACCESS, FALSE, lpMappingName );
    }

    return hMapping;
}

//----------------------------------------------------------------------------
//	
//----------------------------------------------------------------------------

HANDLE	Pipe::CreatePipeDataMutex( LPCTSTR lpName, BOOL bInitialOwner )
{
    HANDLE	hMutex = NULL;
    LPTSTR	lpMutexName = (LPTSTR)alloca( _tcslen(lpName) * sizeof(TCHAR) + sizeof(PIPE_NAME_PREFIX_SYNCHRONIZE) );

    if ( lpMutexName )
    {
        _tcscpy( lpMutexName, PIPE_NAME_PREFIX_SYNCHRONIZE );
        _tcscat( lpMutexName, lpName );

        hMutex = CreateMutex( NULL, bInitialOwner, lpMutexName );
    }

    return hMutex;
}

//----------------------------------------------------------------------------
//	
//----------------------------------------------------------------------------

HANDLE Pipe::CreatePipeConnectionSemaphore( LPCTSTR lpName, LONG lInitialCount, LONG lMaximumCount )
{
    HANDLE	hSemaphore = NULL;
    LPTSTR	lpSemaphoreName = (LPTSTR)alloca( _tcslen(lpName) * sizeof(TCHAR) + sizeof(PIPE_NAME_PREFIX_CONNECTION) );

    if ( lpSemaphoreName )
    {
        _tcscpy( lpSemaphoreName, PIPE_NAME_PREFIX_CONNECTION );
        _tcscat( lpSemaphoreName, lpName );

        hSemaphore = CreateSemaphore( NULL, lInitialCount, lMaximumCount, lpSemaphoreName );
    }

    return hSemaphore;
}


//----------------------------------------------------------------------------
//	Pipe copy ctor
//----------------------------------------------------------------------------

Pipe::Pipe( const Pipe& rPipe ) :
m_hReadPipe( INVALID_HANDLE_VALUE ),
m_hWritePipe( INVALID_HANDLE_VALUE )
{
    DuplicateHandle( 
        GetCurrentProcess(),
        rPipe.m_hReadPipe,
        GetCurrentProcess(),
        &m_hReadPipe,
        0,
        FALSE,
        DUPLICATE_SAME_ACCESS );

    DuplicateHandle( 
        GetCurrentProcess(),
        rPipe.m_hWritePipe,
        GetCurrentProcess(),
        &m_hWritePipe,
        0,
        FALSE,
        DUPLICATE_SAME_ACCESS );
}

//----------------------------------------------------------------------------
//	Pipe assignment operator
//----------------------------------------------------------------------------

const Pipe& Pipe::operator = ( const Pipe& rPipe )
{
    Close();

    DuplicateHandle( 
        GetCurrentProcess(),
        rPipe.m_hReadPipe,
        GetCurrentProcess(),
        &m_hReadPipe,
        0,
        FALSE,
        DUPLICATE_SAME_ACCESS );

    DuplicateHandle( 
        GetCurrentProcess(),
        rPipe.m_hWritePipe,
        GetCurrentProcess(),
        &m_hWritePipe,
        0,
        FALSE,
        DUPLICATE_SAME_ACCESS );
    
    return *this;
}

//----------------------------------------------------------------------------
//	Pipe ctor
//----------------------------------------------------------------------------

Pipe::Pipe( HANDLE hReadPipe, HANDLE hWritePipe ) :
m_hReadPipe( INVALID_HANDLE_VALUE ),
m_hWritePipe( INVALID_HANDLE_VALUE )
{
    DuplicateHandle( 
        GetCurrentProcess(),
        hReadPipe,
        GetCurrentProcess(),
        &m_hReadPipe,
        0,
        FALSE,
        DUPLICATE_SAME_ACCESS );

    DuplicateHandle( 
        GetCurrentProcess(),
        hWritePipe,
        GetCurrentProcess(),
        &m_hWritePipe,
        0,
        FALSE,
        DUPLICATE_SAME_ACCESS );
}

//----------------------------------------------------------------------------
//	Pipe dtor
//----------------------------------------------------------------------------

Pipe::~Pipe()
{
    Close();
}

//----------------------------------------------------------------------------
//	Pipe Close
//----------------------------------------------------------------------------

bool Pipe::Close()
{
    bool	fSuccess = false;	// Assume failure

    if ( IsValidHandle(m_hReadPipe) )
    {
        CloseHandle( m_hReadPipe );
        m_hReadPipe = INVALID_HANDLE_VALUE;
    }

    if ( IsValidHandle(m_hWritePipe) )
    {
        CloseHandle( m_hWritePipe );
        m_hWritePipe = INVALID_HANDLE_VALUE;
    }

    return fSuccess;
}

//----------------------------------------------------------------------------
//	Pipe Write
//----------------------------------------------------------------------------

bool Pipe::Write( LPCVOID lpBuffer, DWORD dwBytesToWrite, LPDWORD lpBytesWritten, bool bWait )
{
    DWORD	dwBytesAvailable = 0;
    BOOL	fSuccess = TRUE;

    if ( !bWait )
        fSuccess = PeekNamedPipe( m_hReadPipe, NULL, 0, NULL, &dwBytesAvailable, NULL );

    if ( fSuccess )
    {
        if ( !bWait && dwBytesToWrite > PIPE_BUFFER_SIZE - dwBytesAvailable )
            dwBytesToWrite = PIPE_BUFFER_SIZE - dwBytesAvailable ;

        return !!WriteFile( m_hWritePipe, lpBuffer, dwBytesToWrite, lpBytesWritten, NULL );
    }

    return false;
}

//----------------------------------------------------------------------------
//	Pipe Read
//----------------------------------------------------------------------------

bool Pipe::Read( LPVOID lpBuffer, DWORD dwBytesToRead, LPDWORD lpBytesRead, bool bWait )
{
    DWORD	dwBytesAvailable = 0;
    BOOL	fSuccess = TRUE;

    if ( !bWait )
        fSuccess = PeekNamedPipe( m_hReadPipe, NULL, 0, NULL, &dwBytesAvailable, NULL );

    if ( fSuccess )
    {
        if ( bWait || dwBytesAvailable )
            return !!ReadFile( m_hReadPipe, lpBuffer, dwBytesToRead, lpBytesRead, NULL );
        else
        {
            *lpBytesRead = 0;
            return true;
        }
    }

    return false;
}



//----------------------------------------------------------------------------
//	Client pipe dtor
//----------------------------------------------------------------------------

ClientPipe::ClientPipe( HANDLE hReadPipe, HANDLE hWritePipe ) : Pipe( hReadPipe, hWritePipe )
{
}

//----------------------------------------------------------------------------
//	Client pipe creation
//----------------------------------------------------------------------------

ClientPipe *ClientPipe::Create( LPCTSTR lpName )
{
    ClientPipe	*pPipe = NULL;	// Assume failure

    HANDLE	hMapping = OpenPipeDataMapping( lpName );

    if ( IsValidHandle(hMapping) )
    {
        PipeData	*pData = (PipeData*)MapViewOfFile( hMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0 );

        if ( pData )
        {
            HANDLE	hSourceProcess = OpenProcess( PROCESS_DUP_HANDLE, FALSE, pData->dwProcessId );

            if ( IsValidHandle(hSourceProcess) )
            {
                BOOL fSuccess;
                HANDLE	hReadPipe = INVALID_HANDLE_VALUE, hWritePipe = INVALID_HANDLE_VALUE;
                
                fSuccess = DuplicateHandle( 
                    hSourceProcess,
                    pData->hReadPipe,
                    GetCurrentProcess(),
                    &hReadPipe,
                    0,
                    FALSE,
                    DUPLICATE_SAME_ACCESS );

                fSuccess = fSuccess && DuplicateHandle( 
                    hSourceProcess,
                    pData->hWritePipe,
                    GetCurrentProcess(),
                    &hWritePipe,
                    0,
                    FALSE,
                    DUPLICATE_SAME_ACCESS );

                if ( fSuccess )
                    pPipe = new ClientPipe( hReadPipe, hWritePipe );

                if ( IsValidHandle(hWritePipe) )
                    CloseHandle( hWritePipe );

                if ( IsValidHandle(hReadPipe) )
                    CloseHandle( hReadPipe );

                HANDLE	hConnectionRequest = CreatePipeConnectionSemaphore( lpName, 0, 1 );

                ReleaseSemaphore( hConnectionRequest, 1, NULL );

                CloseHandle( hConnectionRequest );
                
                CloseHandle( hSourceProcess );
            }

            UnmapViewOfFile( pData );
        }

        CloseHandle( hMapping );
    }

    return pPipe;
}



//----------------------------------------------------------------------------
//	ServerPipe ctor
//----------------------------------------------------------------------------

ServerPipe::ServerPipe( LPCTSTR	lpName, HANDLE hMapping, HANDLE hSynchronize, HANDLE hReadPipe, HANDLE hWritePipe ) : Pipe( hReadPipe, hWritePipe ),
m_hMapping( NULL ),
m_hSynchronize( NULL ),
m_lpName( NULL )
{
    DuplicateHandle( 
        GetCurrentProcess(),
        hMapping,
        GetCurrentProcess(),
        &m_hMapping,
        0,
        FALSE,
        DUPLICATE_SAME_ACCESS );

    DuplicateHandle(
        GetCurrentProcess(),
        hSynchronize,
        GetCurrentProcess(),
        &m_hSynchronize,
        0,
        FALSE,
        DUPLICATE_SAME_ACCESS
        );
    m_lpName = new TCHAR[_tcslen(lpName) + 1];
    if ( m_lpName )
        _tcscpy( m_lpName, lpName );
}

//----------------------------------------------------------------------------
//	ServerPipe dtor
//----------------------------------------------------------------------------

ServerPipe::~ServerPipe()
{
    if ( IsValidHandle(m_hMapping) )
        CloseHandle( m_hMapping );
    if ( m_lpName )
        delete[]m_lpName;
}

//----------------------------------------------------------------------------
//	ServerPipe AcceptConnection
//----------------------------------------------------------------------------

Pipe *ServerPipe::AcceptConnection()
{
    Pipe	*pPipe = NULL;	// Assume failure;

    HANDLE	hConnectionRequest = CreatePipeConnectionSemaphore( m_lpName, 0, 1 );

    if ( WAIT_OBJECT_0 == WaitForSingleObject( hConnectionRequest, INFINITE ) )
    {
        pPipe = new Pipe( *this );
        Close();

        // Create new inbound Pipe

        HANDLE	hClientWritePipe = NULL, hServerReadPipe = NULL;

        BOOL	fSuccess = CreatePipe( &hServerReadPipe, &hClientWritePipe, NULL, PIPE_BUFFER_SIZE );


        if ( fSuccess )
        {
            // Create outbound pipe

            HANDLE	hClientReadPipe = NULL, hServerWritePipe = NULL;

            if ( CreatePipe( &hClientReadPipe, &hServerWritePipe, NULL, PIPE_BUFFER_SIZE ) )
            {
                m_hReadPipe = hServerReadPipe;
                m_hWritePipe = hServerWritePipe;

                PipeData	*pData = (PipeData *)MapViewOfFile( m_hMapping, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(PipeData) );

                HANDLE	hSynchronize = CreatePipeDataMutex( m_lpName, TRUE );

                CloseHandle( pData->hReadPipe );
                CloseHandle( pData->hWritePipe );

                pData->hReadPipe = hClientReadPipe;
                pData->hWritePipe = hClientWritePipe;

                ReleaseMutex( hSynchronize );

                CloseHandle( hSynchronize );

            }
            else
            {
                CloseHandle( hClientWritePipe );
                CloseHandle( hServerWritePipe );	
            }
        }

        ReleaseMutex( hConnectionRequest );
    }

    CloseHandle( hConnectionRequest );

    return pPipe;
}

//----------------------------------------------------------------------------
//	Pipe creation
//----------------------------------------------------------------------------

ServerPipe *ServerPipe::Create( LPCTSTR lpName )
{
    ServerPipe	*pPipe = NULL;

    HANDLE	hMapping = CreatePipeDataMapping( lpName );

    if ( IsValidHandle(hMapping) )
    {
        if ( ERROR_FILE_EXISTS != GetLastError() )
        {
            HANDLE	hSynchronize = CreatePipeDataMutex( lpName, FALSE);
            
            WaitForSingleObject( hSynchronize, INFINITE );

            PipeData	*pData = (PipeData*)MapViewOfFile( hMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0 );

            if ( pData )
            {

                // Initialize pipe data

                pData->dwProcessId = 0;
                pData->hReadPipe = NULL;
                pData->hWritePipe = NULL;

                // Create inbound pipe

                HANDLE	hServerReadPipe = NULL, hClientWritePipe = NULL;

                BOOL	fSuccess = CreatePipe( &hServerReadPipe, &hClientWritePipe, NULL, PIPE_BUFFER_SIZE );

                if ( fSuccess )
                {
                    // Create outbound pipe

                    HANDLE	hServerWritePipe = NULL, hClientReadPipe = NULL;

                    fSuccess = CreatePipe( &hClientReadPipe, &hServerWritePipe, NULL, PIPE_BUFFER_SIZE );

                    if ( fSuccess )
                    {
                        pData->dwProcessId = GetCurrentProcessId();
                        pData->hReadPipe = hClientReadPipe;
                        pData->hWritePipe = hClientWritePipe;
                        pPipe = new ServerPipe( lpName, hMapping, hSynchronize, hServerReadPipe, hServerWritePipe );

                        CloseHandle( hServerWritePipe );
                        CloseHandle( hServerReadPipe );
                    }
                    else
                    {
                        CloseHandle( hServerReadPipe );
                        CloseHandle( hClientWritePipe );
                    }
                }

                UnmapViewOfFile( pData );
            }

            ReleaseMutex( hSynchronize );
            CloseHandle( hSynchronize );
        }

        CloseHandle( hMapping );
    }

    return pPipe;
}


//----------------------------------------------------------------------------
//	C style API
//----------------------------------------------------------------------------

const TCHAR	LOCAL_PIPE_PREFIX[] = TEXT("\\\\.\\PIPE\\" );

extern "C" HANDLE WINAPI CreateSimplePipe( LPCTSTR lpName )
{
    int	nPrefixLen = _tcslen( LOCAL_PIPE_PREFIX );
    if ( 0 == _tcsnicmp( lpName, LOCAL_PIPE_PREFIX, nPrefixLen ) )
        lpName += nPrefixLen;
    return (HANDLE)ServerPipe::Create( lpName );
}

extern "C" HANDLE WINAPI OpenSimplePipe( LPCTSTR lpName )
{
    int	nPrefixLen = _tcslen( LOCAL_PIPE_PREFIX );
    if ( 0 == _tcsnicmp( lpName, LOCAL_PIPE_PREFIX, nPrefixLen ) )
        lpName += nPrefixLen;
    return (HANDLE)ClientPipe::Create( lpName );
}

extern "C" HANDLE WINAPI AcceptSimplePipeConnection( HANDLE hPipe )
{
    Pipe	*pPipe = (Pipe *)hPipe;

    if ( pPipe->is() )
        return (HANDLE)pPipe->AcceptConnection();
    else
    {
        SetLastError( ERROR_INVALID_HANDLE );
        return NULL;
    }
}

extern "C" BOOL WINAPI WaitForSimplePipe( LPCTSTR /*lpName*/, DWORD /*dwTimeOut*/ )
{
    return FALSE;
}

extern "C" BOOL WINAPI WriteSimplePipe( HANDLE hPipe, LPCVOID lpBuffer, DWORD dwBytesToWrite, LPDWORD lpBytesWritten, BOOL bWait )
{
    Pipe	*pPipe = (Pipe *)hPipe;

    if ( pPipe->is() )
        return pPipe->Write( lpBuffer, dwBytesToWrite, lpBytesWritten, bWait );
    else
    {
        SetLastError( ERROR_INVALID_HANDLE );
        return FALSE;
    }
}

extern "C" BOOL WINAPI ReadSimplePipe( HANDLE hPipe, LPVOID lpBuffer, DWORD dwBytesToRead, LPDWORD lpBytesRead, BOOL bWait )
{
    Pipe	*pPipe = (Pipe *)hPipe;

    if ( pPipe->is() )
        return pPipe->Read( lpBuffer, dwBytesToRead, lpBytesRead, bWait );
    else
    {
        SetLastError( ERROR_INVALID_HANDLE );
        return FALSE;
    }
}

extern "C" BOOL WINAPI CloseSimplePipe( HANDLE hPipe )
{
    Pipe	*pPipe = (Pipe *)hPipe;

    if ( pPipe->is() )
    {
        delete pPipe;
        return TRUE;
    }
    else
    {
        SetLastError( ERROR_INVALID_HANDLE );
        return FALSE;
    }
}