Skip to content

Commit

Permalink
Replace shared_ptr<Error> with unique_ptr<Error> in Network module
Browse files Browse the repository at this point in the history
  • Loading branch information
mogemimi committed Dec 15, 2020
1 parent c622d29 commit 1c50d81
Show file tree
Hide file tree
Showing 33 changed files with 192 additions and 181 deletions.
12 changes: 6 additions & 6 deletions include/Pomdog/Network/HTTPClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ class POMDOG_EXPORT HTTPClient final {
~HTTPClient();

/// Sends an HTTP request and returns an HTTP response.
[[nodiscard]] std::shared_ptr<Error>
[[nodiscard]] std::unique_ptr<Error>
Do(const std::shared_ptr<HTTPRequest>& req);

/// Sends a GET request to the specified URL.
[[nodiscard]] std::tuple<Connection, std::shared_ptr<Error>>
[[nodiscard]] std::tuple<Connection, std::unique_ptr<Error>>
Get(const std::string& url,
std::function<void(std::unique_ptr<HTTPResponse>&&, const std::shared_ptr<Error>&)>&& callback);
std::function<void(std::unique_ptr<HTTPResponse>&&, const std::unique_ptr<Error>&)>&& callback);

/// Sends a POST request to the specified URL.
[[nodiscard]] std::tuple<Connection, std::shared_ptr<Error>>
[[nodiscard]] std::tuple<Connection, std::unique_ptr<Error>>
Post(const std::string& url,
const std::string& contentType,
std::vector<char>&& body,
std::function<void(std::unique_ptr<HTTPResponse>&&, const std::shared_ptr<Error>&)>&& callback);
std::function<void(std::unique_ptr<HTTPResponse>&&, const std::unique_ptr<Error>&)>&& callback);

/// Aborts the specified request immediately.
[[nodiscard]] std::shared_ptr<Error>
[[nodiscard]] std::unique_ptr<Error>
CancelRequest(const std::shared_ptr<HTTPRequest>& req);

