/***************************************************************************
                System V IPC message queue class
                -----------------------------------------
    begin      : 2013/06/21
	copyright  : (C) 2005 Solbox Inc.
    author     : Dev 1 Team
    email      : dev1@solbox.com
    version    : 3.2.0

    CopyRight(C) 2005 Solbox Inc. All Rights reserved.
    Redistribution and use in source and binary forms, with or with out
    modification, are not permitted in outside of Solbox Inc.
*****************************************************************************/

// FreeBSD limit : ipcs -Q 

#ifndef __IPC_MESSAGE_QUEUE__
#define __IPC_MESSAGE_QUEUE__

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdint.h>
#include <string>
#include <iostream>

using namespace std;

class CIPCMsgQueue
{
public:
	CIPCMsgQueue();
	CIPCMsgQueue(key_t qkey, int msgflg = IPC_CREAT | 0644, const char* user = "nobody", uint32_t qsize = 0);
	~CIPCMsgQueue();

	// 메세지 큐 생성
	bool Create();
	bool Create(key_t qkey, int msgflg = IPC_CREAT | 0644, const char* user = "nobody", uint32_t qsize = 0);
	// 메세지 큐 변경	
	bool ChangeQueue(key_t qkey, int msgflg = IPC_CREAT | 0644, const char* user = "nobody", uint32_t qsize = 0);
	// 메세지 큐 삭제
	bool Remove();

	// 큐 데이터 pop 
	//  IPC_NOWAIT : 비동기화
	// return 
	//  0 < : 비정상
	//  0   : 큐 비워있음
	//  0 > : 해당 큐에 읽음 데이터 size
	int Pop(void * data, size_t maxmsgsz, int flags = IPC_NOWAIT, long msgtype = 0);
	
	// 큐 데이터 push
	//  IPC_NOWAIT : 비동기화
	// return 
	//  0 >  : 비정상
	//  0 =< : 성공
	int Push(void * data, size_t maxmsgsz, int flags = IPC_NOWAIT);
private:
	bool CtlOperations(int cmd);

	int			m_msqid;

	key_t		m_key;
	int			m_msgflg;

	string		m_user;
	uint32_t	m_qsize;
};




#endif // __IPC_MESSAGE_QUEUE__
