Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame^] | 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 | |
| 22 | TBufferedTransport = TTransportBase:new{ |
| 23 | __type = 'TBufferedTransport', |
| 24 | rBufSize = 2048, |
| 25 | wBufSize = 2048, |
| 26 | wBuf = '', |
| 27 | rBuf = '' |
| 28 | } |
| 29 | |
| 30 | function TBufferedTransport:new(obj) |
| 31 | if ttype(obj) ~= 'table' then |
| 32 | error(ttype(self) .. 'must be initialized with a table') |
| 33 | end |
| 34 | |
| 35 | -- Ensure a transport is provided |
| 36 | if not obj.trans then |
| 37 | error('You must provide ' .. ttype(self) .. ' with a trans') |
| 38 | end |
| 39 | |
| 40 | return TTransportBase:new(obj) |
| 41 | end |
| 42 | |
| 43 | function TBufferedTransport:isOpen() |
| 44 | return self.trans:isOpen() |
| 45 | end |
| 46 | |
| 47 | function TBufferedTransport:open() |
| 48 | return self.trans:open() |
| 49 | end |
| 50 | |
| 51 | function TBufferedTransport:close() |
| 52 | return self.trans:close() |
| 53 | end |
| 54 | |
| 55 | function TBufferedTransport:read(len) |
| 56 | return self.trans:read(len) |
| 57 | end |
| 58 | |
| 59 | function TBufferedTransport:readAll(len) |
| 60 | return self.trans:readAll(len) |
| 61 | end |
| 62 | |
| 63 | function TBufferedTransport:write(buf) |
| 64 | self.wBuf = self.wBuf .. buf |
| 65 | if string.len(self.wBuf) >= self.wBufSize then |
| 66 | self.trans:write(self.wBuf) |
| 67 | self.wBuf = '' |
| 68 | end |
| 69 | end |
| 70 | |
| 71 | function TBufferedTransport:flush() |
| 72 | if string.len(self.wBuf) > 0 then |
| 73 | self.trans:write(self.wBuf) |
| 74 | self.wBuf = '' |
| 75 | end |
| 76 | end |
| 77 | |
| 78 | TBufferedTransportFactory = TTransportFactoryBase:new{ |
| 79 | __type = 'TBufferedTransportFactory' |
| 80 | } |
| 81 | |
| 82 | function TBufferedTransportFactory:getTransport(trans) |
| 83 | if not trans then |
| 84 | terror(TTransportException:new{ |
| 85 | message = 'Must supply a transport to ' .. ttype(self) |
| 86 | }) |
| 87 | end |
| 88 | return TBufferedTransport:new{ |
| 89 | trans = trans |
| 90 | } |
| 91 | end |