summaryrefslogtreecommitdiffstats
path: root/libjava/gnu/java/nio/natSelectorImplWin32.cc
blob: c82c2aa6d0ec41e64d6ebba90ad926362c3c3f10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// natSelectorImplWin32.cc

/* Copyright (C) 2003  Free Software Foundation

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

#include <config.h>
#include <platform.h>

#include <errno.h>
#include <string.h>

#include <gnu/java/nio/SelectorImpl.h>
#include <java/io/IOException.h>

void
helper_put_filedescriptors (jintArray fdArray, fd_set& fds, int& max_fd)
{
  jint* tmpFDArray = elements (fdArray);

  for (int index = 0; index < JvGetArrayLength (fdArray); index++)
    {
      FD_SET (tmpFDArray [index], &fds);

      if (tmpFDArray [index] > max_fd)
        max_fd = tmpFDArray [index];
    }
}

void
helper_get_filedescriptors (jintArray& fdArray, fd_set fds)
{
  jint* tmpFDArray = elements (fdArray);
  
  for (int index = 0; index < JvGetArrayLength (fdArray); index++)
    if (!FD_ISSET (tmpFDArray [index], &fds))
      tmpFDArray [index] = 0;
}

jint
gnu::java::nio::SelectorImpl::implSelect (jintArray read, jintArray write,
                                          jintArray except, jlong timeout)
{
  jint result;
  int max_fd = 0;
  fd_set read_fds;
  fd_set write_fds;
  fd_set except_fds;
  struct timeval real_time_data;
  struct timeval *time_data = NULL;

  real_time_data.tv_sec = 0;
  real_time_data.tv_usec = timeout;

  // If not legal timeout value is given, use NULL.
  // This means an infinite timeout.
  if (timeout >= 0)
    {
      time_data = &real_time_data;
    }

  // Reset all fd_set structures
  FD_ZERO (&read_fds);
  FD_ZERO (&write_fds);
  FD_ZERO (&except_fds);

  // Fill the fd_set data structures for the _Jv_select() call.
  helper_put_filedescriptors (read, read_fds, max_fd);
  helper_put_filedescriptors (write, write_fds, max_fd);
  helper_put_filedescriptors (except, except_fds, max_fd);

  // Actually do the select
  result = _Jv_select (max_fd + 1, &read_fds, &write_fds, &except_fds, time_data);

  if (result < 0)
    {
      char* strerr = strerror (errno);
      throw new ::java::io::IOException (JvNewStringUTF (strerr));
    }

  // Set the file descriptors according to the values returned from select().
  helper_get_filedescriptors (read, read_fds);
  helper_get_filedescriptors (write, write_fds);
  helper_get_filedescriptors (except, except_fds);

  return result;
}
OpenPOWER on IntegriCloud