mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Move all common TCP sockets code to tcp_server_socket.h.
This commit is contained in:
@@ -82,6 +82,7 @@ cc_library(
|
||||
"network_manager_active_connection.h",
|
||||
"network_manager_access_point.h",
|
||||
"stream.h",
|
||||
"tcp_server_socket.h",
|
||||
"wifi_direct.h",
|
||||
"wifi_direct_server_socket.h",
|
||||
"wifi_direct_socket.h",
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <sys/socket.h>
|
||||
#include <memory>
|
||||
|
||||
#include "internal/platform/implementation/linux/tcp_server_socket.h"
|
||||
#include "internal/platform/implementation/linux/wifi_direct.h"
|
||||
#include "internal/platform/implementation/linux/wifi_direct_server_socket.h"
|
||||
#include "internal/platform/implementation/linux/wifi_direct_socket.h"
|
||||
@@ -31,29 +32,10 @@ std::unique_ptr<api::WifiDirectSocket>
|
||||
NetworkManagerWifiDirectMedium::ConnectToService(
|
||||
absl::string_view ip_address, int port,
|
||||
CancellationFlag *cancellation_flag) {
|
||||
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock < 0) {
|
||||
NEARBY_LOGS(ERROR) << __func__
|
||||
<< ": Error opening socket: " << std::strerror(errno);
|
||||
return nullptr;
|
||||
}
|
||||
auto socket = TCPSocket::Connect(std::string(ip_address), port);
|
||||
if (!socket.has_value()) return nullptr;
|
||||
|
||||
NEARBY_LOGS(VERBOSE) << __func__ << ": Connecting to " << ip_address << ":"
|
||||
<< port;
|
||||
struct sockaddr_in addr;
|
||||
addr.sin_addr.s_addr = inet_addr(std::string(ip_address).c_str());
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(port);
|
||||
|
||||
auto ret =
|
||||
connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr));
|
||||
if (ret < 0) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Error connecting to socket: "
|
||||
<< std::strerror(errno);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return std::make_unique<WifiDirectSocket>(sock);
|
||||
return std::make_unique<WifiDirectSocket>(std::move(*socket));
|
||||
}
|
||||
|
||||
std::unique_ptr<api::WifiDirectServerSocket>
|
||||
@@ -72,39 +54,11 @@ NetworkManagerWifiDirectMedium::ListenForService(int port) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock < 0) {
|
||||
NEARBY_LOGS(ERROR) << __func__
|
||||
<< ": Error opening socket: " << std::strerror(errno);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
struct sockaddr_in addr;
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = inet_addr(ip4addresses[0].c_str());
|
||||
addr.sin_port = htons(port);
|
||||
|
||||
auto ret =
|
||||
bind(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr));
|
||||
if (ret < 0) {
|
||||
NEARBY_LOGS(ERROR) << __func__
|
||||
<< ": Error binding to socket: " << std::strerror(errno);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
NEARBY_LOGS(VERBOSE) << __func__ << ": Listening for services on "
|
||||
<< ip4addresses[0] << ":" << port << " on device "
|
||||
<< wireless_device_->getObjectPath();
|
||||
|
||||
ret = listen(sock, 0);
|
||||
if (ret < 0) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Error listening on socket: "
|
||||
<< std::strerror(errno);
|
||||
return nullptr;
|
||||
}
|
||||
auto socket = TCPServerSocket::Listen(std::ref(ip4addresses[0]), port);
|
||||
if (!socket.has_value()) return nullptr;
|
||||
|
||||
return std::make_unique<NetworkManagerWifiDirectServerSocket>(
|
||||
sock, std::move(active_connection), network_manager_);
|
||||
std::move(*socket), std::move(active_connection), network_manager_);
|
||||
}
|
||||
|
||||
bool NetworkManagerWifiDirectMedium::ConnectWifiDirect(
|
||||
|
||||
@@ -34,47 +34,18 @@ std::string NetworkManagerWifiDirectServerSocket::GetIPAddress() const {
|
||||
}
|
||||
|
||||
int NetworkManagerWifiDirectServerSocket::GetPort() const {
|
||||
struct sockaddr_in sin;
|
||||
socklen_t len = sizeof(sin);
|
||||
auto ret =
|
||||
getsockname(fd_.get(), reinterpret_cast<struct sockaddr *>(&sin), &len);
|
||||
if (ret < 0) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Error getting information for socket "
|
||||
<< fd_.get() << ": " << std::strerror(errno);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ntohs(sin.sin_port);
|
||||
return server_socket_.GetPort();
|
||||
}
|
||||
|
||||
std::unique_ptr<api::WifiDirectSocket>
|
||||
NetworkManagerWifiDirectServerSocket::Accept() {
|
||||
struct sockaddr_in addr;
|
||||
socklen_t len = sizeof(addr);
|
||||
|
||||
auto conn =
|
||||
accept(fd_.get(), reinterpret_cast<struct sockaddr *>(&addr), &len);
|
||||
if (conn < 0) {
|
||||
NEARBY_LOGS(ERROR) << __func__
|
||||
<< ": Error accepting incoming connections on socket "
|
||||
<< fd_.get() << ": " << std::strerror(errno);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return std::make_unique<WifiDirectSocket>(conn);
|
||||
auto sock = server_socket_.Accept();
|
||||
if (!sock.has_value()) return nullptr;
|
||||
return std::make_unique<WifiDirectSocket>(std::move(*sock));
|
||||
}
|
||||
|
||||
Exception NetworkManagerWifiDirectServerSocket::Close() {
|
||||
int fd = fd_.release();
|
||||
shutdown(fd, SHUT_RDWR);
|
||||
auto ret = close(fd);
|
||||
if (ret < 0) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Error closing socket " << fd << ": "
|
||||
<< std::strerror(errno);
|
||||
return {Exception::kFailed};
|
||||
}
|
||||
|
||||
return {Exception::kSuccess};
|
||||
return server_socket_.Close();
|
||||
}
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
#include <sdbus-c++/IConnection.h>
|
||||
#include "internal/platform/implementation/linux/network_manager_active_connection.h"
|
||||
#include "internal/platform/implementation/linux/tcp_server_socket.h"
|
||||
#include "internal/platform/implementation/linux/wifi_medium.h"
|
||||
#include "internal/platform/implementation/wifi_direct.h"
|
||||
namespace nearby {
|
||||
@@ -25,9 +26,10 @@ class NetworkManagerWifiDirectServerSocket
|
||||
: public api::WifiDirectServerSocket {
|
||||
public:
|
||||
NetworkManagerWifiDirectServerSocket(
|
||||
int socket, std::unique_ptr<networkmanager::ActiveConnection> active_conn,
|
||||
TCPServerSocket socket,
|
||||
std::unique_ptr<networkmanager::ActiveConnection> active_conn,
|
||||
std::shared_ptr<networkmanager::NetworkManager> network_manager)
|
||||
: fd_(socket),
|
||||
: server_socket_(std::move(socket)),
|
||||
active_conn_(std::move(active_conn)),
|
||||
network_manager_(std::move(network_manager)) {}
|
||||
|
||||
@@ -37,7 +39,7 @@ class NetworkManagerWifiDirectServerSocket
|
||||
Exception Close() override;
|
||||
|
||||
private:
|
||||
sdbus::UnixFd fd_;
|
||||
TCPServerSocket server_socket_;
|
||||
std::unique_ptr<networkmanager::ActiveConnection> active_conn_;
|
||||
std::shared_ptr<networkmanager::NetworkManager> network_manager_;
|
||||
};
|
||||
|
||||
@@ -17,29 +17,22 @@
|
||||
|
||||
#include "internal/platform/exception.h"
|
||||
#include "internal/platform/implementation/linux/stream.h"
|
||||
#include "internal/platform/implementation/linux/tcp_server_socket.h"
|
||||
#include "internal/platform/implementation/wifi_direct.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace linux {
|
||||
class WifiDirectSocket : public api::WifiDirectSocket {
|
||||
public:
|
||||
explicit WifiDirectSocket(int socket)
|
||||
: fd_(sdbus::UnixFd(socket)), output_stream_(fd_), input_stream_(fd_) {}
|
||||
explicit WifiDirectSocket(TCPSocket socket) : socket_(std::move(socket)) {}
|
||||
|
||||
InputStream &GetInputStream() override { return input_stream_; };
|
||||
OutputStream &GetOutputStream() override { return output_stream_; };
|
||||
InputStream &GetInputStream() override { return socket_.GetInputStream(); }
|
||||
OutputStream &GetOutputStream() override { return socket_.GetOutputStream(); }
|
||||
|
||||
Exception Close() override {
|
||||
input_stream_.Close();
|
||||
output_stream_.Close();
|
||||
|
||||
return Exception{Exception::kSuccess};
|
||||
};
|
||||
Exception Close() override { return socket_.Close(); };
|
||||
|
||||
private:
|
||||
sdbus::UnixFd fd_;
|
||||
OutputStream output_stream_;
|
||||
InputStream input_stream_;
|
||||
TCPSocket socket_;
|
||||
};
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include "absl/strings/substitute.h"
|
||||
#include "internal/platform/implementation/linux/avahi.h"
|
||||
#include "internal/platform/implementation/linux/dbus.h"
|
||||
#include "internal/platform/implementation/linux/tcp_server_socket.h"
|
||||
#include "internal/platform/implementation/linux/wifi_lan.h"
|
||||
#include "internal/platform/implementation/linux/wifi_lan_server_socket.h"
|
||||
#include "internal/platform/implementation/linux/wifi_lan_socket.h"
|
||||
@@ -204,64 +205,18 @@ bool WifiLanMedium::StopDiscovery(const std::string &service_type) {
|
||||
std::unique_ptr<api::WifiLanSocket> WifiLanMedium::ConnectToService(
|
||||
const std::string &ip_address, int port,
|
||||
CancellationFlag *cancellation_flag) {
|
||||
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock < 0) {
|
||||
NEARBY_LOGS(ERROR) << __func__
|
||||
<< ": Error opening socket: " << std::strerror(errno);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
NEARBY_LOGS(VERBOSE) << __func__ << ": Connecting to " << ip_address << ":"
|
||||
<< port;
|
||||
struct sockaddr_in addr;
|
||||
addr.sin_addr.s_addr = inet_addr(ip_address.c_str());
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(port);
|
||||
|
||||
auto ret =
|
||||
connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr));
|
||||
if (ret < 0) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Error connecting to socket: "
|
||||
<< std::strerror(errno);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
sdbus::UnixFd fd(sock);
|
||||
return std::make_unique<WifiLanSocket>(std::move(fd));
|
||||
auto socket = TCPSocket::Connect(ip_address, port);
|
||||
if (!socket.has_value()) return nullptr;
|
||||
return std::make_unique<WifiLanSocket>(*socket);
|
||||
}
|
||||
|
||||
std::unique_ptr<api::WifiLanServerSocket> WifiLanMedium::ListenForService(
|
||||
int port) {
|
||||
auto sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock < 0) {
|
||||
NEARBY_LOGS(ERROR) << __func__
|
||||
<< ": Error opening socket: " << std::strerror(errno);
|
||||
return nullptr;
|
||||
}
|
||||
auto socket = TCPServerSocket::Listen(std::nullopt, port);
|
||||
if (!socket.has_value()) return nullptr;
|
||||
|
||||
struct sockaddr_in addr;
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
addr.sin_port = htons(port);
|
||||
|
||||
auto ret =
|
||||
bind(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr));
|
||||
if (ret < 0) {
|
||||
NEARBY_LOGS(ERROR) << __func__
|
||||
<< ": Error binding to socket: " << std::strerror(errno);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ret = listen(sock, 0);
|
||||
if (ret < 0) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Error listening on socket: "
|
||||
<< std::strerror(errno);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
NEARBY_LOGS(VERBOSE) << __func__ << "Listening for services on port " << port;
|
||||
|
||||
return std::make_unique<WifiLanServerSocket>(sock, network_manager_);
|
||||
return std::make_unique<WifiLanServerSocket>(std::move(*socket),
|
||||
network_manager_);
|
||||
}
|
||||
|
||||
absl::optional<std::pair<std::int32_t, std::int32_t>> GetDynamicPortRange() {
|
||||
|
||||
@@ -27,7 +27,6 @@
|
||||
#include "internal/platform/implementation/linux/dbus.h"
|
||||
#include "internal/platform/implementation/linux/wifi_lan_server_socket.h"
|
||||
#include "internal/platform/implementation/linux/wifi_lan_socket.h"
|
||||
#include "internal/platform/implementation/linux/wifi_medium.h"
|
||||
#include "internal/platform/logging.h"
|
||||
|
||||
namespace nearby {
|
||||
@@ -75,46 +74,18 @@ std::string WifiLanServerSocket::GetIPAddress() const {
|
||||
}
|
||||
|
||||
int WifiLanServerSocket::GetPort() const {
|
||||
struct sockaddr_in sin;
|
||||
socklen_t len = sizeof(sin);
|
||||
auto ret =
|
||||
getsockname(fd_.get(), reinterpret_cast<struct sockaddr *>(&sin), &len);
|
||||
if (ret < 0) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Error getting information for socket "
|
||||
<< fd_.get() << ": " << std::strerror(errno);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ntohs(sin.sin_port);
|
||||
return server_socket_.GetPort();
|
||||
}
|
||||
|
||||
std::unique_ptr<api::WifiLanSocket> WifiLanServerSocket::Accept() {
|
||||
struct sockaddr_in addr;
|
||||
socklen_t len = sizeof(addr);
|
||||
auto sock = server_socket_.Accept();
|
||||
if (!sock.has_value()) return nullptr;
|
||||
|
||||
auto conn =
|
||||
accept(fd_.get(), reinterpret_cast<struct sockaddr *>(&addr), &len);
|
||||
if (conn < 0) {
|
||||
NEARBY_LOGS(ERROR) << __func__
|
||||
<< ": Error accepting incoming connections on socket "
|
||||
<< fd_.get() << ": " << std::strerror(errno);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return std::make_unique<WifiLanSocket>(sdbus::UnixFd(conn));
|
||||
return std::make_unique<WifiLanSocket>(std::move(*sock));
|
||||
}
|
||||
|
||||
Exception WifiLanServerSocket::Close() {
|
||||
int fd = fd_.release();
|
||||
shutdown(fd, SHUT_RDWR);
|
||||
auto ret = close(fd);
|
||||
if (ret < 0) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Error closing socket " << fd << ": "
|
||||
<< std::strerror(errno);
|
||||
return {Exception::kFailed};
|
||||
}
|
||||
|
||||
return {Exception::kSuccess};
|
||||
return server_socket_.Close();
|
||||
}
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
|
||||
@@ -22,15 +22,17 @@
|
||||
|
||||
#include "internal/platform/exception.h"
|
||||
#include "internal/platform/implementation/linux/network_manager.h"
|
||||
#include "internal/platform/implementation/linux/tcp_server_socket.h"
|
||||
#include "internal/platform/implementation/wifi_lan.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace linux {
|
||||
class WifiLanServerSocket : public api::WifiLanServerSocket {
|
||||
public:
|
||||
explicit WifiLanServerSocket(int socket,
|
||||
std::shared_ptr<networkmanager::NetworkManager> network_manager)
|
||||
: fd_(sdbus::UnixFd(socket)),
|
||||
explicit WifiLanServerSocket(
|
||||
TCPServerSocket socket,
|
||||
std::shared_ptr<networkmanager::NetworkManager> network_manager)
|
||||
: server_socket_(std::move(socket)),
|
||||
network_manager_(std::move(network_manager)),
|
||||
system_bus_(network_manager_->GetConnection()) {}
|
||||
|
||||
@@ -41,7 +43,7 @@ class WifiLanServerSocket : public api::WifiLanServerSocket {
|
||||
Exception Close() override;
|
||||
|
||||
private:
|
||||
sdbus::UnixFd fd_;
|
||||
TCPServerSocket server_socket_;
|
||||
std::shared_ptr<networkmanager::NetworkManager> network_manager_;
|
||||
std::shared_ptr<sdbus::IConnection> system_bus_;
|
||||
};
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include <sdbus-c++/Types.h>
|
||||
|
||||
#include "internal/platform/implementation/linux/stream.h"
|
||||
#include "internal/platform/implementation/linux/tcp_server_socket.h"
|
||||
#include "internal/platform/implementation/wifi_lan.h"
|
||||
#include "internal/platform/input_stream.h"
|
||||
#include "internal/platform/output_stream.h"
|
||||
@@ -28,21 +29,18 @@ namespace nearby {
|
||||
namespace linux {
|
||||
class WifiLanSocket : public api::WifiLanSocket {
|
||||
public:
|
||||
explicit WifiLanSocket(sdbus::UnixFd fd)
|
||||
: output_stream_(fd), input_stream_(fd) {}
|
||||
explicit WifiLanSocket(TCPSocket sock) : socket_(std::move(sock)) {}
|
||||
|
||||
nearby::InputStream &GetInputStream() override { return input_stream_; };
|
||||
nearby::OutputStream &GetOutputStream() override { return output_stream_; };
|
||||
Exception Close() override {
|
||||
input_stream_.Close();
|
||||
output_stream_.Close();
|
||||
|
||||
return Exception{Exception::kSuccess};
|
||||
};
|
||||
nearby::InputStream &GetInputStream() override {
|
||||
return socket_.GetInputStream();
|
||||
}
|
||||
nearby::OutputStream &GetOutputStream() override {
|
||||
return socket_.GetOutputStream();
|
||||
}
|
||||
Exception Close() override { return socket_.Close(); }
|
||||
|
||||
private:
|
||||
OutputStream output_stream_;
|
||||
InputStream input_stream_;
|
||||
TCPSocket socket_;
|
||||
};
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
|
||||
Reference in New Issue
Block a user