10#ifndef NETWORK_CONTENT_H
11#define NETWORK_CONTENT_H
15#include <unordered_map>
16#include "../core/container_func.hpp"
34 virtual void OnConnect([[maybe_unused]]
bool success) {}
98 void OnReceiveData(std::unique_ptr<
char[]> data,
size_t length)
override;
108 static constexpr std::chrono::seconds
IDLE_TIMEOUT = std::chrono::seconds(60);
136 uint
Length()
const {
return (uint)this->infos.size(); }
156void ShowMissingContentWindow(
const struct GRFConfig *list);
Socket handler for the content server connection.
ConstContentIterator Begin() const
Get the begin of the content inf iterator.
void SelectUpgrade()
Select everything that's an update for something we've got.
void OnConnect(bool success) override
Callback for when the connection has finished.
void DownloadSelectedContentHTTP(const ContentIDList &content)
Initiate downloading the content over HTTP.
std::chrono::steady_clock::time_point lastActivity
The last time there was network activity.
void DownloadSelectedContent(uint &files, uint &bytes, bool fallback=false)
Actually begin downloading the content we selected.
bool isCancelled
Whether the download has been cancelled.
void ToggleSelectedState(const ContentInfo *ci)
Toggle the state of a content info and check its dependencies.
ClientNetworkContentSocketHandler()
Create a socket handler to handle the connection.
bool isConnecting
Whether we're connecting.
void OnDownloadProgress(const ContentInfo *ci, int bytes) override
We have progress in the download of a file.
void RemoveCallback(ContentCallback *cb)
Remove a callback.
static constexpr std::chrono::seconds IDLE_TIMEOUT
The idle timeout; when to close the connection because it's idle.
void OnReceiveData(std::unique_ptr< char[]> data, size_t length) override
We're receiving data.
void Select(ContentID cid)
Select a specific content id.
int http_response_index
Where we are, in the response, with handling it.
NetworkRecvStatus CloseConnection(bool error=true) override
Disconnect from the content server.
ContentIDList requested
ContentIDs we already requested (so we don't do it again)
void UnselectAll()
Unselect everything that we've not downloaded so far.
void RequestContentList(ContentType type)
Request the content list for the given type.
std::vector< char > http_response
The HTTP response to the requests we've been doing.
void ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const
Reverse lookup the dependencies of all parents over a given child.
void DownloadContentInfo(ContentID cid)
Download information of a given Content ID if not already tried.
bool Receive_SERVER_INFO(Packet &p) override
Server sending list of content info: uint8_t type (invalid ID == does not exist) uint32_t id uint32_t...
void SendReceive()
Check whether we received/can send some data from/to the content server and when that's the case hand...
void OnDownloadComplete(ContentID cid) override
We have finished downloading a file.
ContentVector infos
All content info we received.
void OnFailure() override
An error has occurred and the connection has been closed.
ContentInfo * GetContent(ContentID cid) const
Get the content info based on a ContentID.
void AddCallback(ContentCallback *cb)
Add a callback to this class.
bool Receive_SERVER_CONTENT(Packet &p) override
Server sending list of content info: uint32_t unique id uint32_t file size (0 == does not exist) stri...
void Connect()
Connect with the content server.
std::unordered_multimap< ContentID, ContentID > reverse_dependency_map
Content reverse dependency map.
bool BeforeDownload()
Handle the opening of the file before downloading.
void Clear()
Clear all downloaded content information.
ContentInfo * curInfo
Information about the currently downloaded file.
std::optional< FileHandle > curFile
Currently downloaded file.
void CheckDependencyState(ContentInfo *ci)
Check the dependencies (recursively) of this content info.
std::vector< ContentID > ContentIDList
List of content IDs to (possibly) select.
void AfterDownload()
Handle the closing and extracting of a file after downloading it has been done.
bool IsCancelled() const override
Check if there is a request to cancel the transfer.
std::vector< ContentCallback * > callbacks
Callbacks to notify "the world".
ConstContentIterator End() const
Get the end of the content inf iterator.
~ClientNetworkContentSocketHandler()
Clear up the mess ;)
void DownloadSelectedContentFallback(const ContentIDList &content)
Initiate downloading the content over the fallback protocol.
void Unselect(ContentID cid)
Unselect a specific content id.
void Cancel()
Cancel the current download.
void OnDisconnect() override
Callback for when the connection got disconnected.
ConstContentIterator Get(uint32_t index) const
Get the nth position of the content inf iterator.
void ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const
Reverse lookup the dependencies of (direct) parents over a given child.
void SelectAll()
Select everything we can select.
uint Length() const
Get the number of content items we know locally.
void OnReceiveContentInfo(const ContentInfo *ci) override
We received a content info.
Connect to the content server.
Base socket handler for all Content TCP sockets.
bool include(Container &container, typename Container::const_reference &item)
Helper function to append an item to a container if it is not already contained.
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Basic functions to send and receive HTTP packets.
ClientNetworkContentSocketHandler _network_content_client
The client we use to connect to the server.
void ShowNetworkContentListWindow(ContentVector *cv=nullptr, ContentType type1=CONTENT_TYPE_END, ContentType type2=CONTENT_TYPE_END)
Show the content list window with a given set of content.
std::vector< const ContentInfo * > ConstContentVector
Vector with constant content info.
std::vector< ContentInfo * > ContentVector
Vector with content info.
const ContentInfo *const * ConstContentIterator
Iterator for the constant content vector.
ContentInfo ** ContentIterator
Iterator for the content vector.
Callbacks for notifying others about incoming data.
virtual void OnDownloadProgress(const ContentInfo *ci, int bytes)
We have progress in the download of a file.
virtual void OnDisconnect()
Callback for when the connection got disconnected.
virtual void OnDownloadComplete(ContentID cid)
We have finished downloading a file.
virtual void OnConnect(bool success)
Callback for when the connection has finished.
virtual void OnReceiveContentInfo(const ContentInfo *ci)
We received a content info.
virtual ~ContentCallback()=default
Silentium.
Container for all important information about a piece of content.
Information about GRF, used in the game and (part of it) in savegames.
Callback for when the HTTP handler has something to tell us.
Internal entity of a packet.
Basic functions to receive and send TCP packets to/from the content server.
ContentType
The values in the enum are important; they are used as database 'keys'.
@ CONTENT_TYPE_END
Helper to mark the end of the types.
ContentID
Unique identifier for the content.