rllib  1
Public Types | Public Member Functions | Public Attributes | Private Attributes
rlMailbox Class Reference

#include <rlmailbox.h>

Inheritance diagram for rlMailbox:
Inheritance graph
[legend]

List of all members.

Public Types

enum  MailboxEnum {
  MAILBOX_ERROR = -1, MAILBOX_FULL = -2, WAIT = 1, NOWAIT = 0,
  MAX_MAILBOX = 256*256, OK = 2, COULD_NOT_CREATE_MAILBOX = 3, COULD_NOT_GET_KEY = 4,
  COULD_NOT_GET_CHAN_ID = 5
}

Public Member Functions

 rlMailbox (const char *name)
virtual ~rlMailbox ()
int write (const void *buf, int len)
int printf (const char *format,...)
int read (void *buf, int maxlen, int wait=WAIT)
void clear ()

Public Attributes

int status
char * name

Private Attributes

int chanid

Detailed Description

class for a mailbox.
A mailbox is for communication between processes on the same computer.
It works like a fifo.
Many processes can write to 1 mailbox.
Only 1 reader is allowed.

Definition at line 29 of file rlmailbox.h.


Member Enumeration Documentation

Enumerator:
MAILBOX_ERROR 

return codes for write()

MAILBOX_FULL 
WAIT 

wait parameter for read()

NOWAIT 
MAX_MAILBOX 

maximum mailbox length

OK 

status:

COULD_NOT_CREATE_MAILBOX 
COULD_NOT_GET_KEY 
COULD_NOT_GET_CHAN_ID 

Definition at line 32 of file rlmailbox.h.


Constructor & Destructor Documentation

rlMailbox::rlMailbox ( const char *  name)
  construct a named mailbox
  

Definition at line 55 of file rlmailbox.cpp.

{
#ifdef RLUNIX
FILE  *fp;
key_t key;

  status = OK;
  name = new char[strlen(mbxname)+1];
  strcpy(name,mbxname);
  // create file
  fp = fopen(name,"r");
  if(fp == NULL)
  {
    fp = fopen(name,"w");
    if(fp == NULL)
    {
      int ret;
      char buf[1024];
      sprintf(buf,"could not write mailbox=%s\n",mbxname);
      ret = ::write(1,buf,strlen(buf));
      if(ret < 0) exit(-1);
      sprintf(buf,"you have to run this program as root !!!\n");
      ret = ::write(1,buf,strlen(buf));
      if(ret < 0) exit(-1);
      exit(-1);
    }
    fchmod(fileno(fp),0x0fff);
    if(fp == NULL) { status=COULD_NOT_CREATE_MAILBOX; return; }
  }
  fclose(fp);
  // getkey

  // old stuff, without suggestions from Stefan Lievens 
  // key = ftok(name, 'R');

  key = ftok(name, 'b');
  if(key == ((key_t) (-1))) { status=COULD_NOT_GET_KEY; return; }
  // get chanid

  // old stuff, without suggestions from Stefan Lievens 
  // chanid = msgget(key,IPC_CREAT);
  chanid = msgget(key, 0600 | IPC_CREAT);
  if(chanid == -1) { status=COULD_NOT_GET_CHAN_ID; return; }
#endif

#ifdef __VMS
  long   ret;
  short  chan_id;
  struct dsc$descriptor_s dmbxname;

  chanid = -1;
  status = OK;
  name = new char[strlen(mbxname)+1];
  strcpy(name,mbxname);
  dmbxname.dsc$w_length  = strlen(name);
  dmbxname.dsc$a_pointer = name;
  dmbxname.dsc$b_dtype   = DSC$K_DTYPE_T;
  dmbxname.dsc$b_class   = DSC$K_CLASS_S;
  ret = sys$crembx((char)1,                     // 0 temp 1 permanence flag
                   &chan_id,                    // i/o channel
                   MAX_MAILBOX,                 // max msg length
                   MAX_MAILBOX*4,               // max buffer size
                   (long)0,
                   (long)0,
                   &dmbxname);                   // mailbox name
  if(ret == SS$_NORMAL) { chanid = chan_id; return; }
  else                  { status = COULD_NOT_CREATE_MAILBOX; return; }
#endif

#ifdef RLWIN32
  chanid = -1;
  status = OK;
  name = new char[strlen(mbxname)+1];
  strcpy(name,mbxname);
#endif
}
rlMailbox::~rlMailbox ( ) [virtual]
  destruct the mailbox
  

