OpenTTD Source: ClientNetworkContentSocketHandler Class Reference

OpenTTD Source 1.11.0-RC1

Public Member Functions | Static Public Attributes | Protected Types | Protected Member Functions | Protected Attributes | Friends
ClientNetworkContentSocketHandler Class Reference

Socket handler for the content server connection. More...

`#include <network_content.h>`

Inheritance diagram for ClientNetworkContentSocketHandler:

## Public Member Functions
| ClientNetworkContentSocketHandler ()
| Create a socket handler to handle the connection.
| ~ClientNetworkContentSocketHandler ()
| Clear up the mess ;)
void | Connect ()
| Connect with the content server.
void | SendReceive ()
| Check whether we received/can send some data from/to the content server and when that's the case handle it appropriately.
void | Close () override
| Disconnect from the content server.
void | RequestContentList (ContentType type)
| Request the content list for the given type. More...
void | RequestContentList (uint count, const ContentID *content_ids)
| Request the content list for a given number of content IDs. More...
void | RequestContentList (ContentVector *cv, bool send_md5sum=true)
| Request the content list for a list of content. More...
void | DownloadSelectedContent (uint &files, uint &bytes, bool fallback=false)
| Actually begin downloading the content we selected. More...
void | Select (ContentID cid)
| Select a specific content id. More...
void | Unselect (ContentID cid)
| Unselect a specific content id. More...
void | SelectAll ()
| Select everything we can select.
void | SelectUpgrade ()
| Select everything that's an update for something we've got.
void | UnselectAll ()
| Unselect everything that we've not downloaded so far.
void | ToggleSelectedState (const ContentInfo *ci)
| Toggle the state of a content info and check its dependencies.
void | ReverseLookupDependency (ConstContentVector &parents, const ContentInfo *child) const
| Reverse lookup the dependencies of (direct) parents over a given child. More...
void | ReverseLookupTreeDependency (ConstContentVector &tree, const ContentInfo *child) const
| Reverse lookup the dependencies of all parents over a given child. More...
void | CheckDependencyState (ContentInfo *ci)
| Check the dependencies (recursively) of this content info. More...
uint | Length () const
| Get the number of content items we know locally.
ConstContentIterator | Begin () const
| Get the begin of the content inf iterator.
ConstContentIterator | Get (uint32 index) const
| Get the nth position of the content inf iterator.
ConstContentIterator | End () const
| Get the end of the content inf iterator.
void | Clear ()
| Clear all downloaded content information.
void | AddCallback (ContentCallback *cb)
| Add a callback to this class.
void | RemoveCallback (ContentCallback *cb)
| Remove a callback.
Public Member Functions inherited from NetworkContentSocketHandler
| NetworkContentSocketHandler (SOCKET s=INVALID_SOCKET, const NetworkAddress &address=NetworkAddress())
| Create a new cs socket handler for a given cs. More...
virtual | ~NetworkContentSocketHandler ()
| On destructing of this class, the socket needs to be closed.
bool | ReceivePackets ()
| Receive a packet at TCP level. More...
Public Member Functions inherited from NetworkTCPSocketHandler
bool | IsConnected () const
| Whether this socket is currently bound to a socket. More...
NetworkRecvStatus | CloseConnection (bool error=true) override
| Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just means the packet has to be dropped. More...
virtual void | SendPacket (Packet *packet)
| This function puts the packet in the send-queue and it is send as soon as possible. More...
SendPacketsState | SendPackets (bool closing_down=false)
| Sends all the buffered packets out for this client. More...
virtual Packet * | ReceivePacket ()
| Receives a packet for the given client. More...
bool | CanSendReceive ()
| Check whether this socket can send or receive something. More...
bool | HasSendQueue ()
| Whether there is something pending in the send queue. More...
| NetworkTCPSocketHandler (SOCKET s=INVALID_SOCKET)
| Construct a socket handler for a TCP connection. More...
Public Member Functions inherited from NetworkSocketHandler
| NetworkSocketHandler ()
| Create a new unbound socket.
virtual | ~NetworkSocketHandler ()
| Close the socket when destructing the socket handler.
bool | HasClientQuit () const
| Whether the current client connected to the socket has quit. More...
void | Reopen ()
| Reopen the socket so we can send/receive stuff again.
void | SendGRFIdentifier (Packet *p, const GRFIdentifier *grf)
| Serializes the GRFIdentifier (GRF ID and MD5 checksum) to the packet. More...
void | ReceiveGRFIdentifier (Packet *p, GRFIdentifier *grf)
| Deserializes the GRFIdentifier (GRF ID and MD5 checksum) from the packet. More...
void | SendCompanyInformation (Packet *p, const struct Company *c, const struct NetworkCompanyStats *stats, uint max_len=NETWORK_COMPANY_NAME_LENGTH)
| Package some generic company information into a packet. More...

