Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ffadca8af887094e8162588dcb45f0f876bde4d6 (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
# *****************************************************************************
# * Copyright (c) 2011, 2013 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
# *****************************************************************************

import types
from .AbstractChannel import AbstractChannel, EOS, EOM

ESC = 3


class StreamChannel(AbstractChannel):
    """Abstract channel implementation 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.
    """

    def __init__(self, remote_peer, local_peer=None):
        super(StreamChannel, self).__init__(remote_peer, local_peer=local_peer)
        self.bin_data_size = 0
        self.buf = bytearray(0x1000)
        self.buf_pos = 0
        self.buf_len = 0

    def get(self):
        pass

    def put(self, n):
        pass

    def getBuf(self, buf):
        i = 0
        l = len(buf)
        while i < l:
            b = self.get()
            if b < 0:
                if i == 0:
                    return -1
                break
            buf[i] = b
            i += 1
            if i >= self.bin_data_size:
                break
        return i

    def putBuf(self, buf):
        for b in buf:
            self.put(b & 0xff)

    def read(self):
        while True:
            while self.buf_pos >= self.buf_len:
                self.buf_len = self.getBuf(self.buf)
                self.buf_pos = 0
                if self.buf_len < 0:
                    return EOS
            res = self.buf[self.buf_pos] & 0xff
            self.buf_pos += 1
            if self.bin_data_size > 0:
                self.bin_data_size -= 1
                return res
            if res != ESC:
                return res
            while self.buf_pos >= self.buf_len:
                self.buf_len = self.getBuf(self.buf)
                self.buf_pos = 0
                if self.buf_len < 0:
                    return EOS
            n = self.buf[self.buf_pos] & 0xff
            self.buf_pos += 1
            if n == 0:
                return ESC
            elif n == 1:
                return EOM
            elif n == 2:
                return EOS
            elif n == 3:
                for i in xrange(0, 100000, 7):
                    while self.buf_pos >= self.buf_len:
                        self.buf_len = self.getBuf(self.buf)
                        self.buf_pos = 0
                        if self.buf_len < 0:
                            return EOS
                    m = self.buf[self.buf_pos] & 0xff
                    self.buf_pos += 1
                    self.bin_data_size |= (m & 0x7f) << i
                    if (m & 0x80) == 0:
                        break
            else:
                if n < 0:
                    return EOS
                assert False

    def writeByte(self, n):
        if n == ESC:
            self.put(ESC)
            self.put(0)
        elif n == EOM:
            self.put(ESC)
            self.put(1)
        elif n == EOS:
            self.put(ESC)
            self.put(2)
        else:
            assert n >= 0 and n <= 0xff
            self.put(n)

    def write(self, buf):
        t = type(buf)
        if t == types.IntType:
            self.writeByte(buf)
            return
        elif t == types.StringType:
            buf = bytearray(buf)
        if len(buf) > 32 and self.isZeroCopySupported():
            self.put(ESC)
            self.put(3)
            n = len(buf)
            while True:
                if n <= 0x7f:
                    self.put(n)
                    break
                self.put((n & 0x7f) | 0x80)
                n >>= 7
            self.putBuf(buf)
        else:
            for b in buf:
                n = b & 0xff
                self.put(n)
                if n == ESC:
                    self.put(0)

Back to the top