| 30 |
ashish |
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 |
#ifndef _THRIFT_TRANSPORT_TTRANSPORT_H_
|
|
|
21 |
#define _THRIFT_TRANSPORT_TTRANSPORT_H_ 1
|
|
|
22 |
|
|
|
23 |
#include <Thrift.h>
|
|
|
24 |
#include <boost/shared_ptr.hpp>
|
|
|
25 |
#include <transport/TTransportException.h>
|
|
|
26 |
#include <string>
|
|
|
27 |
|
|
|
28 |
namespace apache { namespace thrift { namespace transport {
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Generic interface for a method of transporting data. A TTransport may be
|
|
|
32 |
* capable of either reading or writing, but not necessarily both.
|
|
|
33 |
*
|
|
|
34 |
*/
|
|
|
35 |
class TTransport {
|
|
|
36 |
public:
|
|
|
37 |
/**
|
|
|
38 |
* Virtual deconstructor.
|
|
|
39 |
*/
|
|
|
40 |
virtual ~TTransport() {}
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Whether this transport is open.
|
|
|
44 |
*/
|
|
|
45 |
virtual bool isOpen() {
|
|
|
46 |
return false;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Tests whether there is more data to read or if the remote side is
|
|
|
51 |
* still open. By default this is true whenever the transport is open,
|
|
|
52 |
* but implementations should add logic to test for this condition where
|
|
|
53 |
* possible (i.e. on a socket).
|
|
|
54 |
* This is used by a server to check if it should listen for another
|
|
|
55 |
* request.
|
|
|
56 |
*/
|
|
|
57 |
virtual bool peek() {
|
|
|
58 |
return isOpen();
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Opens the transport for communications.
|
|
|
63 |
*
|
|
|
64 |
* @return bool Whether the transport was successfully opened
|
|
|
65 |
* @throws TTransportException if opening failed
|
|
|
66 |
*/
|
|
|
67 |
virtual void open() {
|
|
|
68 |
throw TTransportException(TTransportException::NOT_OPEN, "Cannot open base TTransport.");
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Closes the transport.
|
|
|
73 |
*/
|
|
|
74 |
virtual void close() {
|
|
|
75 |
throw TTransportException(TTransportException::NOT_OPEN, "Cannot close base TTransport.");
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Attempt to read up to the specified number of bytes into the string.
|
|
|
80 |
*
|
|
|
81 |
* @param buf Reference to the location to write the data
|
|
|
82 |
* @param len How many bytes to read
|
|
|
83 |
* @return How many bytes were actually read
|
|
|
84 |
* @throws TTransportException If an error occurs
|
|
|
85 |
*/
|
|
|
86 |
virtual uint32_t read(uint8_t* /* buf */, uint32_t /* len */) {
|
|
|
87 |
throw TTransportException(TTransportException::NOT_OPEN, "Base TTransport cannot read.");
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Reads the given amount of data in its entirety no matter what.
|
|
|
92 |
*
|
|
|
93 |
* @param s Reference to location for read data
|
|
|
94 |
* @param len How many bytes to read
|
|
|
95 |
* @return How many bytes read, which must be equal to size
|
|
|
96 |
* @throws TTransportException If insufficient data was read
|
|
|
97 |
*/
|
|
|
98 |
virtual uint32_t readAll(uint8_t* buf, uint32_t len) {
|
|
|
99 |
uint32_t have = 0;
|
|
|
100 |
uint32_t get = 0;
|
|
|
101 |
|
|
|
102 |
while (have < len) {
|
|
|
103 |
get = read(buf+have, len-have);
|
|
|
104 |
if (get <= 0) {
|
|
|
105 |
throw TTransportException("No more data to read.");
|
|
|
106 |
}
|
|
|
107 |
have += get;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
return have;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* Called when read is completed.
|
|
|
115 |
* This can be over-ridden to perform a transport-specific action
|
|
|
116 |
* e.g. logging the request to a file
|
|
|
117 |
*
|
|
|
118 |
*/
|
|
|
119 |
virtual void readEnd() {
|
|
|
120 |
// default behaviour is to do nothing
|
|
|
121 |
return;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* Writes the string in its entirety to the buffer.
|
|
|
126 |
*
|
|
|
127 |
* @param buf The data to write out
|
|
|
128 |
* @throws TTransportException if an error occurs
|
|
|
129 |
*/
|
|
|
130 |
virtual void write(const uint8_t* /* buf */, uint32_t /* len */) {
|
|
|
131 |
throw TTransportException(TTransportException::NOT_OPEN, "Base TTransport cannot write.");
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
/**
|
|
|
135 |
* Called when write is completed.
|
|
|
136 |
* This can be over-ridden to perform a transport-specific action
|
|
|
137 |
* at the end of a request.
|
|
|
138 |
*
|
|
|
139 |
*/
|
|
|
140 |
virtual void writeEnd() {
|
|
|
141 |
// default behaviour is to do nothing
|
|
|
142 |
return;
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
/**
|
|
|
146 |
* Flushes any pending data to be written. Typically used with buffered
|
|
|
147 |
* transport mechanisms.
|
|
|
148 |
*
|
|
|
149 |
* @throws TTransportException if an error occurs
|
|
|
150 |
*/
|
|
|
151 |
virtual void flush() {}
|
|
|
152 |
|
|
|
153 |
/**
|
|
|
154 |
* Attempts to return a pointer to \c len bytes, possibly copied into \c buf.
|
|
|
155 |
* Does not consume the bytes read (i.e.: a later read will return the same
|
|
|
156 |
* data). This method is meant to support protocols that need to read
|
|
|
157 |
* variable-length fields. They can attempt to borrow the maximum amount of
|
|
|
158 |
* data that they will need, then consume (see next method) what they
|
|
|
159 |
* actually use. Some transports will not support this method and others
|
|
|
160 |
* will fail occasionally, so protocols must be prepared to use read if
|
|
|
161 |
* borrow fails.
|
|
|
162 |
*
|
|
|
163 |
* @oaram buf A buffer where the data can be stored if needed.
|
|
|
164 |
* If borrow doesn't return buf, then the contents of
|
|
|
165 |
* buf after the call are undefined.
|
|
|
166 |
* @param len *len should initially contain the number of bytes to borrow.
|
|
|
167 |
* If borrow succeeds, *len will contain the number of bytes
|
|
|
168 |
* available in the returned pointer. This will be at least
|
|
|
169 |
* what was requested, but may be more if borrow returns
|
|
|
170 |
* a pointer to an internal buffer, rather than buf.
|
|
|
171 |
* If borrow fails, the contents of *len are undefined.
|
|
|
172 |
* @return If the borrow succeeds, return a pointer to the borrowed data.
|
|
|
173 |
* This might be equal to \c buf, or it might be a pointer into
|
|
|
174 |
* the transport's internal buffers.
|
|
|
175 |
* @throws TTransportException if an error occurs
|
|
|
176 |
*/
|
|
|
177 |
virtual const uint8_t* borrow(uint8_t* /* buf */, uint32_t* /* len */) {
|
|
|
178 |
return NULL;
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
/**
|
|
|
182 |
* Remove len bytes from the transport. This should always follow a borrow
|
|
|
183 |
* of at least len bytes, and should always succeed.
|
|
|
184 |
* TODO(dreiss): Is there any transport that could borrow but fail to
|
|
|
185 |
* consume, or that would require a buffer to dump the consumed data?
|
|
|
186 |
*
|
|
|
187 |
* @param len How many bytes to consume
|
|
|
188 |
* @throws TTransportException If an error occurs
|
|
|
189 |
*/
|
|
|
190 |
virtual void consume(uint32_t /* len */) {
|
|
|
191 |
throw TTransportException(TTransportException::NOT_OPEN, "Base TTransport cannot consume.");
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
protected:
|
|
|
195 |
/**
|
|
|
196 |
* Simple constructor.
|
|
|
197 |
*/
|
|
|
198 |
TTransport() {}
|
|
|
199 |
};
|
|
|
200 |
|
|
|
201 |
/**
|
|
|
202 |
* Generic factory class to make an input and output transport out of a
|
|
|
203 |
* source transport. Commonly used inside servers to make input and output
|
|
|
204 |
* streams out of raw clients.
|
|
|
205 |
*
|
|
|
206 |
*/
|
|
|
207 |
class TTransportFactory {
|
|
|
208 |
public:
|
|
|
209 |
TTransportFactory() {}
|
|
|
210 |
|
|
|
211 |
virtual ~TTransportFactory() {}
|
|
|
212 |
|
|
|
213 |
/**
|
|
|
214 |
* Default implementation does nothing, just returns the transport given.
|
|
|
215 |
*/
|
|
|
216 |
virtual boost::shared_ptr<TTransport> getTransport(boost::shared_ptr<TTransport> trans) {
|
|
|
217 |
return trans;
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
};
|
|
|
221 |
|
|
|
222 |
}}} // apache::thrift::transport
|
|
|
223 |
|
|
|
224 |
#endif // #ifndef _THRIFT_TRANSPORT_TTRANSPORT_H_
|