Category : C++ Source Code
Archive   : OS2MBOX.ZIP
Filename : MAILBOX.CPP

 
Output of file : MAILBOX.CPP contained in archive : OS2MBOX.ZIP

//------------- Member functions for OS/2 mailbox class -----------
//------------- Peter Braswell -----------
//------------- September 17, 1993 -----------


/* $Header$ */
/*
* $Log$

*/

#define STATIC_DEF
#include "mailbox.hpp"
#include
#include
#include

/*
**------------------------------------------------------------
** Function : mailBox :: mailBox( char *taskName )
**
** Description : Default constructor, used to actually build the
** underlying structure of the mailbox.
**
** Input Conditions :
**
**
** Function History : dc091793 creation
**------------------------------------------------------------
*/
mailBox ::
mailBox( char *taskName)
{
APIRET returnCode;
PTIB ptib;
PPIB ppib;
BOOL32 fState;
ULONG flAttr;
UCHAR queueName[40];
PSZ pszQueueName;
REQUESTDATA request;

ULONG dataLength;
PVOID dataAddress;
PVOID baseAddress;
ULONG elementCode;
BYTE elemPriority;
BOOL32 noWait;

noWait = 1;
elementCode = 0;

// Get owner's process information
returnCode = DosGetInfoBlocks( &ptib, &ppib);

// Build a semaphore for the mailbox..
flAttr = DC_SEM_SHARED;
fState = FALSE;
returnCode = DosCreateEventSem(
(PSZ)NULL,
&semHandle,
flAttr,
fState);

// Build queue name from task name...
strcpy( queueName, "\\QUEUES\\");
strcat( queueName, taskName);
strcat( queueName, ".QUE");
pszQueueName = (PSZ)queueName;

// Open the queue
returnCode = DosCreateQueue(
&queueHandle,
QUE_FIFO,
pszQueueName);

// Attatch the semaphore to the queue...
returnCode = DosReadQueue( queueHandle,
&request,
&dataLength,
&dataAddress,
elementCode,
noWait,
&elemPriority,
semHandle);

instances++;
universalQHand = queueHandle;
universalSemHand = semHandle;

} // mailBox()

/*
**------------------------------------------------------------
** Function : mailBox :: getMail()
**
** Description : Gets mail out of the queue within a specified
** amount of time or waits indefinately.
**
** Input Conditions :
**
** Notes:
**
** Function History : dc071492 creation
**------------------------------------------------------------
*/
BOOL mailBox ::
getMail( PVOID mail,
int sizeOfDataBuffer,
int timeToWait)
{
APIRET returnCode;
REQUESTDATA request;
ULONG dataLength;
ULONG ulPostCount;
PVOID dataAddress;
PVOID baseAddress;
ULONG elementCode;
BYTE elemPriority;
BOOL32 noWait;

elementCode = 0;
noWait = 1;

if( timeToWait == -1) // Requestor wants to block and wait
{
returnCode = DosWaitEventSem( semHandle,
SEM_INDEFINITE_WAIT);
} // if( timeToWait == -1)

returnCode = DosReadQueue( queueHandle,
&request,
&dataLength,
&dataAddress,
elementCode,
noWait,
&elemPriority,
semHandle);

if( returnCode == 0)
{
if( (dataLength <= sizeOfDataBuffer) && (returnCode == 0) )
{
memcpy( mail, dataAddress, dataLength);
DosResetEventSem( semHandle,
&ulPostCount);
return TRUE;
} // if( (dataLength <= sizeOfDataBuffer) && (returnCode == 0) )
else
{
return FALSE;
} // else

} // if( returnCode == 0)
else
{
return FALSE;
} // else

} // getMail()

/*
**------------------------------------------------------------
** Function : mailBox :: peekMail()
**
** Description : Used to peek at a mailbox and determines if there is
** mail available or not.
**
** Input Conditions :
**
** Notes:
**
** Function History : dc120193 creation
**------------------------------------------------------------
*/
BOOL mailBox ::
peekMail( )
{
APIRET returnCode;
REQUESTDATA request;
ULONG dataLength;
ULONG ulPostCount;
PVOID dataAddress;
PVOID baseAddress;
ULONG elementCode;
BYTE elemPriority;
BOOL32 noWait;

noWait = 1;
elementCode = 0;

returnCode = DosPeekQueue( mailBox::universalQHand,
&request,
&dataLength,
&dataAddress,
&elementCode,
noWait,
&elemPriority,
universalSemHand);

if( returnCode == 0 ) // Mail is available, idicate so
return( TRUE );
else
return( FALSE ); // Mail is not available...

} // peekMail( )


//*****************************************************************************
//* TEST SECTION *
//*****************************************************************************

#ifdef _UNIT_TEST
#include

mailBox myMailbox( "TASK1\0");
struct
{
int i;
char text[20];

} message;

void main (void)
{
mailBox secondMailbox;
while( TRUE)
{
myMailbox.peekMail( );
secondMailbox.peekMail( );
printf( "\r Message: %20s Rec#: %6d",
message.text, message.i);

} // while( TRUE)

} // void main( void)

#endif