## Static Public Attributes
static constexpr std::chrono::seconds | IDLE_TIMEOUT = std::chrono::seconds(60)
| The idle timeout; when to close the connection because it's idle.

## Protected Types
typedef std::vector< ContentID > | ContentIDList
| List of content IDs to (possibly) select.

## Protected Member Functions
bool | Receive_SERVER_INFO (Packet *p) override
| Server sending list of content info: byte type (invalid ID == does not exist) uint32 id uint32 file_size string name (max 32 characters) string version (max 16 characters) uint32 unique id uint8 md5sum (16 bytes) uint8 dependency count uint32 unique id of dependency (dependency count times) uint8 tag count string tag (max 32 characters for tag count times) More...
bool | Receive_SERVER_CONTENT (Packet *p) override
| Server sending list of content info: uint32 unique id uint32 file size (0 == does not exist) string file name (max 48 characters) After this initial packet, packets with the actual data are send using the same packet type. More...
ContentInfo * | GetContent (ContentID cid)
| Get the content info based on a ContentID. More...
void | DownloadContentInfo (ContentID cid)
| Download information of a given Content ID if not already tried. More...
void | OnConnect (bool success) override
| Callback for when the connection has finished. More...
void | OnDisconnect () override
| Callback for when the connection got disconnected.
void | OnReceiveContentInfo (const ContentInfo *ci) override
| We received a content info. More...
void | OnDownloadProgress (const ContentInfo *ci, int bytes) override
| We have progress in the download of a file. More...
void | OnDownloadComplete (ContentID cid) override
| We have finished downloading a file. More...
void | OnFailure () override
| An error has occurred and the connection has been closed. More...
void | OnReceiveData (const char *data, size_t length) override
| We're receiving data. More...
bool | BeforeDownload ()
| Handle the opening of the file before downloading. More...
void | AfterDownload ()
| Handle the closing and extracting of a file after downloading it has been done.
void | DownloadSelectedContentHTTP (const ContentIDList &content)
| Initiate downloading the content over HTTP. More...
void | DownloadSelectedContentFallback (const ContentIDList &content)
| Initiate downloading the content over the fallback protocol. More...
Protected Member Functions inherited from NetworkContentSocketHandler
bool | ReceiveInvalidPacket (PacketContentType type)
| Helper for logging receiving invalid packets. More...
virtual bool | Receive_CLIENT_INFO_LIST (Packet *p)
| Client requesting a list of content info: byte type uint32 openttd version. More...
virtual bool | Receive_CLIENT_INFO_ID (Packet *p)
| Client requesting a list of content info: uint16 count of ids uint32 id (count times) More...
virtual bool | Receive_CLIENT_INFO_EXTID (Packet *p)
| Client requesting a list of content info based on an external 'unique' id; GRF ID for NewGRFS, shortname and for base graphics and AIs. More...
virtual bool | Receive_CLIENT_INFO_EXTID_MD5 (Packet *p)
| Client requesting a list of content info based on an external 'unique' id; GRF ID + MD5 checksum for NewGRFS, shortname and xor-ed MD5 checksums for base graphics and AIs. More...
virtual bool | Receive_CLIENT_CONTENT (Packet *p)
| Client requesting the actual content: uint16 count of unique ids uint32 unique id (count times) More...
bool | HandlePacket (Packet *p)
| Handle the given packet, i.e. More...

