Skip to content

Commit

Permalink
Refactor ConnectionInfo in C++
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardnormier committed Nov 25, 2024
1 parent f439313 commit eab9f52
Show file tree
Hide file tree
Showing 34 changed files with 499 additions and 200 deletions.
148 changes: 128 additions & 20 deletions cpp/include/Ice/Connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
#include <map>
#include <optional>

#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wshadow-field-in-constructor"
#elif defined(__GNUC__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wshadow"
#endif

namespace Ice
{
/**
Expand Down Expand Up @@ -206,7 +214,6 @@ namespace Ice
class ICE_API ConnectionInfo
{
public:
ConnectionInfo() = default;
virtual ~ConnectionInfo();

// Deleted to prevent accidental slicing.
Expand All @@ -216,19 +223,38 @@ namespace Ice
/**
* The information of the underlying transport or null if there's no underlying transport.
*/
ConnectionInfoPtr underlying;
const ConnectionInfoPtr underlying;

/**
* Whether or not the connection is an incoming or outgoing connection.
*/
bool incoming;
const bool incoming;

/**
* The name of the adapter associated with the connection.
*/
std::string adapterName;
const std::string adapterName;

/**
* The connection id.
*/
std::string connectionId;
const std::string connectionId;

protected:
explicit ConnectionInfo(ConnectionInfoPtr underlyingInfo)
: underlying{std::move(underlyingInfo)},
incoming{underlying->incoming},
adapterName{underlying->adapterName},
connectionId{underlying->connectionId}
{
}

ConnectionInfo(bool incoming, std::string adapterName, std::string connectionId)
: incoming{incoming},
adapterName{std::move(adapterName)},
connectionId{std::move(connectionId)}
{
}
};

/**
Expand All @@ -238,26 +264,45 @@ namespace Ice
class ICE_API IPConnectionInfo : public ConnectionInfo
{
public:
IPConnectionInfo() = default;
IPConnectionInfo(const IPConnectionInfo&) = delete;
IPConnectionInfo& operator=(const IPConnectionInfo&) = delete;

/**
* The local address.
*/
std::string localAddress;
const std::string localAddress;

/**
* The local port.
*/
int localPort = -1;
const int localPort;

/**
* The remote address.
*/
std::string remoteAddress;
const std::string remoteAddress;

/**
* The remote port.
*/
int remotePort = -1;
const int remotePort;

protected:
IPConnectionInfo(
bool incoming,
std::string adapterName,
std::string connectionId,
std::string localAddress,
int localPort,
std::string remoteAddress,
int remotePort)
: ConnectionInfo{incoming, std::move(adapterName), std::move(connectionId)},
localAddress{std::move(localAddress)},
localPort{localPort},
remoteAddress{std::move(remoteAddress)},
remotePort{remotePort}
{
}
};

/**
Expand All @@ -267,19 +312,41 @@ namespace Ice
class ICE_API TCPConnectionInfo final : public IPConnectionInfo
{
public:
TCPConnectionInfo() = default;
~TCPConnectionInfo() final;
TCPConnectionInfo(const TCPConnectionInfo&) = delete;
TCPConnectionInfo& operator=(const TCPConnectionInfo&) = delete;

/**
* The connection buffer receive size.
*/
int rcvSize = 0;
const int rcvSize;
/**
* The connection buffer send size.
*/
int sndSize = 0;
const int sndSize;

// internal constructor
TCPConnectionInfo(
bool incoming,
std::string adapterName,
std::string connectionId,
std::string localAddress,
int localPort,
std::string remoteAddress,
int remotePort,
int rcvSize,
int sndSize)
: IPConnectionInfo{incoming, std::move(adapterName), std::move(connectionId), std::move(localAddress), localPort, std::move(remoteAddress), remotePort},
rcvSize{rcvSize},
sndSize{sndSize}
{
}

