Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f7a0777aaf0d3dddf89e99684d239c70eb17c3e0 (plain) (blame)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*******************************************************************************
 * Copyright (c) 2014 Wind River Systems, Inc. and others. All rights reserved.
 * This program and the accompanying materials are made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.core;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;

import org.eclipse.tcf.protocol.IPeer;
import org.eclipse.tcf.protocol.Protocol;

/**
 * ChannelTCP is a IChannel implementation that works on top of TCP sockets as a transport.
 */
public class ChannelTCP extends StreamChannel {

    private Socket socket;
    private int timeout;
    private InputStream inp;
    private OutputStream out;
    private boolean started;
    private boolean closed;

    private static SSLContext ssl_context;

    public static void setSSLContext(SSLContext ssl_context) {
        ChannelTCP.ssl_context = ssl_context;
    }

    public ChannelTCP(IPeer remote_peer, final String host, final int port, final boolean ssl) {
        super(remote_peer);
        socket = new Socket();
        Protocol.invokeLater(new Runnable() {
            public void run() {
                Thread thread = new Thread() {
                    public void run() {
                        try {
                            socket.connect(new InetSocketAddress(host, port), timeout);
                            socket.setTcpNoDelay(true);
                            socket.setKeepAlive(true);
                            if (ssl) {
                                if (ssl_context == null) throw new Exception("SSL context is not set");
                                socket = ssl_context.getSocketFactory().createSocket(socket, host, port, true);
                                ((SSLSocket)socket).startHandshake();
                            }
                            inp = new BufferedInputStream(socket.getInputStream());
                            out = new BufferedOutputStream(socket.getOutputStream());
                            onSocketConnected(null);
                        }
                        catch (Exception x) {
                            onSocketConnected(x);
                        }
                    }
                };
                thread.setName("TCF Socket Connect");
                thread.start();
            }
        });
    }

    public ChannelTCP(IPeer remote_peer, String host, int port) {
        this(remote_peer, host, port, false);
    }

    public ChannelTCP(IPeer local_peer, IPeer remote_peer, Socket socket) throws IOException {
        super(local_peer, remote_peer);
        this.socket = socket;
        socket.setTcpNoDelay(true);
        socket.setKeepAlive(true);
        inp = new BufferedInputStream(socket.getInputStream());
        out = new BufferedOutputStream(socket.getOutputStream());
        onSocketConnected(null);
    }

    public void setConnectTimeout(int timeout) {
        this.timeout = timeout;
    }

    public void setReuseAddress(boolean on) throws SocketException {
        socket.setReuseAddress(on);
    }

    public void setReceiveBufferSize(int size) throws SocketException{
        socket.setReceiveBufferSize(size);
    }

    public void setSendBufferSize(int size) throws SocketException{
        socket.setSendBufferSize(size);
    }

    public void setSoLinger(boolean on, int linger) throws SocketException {
        socket.setSoLinger(on, linger);
    }

    public void setTrafficClass(int tc) throws SocketException {
        socket.setTrafficClass(tc);
    }

    public void setPerformancePreferences(int connection_time, int latency, int bandwidth) {
        socket.setPerformancePreferences(connection_time, latency, bandwidth);
    }

    private void onSocketConnected(final Throwable x) {
        Protocol.invokeLater(new Runnable() {
            public void run() {
                if (x != null) {
                    terminate(x);
                    closed = true;
                }
                if (closed) {
                    try {
                        if (socket != null) {
                            socket.close();
                            if (out != null) out.close();
                            if (inp != null) inp.close();
                        }
                    }
                    catch (IOException y) {
                        Protocol.log("Cannot close socket", y);
                    }
                }
                else {
                    started = true;
                    start();
                }
            }
        });
    }

    @Override
    protected final int get() throws IOException {
        try {
            if (closed) return -1;
            return inp.read();
        }
        catch (IOException x) {
            if (closed) return -1;
            throw x;
        }
    }

    @Override
    protected final int get(byte[] buf) throws IOException {
        try {
            if (closed) return -1;
            return inp.read(buf);
        }
        catch (IOException x) {
            if (closed) return -1;
            throw x;
        }
    }

    @Override
    protected final void put(int b) throws IOException {
        assert b >= 0 && b <= 0xff;
        if (closed) return;
        out.write(b);
    }

    @Override
    protected final void put(byte[] buf) throws IOException {
        if (closed) return;
        out.write(buf);
    }

    @Override
    protected final void flush() throws IOException {
        if (closed) return;
        out.flush();
    }

    @Override
    protected void stop() throws IOException {
        closed = true;
        if (started) {
            socket.close();
            out.close();
            inp.close();
        }
    }
}

Back to the top