Definition at line 132 of file rlmailbox.cpp.

{
  delete [] name;
  if(chanid < 0) return;

#ifdef __VMS
  sys$dassgn((short)chanid);
#endif

#ifdef RLWIN32
  CloseHandle((HANDLE) chanid);
#endif
}

Member Function Documentation

void rlMailbox::clear ( )
  read all messages from mailbox, clear them
  

Definition at line 334 of file rlmailbox.cpp.

{
  char *buf = new char[MAX_MAILBOX];
  while(read(buf,MAX_MAILBOX,NOWAIT) > 0);
  delete [] buf;
}
int rlMailbox::printf ( const char *  format,
  ... 
)
  similar to printf
  return: bytes written
          MAILBOX_ERROR
          MAILBOX_FULL
  

Definition at line 341 of file rlmailbox.cpp.

{
  int ret;
  char message[rl_PRINTF_LENGTH]; // should be big enough

  va_list ap;
  va_start(ap,format);
  ret = rlvsnprintf(message, rl_PRINTF_LENGTH - 1, format, ap);
  va_end(ap);
  if(ret < 0) return ret;
  return write(message,strlen(message));
}
int rlMailbox::read ( void *  buf,
int  maxlen,
int  wait = WAIT 
)
  read buf from mailbox, maxlen=Maximal length of buf
  return: number of bytes read
  wait = 0 no wait
  wait = 1 wait for message
  

Definition at line 221 of file rlmailbox.cpp.

{
  char *cbuf;
  status = OK;
  cbuf = (char *) buf;

#ifdef RLUNIX
  int len;
  unsigned char *message = new unsigned char [sizeof(long) + maxlen];
  if(wait == WAIT ) len = msgrcv(chanid,(struct msgbuf *) message, maxlen,0,0);
  else              len = msgrcv(chanid,(struct msgbuf *) message, maxlen,0,IPC_NOWAIT);
  if(len < maxlen && len >= 0) 
  {
    memcpy(buf,&message[sizeof(long)],len);
    cbuf[len] = '\0';
  }
  else
  {
    cbuf[0] = '\0';
  }
  delete [] message;
  return len;
#endif

#ifdef __VMS
  int  ret,len;
  IOSB iosb;
  if(wait == NOWAIT)
  {
    ret = sys$qiow(0,
                   (short) chanid,
                   IO$_READVBLK | IO$M_NOW,       // I/O CODE
                   &iosb,
                   0,0,
                   buf,
                   maxlen,0,0,0,0);
  }
  else
  {
    ret = sys$qiow(0,
                   (short) chanid,
                   IO$_READVBLK,                  // I/O CODE
                   &iosb,
                   0,0,
                   buf,
                   maxlen,0,0,0,0);
  }
  len = (int) iosb.msg_len;
  if(len < maxlen && len >= 0) cbuf[len] = '\0';
  if     (ret == SS$_NORMAL && iosb.iostat == SS$_NORMAL)   return len;
  else if(iosb.iostat == SS$_NORMAL)                        { status = -1; return MAILBOX_ERROR; }
  else if(ret         == SS$_NORMAL)
  {
    if(wait == NOWAIT && iosb.iostat == SS$_ENDOFFILE)      { status = -2; return MAILBOX_ERROR; }
    else                                                    { status = -3; return MAILBOX_ERROR; }
  }
                                                              status = -4; return MAILBOX_ERROR;
#endif

#ifdef RLWIN32
  HANDLE h;
  char   mbxname[1024];
  unsigned long lenRead;
  BOOL   bret,bret2;

  if(chanid == -1)
  {
    strcpy(mbxname,"\\\\.\\mailslot\\"); strcat(mbxname,name);
    h = CreateMailslot(
                 mbxname,                // pointer to string for mailslot name
                 MAX_MAILBOX,            // maximum message size
                 MAILSLOT_WAIT_FOREVER,  // milliseconds before read time-out
                 NULL);                  // pointer to security structure
    if(h == INVALID_HANDLE_VALUE) { status = GetLastError(); return MAILBOX_ERROR; }
    chanid = (int) h;

    bret2 = SetMailslotInfo((HANDLE) chanid, MAILSLOT_WAIT_FOREVER);
    if(bret2 == 0) { status = GetLastError();  return MAILBOX_ERROR; }
  }

  if(wait == NOWAIT) // begin wait
  {
    lenRead = 0;
    bret2 = SetMailslotInfo((HANDLE) chanid, 0);
    if(bret2 == 0) { status = GetLastError(); return MAILBOX_ERROR; }
    bret = ReadFile(
             (HANDLE) chanid,                        // handle of file to read
             buf,                                    // pointer to buffer
             maxlen,                                 // number of bytes to read
             &lenRead,                               // pointer to number of bytes read
             NULL                                    // pointer to structure for data
                   );
    bret2 = SetMailslotInfo((HANDLE) chanid, MAILSLOT_WAIT_FOREVER);
    if(bret2 == 0) { status = GetLastError(); return MAILBOX_ERROR; }
    if(bret == 0)  { status = GetLastError(); return MAILBOX_ERROR; }
    if((int) lenRead < maxlen && (int) lenRead >= 0) cbuf[lenRead] = '\0';
    return lenRead;
  } // end wait

  lenRead = 0;
  bret = ReadFile(
           (HANDLE) chanid,                        // handle of file to read
           buf,                                    // pointer to buffer
           maxlen,                                 // number of bytes to read
           &lenRead,                               // pointer to number of bytes read
           NULL                                    // pointer to structure for data
                 );
  if(bret == 0) { status = GetLastError(); return MAILBOX_ERROR; }
  if((int) lenRead < maxlen && (int) lenRead >= 0) cbuf[lenRead] = '\0';
  return lenRead;
#endif
}
int rlMailbox::write ( const void *  buf,
int  len 
)
  write buf with length len to mailbox
  return: bytes written
          MAILBOX_ERROR
          MAILBOX_FULL
  