// internal constructor
TCPConnectionInfo(bool incoming, std::string adapterName, std::string connectionId)
: TCPConnectionInfo{incoming, std::move(adapterName), std::move(connectionId), "", -1, "", -1, 0, 0}
{
}
};

/**
Expand All @@ -289,27 +356,56 @@ namespace Ice
class ICE_API UDPConnectionInfo final : public IPConnectionInfo
{
public:
UDPConnectionInfo() = default;
~UDPConnectionInfo() final;
UDPConnectionInfo(const UDPConnectionInfo&) = delete;
UDPConnectionInfo& operator=(const UDPConnectionInfo&) = delete;

/**
* The multicast address.
*/
std::string mcastAddress;
const std::string mcastAddress;

/**
* The multicast port.
*/
int mcastPort = -1;
const int mcastPort;

/**
* The connection buffer receive size.
*/
int rcvSize = 0;
const int rcvSize;

/**
* The connection buffer send size.
*/
int sndSize = 0;
const int sndSize;

// internal constructor
UDPConnectionInfo(
bool incoming,
std::string adapterName,
std::string connectionId,
std::string localAddress,
int localPort,
std::string remoteAddress,
int remotePort,
std::string mcastAddress,
int mcastPort,
int rcvSize,
int sndSize)
: IPConnectionInfo{incoming, std::move(adapterName), std::move(connectionId), std::move(localAddress), localPort, std::move(remoteAddress), remotePort},
mcastAddress{std::move(mcastAddress)},
mcastPort{mcastPort},
rcvSize{rcvSize},
sndSize{sndSize}
{
}

// internal constructor
UDPConnectionInfo(bool incoming, std::string adapterName, std::string connectionId)
: UDPConnectionInfo{incoming, std::move(adapterName), std::move(connectionId), "", -1, "", -1, "", -1, 0, 0}
{
}
};

/**
Expand All @@ -319,16 +415,28 @@ namespace Ice
class ICE_API WSConnectionInfo final : public ConnectionInfo
{
public:
WSConnectionInfo() = default;
~WSConnectionInfo() final;
WSConnectionInfo(const WSConnectionInfo&) = delete;
WSConnectionInfo& operator=(const WSConnectionInfo&) = delete;

/**
* The headers from the HTTP upgrade request.
*/
HeaderDict headers;
const HeaderDict headers;

// internal constructor
WSConnectionInfo(ConnectionInfoPtr underlying, HeaderDict headers)
: ConnectionInfo{std::move(underlying)},
headers{std::move(headers)}
{
}
};
}

#if defined(__clang__)
# pragma clang diagnostic pop
#elif defined(__GNUC__)
# pragma GCC diagnostic pop
#endif

#endif
12 changes: 8 additions & 4 deletions cpp/include/Ice/Endpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ namespace Ice
const bool compress;

protected:
explicit EndpointInfo(EndpointInfoPtr underlyingEndpoint)
: underlying(std::move(underlyingEndpoint)),
explicit EndpointInfo(EndpointInfoPtr underlyingInfo)
: underlying(std::move(underlyingInfo)),
timeout(underlying->timeout),
compress(underlying->compress)
{
Expand Down Expand Up @@ -226,7 +226,8 @@ namespace Ice

// internal constructor
WSEndpointInfo(EndpointInfoPtr underlying, std::string resource)
: EndpointInfo{std::move(underlying)}, resource{std::move(resource)}
: EndpointInfo{std::move(underlying)},
resource{std::move(resource)}
{
}
};
Expand Down Expand Up @@ -255,7 +256,10 @@ namespace Ice

// internal constructor
OpaqueEndpointInfo(std::int16_t type, Ice::EncodingVersion rawEncoding, std::vector<std::byte> rawBytes)
: EndpointInfo{-1, false}, rawEncoding{rawEncoding}, rawBytes{std::move(rawBytes)}, _type{type}
: EndpointInfo{-1, false},
rawEncoding{rawEncoding},
rawBytes{std::move(rawBytes)},
_type{type}
{
}

