2012-03-23 10:11:34 +00:00
|
|
|
// Copyright (c) 2011-2012 Ryan Prichard
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to
|
|
|
|
// deal in the Software without restriction, including without limitation the
|
|
|
|
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
// sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
// IN THE SOFTWARE.
|
|
|
|
|
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
|