]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/lua/TFramedTransport.lua
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / lua / TFramedTransport.lua
1 --
2 -- Licensed to the Apache Software Foundation (ASF) under one
3 -- or more contributor license agreements. See the NOTICE file
4 -- distributed with this work for additional information
5 -- regarding copyright ownership. The ASF licenses this file
6 -- to you under the Apache License, Version 2.0 (the
7 -- "License"); you may not use this file except in compliance
8 -- with the License. You may obtain a copy of the License at
9 --
10 -- http://www.apache.org/licenses/LICENSE-2.0
11 --
12 -- Unless required by applicable law or agreed to in writing,
13 -- software distributed under the License is distributed on an
14 -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 -- KIND, either express or implied. See the License for the
16 -- specific language governing permissions and limitations
17 -- under the License.
18 --
19
20 require 'TTransport'
21 require 'libluabpack'
22
23 TFramedTransport = TTransportBase:new{
24 __type = 'TFramedTransport',
25 doRead = true,
26 doWrite = true,
27 wBuf = '',
28 rBuf = ''
29 }
30
31 function TFramedTransport:new(obj)
32 if ttype(obj) ~= 'table' then
33 error(ttype(self) .. 'must be initialized with a table')
34 end
35
36 -- Ensure a transport is provided
37 if not obj.trans then
38 error('You must provide ' .. ttype(self) .. ' with a trans')
39 end
40
41 return TTransportBase.new(self, obj)
42 end
43
44 function TFramedTransport:isOpen()
45 return self.trans:isOpen()
46 end
47
48 function TFramedTransport:open()
49 return self.trans:open()
50 end
51
52 function TFramedTransport:close()
53 return self.trans:close()
54 end
55
56 function TFramedTransport:read(len)
57 if string.len(self.rBuf) == 0 then
58 self:__readFrame()
59 end
60
61 if self.doRead == false then
62 return self.trans:read(len)
63 end
64
65 if len > string.len(self.rBuf) then
66 local val = self.rBuf
67 self.rBuf = ''
68 return val
69 end
70
71 local val = string.sub(self.rBuf, 0, len)
72 self.rBuf = string.sub(self.rBuf, len+1)
73 return val
74 end
75
76 function TFramedTransport:__readFrame()
77 local buf = self.trans:readAll(4)
78 local frame_len = libluabpack.bunpack('i', buf)
79 self.rBuf = self.trans:readAll(frame_len)
80 end
81
82
83 function TFramedTransport:write(buf, len)
84 if self.doWrite == false then
85 return self.trans:write(buf, len)
86 end
87
88 if len and len < string.len(buf) then
89 buf = string.sub(buf, 0, len)
90 end
91 self.wBuf = self.wBuf .. buf
92 end
93
94 function TFramedTransport:flush()
95 if self.doWrite == false then
96 return self.trans:flush()
97 end
98
99 -- If the write fails we still want wBuf to be clear
100 local tmp = self.wBuf
101 self.wBuf = ''
102 local frame_len_buf = libluabpack.bpack("i", string.len(tmp))
103 tmp = frame_len_buf .. tmp
104 self.trans:write(tmp)
105 self.trans:flush()
106 end
107
108 TFramedTransportFactory = TTransportFactoryBase:new{
109 __type = 'TFramedTransportFactory'
110 }
111 function TFramedTransportFactory:getTransport(trans)
112 if not trans then
113 terror(TProtocolException:new{
114 message = 'Must supply a transport to ' .. ttype(self)
115 })
116 end
117 return TFramedTransport:new{trans = trans}
118 end