2012-03-14 05:14:39 +00:00
|
|
|
#ifndef NAMEDPIPE_H
|
|
|
|
#define NAMEDPIPE_H
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <string>
|
2012-03-14 06:27:21 +00:00
|
|
|
#include <vector>
|
2012-03-14 05:14:39 +00:00
|
|
|
|
|
|
|
class EventLoop;
|
|
|
|
|
|
|
|
class NamedPipe
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
// The EventLoop uses these private members.
|
|
|
|
friend class EventLoop;
|
|
|
|
NamedPipe();
|
|
|
|
~NamedPipe();
|
2012-03-14 06:27:21 +00:00
|
|
|
bool serviceIo(std::vector<HANDLE> *waitHandles);
|
2012-03-14 05:14:39 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
class IoWorker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
IoWorker(NamedPipe *namedPipe);
|
|
|
|
virtual ~IoWorker();
|
2012-03-14 06:27:21 +00:00
|
|
|
int service();
|
2012-03-14 05:14:39 +00:00
|
|
|
HANDLE getWaitEvent();
|
|
|
|
protected:
|
|
|
|
NamedPipe *m_namedPipe;
|
|
|
|
bool m_pending;
|
2012-03-14 06:27:21 +00:00
|
|
|
int m_currentIoSize;
|
2012-03-14 05:14:39 +00:00
|
|
|
HANDLE m_event;
|
|
|
|
OVERLAPPED m_over;
|
|
|
|
enum { kIoSize = 64 * 1024 };
|
|
|
|
char m_buffer[kIoSize];
|
|
|
|
virtual void completeIo(int size) = 0;
|
|
|
|
virtual bool shouldIssueIo(int *size, bool *isRead) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class InputWorker : public IoWorker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
InputWorker(NamedPipe *namedPipe) : IoWorker(namedPipe) {}
|
|
|
|
protected:
|
|
|
|
virtual void completeIo(int size);
|
|
|
|
virtual bool shouldIssueIo(int *size, bool *isRead);
|
|
|
|
};
|
|
|
|
|
|
|
|
class OutputWorker : public IoWorker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
OutputWorker(NamedPipe *namedPipe) : IoWorker(namedPipe) {}
|
2012-03-14 06:27:21 +00:00
|
|
|
int getPendingIoSize();
|
2012-03-14 05:14:39 +00:00
|
|
|
protected:
|
|
|
|
virtual void completeIo(int size);
|
|
|
|
virtual bool shouldIssueIo(int *size, bool *isRead);
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool connectToServer(LPCWSTR pipeName);
|
2012-03-14 06:27:21 +00:00
|
|
|
int bytesToSend();
|
2012-03-14 05:14:39 +00:00
|
|
|
void write(const void *data, int size);
|
|
|
|
void write(const char *text);
|
|
|
|
int readBufferSize();
|
|
|
|
void setReadBufferSize(int size);
|
|
|
|
int bytesAvailable();
|
|
|
|
int peek(void *data, int size);
|
|
|
|
std::string read(int size);
|
|
|
|
std::string readAll();
|
|
|
|
void closePipe();
|
|
|
|
bool isClosed();
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Input/output buffers
|
|
|
|
int m_readBufferSize;
|
|
|
|
std::string m_inQueue;
|
|
|
|
std::string m_outQueue;
|
|
|
|
HANDLE m_handle;
|
2012-03-14 06:27:21 +00:00
|
|
|
InputWorker *m_inputWorker;
|
|
|
|
OutputWorker *m_outputWorker;
|
2012-03-14 05:14:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // NAMEDPIPE_H
|