aboutsummaryrefslogtreecommitdiffstats
path: root/java/com/tigervnc/network/TcpListener.java
blob: 45ee8a4dd4473d417a5d55a78d5f408b12d5da6d (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* Copyright (C) 2002-2005 RealVNC Ltd.  All Rights Reserved.
 * Copyright (C) 2012 Brian P. Hinz
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 * USA.
 */

package com.tigervnc.network;

import java.io.IOException;
import java.lang.Exception;
import java.nio.*;
import java.nio.channels.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.Set;
import java.util.Iterator;

public class TcpListener extends SocketListener  {

  public static boolean socketsInitialised = false;

  public TcpListener(String listenaddr, int port, boolean localhostOnly,
			               SocketDescriptor sock, boolean close_) throws Exception {
    closeFd = close_;
    if (sock != null) {
      fd = sock;
      return;
    }

    TcpSocket.initSockets();
    try {
      channel = ServerSocketChannel.open();
      channel.configureBlocking(false);
    } catch(IOException e) {
      throw new Exception("unable to create listening socket: "+e.toString());
    }

    // - Bind it to the desired port
    InetAddress addr = null;

    try {
      if (localhostOnly) {
        addr = InetAddress.getByName(null);
      } else if (listenaddr != null) {
          addr = java.net.InetAddress.getByName(listenaddr);
      } else {
        addr = InetAddress.getByName("0.0.0.0");
      }
    } catch (UnknownHostException e) {
      throw new Exception(e.getMessage());
    }

    try {
      channel.socket().bind(new InetSocketAddress(addr, port));
    } catch (IOException e) {
      throw new Exception("unable to bind listening socket: "+e.toString());
    }

    // - Set it to be a listening socket
    try {
      selector = Selector.open();
      channel.register(selector, SelectionKey.OP_ACCEPT);
    } catch (IOException e) {
      throw new Exception("unable to set socket to listening mode: "+e.toString());
    }
  }

  public TcpListener(String listenaddr, int port) throws Exception {
    this(listenaddr, port, false, null, true);
  }

  protected void finalize() throws Exception {
    if (closeFd)
      try {
        ((SocketDescriptor)getFd()).close();
      } catch (IOException e) {
        throw new Exception(e.getMessage());
      }
  }

  public void shutdown() throws Exception {
    try {
      ((SocketDescriptor)getFd()).shutdown();
    } catch (IOException e) {
      throw new Exception(e.getMessage());
    }
  }

  public TcpSocket accept() {
    SocketChannel new_sock = null;

    // Accept an incoming connection
    try {
      if (selector.select(0) > 0) {
        Set<SelectionKey> keys = selector.selectedKeys();
        Iterator<SelectionKey> iter = keys.iterator();
        while (iter.hasNext()) {
          SelectionKey key = (SelectionKey)iter.next();
          iter.remove();
          if (key.isAcceptable()) {
            new_sock = channel.accept();
            break;
          }
        }
        keys.clear();
        if (new_sock == null)
          return null;
      }
    } catch (IOException e) {
      throw new SocketException("unable to accept new connection: "+e.toString());
    }

    // Disable Nagle's algorithm, to reduce latency
    try {
      new_sock.socket().setTcpNoDelay(true);
    } catch (java.net.SocketException e) {
      throw new SocketException(e.getMessage());
    }

    // Create the socket object & check connection is allowed
    SocketDescriptor fd = null;
    try {
      fd = new SocketDescriptor();
    } catch (java.lang.Exception e) {
      throw new SocketException(e.getMessage());
    }
    fd.setChannel(new_sock);
    TcpSocket s = new TcpSocket(fd);
    return s;
  }

  public int getMyPort() {
    return ((SocketDescriptor)getFd()).socket().getLocalPort();
  }

  private boolean closeFd;
  private ServerSocketChannel channel;
  private Selector selector;

}