// modify: Jason 2015-01-16
// 进程通信助手,已经测试
// console.cpp中测试过,两个进程可以正常通信
#pragma once
#ifndef IPC_H
#define IPC_H
// Definitions
//#define IPC_BLOCK_COUNT 512
//#define IPC_BLOCK_SIZE 4096
#define IPC_BLOCK_COUNT 10
#define IPC_BLOCK_SIZE 1024
#define IPC_MAX_ADDR 256
// ---------------------------------------
// -- Inter-Process Communication class --
// ---------------------------------------------------------------
// Provides intercommunication between processes and there threads
// ---------------------------------------------------------------
class dzIPC
{
public:
// Block that represents a piece of data to transmit between the
// writer and reader
struct Block
{
BOOL hasData; // Flag used to signal that this block has been written
DWORD Amount; // Amount of data help in this block
BYTE Data[IPC_BLOCK_SIZE]; // Data contained in this block
};
private:
// Shared memory buffer that contains everything required to transmit
// data between the writer and reader, 'lock ' has been placed in reader or writer object.
struct MemBuff
{
// Block data, this is placed first to remove the offset (optimisation)
Block m_Blocks[IPC_BLOCK_COUNT]; // Array of buffers that are used in the communication
// Cursors
volatile INT m_front; // Start of the read cursor
volatile INT m_rear; // Start of the write cursor
volatile LONG m_lock;
private:
void Lock()
{
while( ::InterlockedCompareExchange((LPLONG)&m_lock, 1, 0) != 0 )
::Sleep(0);
}
VOID Unlock()
{
::InterlockedExchange((LPLONG)(&m_lock), 0);
}
BOOL TryLock()
{
return ::InterlockedCompareExchange((LPLONG)&m_lock, 1, 0) == 0;
}
bool _IsEmpty()
{
if( m_front < 0 || m_front >= IPC_BLOCK_COUNT )
{
m_front = 0;
}
return m_front == m_rear
&& m_Blocks[m_front].hasData == FALSE;
}
bool _IsFull()
{
if( m_front < 0 || m_front >= IPC_BLOCK_COUNT )
{
m_front = 0;
}
return m_front == m_rear
&& m_Blocks[m_front].hasData == TRUE;
}
public:
bool IsEmpty()
{
Lock();
bool re = _IsEmpty();
Unlock();
return re;
}
bool IsFull()
{
Lock();
bool re = _IsFull();
Unlock();
return re;
}
DWORD Read(char * szBuf,DWORD nSize)
{
Lock();
if( _IsEmpty() )
{
Unlock();
return 0;
}
INT n = m_front;
m_front = (++m_front % IPC_BLOCK_COUNT);
Block * pBlock = m_Blocks + n;
if( pBlock->Amount > 0 )
{
nSize = min(nSize,pBlock->Amount);
memcpy(szBuf,pBlock->Data,nSize);
pBlock->hasData = 0;
pBlock->Amount = 0;
Unlock();
return nSize;
}
Unlock();
return 0;
}
DWORD Write(const char * szBuf, DWORD n)
{
Lock();
if( _IsFull() )
{
Unlock();
return 0;
}
int nDx = m_rear;
m_rear = (++m_rear % IPC_BLOCK_COUNT);
Block * pBlock = m_Blocks + nDx;
n = min(n,IPC_BLOCK_SIZE);
memcpy(pBlock->Data,szBuf,n);
pBlock->Amount = n;
pBlock->hasData = 1;
Unlock();
return n;
}
MemBuff()
{
ZeroMemory(m_Blocks,sizeof(m_Blocks));
m_front = m_rear = 0;
m_lock = 0;
}
};
public:
// ID Generator
static DWORD GetID(void)
{
// Generate an ID and return id
static volatile LONG id = 1;
return (DWORD)InterlockedIncrement((LONG*)&id);
};
public:
// Reader class
// Method 'Reader_open' must be called first than 'Writer_open'.
class Reader
{
public:
// Construct / Destruct
Reader();
Reader(const char * szName);
~Reader();
private:
// Internal variables
//char *m_sAddr; // Address of this reader
HANDLE m_hMapFile; // Handle to the mapped memory file
// 进程通信助手,已经测试
// console.cpp中测试过,两个进程可以正常通信
#pragma once
#ifndef IPC_H
#define IPC_H
// Definitions
//#define IPC_BLOCK_COUNT 512
//#define IPC_BLOCK_SIZE 4096
#define IPC_BLOCK_COUNT 10
#define IPC_BLOCK_SIZE 1024
#define IPC_MAX_ADDR 256
// ---------------------------------------
// -- Inter-Process Communication class --
// ---------------------------------------------------------------
// Provides intercommunication between processes and there threads
// ---------------------------------------------------------------
class dzIPC
{
public:
// Block that represents a piece of data to transmit between the
// writer and reader
struct Block
{
BOOL hasData; // Flag used to signal that this block has been written
DWORD Amount; // Amount of data help in this block
BYTE Data[IPC_BLOCK_SIZE]; // Data contained in this block
};
private:
// Shared memory buffer that contains everything required to transmit
// data between the writer and reader, 'lock ' has been placed in reader or writer object.
struct MemBuff
{
// Block data, this is placed first to remove the offset (optimisation)
Block m_Blocks[IPC_BLOCK_COUNT]; // Array of buffers that are used in the communication
// Cursors
volatile INT m_front; // Start of the read cursor
volatile INT m_rear; // Start of the write cursor
volatile LONG m_lock;
private:
void Lock()
{
while( ::InterlockedCompareExchange((LPLONG)&m_lock, 1, 0) != 0 )
::Sleep(0);
}
VOID Unlock()
{
::InterlockedExchange((LPLONG)(&m_lock), 0);
}
BOOL TryLock()
{
return ::InterlockedCompareExchange((LPLONG)&m_lock, 1, 0) == 0;
}
bool _IsEmpty()
{
if( m_front < 0 || m_front >= IPC_BLOCK_COUNT )
{
m_front = 0;
}
return m_front == m_rear
&& m_Blocks[m_front].hasData == FALSE;
}
bool _IsFull()
{
if( m_front < 0 || m_front >= IPC_BLOCK_COUNT )
{
m_front = 0;
}
return m_front == m_rear
&& m_Blocks[m_front].hasData == TRUE;
}
public:
bool IsEmpty()
{
Lock();
bool re = _IsEmpty();
Unlock();
return re;
}
bool IsFull()
{
Lock();
bool re = _IsFull();
Unlock();
return re;
}
DWORD Read(char * szBuf,DWORD nSize)
{
Lock();
if( _IsEmpty() )
{
Unlock();
return 0;
}
INT n = m_front;
m_front = (++m_front % IPC_BLOCK_COUNT);
Block * pBlock = m_Blocks + n;
if( pBlock->Amount > 0 )
{
nSize = min(nSize,pBlock->Amount);
memcpy(szBuf,pBlock->Data,nSize);
pBlock->hasData = 0;
pBlock->Amount = 0;
Unlock();
return nSize;
}
Unlock();
return 0;
}
DWORD Write(const char * szBuf, DWORD n)
{
Lock();
if( _IsFull() )
{
Unlock();
return 0;
}
int nDx = m_rear;
m_rear = (++m_rear % IPC_BLOCK_COUNT);
Block * pBlock = m_Blocks + nDx;
n = min(n,IPC_BLOCK_SIZE);
memcpy(pBlock->Data,szBuf,n);
pBlock->Amount = n;
pBlock->hasData = 1;
Unlock();
return n;
}
MemBuff()
{
ZeroMemory(m_Blocks,sizeof(m_Blocks));
m_front = m_rear = 0;
m_lock = 0;
}
};
public:
// ID Generator
static DWORD GetID(void)
{
// Generate an ID and return id
static volatile LONG id = 1;
return (DWORD)InterlockedIncrement((LONG*)&id);
};
public:
// Reader class
// Method 'Reader_open' must be called first than 'Writer_open'.
class Reader
{
public:
// Construct / Destruct
Reader();
Reader(const char * szName);
~Reader();
private:
// Internal variables
//char *m_sAddr; // Address of this reader
HANDLE m_hMapFile; // Handle to the mapped memory file