title: Correct way to write/read data over a fifo
body: <p>I've created, as an homework, a big project which simulate a mailbox server (only through process on the same computer, so through fifo, it's a homework)</p>
  
  <p>I can't post the project because is big (there are a lot of files), but I can say that sometimes I lost some data or it doesn't preserve it's integrity.</p>
  
  <p>I use these code snippet to transmit data, is it somewhat wrong?Network_IO is the function I'm talking about:</p>
  
  <pre><code>#include "Network.h"
  
  int Network_Open(const char* path,int oflag)
  {
      return open(path,oflag);
  }
  
  ssize_t Network_IO(int fifo,NetworkOpCodes opcode,void* data,size_t dataSize)
  {
      ssize_t retsize = 0;
      ssize_t tmpDataSize = (ssize_t)dataSize;
      errno = 0;
  
      if (tmpDataSize == 0) return 0;
  
      while ((retsize = (opcode == NetworkOpCode_Write? write(fifo,data,tmpDataSize) : read(fifo,data,tmpDataSize))) != tmpDataSize)
      {
          if (errno != EINTR) break;
      }
  
      return retsize;
  }
  
  Boolean Network_Send(int fifo,const void* data,size_t dataSize)
  {
      return ((ssize_t)dataSize) == Network_IO(fifo,NetworkOpCode_Write,(void*)data,dataSize);
  }
  
  Boolean Network_Receive(int fifo,void* data,size_t dataSize)
  {
      return ((ssize_t)dataSize) == Network_IO(fifo,NetworkOpCode_Read,data,dataSize);
  }
  
  Boolean Network_Close(int fifo)
  {
      if (fifo &gt;= 0)
          return close(fifo) == 0;
  }
  </code></pre>
  
  <p><strong>Edit 1:</strong> Code snippet which I'm using to test actually</p>
  
  <pre><code>Boolean Network_IO(int fifo,NetworkOpCodes opcode,void* data,size_t dataSize)
  {
      ssize_t retsize = 0;
      ssize_t tmpDataSize = (ssize_t)dataSize;
      ssize_t sentDataSize = 0;
      errno = 0;
  
      if (tmpDataSize == 0) return True;
  
      while (sentDataSize &lt; tmpDataSize)
      {
          switch(opcode)
          {
              case NetworkOpCode_Write:
                  retsize = write(fifo,data + sentDataSize,tmpDataSize - sentDataSize);
                  break;
              case NetworkOpCode_Read:
                  retsize = read(fifo,data + sentDataSize,tmpDataSize - sentDataSize);
                  break;
          }
          if (retsize &lt; 0)
          {
              if (errno != EINTR) return False;
              else
              {
                  errno = 0;
                  continue;
              }
          }
          sentDataSize += retsize;
      }
  
      if (errno != 0)
          return False;
  
      return sentDataSize == tmpDataSize;
  }
  
  Boolean Network_Send(int fifo,const void* data,size_t dataSize)
  {
      return Network_IO(fifo,NetworkOpCode_Write,(void*)data,dataSize);
  }
  
  Boolean Network_Receive(int fifo,void* data,size_t dataSize)
  {
      return Network_IO(fifo,NetworkOpCode_Read,data,dataSize);
  }
  </code></pre>