Click here to Skip to main content
15,892,697 members
Articles / Desktop Programming / Win32

Cross-Platform IPC Event Manager for Interaction with Service Providers

Rate me:
Please Sign up or sign in to vote.
4.40/5 (5 votes)
6 May 2009LGPL35 min read 38.6K   713   38  
This article shows you how to send or post events among processes using shared memory queues for the Windows and Linux platforms.
/* ==============================================================================================================================
 * This notice must be untouched at all times.
 *
 * Copyright  IntelliWizard Inc. 
 * All rights reserved.
 * LICENSE: LGPL. 
 * Redistributions of source code modifications must send back to the Intelliwizard Project and republish them. 
 * Web: http://www.intelliwizard.com
 * eMail: info@intelliwizard.com
 * We provide technical supports for UML StateWizard users.
 * ==============================================================================================================================*/

#ifndef EXT_IPC_EVENT_H
#define EXT_IPC_EVENT_H 1

#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif

#include "shared_memory_queue.h"

struct XSemaphore
{
	XSemaphore(unsigned int initial_count, unsigned int max_count, const char *name, bool create_new);
	~XSemaphore();
	int P();
	int V();

#ifdef WIN32
	typedef HANDLE handle_t;
#else
	typedef int handle_t;
#endif

	handle_t sem_;

	bool owned;
	const char* name_;

private:
	XSemaphore(const XSemaphore&);
	XSemaphore& operator=(const XSemaphore&);
};

void SleepInMilliseconds(int msec);

//////////////////////////////////////////////////////////////////////////////////////////////////////

#define SME_SHAREMEMORY_SEMAPHORE_NUM 1

struct IPCEventInfo 
{
	int SourceProcessID;
	int DestProcessID;
	int IPCEventStatus;
	int EventID;
	unsigned long ulParameter1;
	unsigned long ulParameter2;
};

typedef int (*SME_IPC_EVENT_CALLBACK_T)(void* pInEventInfo);

//////////////////////////////////////////////////////////////////////////////////////////////////////

class XExtIPCEvent 
{
  public:
      XExtIPCEvent();

      ~XExtIPCEvent();


	  enum {IPC_EVENT_BUFFER_SIZE=1024};

	  enum {IPC_QUEUE_NUMBER=4};
	  enum {IPC_QUEUE_RESOURCE=0,IPC_QUEUE_SENDING=1,IPC_QUEUE_POSTING=3};

	  //Four queues: empty, send queue, wait for sending, and post

		enum TmCommandStatus
		{
			EVENT_STATUS_UNUSED = 0, EVENT_STATUS_SENDING, EVENT_STATUS_POSTING, EVENT_STATUS_CONSUMED
		};



      static XExtIPCEvent* GetInstance ();

      int Create(int ProcessID, bool bIsMasterProcess = false);

      void Release (bool bIsMasterProcess = false);

      int SendIPCEvent(IPCEventInfo* pInEventInfo, IPCEventInfo* pOutEventInfo, int nTimeout = -1);
      int PostIPCEvent(IPCEventInfo* pInEventInfo);
      int QueryIPCEvent(IPCEventInfo* pOutEventInfo, SME_IPC_EVENT_CALLBACK_T pfnCallbak);

      void RecycleEvent(int idProcess);
      bool DumpInfo ();

  protected:
  private:

      int Lock ();
      int UnLock();

  private: 

      XSharedMemoryQueue* m_pSMQueue;
      XSemaphore* m_pSem;
      int m_idProcess;
};

// Class XExtIPCEvent 

int XExtIPCEventCreate(int ProcessID, int bIsMasterProcess = 0);
void XExtIPCEventRelease(int bIsMasterProcess = 0);
int XExtIPCEventSend(IPCEventInfo* pInEventInfo, IPCEventInfo* pOutEventInfo, int nTimeout = -1 );
int XExtIPCEventPost(IPCEventInfo* pInEventInfo);
int XExtIPCEventQueryIPCEvent(IPCEventInfo* pOutEventInfo, SME_IPC_EVENT_CALLBACK_T pfnCallbak);
void XExtIPCEventRecycleEvent(int idProcess);
bool XExtIPCEventDumpInfo();


#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior)
United States United States
Alex "Question is more important than the answer."

Comments and Discussions