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
|
// --------------------------------------------------------------------------
//
// File
// Name: WinNamedPipeStream.h
// Purpose: I/O stream interface for Win32 named pipes
// Created: 2005/12/07
//
// --------------------------------------------------------------------------
#if ! defined WINNAMEDPIPESTREAM__H && defined WIN32
#define WINNAMEDPIPESTREAM__H
#include <list>
#include "IOStream.h"
// --------------------------------------------------------------------------
//
// Class
// Name: WinNamedPipeStream
// Purpose: I/O stream interface for Win32 named pipes
// Created: 2003/07/31
//
// --------------------------------------------------------------------------
class WinNamedPipeStream : public IOStream
{
public:
WinNamedPipeStream();
WinNamedPipeStream(HANDLE hNamedPipe);
~WinNamedPipeStream();
// server side - create the named pipe and listen for connections
// use WinNamedPipeListener to do this instead.
// client side - connect to a waiting server
void Connect(const std::string& rName);
// both sides
virtual int Read(void *pBuffer, int NBytes,
int Timeout = IOStream::TimeOutInfinite);
virtual void Write(const void *pBuffer, int NBytes,
int Timeout = IOStream::TimeOutInfinite);
virtual void WriteAllBuffered();
virtual void Close();
virtual bool StreamDataLeft();
virtual bool StreamClosed();
// Why not inherited from IOStream? Never mind, we want to enforce
// supplying a timeout for network operations anyway.
virtual void Write(const std::string& rBuffer, int Timeout)
{
IOStream::Write(rBuffer, Timeout);
}
protected:
void MarkAsReadClosed() {mReadClosed = true;}
void MarkAsWriteClosed() {mWriteClosed = true;}
bool WaitForOverlappedOperation(OVERLAPPED& Overlapped,
int Timeout, int64_t* pBytesTransferred);
void StartFirstRead();
void StartOverlappedRead();
private:
WinNamedPipeStream(const WinNamedPipeStream &rToCopy)
{ /* do not call */ }
HANDLE mSocketHandle;
HANDLE mReadableEvent;
OVERLAPPED mReadOverlap;
uint8_t mReadBuffer[4096];
size_t mBytesInBuffer;
bool mReadClosed;
bool mWriteClosed;
bool mIsServer;
bool mIsConnected;
bool mNeedAnotherRead;
class WriteInProgress {
private:
friend class WinNamedPipeStream;
std::string mBuffer;
OVERLAPPED mOverlap;
WriteInProgress(const WriteInProgress& other); // do not call
public:
WriteInProgress(const std::string& dataToWrite)
: mBuffer(dataToWrite)
{
// create the Writable event
HANDLE writable_event = CreateEvent(NULL, TRUE, FALSE,
NULL);
if (writable_event == INVALID_HANDLE_VALUE)
{
BOX_LOG_WIN_ERROR("Failed to create the "
"Writable event");
THROW_EXCEPTION(CommonException, Internal)
}
memset(&mOverlap, 0, sizeof(mOverlap));
mOverlap.hEvent = writable_event;
}
~WriteInProgress()
{
CloseHandle(mOverlap.hEvent);
}
};
std::list<WriteInProgress*> mWritesInProgress;
public:
static std::string sPipeNamePrefix;
};
#endif // WINNAMEDPIPESTREAM__H
|