windows下的IPC实现,已经过测试

// 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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值