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
10 -- http://www.apache.org/licenses/LICENSE-2.0
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
23 TFramedTransport = TTransportBase:new{
24 __type = 'TFramedTransport',
31 function TFramedTransport:new(obj)
32 if ttype(obj) ~= 'table' then
33 error(ttype(self) .. 'must be initialized with a table')
36 -- Ensure a transport is provided
38 error('You must provide ' .. ttype(self) .. ' with a trans')
41 return TTransportBase.new(self, obj)
44 function TFramedTransport:isOpen()
45 return self.trans:isOpen()
48 function TFramedTransport:open()
49 return self.trans:open()
52 function TFramedTransport:close()
53 return self.trans:close()
56 function TFramedTransport:read(len)
57 if string.len(self.rBuf) == 0 then
61 if self.doRead == false then
62 return self.trans:read(len)
65 if len > string.len(self.rBuf) then
71 local val = string.sub(self.rBuf, 0, len)
72 self.rBuf = string.sub(self.rBuf, len+1)
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)
83 function TFramedTransport:write(buf, len)
84 if self.doWrite == false then
85 return self.trans:write(buf, len)
88 if len and len < string.len(buf) then
89 buf = string.sub(buf, 0, len)
91 self.wBuf = self.wBuf .. buf
94 function TFramedTransport:flush()
95 if self.doWrite == false then
96 return self.trans:flush()
99 -- If the write fails we still want wBuf to be clear
100 local tmp = self.wBuf
102 local frame_len_buf = libluabpack.bpack("i", string.len(tmp))
103 tmp = frame_len_buf .. tmp
104 self.trans:write(tmp)
108 TFramedTransportFactory = TTransportFactoryBase:new{
109 __type = 'TFramedTransportFactory'
111 function TFramedTransportFactory:getTransport(trans)
113 terror(TProtocolException:new{
114 message = 'Must supply a transport to ' .. ttype(self)
117 return TFramedTransport:new{trans = trans}