Exemple #1
0
  /** @inheritDoc */
  public int writeBuf(int fd, byte buffer[], int off, int len) throws IOException {
    int result = 0;
    byte[] buf = buffer;
    if (off != 0) {
      buf = new byte[len];
      System.arraycopy(buffer, off, buf, 0, len);
    }

    if (DEBUG) {
      System.err.println("writeBuf(" + fd + ") before write.");
    }

    result = libc.write(fd, buf, len); // We rely on open0() for setting the socket to non-blocking

    if (result < 0) {
      int err_code = LibCUtil.errno();
      if (err_code == LibC.EWOULDBLOCK) {
        VMThread.getSystemEvents().waitForWriteEvent(fd);
        if (DEBUG) {
          System.err.println("writeBuf(" + fd + ") returned from select. retry.");
        }
        result =
            libc.write(fd, buf, len); // We rely on open0() for setting the socket to non-blocking
      }
      if (DEBUG) {
        System.err.println("writeBuf(" + fd + ") error:");
      }
      LibCUtil.errCheckNeg(result);
    }

    return result;
  }
Exemple #2
0
  /** @inheritDoc */
  public int readBuf(int fd, byte b[], int offset, int length) throws IOException {
    byte[] buf = b;
    if (offset != 0) {
      buf = new byte[length];
    }
    int result =
        libc.read(fd, buf, length); // We rely on open0() for setting the socket to non-blocking

    if (result < 0) {
      int err_code = LibCUtil.errno();
      if (err_code == LibC.EWOULDBLOCK) {
        VMThread.getSystemEvents().waitForReadEvent(fd);
        result =
            libc.read(fd, buf, length); // We rely on open0() for setting the socket to non-blocking
      }
      LibCUtil.errCheckNeg(result);
    }

    if (result == 0) {
      // If remote side has shut down the connection gracefully, and all
      // data has been received, recv() will complete immediately with
      // zero bytes received.
      //
      // This is true for Win32/CE and Linux
      result = -1;
    }

    return result;
  }
Exemple #3
0
  /**
   * Accept client connections on server socket fd. Blocks until a client connects.
   *
   * @param fd open server socket. See {@link #openServer}.
   * @return a native handle to the network connection.
   * @throws IOException
   */
  public int accept(int fd) throws IOException {
    VMThread.getSystemEvents().waitForReadEvent(fd);

    Socket.sockaddr_in remote_sin = new Socket.sockaddr_in();
    IntByReference address_len = new IntByReference(4);
    int newSocket = sockets.accept(fd, remote_sin, address_len);
    if (newSocket < 0) {
      throw newError(fd, "accept");
    }
    address_len.free();
    set_blocking_flags(newSocket, /*is_blocking*/ false);
    // we could read info about client from remote_sin, but don't need to.

    return newSocket;
  }
Exemple #4
0
  /** @inheritDoc */
  public int open(String hostname, int port, int mode) throws IOException {
    // init_sockets(); win32 only
    int fd = -1;

    fd = sockets.socket(Socket.AF_INET, Socket.SOCK_STREAM, 0);
    if (DEBUG) {
      System.err.println("Socket.socket() = " + fd);
    }
    if (fd < 0) {
      throw newError(fd, "socket create");
    }

    set_blocking_flags(fd, /*is_blocking*/ false);

    NetDB.hostent phostent;
    // hostname is always NUL terminated. See socket/Protocol.java for detail.
    phostent = NetDB.INSTANCE.gethostbyname(hostname);
    if (phostent == null) {
      throw newError(fd, "gethostbyname (herrono = " + NetDB.INSTANCE.h_errno() + ")");
    }

    Socket.sockaddr_in destination_sin = new Socket.sockaddr_in();
    destination_sin.sin_family = Socket.AF_INET;
    destination_sin.sin_port = Inet.htons((short) port);
    destination_sin.sin_addr = phostent.h_addr_list[0];

    if (DEBUG) {
      System.err.println("Socket.sockaddr_in: " + destination_sin);
      System.err.println("connect: hostname: " + hostname + " port: " + port + " mode: " + mode);
    }
    if (sockets.connect(fd, destination_sin, destination_sin.size()) < 0) {
      int err_code = LibCUtil.errno();
      if (err_code == LibC.EINPROGRESS || err_code == LibC.EWOULDBLOCK) {
        // When the socket is ready for connect, it becomes *writable*
        // (according to BSD socket spec of select())
        VMThread.getSystemEvents().waitForWriteEvent(fd);
      } else {
        throw newError(fd, "connect");
      }
    }

    return fd;
  }
