Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4ec25f3a04edb6fd0f96cbdf32725ca1a8fdc11e (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
/*******************************************************************************
 * Copyright (c) 2007-2019 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 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.core;

import java.io.IOException;

import org.eclipse.tcf.protocol.IPeer;

/**
 * Abstract implementation of IChannel interface for stream oriented transport protocols.
 *
 * StreamChannel implements communication link connecting two end points (peers).
 * The channel asynchronously transmits messages: commands, results and events.
 *
 * StreamChannel uses escape sequences to represent End-Of-Message and End-Of-Stream markers.
 *
 * Clients can subclass StreamChannel to support particular stream oriented transport (wire) protocol.
 * Also, see ChannelTCP for a concrete IChannel implementation that works on top of TCP sockets as a transport.
 */
public abstract class StreamChannel extends AbstractChannel {

    public static final int ESC = 3;

    private int bin_data_size;

    private final byte[] esc_buf = new byte[0x1000];

    private final byte[] inp_buf = new byte[0x4000];
    private int inp_buf_pos;
    private int inp_buf_len;

    public StreamChannel(IPeer remote_peer) {
        super(remote_peer);
    }

    public StreamChannel(IPeer local_peer, IPeer remote_peer) {
        super(local_peer, remote_peer);
    }

    protected abstract int get() throws IOException;
    protected abstract void put(int n) throws IOException;

    protected int get(byte[] buf) throws IOException {
        /* Default implementation - it is expected to be overridden */
        int i = 0;
        while (i < buf.length) {
            int b = get();
            if (b < 0) {
                if (i == 0) return -1;
                break;
            }
            buf[i++] = (byte)b;
            if (i >= bin_data_size) break;
        }
        return i;
    }

    protected void put(byte[] buf) throws IOException {
        /* Default implementation - it is expected to be overridden */
        for (byte b : buf) put(b & 0xff);
    }

    /**
     * @since 1.3
     */
    protected void put(byte[] buf, int pos, int len) throws IOException {
        /* Default implementation - it is expected to be overridden */
        int end = pos + len;
        while (pos < end) put(buf[pos++] & 0xff);
    }

    @Override
    protected final int read() throws IOException {
        for (;;) {
            while (inp_buf_pos >= inp_buf_len) {
                inp_buf_len = get(inp_buf);
                inp_buf_pos = 0;
                if (inp_buf_len < 0) return EOS;
            }
            int res = inp_buf[inp_buf_pos++] & 0xff;
            if (bin_data_size > 0) {
                bin_data_size--;
                return res;
            }
            if (res != ESC) return res;
            while (inp_buf_pos >= inp_buf_len) {
                inp_buf_len = get(inp_buf);
                inp_buf_pos = 0;
                if (inp_buf_len < 0) return EOS;
            }
            int n = inp_buf[inp_buf_pos++] & 0xff;
            switch (n) {
            case 0: return ESC;
            case 1: return EOM;
            case 2: return EOS;
            case 3:
                for (int i = 0;; i += 7) {
                    while (inp_buf_pos >= inp_buf_len) {
                        inp_buf_len = get(inp_buf);
                        inp_buf_pos = 0;
                        if (inp_buf_len < 0) return EOS;
                    }
                    int m = inp_buf[inp_buf_pos++] & 0xff;
                    bin_data_size |= (m & 0x7f) << i;
                    if ((m & 0x80) == 0) break;
                }
                break;
            default:
                throw new IOException("Invalid escape sequence: " + ESC + " " + n);
            }
        }
    }

    @Override
    protected final void write(int n) throws IOException {
        switch (n) {
        case ESC:
            esc_buf[0] = ESC;
            esc_buf[1] = 0;
            put(esc_buf, 0, 2);
            break;
        case EOM:
            esc_buf[0] = ESC;
            esc_buf[1] = 1;
            put(esc_buf, 0, 2);
            break;
        case EOS:
            esc_buf[0] = ESC;
            esc_buf[1] = 2;
            put(esc_buf, 0, 2);
            break;
        default:
            assert n >= 0 && n <= 0xff;
            put(n);
            break;
        }
    }

    @Override
    protected final void write(byte[] buf) throws IOException {
        write(buf, 0, buf.length);
    }

    @Override
    protected final void write(byte[] buf, int pos, int len) throws IOException {
        if (len > 32 && isZeroCopySupported()) {
            int n = len;
            int esc_buf_pos = 0;
            esc_buf[esc_buf_pos++] = ESC;
            esc_buf[esc_buf_pos++] = 3;
            for (;;) {
                if (n <= 0x7f) {
                    esc_buf[esc_buf_pos++] = (byte)n;
                    break;
                }
                esc_buf[esc_buf_pos++] = (byte)((n & 0x7f) | 0x80);
                n = n >> 7;
            }
            put(esc_buf, 0, esc_buf_pos);
            put(buf, pos, len);
        }
        else {
            int esc_buf_pos = 0;
            int end = pos + len;
            for (int i = pos; i < end; i++) {
                if (esc_buf_pos + 2 > esc_buf.length) {
                    put(esc_buf, 0, esc_buf_pos);
                    esc_buf_pos = 0;
                }
                byte b = buf[i];
                esc_buf[esc_buf_pos++] = b;
                if (b == ESC) esc_buf[esc_buf_pos++] = 0;
            }
            put(esc_buf, 0, esc_buf_pos);
        }
    }
}

Back to the top