Skip to content

Commit

Permalink
netplay: pass compression algorithm to IClientConnection:s externally
Browse files Browse the repository at this point in the history
Make `IClientConnection` agnostic on the compression algorithm
in use.

Introduce a global singleton `WzCompressionProvider`, which
currently is only able to create `ZlibCompressionAdapter:s`.
Although, in the future we would want to pass extra configuration
info to it so that users can globally specify the compression
algorithm to use.

Signed-off-by: Pavel Solodovnikov <pavel.al.solodovnikov@gmail.com>
  • Loading branch information
ManManson authored and past-due committed Feb 27, 2025
1 parent 45b93a9 commit e96d22e
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 18 deletions.
9 changes: 6 additions & 3 deletions lib/netplay/client_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
#include "lib/netplay/pending_writes_manager.h"
#include "lib/netplay/polling_util.h"
#include "lib/netplay/wz_connection_provider.h"
#include "lib/netplay/zlib_compression_adapter.h"
#include "lib/netplay/wz_compression_provider.h"

IClientConnection::IClientConnection(WzConnectionProvider& connProvider)
IClientConnection::IClientConnection(WzConnectionProvider& connProvider, WzCompressionProvider& compressionProvider)
: selfConnList_({ this }),
connProvider_(&connProvider),
compressionProvider_(&compressionProvider),
readAllDescriptorSet_(connProvider_->newDescriptorSet(PollEventType::READABLE))
{}

Expand Down Expand Up @@ -233,9 +234,11 @@ void IClientConnection::enableCompression()
return; // Nothing to do.
}

ASSERT_OR_RETURN(, compressionProvider_ != nullptr, "Invalid compression provider");