Exemple #5
0
  /**
   * Accept client connections on server socket fd. Blocks until a client connects.
   *
   * @param fd open server socket. See {@link #openServer}.
   * @return a native handle to the network connection.
   * @throws IOException
   */
  public int accept(int fd) throws IOException {
    Socket.sockaddr_in remote_sin = new Socket.sockaddr_in();
    IntByReference address_len = new IntByReference(4);
    int newSocket;

    try {
      if (DEBUG) {
        System.err.println("Socket.accept(" + fd + ", " + remote_sin + ")...");
      }

      newSocket = sockets.accept(fd, remote_sin, address_len);

      if (newSocket < 0) {
        if (DEBUG) {
          System.err.println("Need to block for accept...");
        }

        int err_code = LibCUtil.errno();
        if (err_code == LibC.EAGAIN || err_code == LibC.EWOULDBLOCK) {
          VMThread.getSystemEvents().waitForReadEvent(fd);
          newSocket = sockets.accept(fd, remote_sin, address_len);
          if (newSocket < 0) {
            throw newError(fd, "accept");
          }
        } else {
          // BUG!
          throw newError(fd, "accept");
        }
      }
    } finally {
      address_len.free();
    }

    set_blocking_flags(newSocket, /*is_blocking*/ false);
    // we could read info about client from remote_sin, but don't need to.

    if (DEBUG) {
      System.err.println("    Socket.accept(...) = " + newSocket);
    }
    return newSocket;
  }
Exemple #6
0
  /** @inheritDoc */
  public int readBuf(int fd, byte b[], int offset, int length) throws IOException {
    byte[] buf = b;
    int result;

    if (offset != 0) {
      if (DEBUG) {
        System.err.println("readBuf() into temp buf");
      }
      buf = new byte[length];
    }

    if (NBIO_WORKS) {
      result =
          libc.read(fd, buf, length); // We rely on open0() for setting the socket to non-blocking
      if (result < 0) {
        int err_code = LibCUtil.errno();
        if (err_code == LibC.EWOULDBLOCK) {
          if (DEBUG) {
            System.err.println("Wait for read in select...");
          }
          VMThread.getSystemEvents().waitForReadEvent(fd);
          result =
              libc.read(
                  fd, buf, length); // We rely on open0() for setting the socket to non-blocking
        }
        LibCUtil.errCheckNeg(result);
      }
    } else {
      // If non-blocking IO doesn't seems to be working, try this hack...

      int bAvail = available(fd); // may throw IOException

      if (bAvail == 0) {
        if (DEBUG) {
          System.err.println("Wait for read in select...");
        }
        VMThread.getSystemEvents().waitForReadEvent(fd);
        bAvail = available(fd);
        if (bAvail == 0) { // woke up because connection is closed
          if (DEBUG) {
            System.err.println("readBuf(" + fd + ") signalling EOF.");
          }
          return -1; // signal EOF
        }
      }
      if (DEBUG) {
        System.err.println("readBuf(" + fd + ") returned from select. retry.");
      }

      int n = Math.min(bAvail, length); // don't read more than is asked for...
      result = libc.read(fd, buf, n); // only read what we know is there...
      LibCUtil.errCheckNeg(result);
    }

    if (result == 0) {
      // If remote side has shut down the connection gracefully, and all
      // data has been received, recv() will complete immediately with
      // zero bytes received.
      //
      // This is true for Win32/CE and Linux
      if (DEBUG) {
        System.err.println("readBuf(" + fd + ") saw remote side shutdown.");
      }
      result = -1;
    }

    if (offset != 0 && result > 0) {
      System.arraycopy(buf, 0, b, offset, result);
    }
    if (DEBUG) {
      System.out.println("readBuf(" + fd + ") = " + result);
    }

    return result;
  }