Definition at line 146 of file rlmailbox.cpp.

{
  status = OK;

#ifdef RLUNIX
  int retlen;
  unsigned char *message = new unsigned char [sizeof(long) + len];
  long *lptr = (long *) message;
  *lptr = 1; // mtype
  memcpy(&message[sizeof(long)],buf,len);
  retlen = msgsnd(chanid,(struct msgbuf *) message, len, 0);
  delete [] message;
  return retlen;
#endif

#ifdef __VMS
  int  ret;
  IOSB iosb;
  ret = sys$qiow (0,
                  (short) chanid,
                  IO$_WRITEVBLK | IO$M_NOW | IO$M_NORSWAIT,
                  &iosb,
                  0,0,
                  buf,
                  len,0,0,0,0);
  len = iosb.msg_len;
  if     (ret == SS$_MBFULL) return MAILBOX_FULL;
  else if(ret != SS$_NORMAL) return MAILBOX_ERROR;
  else                       return len; // Success
#endif

#ifdef RLWIN32
  BOOL bret;
  unsigned long numWritten;

  if(chanid == -1)
  {
    HANDLE h;
    char   mbxname[1024];
    strcpy(mbxname,"\\\\.\\mailslot\\"); strcat(mbxname,name);
    h = CreateFile(
              mbxname,                             // pointer to name of the file
              GENERIC_READ | GENERIC_WRITE,        // access (read-write) mode
              FILE_SHARE_READ | FILE_SHARE_WRITE,  // share mode
              NULL,                                // pointer to security attributes
              OPEN_EXISTING,                       // how to create
              FILE_ATTRIBUTE_NORMAL,               // file attributes
              NULL                                 // handle to file with attributes to copy
                  );
    if(h == INVALID_HANDLE_VALUE)
    {
      status = GetLastError();
      return MAILBOX_ERROR;
    }
    chanid = (int) h;
  }

  bret = WriteFile(
            (HANDLE) chanid,      // handle to file to write to
            buf,                  // pointer to data to write to file
            len,                  // number of bytes to write
            &numWritten,          // pointer to number of bytes written
            NULL                  // pointer to structure for overlapped I/O
                  );
  if(bret==0)
  {
    status = GetLastError();
    CloseHandle((HANDLE) chanid);
    chanid = -1;
    return MAILBOX_ERROR;
  }
  return numWritten;
#endif
}

Member Data Documentation

int rlMailbox::chanid [private]

Definition at line 101 of file rlmailbox.h.

  Name of mailbox
  

Definition at line 98 of file rlmailbox.h.

  should be: OK
  can be:    COULD_NOT_CREATE_MAILBOX
             COULD_NOT_GET_CHAN_ID
  

Definition at line 93 of file rlmailbox.h.


The documentation for this class was generated from the following files:
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines