Unified the exception handling in socket

PiperOrigin-RevId: 485457718
This commit is contained in:
Guogang Li
2022-11-01 17:15:51 -07:00
committed by Copybara-Service
parent 03b1c773fe
commit 33c5f44ea1
7 changed files with 287 additions and 138 deletions
@@ -15,6 +15,7 @@
#include "internal/platform/implementation/windows/bluetooth_classic_server_socket.h" #include "internal/platform/implementation/windows/bluetooth_classic_server_socket.h"
#include <codecvt> #include <codecvt>
#include <exception>
#include <functional> #include <functional>
#include <locale> #include <locale>
#include <memory> #include <memory>
@@ -89,37 +90,45 @@ Exception BluetoothServerSocket::Close() {
NEARBY_LOGS(INFO) << __func__ << ": Close completed succesfully."; NEARBY_LOGS(INFO) << __func__ << ": Close completed succesfully.";
return {Exception::kSuccess}; return {Exception::kSuccess};
} catch (...) { } catch (std::exception exception) {
closed_ = true; closed_ = true;
cond_.SignalAll(); cond_.SignalAll();
NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
NEARBY_LOGS(INFO) << __func__ << ": Failed to close server socket."; return {Exception::kIo};
} catch (const winrt::hresult_error& error) {
closed_ = true;
cond_.SignalAll();
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {Exception::kIo}; return {Exception::kIo};
} }
} }
bool BluetoothServerSocket::listen() { bool BluetoothServerSocket::listen() {
// Setup stream socket listener.
stream_socket_listener_ = StreamSocketListener();
stream_socket_listener_.Control().QualityOfService(
SocketQualityOfService::LowLatency);
stream_socket_listener_.Control().KeepAlive(true);
// Setup socket event of ConnectionReceived.
listener_event_token_ = stream_socket_listener_.ConnectionReceived(
{this, &BluetoothServerSocket::Listener_ConnectionReceived});
try { try {
// Setup stream socket listener.
stream_socket_listener_ = StreamSocketListener();
stream_socket_listener_.Control().QualityOfService(
SocketQualityOfService::LowLatency);
stream_socket_listener_.Control().KeepAlive(true);
// Setup socket event of ConnectionReceived.
listener_event_token_ = stream_socket_listener_.ConnectionReceived(
{this, &BluetoothServerSocket::Listener_ConnectionReceived});
stream_socket_listener_ stream_socket_listener_
.BindServiceNameAsync(winrt::to_hstring(service_name_), .BindServiceNameAsync(winrt::to_hstring(service_name_),
SocketProtectionLevel::PlainSocket) SocketProtectionLevel::PlainSocket)
.get(); .get();
return true; return true;
} catch (...) { } catch (std::exception exception) {
NEARBY_LOGS(WARNING) << "cannot accept connection on preferred port."; NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
} }
return false; return false;
@@ -67,8 +67,12 @@ Exception BluetoothSocket::Close() {
windows_socket_ = nullptr; windows_socket_ = nullptr;
} }
return {Exception::kSuccess}; return {Exception::kSuccess};
} catch (...) { } catch (std::exception exception) {
NEARBY_LOGS(ERROR) << "Failed to close Bluetooth socket."; NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
return {Exception::kIo};
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {Exception::kIo}; return {Exception::kIo};
} }
} }
@@ -85,36 +89,46 @@ api::BluetoothDevice* BluetoothSocket::GetRemoteDevice() {
// service name. // service name.
bool BluetoothSocket::Connect(HostName connectionHostName, bool BluetoothSocket::Connect(HostName connectionHostName,
winrt::hstring connectionServiceName) { winrt::hstring connectionServiceName) {
if (connectionHostName == nullptr || connectionServiceName.empty()) { try {
NEARBY_LOGS(ERROR) if (connectionHostName == nullptr || connectionServiceName.empty()) {
<< __func__ NEARBY_LOGS(ERROR)
<< ": Bluetooth socket connection failed. Attempting to " << __func__
"connect to empty HostName/MAC address or ServiceName."; << ": Bluetooth socket connection failed. Attempting to "
"connect to empty HostName/MAC address or ServiceName.";
return false;
}
windows_socket_ = winrt::Windows::Networking::Sockets::StreamSocket();
// https://docs.microsoft.com/en-us/uwp/api/windows.networking.sockets.streamsocket.connectasync?view=winrt-20348
windows_socket_.ConnectAsync(connectionHostName, connectionServiceName)
.get();
auto info = windows_socket_.Information();
auto hostName = info.RemoteHostName();
bluetooth_device_ = std::make_unique<BluetoothDevice>(
winrt::Windows::Devices::Bluetooth::BluetoothDevice::FromHostNameAsync(
windows_socket_.Information().RemoteHostName())
.get());
input_stream_ =
std::make_unique<BluetoothInputStream>(windows_socket_.InputStream());
output_stream_ =
std::make_unique<BluetoothOutputStream>(windows_socket_.OutputStream());
NEARBY_LOGS(INFO) << __func__
<< ": Bluetooth socket successfully connected to "
<< bluetooth_device_->GetName();
return true;
} catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
return false;
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return false; return false;
} }
windows_socket_ = winrt::Windows::Networking::Sockets::StreamSocket();
// https://docs.microsoft.com/en-us/uwp/api/windows.networking.sockets.streamsocket.connectasync?view=winrt-20348
windows_socket_.ConnectAsync(connectionHostName, connectionServiceName).get();
auto info = windows_socket_.Information();
auto hostName = info.RemoteHostName();
bluetooth_device_ = std::make_unique<BluetoothDevice>(
winrt::Windows::Devices::Bluetooth::BluetoothDevice::FromHostNameAsync(
windows_socket_.Information().RemoteHostName())
.get());
input_stream_ =
std::make_unique<BluetoothInputStream>(windows_socket_.InputStream());
output_stream_ =
std::make_unique<BluetoothOutputStream>(windows_socket_.OutputStream());
NEARBY_LOGS(INFO) << __func__
<< ": Bluetooth socket successfully connected to "
<< bluetooth_device_->GetName();
return true;
} }
BluetoothSocket::BluetoothInputStream::BluetoothInputStream( BluetoothSocket::BluetoothInputStream::BluetoothInputStream(
@@ -134,8 +148,12 @@ ExceptionOr<ByteArray> BluetoothSocket::BluetoothInputStream::Read(
DataReader dataReader = DataReader::FromBuffer(buffer); DataReader dataReader = DataReader::FromBuffer(buffer);
ByteArray data((char*)buffer.data(), buffer.Length()); ByteArray data((char*)buffer.data(), buffer.Length());
return ExceptionOr(data); return ExceptionOr(data);
} catch (...) { } catch (std::exception exception) {
NEARBY_LOGS(ERROR) << "Failed to read data from input stream."; NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
return {Exception::kIo};
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {Exception::kIo}; return {Exception::kIo};
} }
} }
@@ -155,8 +173,12 @@ Exception BluetoothSocket::BluetoothInputStream::Close() {
winrt_stream_.Close(); winrt_stream_.Close();
winrt_stream_ = nullptr; winrt_stream_ = nullptr;
return {Exception::kSuccess}; return {Exception::kSuccess};
} catch (...) { } catch (std::exception exception) {
NEARBY_LOGS(ERROR) << "Failed to close input stream."; NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
return {Exception::kIo};
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {Exception::kIo}; return {Exception::kIo};
} }
} }
@@ -178,10 +200,12 @@ Exception BluetoothSocket::BluetoothOutputStream::Write(const ByteArray& data) {
winrt::hresult hresult = winrt_stream_.WriteAsync(buffer).get(); winrt::hresult hresult = winrt_stream_.WriteAsync(buffer).get();
return {Exception::kSuccess}; return {Exception::kSuccess};
} catch (winrt::hresult_error const& ex) { } catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__ << ": winrt exception: " << ex.code() << ": " NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
<< winrt::to_string(ex.message()); return {Exception::kIo};
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {Exception::kIo}; return {Exception::kIo};
} }
} }
@@ -194,8 +218,12 @@ Exception BluetoothSocket::BluetoothOutputStream::Flush() {
winrt_stream_.FlushAsync().get(); winrt_stream_.FlushAsync().get();
return {Exception::kSuccess}; return {Exception::kSuccess};
} catch (...) { } catch (std::exception exception) {
NEARBY_LOGS(ERROR) << "Failed to flush data."; NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
return {Exception::kIo};
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {Exception::kIo}; return {Exception::kIo};
} }
} }
@@ -209,8 +237,12 @@ Exception BluetoothSocket::BluetoothOutputStream::Close() {
winrt_stream_.Close(); winrt_stream_.Close();
winrt_stream_ = nullptr; winrt_stream_ = nullptr;
return {Exception::kSuccess}; return {Exception::kSuccess};
} catch (...) { } catch (std::exception exception) {
NEARBY_LOGS(ERROR) << "Failed to close output stream."; NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
return {Exception::kIo};
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {Exception::kIo}; return {Exception::kIo};
} }
} }
@@ -15,6 +15,8 @@
#ifndef PLATFORM_IMPL_WINDOWS_BLUETOOTH_CLASSIC_SOCKET_H_ #ifndef PLATFORM_IMPL_WINDOWS_BLUETOOTH_CLASSIC_SOCKET_H_
#define PLATFORM_IMPL_WINDOWS_BLUETOOTH_CLASSIC_SOCKET_H_ #define PLATFORM_IMPL_WINDOWS_BLUETOOTH_CLASSIC_SOCKET_H_
#include <memory>
#include "internal/platform/implementation/bluetooth_classic.h" #include "internal/platform/implementation/bluetooth_classic.h"
#include "internal/platform/implementation/windows/bluetooth_classic_device.h" #include "internal/platform/implementation/windows/bluetooth_classic_device.h"
#include "internal/platform/implementation/windows/generated/winrt/Windows.Foundation.h" #include "internal/platform/implementation/windows/generated/winrt/Windows.Foundation.h"
@@ -66,7 +68,7 @@ class BluetoothSocket : public api::BluetoothSocket {
public: public:
BluetoothSocket(); BluetoothSocket();
BluetoothSocket(StreamSocket streamSocket); explicit BluetoothSocket(StreamSocket streamSocket);
~BluetoothSocket() override; ~BluetoothSocket() override;
@@ -102,7 +104,7 @@ class BluetoothSocket : public api::BluetoothSocket {
private: private:
class BluetoothInputStream : public InputStream { class BluetoothInputStream : public InputStream {
public: public:
BluetoothInputStream(IInputStream stream); explicit BluetoothInputStream(IInputStream stream);
~BluetoothInputStream() override = default; ~BluetoothInputStream() override = default;
ExceptionOr<ByteArray> Read(std::int64_t size) override; ExceptionOr<ByteArray> Read(std::int64_t size) override;
@@ -114,7 +116,7 @@ class BluetoothSocket : public api::BluetoothSocket {
class BluetoothOutputStream : public OutputStream { class BluetoothOutputStream : public OutputStream {
public: public:
BluetoothOutputStream(IOutputStream stream); explicit BluetoothOutputStream(IOutputStream stream);
~BluetoothOutputStream() override = default; ~BluetoothOutputStream() override = default;
Exception Write(const ByteArray& data) override; Exception Write(const ByteArray& data) override;
@@ -107,11 +107,16 @@ Exception WifiHotspotServerSocket::Close() {
NEARBY_LOGS(INFO) << __func__ << ": Close completed succesfully."; NEARBY_LOGS(INFO) << __func__ << ": Close completed succesfully.";
return {Exception::kSuccess}; return {Exception::kSuccess};
} catch (...) { } catch (std::exception exception) {
closed_ = true; closed_ = true;
cond_.SignalAll(); cond_.SignalAll();
NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
NEARBY_LOGS(INFO) << __func__ << ": Failed to close server socket."; return {Exception::kIo};
} catch (const winrt::hresult_error &error) {
closed_ = true;
cond_.SignalAll();
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {Exception::kIo}; return {Exception::kIo};
} }
} }
@@ -158,9 +163,16 @@ bool WifiHotspotServerSocket::listen() {
} }
return true; return true;
} catch (...) { } catch (std::exception exception) {
// Cannot bind to the preferred port, will let system to assign port. NEARBY_LOGS(ERROR)
NEARBY_LOGS(WARNING) << "cannot accept connection on preferred port."; << __func__
<< ": Cannot accept connection on preferred port. Exception: "
<< exception.what();
} catch (const winrt::hresult_error &error) {
NEARBY_LOGS(ERROR)
<< __func__
<< ":Cannot accept connection on preferred port. WinRT exception: "
<< error.code() << ": " << winrt::to_string(error.message());
} }
try { try {
@@ -170,9 +182,14 @@ bool WifiHotspotServerSocket::listen() {
std::stoi(stream_socket_listener_.Information().LocalPort().c_str()); std::stoi(stream_socket_listener_.Information().LocalPort().c_str());
NEARBY_LOGS(INFO) << "Server Socket port: " << port_; NEARBY_LOGS(INFO) << "Server Socket port: " << port_;
return true; return true;
} catch (...) { } catch (std::exception exception) {
// Cannot bind to the preferred port, will let system to assign port. NEARBY_LOGS(ERROR) << __func__ << ": Cannot bind to any port. Exception: "
NEARBY_LOGS(ERROR) << "cannot bind to any port."; << exception.what();
} catch (const winrt::hresult_error &error) {
NEARBY_LOGS(ERROR) << __func__
<< ": Cannot bind to any port. WinRT exception: "
<< error.code() << ": "
<< winrt::to_string(error.message());
} }
return false; return false;
@@ -238,17 +255,15 @@ std::string WifiHotspotServerSocket::GetHotspotIpAddresses() const {
} }
} }
} }
} catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__ << ": Exception to GetHotspotIpAddresses: "
<< exception.what();
return {}; return {};
} catch (const winrt::hresult_error &ex) { } catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__ NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
<< ": Exception to GetHotspotIpAddresses: " << ex.code() return {};
<< ": " << winrt::to_string(ex.message()); } catch (const winrt::hresult_error &error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {}; return {};
} }
return {};
} }
} // namespace windows } // namespace windows
@@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include <cstring>
#include <exception>
#include "internal/platform/implementation/windows/wifi_hotspot.h" #include "internal/platform/implementation/windows/wifi_hotspot.h"
#include "internal/platform/logging.h" #include "internal/platform/logging.h"
@@ -26,12 +29,15 @@ WifiHotspotSocket::WifiHotspotSocket(StreamSocket socket) {
} }
WifiHotspotSocket::~WifiHotspotSocket() { WifiHotspotSocket::~WifiHotspotSocket() {
if (stream_soket_ != nullptr) { try {
try { if (stream_soket_ != nullptr) {
Close(); Close();
} catch (...) {
NEARBY_LOGS(ERROR) << "Failed to destructor class WifiHotspotSocket.";
} }
} catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
} }
} }
@@ -45,7 +51,12 @@ Exception WifiHotspotSocket::Close() {
stream_soket_.Close(); stream_soket_.Close();
} }
return {Exception::kSuccess}; return {Exception::kSuccess};
} catch (...) { } catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
return {Exception::kIo};
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {Exception::kIo}; return {Exception::kIo};
} }
} }
@@ -70,8 +81,13 @@ ExceptionOr<ByteArray> WifiHotspotSocket::SocketInputStream::Read(
ByteArray data((char*)ibuffer.data(), ibuffer.Length()); ByteArray data((char*)ibuffer.data(), ibuffer.Length());
return ExceptionOr(data); return ExceptionOr(data);
} catch (...) { } catch (std::exception exception) {
return Exception{Exception::kIo}; NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
return {Exception::kIo};
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {Exception::kIo};
} }
} }
@@ -82,19 +98,28 @@ ExceptionOr<size_t> WifiHotspotSocket::SocketInputStream::Skip(size_t offset) {
auto ibuffer = auto ibuffer =
input_stream_.ReadAsync(buffer, offset, InputStreamOptions::None).get(); input_stream_.ReadAsync(buffer, offset, InputStreamOptions::None).get();
return ExceptionOr((size_t)ibuffer.Length()); return ExceptionOr((size_t)ibuffer.Length());
} catch (...) { } catch (std::exception exception) {
return Exception{Exception::kIo}; NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
return {Exception::kIo};
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {Exception::kIo};
} }
} }
Exception WifiHotspotSocket::SocketInputStream::Close() { Exception WifiHotspotSocket::SocketInputStream::Close() {
try { try {
input_stream_.Close(); input_stream_.Close();
} catch (...) { return {Exception::kSuccess};
} catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
return {Exception::kIo};
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {Exception::kIo}; return {Exception::kIo};
} }
return {Exception::kSuccess};
} }
// SocketOutputStream // SocketOutputStream
@@ -104,37 +129,49 @@ WifiHotspotSocket::SocketOutputStream::SocketOutputStream(
} }
Exception WifiHotspotSocket::SocketOutputStream::Write(const ByteArray& data) { Exception WifiHotspotSocket::SocketOutputStream::Write(const ByteArray& data) {
Buffer buffer = Buffer(data.size());
std::memcpy(buffer.data(), data.data(), data.size());
buffer.Length(data.size());
try { try {
Buffer buffer = Buffer(data.size());
std::memcpy(buffer.data(), data.data(), data.size());
buffer.Length(data.size());
output_stream_.WriteAsync(buffer).get(); output_stream_.WriteAsync(buffer).get();
} catch (...) { return {Exception::kSuccess};
} catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
return {Exception::kIo};
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {Exception::kIo}; return {Exception::kIo};
} }
return {Exception::kSuccess};
} }
Exception WifiHotspotSocket::SocketOutputStream::Flush() { Exception WifiHotspotSocket::SocketOutputStream::Flush() {
try { try {
output_stream_.FlushAsync().get(); output_stream_.FlushAsync().get();
} catch (...) { return {Exception::kSuccess};
} catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
return {Exception::kIo};
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {Exception::kIo}; return {Exception::kIo};
} }
return {Exception::kSuccess};
} }
Exception WifiHotspotSocket::SocketOutputStream::Close() { Exception WifiHotspotSocket::SocketOutputStream::Close() {
try { try {
output_stream_.Close(); output_stream_.Close();
} catch (...) { return {Exception::kSuccess};
} catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
return {Exception::kIo};
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {Exception::kIo}; return {Exception::kIo};
} }
return {Exception::kSuccess};
} }
} // namespace windows } // namespace windows
@@ -14,6 +14,7 @@
#include <windows.h> #include <windows.h>
#include <exception>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <string> #include <string>
@@ -118,11 +119,16 @@ Exception WifiLanServerSocket::Close() {
NEARBY_LOGS(INFO) << __func__ << ": Close completed succesfully."; NEARBY_LOGS(INFO) << __func__ << ": Close completed succesfully.";
return {Exception::kSuccess}; return {Exception::kSuccess};
} catch (...) { } catch (std::exception exception) {
closed_ = true; closed_ = true;
cond_.SignalAll(); cond_.SignalAll();
NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
NEARBY_LOGS(INFO) << __func__ << ": Failed to close server socket."; return {Exception::kIo};
} catch (const winrt::hresult_error& error) {
closed_ = true;
cond_.SignalAll();
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {Exception::kIo}; return {Exception::kIo};
} }
} }
@@ -158,9 +164,16 @@ bool WifiLanServerSocket::listen() {
} }
return true; return true;
} catch (...) { } catch (std::exception exception) {
// Cannot bind to the preferred port, will let system to assign port. NEARBY_LOGS(ERROR)
NEARBY_LOGS(WARNING) << "cannot accept connection on preferred port."; << __func__
<< ": Cannot accept connection on preferred port. Exception: "
<< exception.what();
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR)
<< __func__
<< ": Cannot accept connection on preferred port. WinRT exception: "
<< error.code() << ": " << winrt::to_string(error.message());
} }
try { try {
@@ -170,9 +183,14 @@ bool WifiLanServerSocket::listen() {
port_ = port_ =
std::stoi(stream_socket_listener_.Information().LocalPort().c_str()); std::stoi(stream_socket_listener_.Information().LocalPort().c_str());
return true; return true;
} catch (...) { } catch (std::exception exception) {
// Cannot bind to the preferred port, will let system to assign port. NEARBY_LOGS(ERROR) << __func__ << ": Cannot bind to any port. Exception: "
NEARBY_LOGS(ERROR) << "cannot bind to any port."; << exception.what();
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__
<< ": Cannot bind to any port. WinRT exception: "
<< error.code() << ": "
<< winrt::to_string(error.message());
} }
return false; return false;
@@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include <cstring>
#include <exception>
#include "internal/platform/implementation/windows/wifi_lan.h" #include "internal/platform/implementation/windows/wifi_lan.h"
#include "internal/platform/logging.h" #include "internal/platform/logging.h"
@@ -26,12 +29,15 @@ WifiLanSocket::WifiLanSocket(StreamSocket socket) {
} }
WifiLanSocket::~WifiLanSocket() { WifiLanSocket::~WifiLanSocket() {
if (stream_soket_ != nullptr) { try {
try { if (stream_soket_ != nullptr) {
Close(); Close();
} catch (...) {
NEARBY_LOGS(ERROR) << "Failed to destructor class WifiLanSocket.";
} }
} catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
} }
} }
@@ -45,7 +51,12 @@ Exception WifiLanSocket::Close() {
stream_soket_.Close(); stream_soket_.Close();
} }
return {Exception::kSuccess}; return {Exception::kSuccess};
} catch (...) { } catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
return {Exception::kIo};
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {Exception::kIo}; return {Exception::kIo};
} }
} }
@@ -70,8 +81,13 @@ ExceptionOr<ByteArray> WifiLanSocket::SocketInputStream::Read(
ByteArray data((char*)ibuffer.data(), ibuffer.Length()); ByteArray data((char*)ibuffer.data(), ibuffer.Length());
return ExceptionOr(data); return ExceptionOr(data);
} catch (...) { } catch (std::exception exception) {
return Exception{Exception::kIo}; NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
return {Exception::kIo};
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {Exception::kIo};
} }
} }
@@ -82,19 +98,28 @@ ExceptionOr<size_t> WifiLanSocket::SocketInputStream::Skip(size_t offset) {
auto ibuffer = auto ibuffer =
input_stream_.ReadAsync(buffer, offset, InputStreamOptions::None).get(); input_stream_.ReadAsync(buffer, offset, InputStreamOptions::None).get();
return ExceptionOr((size_t)ibuffer.Length()); return ExceptionOr((size_t)ibuffer.Length());
} catch (...) { } catch (std::exception exception) {
return Exception{Exception::kIo}; NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
return {Exception::kIo};
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {Exception::kIo};
} }
} }
Exception WifiLanSocket::SocketInputStream::Close() { Exception WifiLanSocket::SocketInputStream::Close() {
try { try {
input_stream_.Close(); input_stream_.Close();
} catch (...) { return {Exception::kSuccess};
} catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
return {Exception::kIo};
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {Exception::kIo}; return {Exception::kIo};
} }
return {Exception::kSuccess};
} }
// SocketOutputStream // SocketOutputStream
@@ -104,37 +129,48 @@ WifiLanSocket::SocketOutputStream::SocketOutputStream(
} }
Exception WifiLanSocket::SocketOutputStream::Write(const ByteArray& data) { Exception WifiLanSocket::SocketOutputStream::Write(const ByteArray& data) {
Buffer buffer = Buffer(data.size());
std::memcpy(buffer.data(), data.data(), data.size());
buffer.Length(data.size());
try { try {
Buffer buffer = Buffer(data.size());
std::memcpy(buffer.data(), data.data(), data.size());
buffer.Length(data.size());
output_stream_.WriteAsync(buffer).get(); output_stream_.WriteAsync(buffer).get();
} catch (...) { return {Exception::kSuccess};
} catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
return {Exception::kIo};
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {Exception::kIo}; return {Exception::kIo};
} }
return {Exception::kSuccess};
} }
Exception WifiLanSocket::SocketOutputStream::Flush() { Exception WifiLanSocket::SocketOutputStream::Flush() {
try { try {
output_stream_.FlushAsync().get(); output_stream_.FlushAsync().get();
} catch (...) { return {Exception::kSuccess};
} catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
return {Exception::kIo};
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {Exception::kIo}; return {Exception::kIo};
} }
return {Exception::kSuccess};
} }
Exception WifiLanSocket::SocketOutputStream::Close() { Exception WifiLanSocket::SocketOutputStream::Close() {
try { try {
output_stream_.Close(); output_stream_.Close();
} catch (...) { return {Exception::kSuccess};
} catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what();
return {Exception::kIo};
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
return {Exception::kIo}; return {Exception::kIo};
} }
return {Exception::kSuccess};
} }
} // namespace windows } // namespace windows