PendingWritesManager::instance().executeUnderLock([this]
{
compressionAdapter_ = std::make_unique<ZlibCompressionAdapter>();
compressionAdapter_ = compressionProvider_->newCompressionAdapter();
const auto initRes = compressionAdapter_->initialize();
if (!initRes.has_value())
{
Expand Down
9 changes: 6 additions & 3 deletions lib/netplay/client_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
using nonstd::optional;

class IDescriptorSet;
class WzCompressionProvider;
class WzConnectionProvider;

/// <summary>
Expand Down Expand Up @@ -222,7 +223,7 @@ class IClientConnection
// disposed connections.
friend class PendingWritesManager;

IClientConnection(WzConnectionProvider& connProvider);
IClientConnection(WzConnectionProvider& connProvider, WzCompressionProvider& compressionProvider);
// Hide the destructor so that external code cannot accidentally
// `delete` the connection directly and has to use `close()` method
// to dispose of the connection object.
Expand All @@ -235,8 +236,10 @@ class IClientConnection
// (like `readAll()` and `connectionStatus()`) to avoid extra
// memory allocations.
const std::vector<IClientConnection*> selfConnList_;
// Connection provider used to create internal descriptor sets
WzConnectionProvider* connProvider_;
// Connection provider used to create internal descriptor sets.
WzConnectionProvider* connProvider_ = nullptr;
// Compression provider which is used to initialize compression algorithm in `enableCompression()`.
WzCompressionProvider* compressionProvider_ = nullptr;

private:

Expand Down
26 changes: 26 additions & 0 deletions lib/netplay/listen_socket.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: GPL-2.0-or-later

/*
This file is part of Warzone 2100.
Copyright (C) 2025 Warzone 2100 Project
Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "listen_socket.h"

IListenSocket::IListenSocket(WzCompressionProvider& compressionProvider)
: compressionProvider_(&compressionProvider)
{}
7 changes: 7 additions & 0 deletions lib/netplay/listen_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <type_traits>

class IClientConnection;
class WzCompressionProvider;

/// <summary>
/// Server-side listen socket abstraction.
Expand All @@ -45,4 +46,10 @@ class IListenSocket
/// </summary>
virtual IClientConnection* accept() = 0;
virtual IPVersionsMask supportedIpVersions() const = 0;

protected:

IListenSocket(WzCompressionProvider& compressionProvider);

WzCompressionProvider* compressionProvider_ = nullptr;
};
4 changes: 2 additions & 2 deletions lib/netplay/tcp/tcp_client_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
namespace tcp
{

TCPClientConnection::TCPClientConnection(WzConnectionProvider& connProvider, Socket* rawSocket)
: IClientConnection(connProvider),
TCPClientConnection::TCPClientConnection(WzConnectionProvider& connProvider, WzCompressionProvider& compressionProvider, Socket* rawSocket)
: IClientConnection(connProvider, compressionProvider),
socket_(rawSocket),
connStatusDescriptorSet_(connProvider_->newDescriptorSet(PollEventType::READABLE))
{
Expand Down
5 changes: 3 additions & 2 deletions lib/netplay/tcp/tcp_client_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "lib/netplay/descriptor_set.h"
#include "lib/netplay/tcp/netsocket.h" // for SOCKET

class WzCompressionProvider;
class WzConnectionProvider;

namespace tcp
Expand All @@ -34,7 +35,7 @@ class TCPClientConnection : public IClientConnection
{
public:

explicit TCPClientConnection(WzConnectionProvider& connProvider, Socket* rawSocket);
explicit TCPClientConnection(WzConnectionProvider& connProvider, WzCompressionProvider& compressionProvider, Socket* rawSocket);
virtual ~TCPClientConnection() override;

virtual net::result<ssize_t> sendImpl(const std::vector<uint8_t>& data) override;
Expand All @@ -55,7 +56,7 @@ class TCPClientConnection : public IClientConnection

friend class TCPConnectionPollGroup;

Socket* socket_;
Socket* socket_ = nullptr;

std::unique_ptr<IDescriptorSet> connStatusDescriptorSet_;
};
Expand Down
6 changes: 4 additions & 2 deletions lib/netplay/tcp/tcp_connection_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "lib/netplay/tcp/tcp_listen_socket.h"

#include "lib/netplay/open_connection_result.h"
#include "lib/netplay/wz_compression_provider.h"

#include "lib/framework/wzapp.h"

#ifdef WZ_OS_WIN
Expand Down Expand Up @@ -64,7 +66,7 @@ net::result<IListenSocket*> TCPConnectionProvider::openListenSocket(uint16_t por
{
return tl::make_unexpected(res.error());
}
return new TCPListenSocket(*this, res.value());
return new TCPListenSocket(*this, WzCompressionProvider::Instance(), res.value());
}

net::result<IClientConnection*> TCPConnectionProvider::openClientConnectionAny(const IConnectionAddress& addr, unsigned timeout)
Expand All @@ -81,7 +83,7 @@ net::result<IClientConnection*> TCPConnectionProvider::openClientConnectionAny(c
{
return tl::make_unexpected(res.error());
}
return new TCPClientConnection(*this, res.value());
return new TCPClientConnection(*this, WzCompressionProvider::Instance(), res.value());
}

IConnectionPollGroup* TCPConnectionProvider::newConnectionPollGroup()
Expand Down
7 changes: 4 additions & 3 deletions lib/netplay/tcp/tcp_listen_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
namespace tcp
{

TCPListenSocket::TCPListenSocket(WzConnectionProvider& connProvider, Socket* rawSocket)
: listenSocket_(rawSocket),
TCPListenSocket::TCPListenSocket(WzConnectionProvider& connProvider, WzCompressionProvider& compressionProvider, Socket* rawSocket)
: IListenSocket(compressionProvider),
listenSocket_(rawSocket),
connProvider_(&connProvider)
{}

Expand All @@ -51,7 +52,7 @@ IClientConnection* TCPListenSocket::accept()
{
return nullptr;
}
return new TCPClientConnection(*connProvider_, s);
return new TCPClientConnection(*connProvider_, *compressionProvider_, s);
}

IListenSocket::IPVersionsMask TCPListenSocket::supportedIpVersions() const
Expand Down
7 changes: 4 additions & 3 deletions lib/netplay/tcp/tcp_listen_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "lib/netplay/listen_socket.h"

class WzCompressionProvider;
class WzConnectionProvider;

namespace tcp
Expand All @@ -34,16 +35,16 @@ class TCPListenSocket : public IListenSocket
{
public:

explicit TCPListenSocket(WzConnectionProvider& connProvider, tcp::Socket* rawSocket);
explicit TCPListenSocket(WzConnectionProvider& connProvider, WzCompressionProvider& compressionProvider, tcp::Socket* rawSocket);
virtual ~TCPListenSocket() override;

virtual IClientConnection* accept() override;
virtual IPVersionsMask supportedIpVersions() const override;

private:

tcp::Socket* listenSocket_;
WzConnectionProvider* connProvider_;
tcp::Socket* listenSocket_ = nullptr;
WzConnectionProvider* connProvider_ = nullptr;
};

} // namespace tcp
39 changes: 39 additions & 0 deletions lib/netplay/wz_compression_provider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: GPL-2.0-or-later

/*
This file is part of Warzone 2100.
Copyright (C) 2025 Warzone 2100 Project
Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "wz_compression_provider.h"

#include "lib/netplay/zlib_compression_adapter.h"

WzCompressionProvider& WzCompressionProvider::Instance()
{
static WzCompressionProvider instance;
return instance;
}

std::unique_ptr<ICompressionAdapter> WzCompressionProvider::newCompressionAdapter()
{
// Only support Zlib for the time being.
// TODO: in the future, we might want to support more compression algorithms, e.g. Zstd.
// In this case, we would need to somehow configure `WzCompressionAdapter` from
// the global WZ config to specify compression algorithm to use.
return std::make_unique<ZlibCompressionAdapter>();
}
46 changes: 46 additions & 0 deletions lib/netplay/wz_compression_provider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: GPL-2.0-or-later

/*
This file is part of Warzone 2100.
Copyright (C) 2025 Warzone 2100 Project
Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#pragma once

#include <memory>

class ICompressionAdapter;

/// <summary>
/// This class provides is responsible for creating `ICompressionAdapter:s`,
/// which are thin wrappers over some compression algorithm, intended for
/// use in `IClientConnection` to provide compression over raw net messages.
/// </summary>
class WzCompressionProvider
{
public:

static WzCompressionProvider& Instance();

std::unique_ptr<ICompressionAdapter> newCompressionAdapter();

private:

WzCompressionProvider() = default;
WzCompressionProvider(const WzCompressionProvider&) = delete;
WzCompressionProvider(WzCompressionProvider&&) = delete;
};

0 comments on commit e96d22e

Please sign in to comment.