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 <codecvt>
#include <exception>
#include <functional>
#include <locale>
#include <memory>
@@ -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;
@@ -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<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;
}
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(
@@ -134,8 +148,12 @@ ExceptionOr<ByteArray> 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};
}
}
@@ -15,6 +15,8 @@
#ifndef 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/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<ByteArray> 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;
@@ -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
@@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cstring>
#include <exception>
#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<ByteArray> 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<size_t> 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
@@ -14,6 +14,7 @@
#include <windows.h>
#include <exception>
#include <functional>
#include <memory>
#include <string>
@@ -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;
@@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cstring>
#include <exception>
#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<ByteArray> 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<size_t> 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