| 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 |
#include <cerrno>
|
|
|
21 |
#include <exception>
|
|
|
22 |
|
|
|
23 |
#include <transport/TFDTransport.h>
|
|
|
24 |
|
|
|
25 |
#include <unistd.h>
|
|
|
26 |
|
|
|
27 |
using namespace std;
|
|
|
28 |
|
|
|
29 |
namespace apache { namespace thrift { namespace transport {
|
|
|
30 |
|
|
|
31 |
void TFDTransport::close() {
|
|
|
32 |
if (!isOpen()) {
|
|
|
33 |
return;
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
int rv = ::close(fd_);
|
|
|
37 |
int errno_copy = errno;
|
|
|
38 |
fd_ = -1;
|
|
|
39 |
// Have to check uncaught_exception because this is called in the destructor.
|
|
|
40 |
if (rv < 0 && !std::uncaught_exception()) {
|
|
|
41 |
throw TTransportException(TTransportException::UNKNOWN,
|
|
|
42 |
"TFDTransport::close()",
|
|
|
43 |
errno_copy);
|
|
|
44 |
}
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
uint32_t TFDTransport::read(uint8_t* buf, uint32_t len) {
|
|
|
48 |
ssize_t rv = ::read(fd_, buf, len);
|
|
|
49 |
if (rv < 0) {
|
|
|
50 |
int errno_copy = errno;
|
|
|
51 |
throw TTransportException(TTransportException::UNKNOWN,
|
|
|
52 |
"TFDTransport::read()",
|
|
|
53 |
errno_copy);
|
|
|
54 |
}
|
|
|
55 |
return rv;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
void TFDTransport::write(const uint8_t* buf, uint32_t len) {
|
|
|
59 |
while (len > 0) {
|
|
|
60 |
ssize_t rv = ::write(fd_, buf, len);
|
|
|
61 |
|
|
|
62 |
if (rv < 0) {
|
|
|
63 |
int errno_copy = errno;
|
|
|
64 |
throw TTransportException(TTransportException::UNKNOWN,
|
|
|
65 |
"TFDTransport::write()",
|
|
|
66 |
errno_copy);
|
|
|
67 |
} else if (rv == 0) {
|
|
|
68 |
throw TTransportException(TTransportException::END_OF_FILE,
|
|
|
69 |
"TFDTransport::write()");
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
buf += rv;
|
|
|
73 |
len -= rv;
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
}}} // apache::thrift::transport
|