## Protected Attributes
std::vector< ContentCallback * > | callbacks
| Callbacks to notify "the world".
ContentIDList | requested
| ContentIDs we already requested (so we don't do it again)
ContentVector | infos
| All content info we received.
std::vector< char > | http_response
| The HTTP response to the requests we've been doing.
int | http_response_index
| Where we are, in the response, with handling it.
FILE * | curFile
| Currently downloaded file.
ContentInfo * | curInfo
| Information about the currently downloaded file.
bool | isConnecting
| Whether we're connecting.
std::chrono::steady_clock::time_point | lastActivity
| The last time there was network activity.
Protected Attributes inherited from NetworkContentSocketHandler
NetworkAddress | client_addr
| The address we're connected to.

## Friends
class | NetworkContentConnecter

## Additional Inherited Members
Data Fields inherited from NetworkTCPSocketHandler
SOCKET | sock
| The socket currently connected to.
bool | writable
| Can we write to this socket?
Private Member Functions inherited from ContentCallback
virtual | ~ContentCallback ()
| Silentium.
Private Member Functions inherited from HTTPCallback
virtual | ~HTTPCallback ()
| Silentium.

## Detailed Description

Socket handler for the content server connection.

Definition at line 65 of file network_content.h.

## Member Function Documentation

## ◆ BeforeDownload()

bool ClientNetworkContentSocketHandler::BeforeDownload | ( |  | ) |
| protected

Handle the opening of the file before downloading.

Returns
  false on any error.

Definition at line 502 of file network_content.cpp.

References curInfo, ContentInfo::filesize, GetFullFilename(), and ContentInfo::IsValid().

Referenced by Receive_SERVER_CONTENT().

## ◆ CheckDependencyState()

void ClientNetworkContentSocketHandler::CheckDependencyState | ( | ContentInfo * | ci | ) |

Check the dependencies (recursively) of this content info.

Parameters

  ci | the content info to check the dependencies of

Definition at line 945 of file network_content.cpp.

References ContentInfo::ALREADY_HERE, ContentInfo::AUTOSELECTED, ContentInfo::dependencies, ContentInfo::dependency_count, DownloadContentInfo(), GetContent(), ContentInfo::id, ContentInfo::IsSelected(), ReverseLookupDependency(), ReverseLookupTreeDependency(), ContentInfo::SELECTED, ContentInfo::state, Unselect(), and ContentInfo::UNSELECTED.

Referenced by Receive_SERVER_INFO(), Select(), SelectAll(), SelectUpgrade(), and Unselect().

## ◆ DownloadContentInfo()

void ClientNetworkContentSocketHandler::DownloadContentInfo | ( | ContentID | cid | ) |
| protected

Download information of a given Content ID if not already tried.

Parameters

  cid | the ID to try

Definition at line 802 of file network_content.cpp.

References RequestContentList(), and requested.

Referenced by CheckDependencyState().

## ◆ DownloadSelectedContent()

void ClientNetworkContentSocketHandler::DownloadSelectedContent | ( | uint & | files,
|  | uint & | bytes,
|  | bool | fallback = `false`
| ) |  |

Actually begin downloading the content we selected.

Parameters

  [out] | files | The number of files we are going to download.
  [out] | bytes | The number of bytes we are going to download.
  | fallback | Whether to use the fallback or not.

Definition at line 292 of file network_content.cpp.

References ContentInfo::ALREADY_HERE, and infos.

Referenced by OnFailure().

## ◆ DownloadSelectedContentFallback()

void ClientNetworkContentSocketHandler::DownloadSelectedContentFallback | ( | const ContentIDList & | content | ) |
| protected

Initiate downloading the content over the fallback protocol.

Parameters

  content | The content to download.

Definition at line 355 of file network_content.cpp.

## ◆ DownloadSelectedContentHTTP()

void ClientNetworkContentSocketHandler::DownloadSelectedContentHTTP | ( | const ContentIDList & | content | ) |
| protected

Initiate downloading the content over HTTP.

Parameters

  content | The content to download.

Definition at line 327 of file network_content.cpp.

## ◆ GetContent()

ContentInfo * ClientNetworkContentSocketHandler::GetContent | ( | ContentID | cid | ) |
| protected

Get the content info based on a ContentID.

Parameters

  cid | the ContentID to search for

Returns
  the ContentInfo or nullptr if not found

Definition at line 816 of file network_content.cpp.

References infos.

Referenced by CheckDependencyState(), OnDownloadComplete(), Select(), and Unselect().

## ◆ OnConnect()

void ClientNetworkContentSocketHandler::OnConnect | ( | bool | success | ) |
| overrideprotectedvirtual

Callback for when the connection has finished.

Parameters

  success | whether the connection was made or that we failed to make it

Reimplemented from ContentCallback.

Definition at line 1043 of file network_content.cpp.

References callbacks, and ContentCallback::OnConnect().

Referenced by NetworkContentConnecter::OnConnect(), and NetworkContentConnecter::OnFailure().

## ◆ OnDownloadComplete()

void ClientNetworkContentSocketHandler::OnDownloadComplete | ( | ContentID | cid | ) |
| overrideprotectedvirtual

We have finished downloading a file.

Parameters

  cid | the ContentID of the downloaded file

Reimplemented from ContentCallback.

Definition at line 1081 of file network_content.cpp.

References ContentInfo::ALREADY_HERE, callbacks, GetContent(), ContentCallback::OnDownloadComplete(), and ContentInfo::state.

## ◆ OnDownloadProgress()

void ClientNetworkContentSocketHandler::OnDownloadProgress | ( | const ContentInfo * | ci,
|  | int | bytes
| ) |  |
| overrideprotectedvirtual

We have progress in the download of a file.

Parameters

  ci | the content info of the file
  bytes | the number of bytes downloaded since the previous call

Reimplemented from ContentCallback.

Definition at line 1072 of file network_content.cpp.

References callbacks, and ContentCallback::OnDownloadProgress().

Referenced by OnFailure(), OnReceiveData(), and Receive_SERVER_CONTENT().

## ◆ OnFailure()

void ClientNetworkContentSocketHandler::OnFailure | ( |  | ) |
| overrideprotectedvirtual

An error has occurred and the connection has been closed.

Note
  HTTP socket handler is closed/freed.

Implements HTTPCallback.

Definition at line 561 of file network_content.cpp.

References curFile, curInfo, DownloadSelectedContent(), http_response, http_response_index, and OnDownloadProgress().

Referenced by OnReceiveData().

## ◆ OnReceiveContentInfo()

void ClientNetworkContentSocketHandler::OnReceiveContentInfo | ( | const ContentInfo * | ci | ) |
| overrideprotectedvirtual

We received a content info.

Parameters

  ci | the content info

Reimplemented from ContentCallback.

Definition at line 1062 of file network_content.cpp.

References callbacks, and ContentCallback::OnReceiveContentInfo().

Referenced by Receive_SERVER_INFO().

## ◆ OnReceiveData()

void ClientNetworkContentSocketHandler::OnReceiveData | ( | const char * | data,
|  | size_t | length
| ) |  |
| overrideprotectedvirtual

We're receiving data.

Parameters

  data | the received data, nullptr when all data has been received.
  length | the amount of received data, 0 when all data has been received.

Note
  When nullptr is sent the HTTP socket handler is closed/freed.

Check p for not being null and return calling OnFailure if that's not the case.

Check p for not being null and then terminate, or return calling OnFailure.

Implements HTTPCallback.

Definition at line 581 of file network_content.cpp.

References AfterDownload(), curFile, curInfo, http_response, http_response_index, OnDownloadProgress(), and OnFailure().

## ◆ Receive_SERVER_CONTENT()

bool ClientNetworkContentSocketHandler::Receive_SERVER_CONTENT | ( | Packet * | p | ) |
| overrideprotectedvirtual

Server sending list of content info: uint32 unique id uint32 file size (0 == does not exist) string file name (max 48 characters) After this initial packet, packets with the actual data are send using the same packet type.

Parameters

  p | The packet that was just received.

Returns
  True upon success, otherwise false.

Reimplemented from NetworkContentSocketHandler.

Definition at line 462 of file network_content.cpp.

References AfterDownload(), BeforeDownload(), Packet::buffer, Close(), curFile, curInfo, DeleteWindowById(), ContentInfo::filename, ContentInfo::filesize, ContentInfo::id, lengthof, OnDownloadProgress(), Packet::pos, Packet::Recv_string(), Packet::Recv_uint32(), Packet::Recv_uint8(), ShowErrorMessage(), Packet::size, ContentInfo::type, WC_NETWORK_STATUS_WINDOW, WL_ERROR, and WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD.

## ◆ Receive_SERVER_INFO()

bool ClientNetworkContentSocketHandler::Receive_SERVER_INFO | ( | Packet * | p | ) |
| overrideprotectedvirtual

Server sending list of content info: byte type (invalid ID == does not exist) uint32 id uint32 file_size string name (max 32 characters) string version (max 16 characters) uint32 unique id uint8 md5sum (16 bytes) uint8 dependency count uint32 unique id of dependency (dependency count times) uint8 tag count string tag (max 32 characters for tag count times)

Parameters

  p | The packet that was just received.

Returns
  True upon success, otherwise false.

Reimplemented from NetworkContentSocketHandler.

Definition at line 52 of file network_content.cpp.

References ContentInfo::ALREADY_HERE, CheckDependencyState(), Close(), CONTENT_TYPE_AI, CONTENT_TYPE_AI_LIBRARY, CONTENT_TYPE_BASE_GRAPHICS, CONTENT_TYPE_BASE_MUSIC, CONTENT_TYPE_BASE_SOUNDS, CONTENT_TYPE_GAME, CONTENT_TYPE_GAME_LIBRARY, CONTENT_TYPE_HEIGHTMAP, CONTENT_TYPE_NEWGRF, CONTENT_TYPE_SCENARIO, ContentInfo::dependencies, ContentInfo::dependency_count, ContentInfo::description, ContentInfo::DOES_NOT_EXIST, ContentInfo::filesize, AI::HasAI(), Game::HasGame(), HasGRFConfig(), HasScenario(), BaseMedia< GraphicsSet >::HasSet(), BaseMedia< MusicSet >::HasSet(), BaseMedia< SoundsSet >::HasSet(), ContentInfo::id, infos, ContentInfo::IsValid(), lastof, lengthof, ContentInfo::md5sum, ContentInfo::name, OnReceiveContentInfo(), Packet::Recv_string(), Packet::Recv_uint32(), Packet::Recv_uint8(), ContentInfo::state, strecpy(), StrEmpty(), SVS_ALLOW_NEWLINE, SVS_REPLACE_WITH_QUESTION_MARK, ContentInfo::tag_count, ContentInfo::tags, ContentInfo::type, ContentInfo::unique_id, ContentInfo::UNSELECTED, ContentInfo::upgrade, ContentInfo::url, and ContentInfo::version.

## ◆ RequestContentList() [1/3]

void ClientNetworkContentSocketHandler::RequestContentList | ( | ContentType | type | ) |

Request the content list for the given type.

Parameters

  type | The content type to request the list for.

Definition at line 185 of file network_content.cpp.

References Connect(), CONTENT_TYPE_AI, CONTENT_TYPE_AI_LIBRARY, CONTENT_TYPE_BASE_GRAPHICS, CONTENT_TYPE_BASE_MUSIC, CONTENT_TYPE_BASE_SOUNDS, CONTENT_TYPE_END, CONTENT_TYPE_GAME, CONTENT_TYPE_GAME_LIBRARY, CONTENT_TYPE_HEIGHTMAP, CONTENT_TYPE_NEWGRF, CONTENT_TYPE_SCENARIO, PACKET_CONTENT_CLIENT_INFO_LIST, Packet::Send_uint32(), and Packet::Send_uint8().

Referenced by DownloadContentInfo(), BootstrapAskForDownloadWindow::OnConnect(), and ShowNetworkContentListWindow().

## ◆ RequestContentList() [2/3]

void ClientNetworkContentSocketHandler::RequestContentList | ( | ContentVector * | cv,
|  | bool | send_md5sum = `true`
| ) |  |

Request the content list for a list of content.

Parameters

  cv | List with unique IDs and MD5 checksums.
  send_md5sum | Whether we want a MD5 checksum matched set of files or not.

Definition at line 244 of file network_content.cpp.

References Connect(), infos, PACKET_CONTENT_CLIENT_INFO_EXTID, PACKET_CONTENT_CLIENT_INFO_EXTID_MD5, SEND_MTU, Packet::Send_uint32(), Packet::Send_uint8(), and NetworkTCPSocketHandler::SendPacket().

## ◆ RequestContentList() [3/3]

void ClientNetworkContentSocketHandler::RequestContentList | ( | uint | count,
|  | const ContentID * | content_ids
| ) |  |

Request the content list for a given number of content IDs.

Parameters

  count | The number of IDs to request.
  content_ids | The unique identifiers of the content to request information about.

Definition at line 215 of file network_content.cpp.

References Connect(), PACKET_CONTENT_CLIENT_INFO_ID, SEND_MTU, Packet::Send_uint16(), Packet::Send_uint32(), and NetworkTCPSocketHandler::SendPacket().

## ◆ ReverseLookupDependency()

void ClientNetworkContentSocketHandler::ReverseLookupDependency | ( | ConstContentVector & | parents,
|  | const ContentInfo * | child
| ) |  | const

Reverse lookup the dependencies of (direct) parents over a given child.

Parameters

  parents | list to store all parents in (is not cleared)
  child | the child to search the parents' dependencies for

Definition at line 904 of file network_content.cpp.

References ContentInfo::id, and infos.

Referenced by CheckDependencyState(), and ReverseLookupTreeDependency().

## ◆ ReverseLookupTreeDependency()

void ClientNetworkContentSocketHandler::ReverseLookupTreeDependency | ( | ConstContentVector & | tree,
|  | const ContentInfo * | child
| ) |  | const

Reverse lookup the dependencies of all parents over a given child.

Parameters

  tree | list to store all parents in (is not cleared)
  child | the child to search the parents' dependencies for

Definition at line 923 of file network_content.cpp.

References include(), and ReverseLookupDependency().

Referenced by CheckDependencyState().

## ◆ Select()

void ClientNetworkContentSocketHandler::Select | ( | ContentID | cid | ) |

Select a specific content id.

Parameters

  cid | the content ID to select

Definition at line 829 of file network_content.cpp.

References CheckDependencyState(), GetContent(), ContentInfo::SELECTED, ContentInfo::state, and ContentInfo::UNSELECTED.

Referenced by BootstrapAskForDownloadWindow::OnReceiveContentInfo(), and ToggleSelectedState().

## ◆ Unselect()

void ClientNetworkContentSocketHandler::Unselect | ( | ContentID | cid | ) |

Unselect a specific content id.

Parameters

  cid | the content ID to deselect

Definition at line 842 of file network_content.cpp.

References CheckDependencyState(), GetContent(), ContentInfo::IsSelected(), ContentInfo::state, and ContentInfo::UNSELECTED.

Referenced by CheckDependencyState(), and ToggleSelectedState().

The documentation for this class was generated from the following files:

- src/network/network_content.h
- src/network/network_content.cpp