vpr::Uint32 SocketImplBSD::readn_i(void* buffer, const vpr::Uint32 length,
                                   const vpr::Interval& timeout)
{
   vprASSERT(NULL != mHandle &&
             "Can not read from a socket with a NULL handle.");

   mBlockingFixed = true;
   const vpr::Uint32 bytes = mHandle->readn_i(buffer, length, timeout);

   // XXX: Should never happen.
   if ( 0 == bytes )
   {
      throw ConnectionResetException("Socket closed.", VPR_LOCATION);
   }

   return bytes;
}
vpr::Uint32 SocketImplBSD::read_i(void* buffer, const vpr::Uint32 length,
                                  const vpr::Interval& timeout)
{
   vprASSERT(NULL != mHandle &&
             "Can not read from a socket with a NULL handle.");

   mBlockingFixed = true;
   const vpr::Uint32 bytes = mHandle->read_i(buffer, length, timeout);

   // If read returns 0, then other side shut down cleanly.
   // Note: bytes is also 0 when non-blocking and can't get data but in that
   //       case we throw WouldBlockException.
   if ( 0 == bytes )
   {
      throw ConnectionResetException("Socket disconnected cleanly.",
                                     VPR_LOCATION);
   }

   return bytes;
}
Exemple #3
0
void SocketImpl::error(int code, const std::string& arg)
{
	switch (code)
	{
	case POCO_ENOERR: return;
	case POCO_ESYSNOTREADY:
		throw NetException("Net subsystem not ready", code);
	case POCO_ENOTINIT:
		throw NetException("Net subsystem not initialized", code);
	case POCO_EINTR:
		throw IOException("Interrupted", code);
	case POCO_EACCES:
		throw IOException("Permission denied", code);
	case POCO_EFAULT:
		throw IOException("Bad address", code);
	case POCO_EINVAL:
		throw InvalidArgumentException(code);
	case POCO_EMFILE:
		throw IOException("Too many open files", code);
	case POCO_EWOULDBLOCK:
		throw IOException("Operation would block", code);
	case POCO_EINPROGRESS:
		throw IOException("Operation now in progress", code);
	case POCO_EALREADY:
		throw IOException("Operation already in progress", code);
	case POCO_ENOTSOCK:
		throw IOException("Socket operation attempted on non-socket", code);
	case POCO_EDESTADDRREQ:
		throw NetException("Destination address required", code);
	case POCO_EMSGSIZE:
		throw NetException("Message too long", code);
	case POCO_EPROTOTYPE:
		throw NetException("Wrong protocol type", code);
	case POCO_ENOPROTOOPT:
		throw NetException("Protocol not available", code);
	case POCO_EPROTONOSUPPORT:
		throw NetException("Protocol not supported", code);
	case POCO_ESOCKTNOSUPPORT:
		throw NetException("Socket type not supported", code);
	case POCO_ENOTSUP:
		throw NetException("Operation not supported", code);
	case POCO_EPFNOSUPPORT:
		throw NetException("Protocol family not supported", code);
	case POCO_EAFNOSUPPORT:
		throw NetException("Address family not supported", code);
	case POCO_EADDRINUSE:
		throw NetException("Address already in use", arg, code);
	case POCO_EADDRNOTAVAIL:
		throw NetException("Cannot assign requested address", arg, code);
	case POCO_ENETDOWN:
		throw NetException("Network is down", code);
	case POCO_ENETUNREACH:
		throw NetException("Network is unreachable", code);
	case POCO_ENETRESET:
		throw NetException("Network dropped connection on reset", code);
	case POCO_ECONNABORTED:
		throw ConnectionAbortedException(code);
	case POCO_ECONNRESET:
		throw ConnectionResetException(code);
	case POCO_ENOBUFS:
		throw IOException("No buffer space available", code);
	case POCO_EISCONN:
		throw NetException("Socket is already connected", code);
	case POCO_ENOTCONN:
		throw NetException("Socket is not connected", code);
	case POCO_ESHUTDOWN:
		throw NetException("Cannot send after socket shutdown", code);
	case POCO_ETIMEDOUT:
		throw TimeoutException(code);
	case POCO_ECONNREFUSED:
		throw ConnectionRefusedException(arg, code);
	case POCO_EHOSTDOWN:
		throw NetException("Host is down", arg, code);
	case POCO_EHOSTUNREACH:
		throw NetException("No route to host", arg, code);
#if defined(POCO_OS_FAMILY_UNIX)
	case EPIPE:
		throw IOException("Broken pipe", code);
	case EBADF:
		throw IOException("Bad socket descriptor", code);
#endif
	default:
		throw IOException(NumberFormatter::format(code), arg, code);
	}
}
vpr::Uint32 SocketDatagramImplBSD::sendto(const void* msg,
                                          const vpr::Uint32 length,
                                          const vpr::InetAddr& to,
                                          const vpr::Interval& timeout)
{
   vpr::Uint32 bytes_sent(0);

   // If not writable within timeout interval throw exception.
   if ( ! mHandle->isWriteable(timeout) )
   {
      std::ostringstream msg_stream;
      msg_stream << "Timeout occured while trying to write to "
                 << mHandle->getName();
      throw TimeoutException(msg_stream.str(), VPR_LOCATION);
   }

   ssize_t bytes;

   mBlockingFixed = true;

   bytes = ::sendto(mHandle->mFdesc, msg, length, 0,
                    reinterpret_cast<const sockaddr*>(&to.mAddr), to.size());

   if ( bytes == -1 )
   {
      if ( errno == EAGAIN && ! isBlocking() )
      {
         throw WouldBlockException("Would block while reading.", VPR_LOCATION);
      }
      else
      {
         std::stringstream ss;
         ss << "[vpr::SocketDatagramImplBSD::sendto()] ERROR: Could not send "
            << " to " << to << " on socket (" << mRemoteAddr << "): "
            << strerror(errno);

         throw SocketException(ss.str(), VPR_LOCATION);
      }
   }

   if ( ECONNRESET == errno)
   {
      throw ConnectionResetException("Connection reset.", VPR_LOCATION);
   }
   else if (EHOSTUNREACH == errno)
   {
      throw NoRouteToHostException("No route to host.", VPR_LOCATION);
   }
   else if (EHOSTDOWN == errno)
   {
      throw UnknownHostException("Unknown host.", VPR_LOCATION);
   }
   else if (ENETDOWN == errno)
   {
      throw IOException("Network is down.", VPR_LOCATION);
   }
   else
   {
      bytes_sent = bytes;
   }

   return bytes_sent;
}