Example #1
0
bool AbstractSocket::waitUntilReady(bool read, int64_t timeout)
{
   Exception* e = NULL;

   // wait for readability/writability
   int error = SocketTools::poll(read, mFileDescriptor, timeout);
   if(error < 0)
   {
      if(errno == EINTR)
      {
         const char* msg = read ?
            "Socket read interrupted." :
            "Socket write interrupted.";

         // interrupted exception
         e = new Exception(msg, "monarch.io.InterruptedException");
         e->getDetails()["error"] = strerror(errno);
      }
      else
      {
         const char* msg = read ?
            "Could not read from socket." :
            "Could not write to socket.";

         // error occurred, get string message
         e = new Exception(msg, SOCKET_EXCEPTION_TYPE);
         e->getDetails()["error"] = strerror(errno);

         // clear file descriptor and close if its bad
         if(errno == EBADF)
         {
            mFileDescriptor = -1;
            close();
         }
      }
   }
   else if(error == 0)
   {
      const char* msg = read ?
         "Socket read timed out." :
         "Socket write timed out.";

      // timeout occurred, get string message
      e = new Exception(msg, SOCKET_TIMEOUT_EXCEPTION_TYPE);
      e->getDetails()["error"] = strerror(errno);
   }

   if(e != NULL)
   {
      ExceptionRef ref = e;
      Exception::set(ref);
   }

   return e == NULL;
}
Example #2
0
Exception* Thread::createInterruptedException()
{
   Exception* rval = NULL;

   rval = new Exception("Thread interrupted", "monarch.rt.Interrupted");
   const char* name = getName();
   rval->getDetails()["name"] = ((name == NULL) ? "" : name);

   return rval;
}
Example #3
0
bool AbstractSocket::waitUntilReady(bool read, int64_t timeout)
{
   Exception* e = NULL;

   // wait for readability/writability
   int error = SocketTools::poll(read, mFileDescriptor, timeout);
   if(error < 0)
   {
      if(errno == EINTR)
      {
         if(read)
         {
            // interrupted exception
            e = new Exception(
               "Socket read interrupted.", "monarch.io.InterruptedException");
            e->getDetails()["error"] = strerror(errno);
         }
         else
         {
            // interrupted exception
            e = new Exception(
               "Socket write interrupted.", "monarch.io.InterruptedException");
            e->getDetails()["error"] = strerror(errno);
         }
      }
      else
      {
         if(read)
         {
            // error occurred, get string message
            e = new Exception(
               "Could not read from socket.", SOCKET_EXCEPTION_TYPE);
            e->getDetails()["error"] = strerror(errno);
         }
         else
         {
            // error occurred, get string message
            e = new Exception(
               "Could not write to socket.", SOCKET_EXCEPTION_TYPE);
            e->getDetails()["error"] = strerror(errno);
         }

         // clear file descriptor and close if its bad
         if(errno == EBADF)
         {
            mFileDescriptor = -1;
            close();
         }
      }
   }
   else if(error == 0)
   {
      if(read)
      {
         // read timeout occurred
         e = new Exception(
            "Socket read timed out.", SOCKET_TIMEOUT_EXCEPTION_TYPE);
         e->getDetails()["error"] = strerror(errno);
      }
      else
      {
         // write timeout occurred
         e = new Exception(
            "Socket write timed out.", SOCKET_TIMEOUT_EXCEPTION_TYPE);
         e->getDetails()["error"] = strerror(errno);
      }
   }
   else
   {
      // get the last error on the socket
      int lastError;
      socklen_t lastErrorLength = sizeof(lastError);
      getsockopt(
         mFileDescriptor, SOL_SOCKET, SO_ERROR,
         (char*)&lastError, &lastErrorLength);
      if(lastError != 0 && lastError != EINPROGRESS)
      {
         if(read)
         {
            // error occurred, get string message
            e = new Exception(
               "Could not read from socket.", SOCKET_EXCEPTION_TYPE);
            e->getDetails()["error"] = strerror(lastError);
         }
         else
         {
            // error occurred, get string message
            e = new Exception(
               "Could not write to socket.", SOCKET_EXCEPTION_TYPE);
            e->getDetails()["error"] = strerror(lastError);
         }
      }
   }

   if(e != NULL)
   {
      ExceptionRef ref = e;
      Exception::set(ref);
   }

   return e == NULL;
}