From 33c5f44ea1f39cf435ee711a59d73cb946c0ae2a Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Tue, 1 Nov 2022 17:13:52 -0700 Subject: [PATCH] Unified the exception handling in socket PiperOrigin-RevId: 485457718 --- .../bluetooth_classic_server_socket.cc | 43 ++++--- .../windows/bluetooth_classic_socket.cc | 116 +++++++++++------- .../windows/bluetooth_classic_socket.h | 8 +- .../windows/wifi_hotspot_server_socket.cc | 49 +++++--- .../windows/wifi_hotspot_socket.cc | 87 +++++++++---- .../windows/wifi_lan_server_socket.cc | 36 ++++-- .../implementation/windows/wifi_lan_socket.cc | 86 +++++++++---- 7 files changed, 287 insertions(+), 138 deletions(-) diff --git a/internal/platform/implementation/windows/bluetooth_classic_server_socket.cc b/internal/platform/implementation/windows/bluetooth_classic_server_socket.cc index affeee9a..9220f280 100644 --- a/internal/platform/implementation/windows/bluetooth_classic_server_socket.cc +++ b/internal/platform/implementation/windows/bluetooth_classic_server_socket.cc @@ -15,6 +15,7 @@ #include "internal/platform/implementation/windows/bluetooth_classic_server_socket.h" #include +#include #include #include #include @@ -89,37 +90,45 @@ Exception BluetoothServerSocket::Close() { NEARBY_LOGS(INFO) << __func__ << ": Close completed succesfully."; return {Exception::kSuccess}; - } catch (...) { + } catch (std::exception exception) { closed_ = true; cond_.SignalAll(); - - NEARBY_LOGS(INFO) << __func__ << ": Failed to close server socket."; + NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what(); + 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}; } } 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 { + // 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_ .BindServiceNameAsync(winrt::to_hstring(service_name_), SocketProtectionLevel::PlainSocket) .get(); return true; - } catch (...) { - NEARBY_LOGS(WARNING) << "cannot accept connection on preferred port."; + } 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()); } return false; diff --git a/internal/platform/implementation/windows/bluetooth_classic_socket.cc b/internal/platform/implementation/windows/bluetooth_classic_socket.cc index 70613fe8..3439c43b 100644 --- a/internal/platform/implementation/windows/bluetooth_classic_socket.cc +++ b/internal/platform/implementation/windows/bluetooth_classic_socket.cc @@ -67,8 +67,12 @@ Exception BluetoothSocket::Close() { windows_socket_ = nullptr; } return {Exception::kSuccess}; - } catch (...) { - NEARBY_LOGS(ERROR) << "Failed to close Bluetooth socket."; + } 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}; } } @@ -85,36 +89,46 @@ api::BluetoothDevice* BluetoothSocket::GetRemoteDevice() { // service name. bool BluetoothSocket::Connect(HostName connectionHostName, winrt::hstring connectionServiceName) { - if (connectionHostName == nullptr || connectionServiceName.empty()) { - NEARBY_LOGS(ERROR) - << __func__ - << ": Bluetooth socket connection failed. Attempting to " - "connect to empty HostName/MAC address or ServiceName."; + try { + if (connectionHostName == nullptr || connectionServiceName.empty()) { + NEARBY_LOGS(ERROR) + << __func__ + << ": 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( + winrt::Windows::Devices::Bluetooth::BluetoothDevice::FromHostNameAsync( + windows_socket_.Information().RemoteHostName()) + .get()); + + input_stream_ = + std::make_unique(windows_socket_.InputStream()); + output_stream_ = + std::make_unique(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; } - - 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( - winrt::Windows::Devices::Bluetooth::BluetoothDevice::FromHostNameAsync( - windows_socket_.Information().RemoteHostName()) - .get()); - - input_stream_ = - std::make_unique(windows_socket_.InputStream()); - output_stream_ = - std::make_unique(windows_socket_.OutputStream()); - - NEARBY_LOGS(INFO) << __func__ - << ": Bluetooth socket successfully connected to " - << bluetooth_device_->GetName(); - return true; } BluetoothSocket::BluetoothInputStream::BluetoothInputStream( @@ -134,8 +148,12 @@ ExceptionOr BluetoothSocket::BluetoothInputStream::Read( DataReader dataReader = DataReader::FromBuffer(buffer); ByteArray data((char*)buffer.data(), buffer.Length()); return ExceptionOr(data); - } catch (...) { - NEARBY_LOGS(ERROR) << "Failed to read data from input stream."; + } 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}; } } @@ -155,8 +173,12 @@ Exception BluetoothSocket::BluetoothInputStream::Close() { winrt_stream_.Close(); winrt_stream_ = nullptr; return {Exception::kSuccess}; - } catch (...) { - NEARBY_LOGS(ERROR) << "Failed to close input stream."; + } 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}; } } @@ -178,10 +200,12 @@ Exception BluetoothSocket::BluetoothOutputStream::Write(const ByteArray& data) { winrt::hresult hresult = winrt_stream_.WriteAsync(buffer).get(); return {Exception::kSuccess}; - } catch (winrt::hresult_error const& ex) { - NEARBY_LOGS(ERROR) << __func__ << ": winrt exception: " << ex.code() << ": " - << winrt::to_string(ex.message()); - + } 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}; } } @@ -194,8 +218,12 @@ Exception BluetoothSocket::BluetoothOutputStream::Flush() { winrt_stream_.FlushAsync().get(); return {Exception::kSuccess}; - } catch (...) { - NEARBY_LOGS(ERROR) << "Failed to flush data."; + } 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}; } } @@ -209,8 +237,12 @@ Exception BluetoothSocket::BluetoothOutputStream::Close() { winrt_stream_.Close(); winrt_stream_ = nullptr; return {Exception::kSuccess}; - } catch (...) { - NEARBY_LOGS(ERROR) << "Failed to close output stream."; + } 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}; } } diff --git a/internal/platform/implementation/windows/bluetooth_classic_socket.h b/internal/platform/implementation/windows/bluetooth_classic_socket.h index 83d4ad34..0e73b178 100644 --- a/internal/platform/implementation/windows/bluetooth_classic_socket.h +++ b/internal/platform/implementation/windows/bluetooth_classic_socket.h @@ -15,6 +15,8 @@ #ifndef PLATFORM_IMPL_WINDOWS_BLUETOOTH_CLASSIC_SOCKET_H_ #define PLATFORM_IMPL_WINDOWS_BLUETOOTH_CLASSIC_SOCKET_H_ +#include + #include "internal/platform/implementation/bluetooth_classic.h" #include "internal/platform/implementation/windows/bluetooth_classic_device.h" #include "internal/platform/implementation/windows/generated/winrt/Windows.Foundation.h" @@ -66,7 +68,7 @@ class BluetoothSocket : public api::BluetoothSocket { public: BluetoothSocket(); - BluetoothSocket(StreamSocket streamSocket); + explicit BluetoothSocket(StreamSocket streamSocket); ~BluetoothSocket() override; @@ -102,7 +104,7 @@ class BluetoothSocket : public api::BluetoothSocket { private: class BluetoothInputStream : public InputStream { public: - BluetoothInputStream(IInputStream stream); + explicit BluetoothInputStream(IInputStream stream); ~BluetoothInputStream() override = default; ExceptionOr Read(std::int64_t size) override; @@ -114,7 +116,7 @@ class BluetoothSocket : public api::BluetoothSocket { class BluetoothOutputStream : public OutputStream { public: - BluetoothOutputStream(IOutputStream stream); + explicit BluetoothOutputStream(IOutputStream stream); ~BluetoothOutputStream() override = default; Exception Write(const ByteArray& data) override; diff --git a/internal/platform/implementation/windows/wifi_hotspot_server_socket.cc b/internal/platform/implementation/windows/wifi_hotspot_server_socket.cc index d9f74e23..451ffb34 100644 --- a/internal/platform/implementation/windows/wifi_hotspot_server_socket.cc +++ b/internal/platform/implementation/windows/wifi_hotspot_server_socket.cc @@ -107,11 +107,16 @@ Exception WifiHotspotServerSocket::Close() { NEARBY_LOGS(INFO) << __func__ << ": Close completed succesfully."; return {Exception::kSuccess}; - } catch (...) { + } catch (std::exception exception) { closed_ = true; cond_.SignalAll(); - - NEARBY_LOGS(INFO) << __func__ << ": Failed to close server socket."; + NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what(); + 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}; } } @@ -158,9 +163,16 @@ bool WifiHotspotServerSocket::listen() { } return true; - } catch (...) { - // Cannot bind to the preferred port, will let system to assign port. - NEARBY_LOGS(WARNING) << "cannot accept connection on preferred port."; + } catch (std::exception exception) { + NEARBY_LOGS(ERROR) + << __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 { @@ -170,9 +182,14 @@ bool WifiHotspotServerSocket::listen() { std::stoi(stream_socket_listener_.Information().LocalPort().c_str()); NEARBY_LOGS(INFO) << "Server Socket port: " << port_; return true; - } catch (...) { - // Cannot bind to the preferred port, will let system to assign port. - NEARBY_LOGS(ERROR) << "cannot bind to any port."; + } catch (std::exception exception) { + NEARBY_LOGS(ERROR) << __func__ << ": Cannot bind to any port. Exception: " + << 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; @@ -238,17 +255,15 @@ std::string WifiHotspotServerSocket::GetHotspotIpAddresses() const { } } } - } catch (std::exception exception) { - NEARBY_LOGS(ERROR) << __func__ << ": Exception to GetHotspotIpAddresses: " - << exception.what(); return {}; - } catch (const winrt::hresult_error &ex) { - NEARBY_LOGS(ERROR) << __func__ - << ": Exception to GetHotspotIpAddresses: " << ex.code() - << ": " << winrt::to_string(ex.message()); + } catch (std::exception exception) { + NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what(); + return {}; + } catch (const winrt::hresult_error &error) { + NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code() + << ": " << winrt::to_string(error.message()); return {}; } - return {}; } } // namespace windows diff --git a/internal/platform/implementation/windows/wifi_hotspot_socket.cc b/internal/platform/implementation/windows/wifi_hotspot_socket.cc index b6dc1c7e..366cc101 100644 --- a/internal/platform/implementation/windows/wifi_hotspot_socket.cc +++ b/internal/platform/implementation/windows/wifi_hotspot_socket.cc @@ -12,6 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include +#include + #include "internal/platform/implementation/windows/wifi_hotspot.h" #include "internal/platform/logging.h" @@ -26,12 +29,15 @@ WifiHotspotSocket::WifiHotspotSocket(StreamSocket socket) { } WifiHotspotSocket::~WifiHotspotSocket() { - if (stream_soket_ != nullptr) { - try { + try { + if (stream_soket_ != nullptr) { 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(); } 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}; } } @@ -70,8 +81,13 @@ ExceptionOr WifiHotspotSocket::SocketInputStream::Read( ByteArray data((char*)ibuffer.data(), ibuffer.Length()); return ExceptionOr(data); - } catch (...) { - return Exception{Exception::kIo}; + } 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}; } } @@ -82,19 +98,28 @@ ExceptionOr WifiHotspotSocket::SocketInputStream::Skip(size_t offset) { auto ibuffer = input_stream_.ReadAsync(buffer, offset, InputStreamOptions::None).get(); return ExceptionOr((size_t)ibuffer.Length()); - } catch (...) { - return Exception{Exception::kIo}; + } 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}; } } Exception WifiHotspotSocket::SocketInputStream::Close() { try { 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::kSuccess}; } // SocketOutputStream @@ -104,37 +129,49 @@ WifiHotspotSocket::SocketOutputStream::SocketOutputStream( } 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 { + Buffer buffer = Buffer(data.size()); + std::memcpy(buffer.data(), data.data(), data.size()); + buffer.Length(data.size()); + 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::kSuccess}; } Exception WifiHotspotSocket::SocketOutputStream::Flush() { try { 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::kSuccess}; } Exception WifiHotspotSocket::SocketOutputStream::Close() { try { 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::kSuccess}; } } // namespace windows diff --git a/internal/platform/implementation/windows/wifi_lan_server_socket.cc b/internal/platform/implementation/windows/wifi_lan_server_socket.cc index 9bfceece..d788901d 100644 --- a/internal/platform/implementation/windows/wifi_lan_server_socket.cc +++ b/internal/platform/implementation/windows/wifi_lan_server_socket.cc @@ -14,6 +14,7 @@ #include +#include #include #include #include @@ -118,11 +119,16 @@ Exception WifiLanServerSocket::Close() { NEARBY_LOGS(INFO) << __func__ << ": Close completed succesfully."; return {Exception::kSuccess}; - } catch (...) { + } catch (std::exception exception) { closed_ = true; cond_.SignalAll(); - - NEARBY_LOGS(INFO) << __func__ << ": Failed to close server socket."; + NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what(); + 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}; } } @@ -158,9 +164,16 @@ bool WifiLanServerSocket::listen() { } return true; - } catch (...) { - // Cannot bind to the preferred port, will let system to assign port. - NEARBY_LOGS(WARNING) << "cannot accept connection on preferred port."; + } catch (std::exception exception) { + NEARBY_LOGS(ERROR) + << __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 { @@ -170,9 +183,14 @@ bool WifiLanServerSocket::listen() { port_ = std::stoi(stream_socket_listener_.Information().LocalPort().c_str()); return true; - } catch (...) { - // Cannot bind to the preferred port, will let system to assign port. - NEARBY_LOGS(ERROR) << "cannot bind to any port."; + } catch (std::exception exception) { + NEARBY_LOGS(ERROR) << __func__ << ": Cannot bind to any port. Exception: " + << 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; diff --git a/internal/platform/implementation/windows/wifi_lan_socket.cc b/internal/platform/implementation/windows/wifi_lan_socket.cc index 64d07d0e..2e36dac2 100644 --- a/internal/platform/implementation/windows/wifi_lan_socket.cc +++ b/internal/platform/implementation/windows/wifi_lan_socket.cc @@ -12,6 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include +#include + #include "internal/platform/implementation/windows/wifi_lan.h" #include "internal/platform/logging.h" @@ -26,12 +29,15 @@ WifiLanSocket::WifiLanSocket(StreamSocket socket) { } WifiLanSocket::~WifiLanSocket() { - if (stream_soket_ != nullptr) { - try { + try { + if (stream_soket_ != nullptr) { 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(); } 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}; } } @@ -70,8 +81,13 @@ ExceptionOr WifiLanSocket::SocketInputStream::Read( ByteArray data((char*)ibuffer.data(), ibuffer.Length()); return ExceptionOr(data); - } catch (...) { - return Exception{Exception::kIo}; + } 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}; } } @@ -82,19 +98,28 @@ ExceptionOr WifiLanSocket::SocketInputStream::Skip(size_t offset) { auto ibuffer = input_stream_.ReadAsync(buffer, offset, InputStreamOptions::None).get(); return ExceptionOr((size_t)ibuffer.Length()); - } catch (...) { - return Exception{Exception::kIo}; + } 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}; } } Exception WifiLanSocket::SocketInputStream::Close() { try { 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::kSuccess}; } // SocketOutputStream @@ -104,37 +129,48 @@ WifiLanSocket::SocketOutputStream::SocketOutputStream( } 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 { + Buffer buffer = Buffer(data.size()); + std::memcpy(buffer.data(), data.data(), data.size()); + buffer.Length(data.size()); 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::kSuccess}; } Exception WifiLanSocket::SocketOutputStream::Flush() { try { 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::kSuccess}; } Exception WifiLanSocket::SocketOutputStream::Close() { try { 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::kSuccess}; } } // namespace windows