Added getter and setter for domain socket path, and minor fix for printing abstract domain socket paths
Client: cpp
Patch: Mario Emmenlauer
This closes #2233
diff --git a/lib/cpp/src/thrift/transport/TSocket.cpp b/lib/cpp/src/thrift/transport/TSocket.cpp
index 81aaccf..5000baf 100644
--- a/lib/cpp/src/thrift/transport/TSocket.cpp
+++ b/lib/cpp/src/thrift/transport/TSocket.cpp
@@ -438,7 +438,7 @@
void TSocket::unix_open() {
if (!path_.empty()) {
- // Unix Domain SOcket does not need addrinfo struct, so we pass nullptr
+ // Unix Domain Socket does not need addrinfo struct, so we pass NULL
openConnection(nullptr);
}
}
@@ -711,6 +711,10 @@
return port_;
}
+std::string TSocket::getPath() {
+ return path_;
+}
+
void TSocket::setHost(string host) {
host_ = host;
}
@@ -719,6 +723,10 @@
port_ = port;
}
+void TSocket::setPath(std::string path) {
+ path_ = path;
+}
+
void TSocket::setLinger(bool on, int linger) {
lingerOn_ = on;
lingerVal_ = linger;
@@ -828,7 +836,11 @@
oss << "<Host: " << host_ << " Port: " << port_ << ">";
}
} else {
- oss << "<Path: " << path_ << ">";
+ std::string fmt_path_ = path_;
+ // Handle printing abstract sockets (first character is a '\0' char):
+ if (!fmt_path_.empty() && fmt_path_[0] == '\0')
+ fmt_path_[0] = '@';
+ oss << "<Path: " << fmt_path_ << ">";
}
return oss.str();
}
diff --git a/lib/cpp/src/thrift/transport/TSocket.h b/lib/cpp/src/thrift/transport/TSocket.h
index 043f0de..8a224f2 100644
--- a/lib/cpp/src/thrift/transport/TSocket.h
+++ b/lib/cpp/src/thrift/transport/TSocket.h
@@ -68,6 +68,7 @@
* Note that this does NOT actually connect the socket.
*
* @param path The Unix domain socket e.g. "/tmp/ThriftTest.binary.thrift"
+ * or a zero-prefixed string to create an abstract domain socket on Linux.
*/
TSocket(const std::string& path, std::shared_ptr<TConfiguration> config = nullptr);
@@ -150,6 +151,13 @@
int getPort();
/**
+ * Get the Unix domain socket path that the socket is connected to
+ *
+ * @return std::string path
+ */
+ std::string getPath();
+
+ /**
* Set the host that socket will connect to
*
* @param host host identifier
@@ -164,6 +172,13 @@
void setPort(int port);
/**
+ * Set the Unix domain socket path for the socket
+ *
+ * @param path std::string path
+ */
+ void setPath(std::string path);
+
+ /**
* Controls whether the linger option is set on the socket.
*
* @param on Whether SO_LINGER is on