private:
Expand Down
2 changes: 1 addition & 1 deletion include/Pomdog/Network/HTTPRequest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class POMDOG_EXPORT HTTPRequest final {
std::vector<char> Body;

/// Callback function that will be called on request completion or error caused
Delegate<void(std::unique_ptr<HTTPResponse>&&, const std::shared_ptr<Error>&)> OnCompleted;
Delegate<void(std::unique_ptr<HTTPResponse>&&, const std::unique_ptr<Error>&)> OnCompleted;

/// The method for the request
HTTPMethod Method;
Expand Down
4 changes: 2 additions & 2 deletions include/Pomdog/Network/IOService.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class POMDOG_EXPORT IOService final {
IOService(const IOService&) = delete;
IOService& operator=(const IOService&) = delete;

[[nodiscard]] std::shared_ptr<Error> Initialize();
[[nodiscard]] std::unique_ptr<Error> Initialize();

[[nodiscard]] std::shared_ptr<Error> Shutdown();
[[nodiscard]] std::unique_ptr<Error> Shutdown();

void Step();

Expand Down
10 changes: 5 additions & 5 deletions include/Pomdog/Network/TCPStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ class POMDOG_EXPORT TCPStream final {
~TCPStream();

/// Opens a TCP connection to a remote host.
[[nodiscard]] static std::tuple<TCPStream, std::shared_ptr<Error>>
[[nodiscard]] static std::tuple<TCPStream, std::unique_ptr<Error>>
Connect(IOService* service, std::string_view address);

/// Opens a TCP connection to a remote host.
[[nodiscard]] static std::tuple<TCPStream, std::shared_ptr<Error>>
[[nodiscard]] static std::tuple<TCPStream, std::unique_ptr<Error>>
Connect(IOService* service, std::string_view address, const Duration& timeout);

/// Closes the connection.
void Disconnect();

/// Writes data to the connection.
[[nodiscard]] std::shared_ptr<Error>
[[nodiscard]] std::unique_ptr<Error>
Write(const ArrayView<std::uint8_t const>& data);

/// @return True if the socket is connected to a remote host, false otherwise.
Expand All @@ -50,15 +50,15 @@ class POMDOG_EXPORT TCPStream final {

/// Sets a callback function that is called when a connection is successfully established.
[[nodiscard]] Connection
OnConnected(std::function<void(const std::shared_ptr<Error>&)>&& callback);
OnConnected(std::function<void(const std::unique_ptr<Error>&)>&& callback);

/// Sets a callback function that is called when a connection is disconnected.
[[nodiscard]] Connection
OnDisconnect(std::function<void()>&& callback);

/// Sets a callback function that is called when a data packet is received.
[[nodiscard]] Connection
OnRead(std::function<void(const ArrayView<std::uint8_t>&, const std::shared_ptr<Error>&)>&& callback);
OnRead(std::function<void(const ArrayView<std::uint8_t>&, const std::unique_ptr<Error>&)>&& callback);

private:
std::unique_ptr<Detail::NativeTCPStream> nativeStream;
Expand Down
10 changes: 5 additions & 5 deletions include/Pomdog/Network/TLSStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ class POMDOG_EXPORT TLSStream final {
~TLSStream();

/// Opens a TLS connection over TCP to a remote host.
[[nodiscard]] static std::tuple<TLSStream, std::shared_ptr<Error>>
[[nodiscard]] static std::tuple<TLSStream, std::unique_ptr<Error>>
Connect(IOService* service, std::string_view address);

/// Opens a TLS connection over TCP to a remote host.
[[nodiscard]] static std::tuple<TLSStream, std::shared_ptr<Error>>
[[nodiscard]] static std::tuple<TLSStream, std::unique_ptr<Error>>
Connect(IOService* service, std::string_view address, const Duration& timeout, const ArrayView<std::uint8_t const>& certPEM);

/// Closes the connection.
void Disconnect();

/// Writes data to the connection.
[[nodiscard]] std::shared_ptr<Error>
[[nodiscard]] std::unique_ptr<Error>
Write(const ArrayView<std::uint8_t const>& data);

/// @return True if the socket is connected to a remote host, false otherwise.
Expand All @@ -50,15 +50,15 @@ class POMDOG_EXPORT TLSStream final {

/// Sets a callback function that is called when a connection is successfully established.
[[nodiscard]] Connection
OnConnected(std::function<void(const std::shared_ptr<Error>&)>&& callback);
OnConnected(std::function<void(const std::unique_ptr<Error>&)>&& callback);

/// Sets a callback function that is called when a connection is disconnected.
[[nodiscard]] Connection
OnDisconnect(std::function<void()>&& callback);

/// Sets a callback function that is called when a data packet is received.
[[nodiscard]] Connection
OnRead(std::function<void(const ArrayView<std::uint8_t>&, const std::shared_ptr<Error>&)>&& callback);
OnRead(std::function<void(const ArrayView<std::uint8_t>&, const std::unique_ptr<Error>&)>&& callback);

private:
std::unique_ptr<Detail::NativeTLSStream> nativeStream;
Expand Down
14 changes: 7 additions & 7 deletions include/Pomdog/Network/UDPStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,34 @@ class POMDOG_EXPORT UDPStream final {
~UDPStream();

/// Opens a UDP connection to a remote host.
static std::tuple<UDPStream, std::shared_ptr<Error>>
static std::tuple<UDPStream, std::unique_ptr<Error>>
Connect(IOService* service, std::string_view address);

/// Starts listening for incoming datagrams.
static std::tuple<UDPStream, std::shared_ptr<Error>>
static std::tuple<UDPStream, std::unique_ptr<Error>>
Listen(IOService* service, std::string_view address);

/// Closes the connection.
void Disconnect();

/// Writes data to the connection.
std::shared_ptr<Error> Write(const ArrayView<std::uint8_t const>& data);
std::unique_ptr<Error> Write(const ArrayView<std::uint8_t const>& data);

/// Writes data to address.
std::shared_ptr<Error>
std::unique_ptr<Error>
WriteTo(const ArrayView<std::uint8_t const>& data, std::string_view address);

/// Sets a callback function that is called when a connection is successfully established.
[[nodiscard]] Connection
OnConnected(std::function<void(const std::shared_ptr<Error>&)>&& callback);
OnConnected(std::function<void(const std::unique_ptr<Error>&)>&& callback);

/// Sets a callback function that is called when a data packet is received.
[[nodiscard]] Connection
OnRead(std::function<void(const ArrayView<std::uint8_t>&, const std::shared_ptr<Error>&)>&& callback);
OnRead(std::function<void(const ArrayView<std::uint8_t>&, const std::unique_ptr<Error>&)>&& callback);

/// Sets a callback function that is called when a data packet is received from the connection.
[[nodiscard]] Connection
OnReadFrom(std::function<void(const ArrayView<std::uint8_t>&, std::string_view address, const std::shared_ptr<Error>&)>&& callback);
OnReadFrom(std::function<void(const ArrayView<std::uint8_t>&, std::string_view address, const std::unique_ptr<Error>&)>&& callback);

private:
std::unique_ptr<Detail::NativeUDPStream> nativeStream;
Expand Down
29 changes: 17 additions & 12 deletions src/Network.MbedTLS/TLSStreamMbedTLS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ TLSStreamMbedTLS::~TLSStreamMbedTLS()
mbedtls_entropy_free(&entropy);
}

std::shared_ptr<Error>
std::unique_ptr<Error>
TLSStreamMbedTLS::Connect(
std::string_view host,
std::string_view port,
Expand All @@ -100,17 +100,19 @@ TLSStreamMbedTLS::Connect(
reinterpret_cast<const unsigned char*>(pers),
std::strlen(pers)); ret != 0) {
auto err = Errors::New("mbedtls_ctr_drbg_seed failed, " + MbedTLSErrorToString(ret));
errorConn = service->ScheduleTask([this, err] {
this->OnConnected(std::move(err));
std::shared_ptr<Error> shared = err->Clone();
errorConn = service->ScheduleTask([this, err = std::move(shared)] {
this->OnConnected(err->Clone());
this->errorConn.Disconnect();
});
return err;
}

if (!certPEM.IsEmpty() && (certPEM.GetBack() != 0)) {
auto err = Errors::New("Certificates (PEM) must be null-terminated string");
errorConn = service->ScheduleTask([this, err] {
this->OnConnected(std::move(err));
std::shared_ptr<Error> shared = err->Clone();
errorConn = service->ScheduleTask([this, err = std::move(shared)] {
this->OnConnected(err->Clone());
this->errorConn.Disconnect();
});
return err;
Expand All @@ -122,8 +124,9 @@ TLSStreamMbedTLS::Connect(
reinterpret_cast<const unsigned char*>(certPEM.GetData()),
certPEM.GetSize()); ret < 0) {
auto err = Errors::New("mbedtls_x509_crt_parse failed, " + MbedTLSErrorToString(ret));
errorConn = service->ScheduleTask([this, err] {
this->OnConnected(std::move(err));
std::shared_ptr<Error> shared = err->Clone();
errorConn = service->ScheduleTask([this, err = std::move(shared)] {
this->OnConnected(err->Clone());
this->errorConn.Disconnect();
});
return err;
Expand Down Expand Up @@ -159,8 +162,9 @@ TLSStreamMbedTLS::Connect(
auto [fd, err] = ConnectSocketFunc(hostBuf, portBuf, SocketProtocol::TCP, connectTimeoutIn);

if (err != nullptr) {
errorConn = service->ScheduleTask([this, err = std::move(err)] {
this->OnConnected(std::move(err));
std::shared_ptr<Error> shared = std::move(err);
errorConn = service->ScheduleTask([this, err = std::move(shared)] {
this->OnConnected(err->Clone());
this->errorConn.Disconnect();
});
return;
Expand Down Expand Up @@ -250,8 +254,9 @@ TLSStreamMbedTLS::Connect(
buf.back() = 0;
#if defined(DEBUG) && !defined(NDEBUG)
auto e = Errors::New(std::string{"mbedtls_ssl_get_verify_result failed, "} + buf.data());
errorConn = service->ScheduleTask([this, err = std::move(e)] {
this->OnConnected(std::move(err));
std::shared_ptr<Error> shared = std::move(e);
errorConn = service->ScheduleTask([this, err = std::move(shared)] {
this->OnConnected(err->Clone());
this->errorConn.Disconnect();
});
#endif
Expand Down Expand Up @@ -283,7 +288,7 @@ void TLSStreamMbedTLS::Close()
mbedtls_ssl_close_notify(&ssl);
}

std::shared_ptr<Error>
std::unique_ptr<Error>
TLSStreamMbedTLS::Write(const ArrayView<std::uint8_t const>& data)
{
for (;;) {
Expand Down
8 changes: 4 additions & 4 deletions src/Network.MbedTLS/TLSStreamMbedTLS.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ class TLSStreamMbedTLS final {
~TLSStreamMbedTLS();

/// Opens a TCP connection over TCP to a remote host.
[[nodiscard]] std::shared_ptr<Error>
[[nodiscard]] std::unique_ptr<Error>
Connect(std::string_view host, std::string_view port, const Duration& timeout, const ArrayView<std::uint8_t const>& certPEM);

/// Closes the connection.
void Close();

/// Writes data to the connection.
[[nodiscard]] std::shared_ptr<Error>
[[nodiscard]] std::unique_ptr<Error>
Write(const ArrayView<std::uint8_t const>& data);

/// @return True if the socket is connected to a remote host, false otherwise.
Expand All @@ -58,13 +58,13 @@ class TLSStreamMbedTLS final {
void SetTimeout(const Duration& timeout);

/// Delegate that fires when a connection is successfully established.
Delegate<void(const std::shared_ptr<Error>&)> OnConnected;
Delegate<void(const std::unique_ptr<Error>&)> OnConnected;

/// Delegate that fires when a connection is disconnected.
Delegate<void()> OnDisconnect;

/// Delegate that fires when a data packet is received.
Delegate<void(const ArrayView<std::uint8_t>&, const std::shared_ptr<Error>&)> OnRead;
Delegate<void(const ArrayView<std::uint8_t>&, const std::unique_ptr<Error>&)> OnRead;

private:
void ReadEventLoop();
Expand Down
4 changes: 2 additions & 2 deletions src/Network.POSIX/SocketHelperPOSIX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct timeval ToTimeval(const Duration& d)

} // namespace

std::tuple<int, std::shared_ptr<Error>>
std::tuple<int, std::unique_ptr<Error>>
ConnectSocketPOSIX(
const std::string& host,
const std::string& port,
Expand Down Expand Up @@ -121,7 +121,7 @@ ConnectSocketPOSIX(
return std::make_tuple(descriptor, nullptr);
}

std::tuple<int, std::shared_ptr<Error>>
std::tuple<int, std::unique_ptr<Error>>
BindSocketPOSIX(
const std::string& host,
const std::string& port,
Expand Down
4 changes: 2 additions & 2 deletions src/Network.POSIX/SocketHelperPOSIX.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

namespace Pomdog::Detail {

[[nodiscard]] std::tuple<int, std::shared_ptr<Error>>
[[nodiscard]] std::tuple<int, std::unique_ptr<Error>>
ConnectSocketPOSIX(const std::string& host, const std::string& port, SocketProtocol protocol, const Duration& timeout);

[[nodiscard]] std::tuple<int, std::shared_ptr<Error>>
[[nodiscard]] std::tuple<int, std::unique_ptr<Error>>
BindSocketPOSIX(const std::string& host, const std::string& port, SocketProtocol protocol);

} // namespace Pomdog::Detail
9 changes: 5 additions & 4 deletions src/Network.POSIX/TCPStreamPOSIX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void TCPStreamPOSIX::Close()
}
}

std::shared_ptr<Error>
std::unique_ptr<Error>
TCPStreamPOSIX::Connect(std::string_view host, std::string_view port, const Duration& connectTimeout)
{
POMDOG_ASSERT(service != nullptr);
Expand All @@ -74,8 +74,9 @@ TCPStreamPOSIX::Connect(std::string_view host, std::string_view port, const Dura

if (err != nullptr) {
auto wrapped = Errors::Wrap(std::move(err), "couldn't connect to TCP socket on " + hostBuf + ":" + portBuf);
errorConn = service->ScheduleTask([this, err = std::move(wrapped)] {
this->OnConnected(std::move(err));
std::shared_ptr<Error> shared = std::move(wrapped);
errorConn = service->ScheduleTask([this, err = std::move(shared)] {
this->OnConnected(err->Clone());
this->errorConn.Disconnect();
});
return;
Expand All @@ -99,7 +100,7 @@ TCPStreamPOSIX::Connect(std::string_view host, std::string_view port, const Dura
return nullptr;
}

std::shared_ptr<Error>
std::unique_ptr<Error>
TCPStreamPOSIX::Write(const ArrayView<std::uint8_t const>& data)
{
POMDOG_ASSERT(isSocketValid(descriptor));
Expand Down
8 changes: 4 additions & 4 deletions src/Network.POSIX/TCPStreamPOSIX.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ class TCPStreamPOSIX final {
TCPStreamPOSIX& operator=(TCPStreamPOSIX&&) = delete;

/// Opens a TCP connection over TCP to a remote host.
[[nodiscard]] std::shared_ptr<Error>
[[nodiscard]] std::unique_ptr<Error>
Connect(std::string_view host, std::string_view port, const Duration& timeout);

/// Closes the connection.
void Close();

/// Writes data to the connection.
[[nodiscard]] std::shared_ptr<Error>
[[nodiscard]] std::unique_ptr<Error>
Write(const ArrayView<std::uint8_t const>& data);

/// @return True if the socket is connected to a remote host, false otherwise.
Expand All @@ -58,13 +58,13 @@ class TCPStreamPOSIX final {
[[nodiscard]] int GetHandle() const noexcept;

/// Delegate that fires when a connection is successfully established.
Delegate<void(const std::shared_ptr<Error>&)> OnConnected;
Delegate<void(const std::unique_ptr<Error>&)> OnConnected;

/// Delegate that fires when a connection is disconnected.
Delegate<void()> OnDisconnect;

/// Delegate that fires when a data packet is received.
Delegate<void(const ArrayView<std::uint8_t>&, const std::shared_ptr<Error>&)> OnRead;
Delegate<void(const ArrayView<std::uint8_t>&, const std::unique_ptr<Error>&)> OnRead;

private:
void ReadEventLoop();
Expand Down
Loading

0 comments on commit 1c50d81

Please sign in to comment.