examples/PIPS/opencmessagequeuelibraryex/engine/src/msgqsend.c

00001 
00010 #include <errno.h>
00011 #include <sys/msg.h>
00012 #include <sys/sem.h>
00013 #include <string.h>
00014 #include "MsgQInternal.h"
00015 
00016 
00017 /*******************************************************************************
00018 * MsgQSend (qName, msg, nBytes, priority, timeout, err)
00019 * Description:  Function for sending a message with internal copy
00020 *********************************************************************************/
00021 
00022 EXPORT_C int MsgQSend(ULONG qName, char* msg, ULONG nBytes, ULONG priority, int timeout, int* err)
00023 {
00024         MSGQ_INFO* pMsgQInfo = NULL;
00025 
00026         struct {
00027                 long mtype;
00028                 char mtext[MAX_MSG_LEN];
00029         } message;
00030 
00031         /* structure used for semaphore post operation */
00032         struct sembuf op;
00033         
00034         /* init the  semop () structure which is used for wait and signal operations */
00035         op.sem_num = 0;
00036         op.sem_op = -1;
00037         op.sem_flg = SEM_UNDO;
00038 
00039         /* check parameters */
00040         if ((priority == MSG_PRI_NORMAL) || (priority == MSG_PRI_URGENT)) {
00041                 if((pMsgQInfo = MsgQTableLookup(qName)) != NULL) {
00042                         if (pMsgQInfo->sendState == MSG_Q_READY) {
00043                                 op.sem_flg = op.sem_flg | timeout;
00044                                 if((semop(pMsgQInfo->semId, &op, 1)) == OK) {
00045                                         pMsgQInfo->numMsgs++;
00046                                         if(pMsgQInfo->maxNumMsgs < pMsgQInfo->numMsgs)          
00047                                         pMsgQInfo->maxNumMsgs = pMsgQInfo->numMsgs;
00048 
00049                                         message.mtype = 1;
00050                                         bcopy(msg, message.mtext, nBytes);
00051                                         message.mtext[nBytes] = '\0';
00052 
00053                                         if(msgsnd (pMsgQInfo->qId, &message, (size_t)nBytes+4, timeout) == OK) {
00054                                                 *err = OK;
00055                                                 /* After successfull send, unlock the message queue by using post operation on semaphore.*/
00056                                                 op.sem_op = 1;
00057                                                 semop(pMsgQInfo->semId, &op, 1);
00058                                                 return (OK);
00059                                         }
00060                                         else {
00061                                                 *err = errno;
00062                                                 pMsgQInfo->numMsgs--;
00063                                                 op.sem_op = 1;
00064                                                 semop(pMsgQInfo->semId, &op, 1);
00065                                         }
00066                                 }
00067                                 else
00068                                         *err = errno;
00069                         }
00070                         else
00071                                 *err = KMsgQLibQFlushErr;
00072                 }
00073                 else
00074                         *err = KMsgQLibQIdErr;
00075         }
00076         else
00077                 *err = KMsgQLibParamErr;
00078 
00079         return(ERROR);
00080 }
00081 
00082 

Generated by  doxygen 1.6.2