Expand Down
44 changes: 38 additions & 6 deletions cpp/include/Ice/SSL/ConnectionInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
#include "ConnectionInfoF.h"
#include "Ice/Connection.h"

#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wshadow-field-in-constructor"
#elif defined(__GNUC__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wshadow"
#endif

// This file defines the `XxxConnectionInfo` class for each platform-specific SSL implementation. The
// `#if defined(ICE_USE_XXX)/#endif` directives are used to include the appropriate structure based on the platform. We
// avoid using `#elif` directives because, we want to define all the classes when building the doxygen documentation.
Expand All @@ -28,15 +36,21 @@ namespace Ice::SSL
class ICE_API SchannelConnectionInfo final : public Ice::ConnectionInfo
{
public:
SchannelConnectionInfo() = default;
~SchannelConnectionInfo() final;
SchannelConnectionInfo(const SchannelConnectionInfo&) = delete;
SchannelConnectionInfo& operator=(const SchannelConnectionInfo&) = delete;

/**
* The peer certificate.
*/
PCCERT_CONTEXT peerCertificate = nullptr;
const PCCERT_CONTEXT peerCertificate;

// internal constructor
SchannelConnectionInfo(Ice::ConnectionInfoPtr underlying, PCCERT_CONTEXT peerCertificate)
: ConnectionInfo{std::move(underlying)},
peerCertificate{peerCertificate}
{
}
};
#endif

Expand All @@ -53,15 +67,21 @@ namespace Ice::SSL
class ICE_API SecureTransportConnectionInfo final : public Ice::ConnectionInfo
{
public:
SecureTransportConnectionInfo() = default;
~SecureTransportConnectionInfo() final;
SecureTransportConnectionInfo(const SecureTransportConnectionInfo&) = delete;
SecureTransportConnectionInfo& operator=(const SecureTransportConnectionInfo&) = delete;

/**
* The peer certificate.
*/
SecCertificateRef peerCertificate = nullptr;
const SecCertificateRef peerCertificate;

// internal constructor
SecureTransportConnectionInfo(Ice::ConnectionInfoPtr underlying, SecCertificateRef peerCertificate)
: ConnectionInfo{std::move(underlying)},
peerCertificate{peerCertificate}
{
}
};
#endif

Expand All @@ -78,17 +98,29 @@ namespace Ice::SSL
class ICE_API OpenSSLConnectionInfo final : public Ice::ConnectionInfo
{
public:
OpenSSLConnectionInfo() = default;
~OpenSSLConnectionInfo() final;
OpenSSLConnectionInfo(const OpenSSLConnectionInfo&) = delete;
OpenSSLConnectionInfo& operator=(const OpenSSLConnectionInfo&) = delete;

/**
* The peer certificate.
*/
X509* peerCertificate = nullptr;
X509* const peerCertificate;

// internal constructor
OpenSSLConnectionInfo(Ice::ConnectionInfoPtr underlying, X509* peerCertificate)
: ConnectionInfo{std::move(underlying)},
peerCertificate{peerCertificate}
{
}
};
#endif
}

#if defined(__clang__)
# pragma clang diagnostic pop
#elif defined(__GNUC__)
# pragma GCC diagnostic pop
#endif

#endif
5 changes: 1 addition & 4 deletions cpp/include/Ice/SSL/EndpointInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ namespace Ice::SSL
EndpointInfo& operator=(const EndpointInfo&) = delete;

// internal constructor
explicit EndpointInfo(Ice::EndpointInfoPtr underlying)
: Ice::EndpointInfo{std::move(underlying)}
{
}
explicit EndpointInfo(Ice::EndpointInfoPtr underlying) : Ice::EndpointInfo{std::move(underlying)} {}
};

using EndpointInfoPtr = std::shared_ptr<EndpointInfo>;
Expand Down
Loading

0 comments on commit eab9f52

Please sign in to comment.