OpenTTD
|
Socket handler for the content server connection. More...
#include <network_content.h>
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 () |
Disconnect from the content server. | |
void | RequestContentList (ContentType type) |
Request the content list for the given type. | |
void | RequestContentList (uint count, const ContentID *content_ids) |
Request the content list for a given number of content IDs. | |
void | RequestContentList (ContentVector *cv, bool send_md5sum=true) |
Request the content list for a list of content. | |
void | DownloadSelectedContent (uint &files, uint &bytes, bool fallback=false) |
Actually begin downloading the content we selected. | |
void | Select (ContentID cid) |
Select a specific content id. | |
void | Unselect (ContentID cid) |
Unselect a specific content id. | |
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. | |
void | ReverseLookupTreeDependency (ConstContentVector &tree, const ContentInfo *child) const |
Reverse lookup the dependencies of all parents over a given child. | |
void | CheckDependencyState (ContentInfo *ci) |
Check the dependencies (recursively) of this content info. | |
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. | |
![]() | |
NetworkContentSocketHandler (SOCKET s=INVALID_SOCKET, const NetworkAddress &address=NetworkAddress()) | |
Create a new cs socket handler for a given cs. | |
virtual | ~NetworkContentSocketHandler () |
On destructing of this class, the socket needs to be closed. | |
bool | ReceivePackets () |
Receive a packet at TCP level. | |
![]() | |
bool | IsConnected () const |
Whether this socket is currently bound to a socket. | |
virtual NetworkRecvStatus | CloseConnection (bool error=true) |
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. | |
virtual void | SendPacket (Packet *packet) |
This function puts the packet in the send-queue and it is send as soon as possible. | |
SendPacketsState | SendPackets (bool closing_down=false) |
Sends all the buffered packets out for this client. | |
virtual Packet * | ReceivePacket () |
Receives a packet for the given client. | |
bool | CanSendReceive () |
Check whether this socket can send or receive something. | |
bool | HasSendQueue () |
Whether there is something pending in the send queue. | |
NetworkTCPSocketHandler (SOCKET s=INVALID_SOCKET) | |
Construct a socket handler for a TCP connection. | |
![]() | |
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. | |
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. | |
void | ReceiveGRFIdentifier (Packet *p, GRFIdentifier *grf) |
Deserializes the GRFIdentifier (GRF ID and MD5 checksum) from the packet. | |
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. |
Static Public Attributes | |
static const int | IDLE_TIMEOUT = 60 * 1000 |
The idle timeout; when to close the connection because it's idle. |
Protected Types | |
typedef SmallVector< ContentID, 4 > | ContentIDList |
List of content IDs to (possibly) select. |
Protected Member Functions | |
virtual bool | Receive_SERVER_INFO (Packet *p) |
virtual bool | Receive_SERVER_CONTENT (Packet *p) |
ContentInfo * | GetContent (ContentID cid) |
Get the content info based on a ContentID. | |
void | DownloadContentInfo (ContentID cid) |
Download information of a given Content ID if not already tried. | |
void | OnConnect (bool success) |
void | OnDisconnect () |
void | OnReceiveContentInfo (const ContentInfo *ci) |
void | OnDownloadProgress (const ContentInfo *ci, int bytes) |
void | OnDownloadComplete (ContentID cid) |
void | OnFailure () |
void | OnReceiveData (const char *data, size_t length) |
bool | BeforeDownload () |
Handle the opening of the file before downloading. | |
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. | |
void | DownloadSelectedContentFallback (const ContentIDList &content) |
Initiate downloading the content over the fallback protocol. | |
![]() | |
virtual void | Close () |
Really close the socket. | |
bool | ReceiveInvalidPacket (PacketContentType type) |
Helper for logging receiving invalid packets. | |
virtual bool | Receive_CLIENT_INFO_LIST (Packet *p) |
Client requesting a list of content info: byte type uint32 openttd version. | |
virtual bool | Receive_CLIENT_INFO_ID (Packet *p) |
Client requesting a list of content info: uint16 count of ids uint32 id (count times) | |
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. | |
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. | |
virtual bool | Receive_SERVER_INFO (Packet *p) |
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) | |
virtual bool | Receive_CLIENT_CONTENT (Packet *p) |
Client requesting the actual content: uint16 count of unique ids uint32 unique id (count times) | |
virtual bool | Receive_SERVER_CONTENT (Packet *p) |
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. | |
bool | HandlePacket (Packet *p) |
Handle the given packet, i.e. |
Protected Attributes | |
SmallVector< ContentCallback *, 2 > | 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. | |
SmallVector< char, 1024 > | 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. | |
uint32 | lastActivity |
The last time there was network activity. | |
![]() | |
NetworkAddress | client_addr |
The address we're connected to. |
Friends | |
class | NetworkContentConnecter |
Additional Inherited Members | |
![]() | |
SOCKET | sock |
The socket currently connected to. | |
bool | writable |
Can we write to this socket? | |
![]() | |
virtual void | OnConnect (bool success) |
Callback for when the connection has finished. | |
virtual void | OnDisconnect () |
Callback for when the connection got disconnected. | |
virtual void | OnReceiveContentInfo (const ContentInfo *ci) |
We received a content info. | |
virtual void | OnDownloadProgress (const ContentInfo *ci, int bytes) |
We have progress in the download of a file. | |
virtual void | OnDownloadComplete (ContentID cid) |
We have finished downloading a file. | |
virtual | ~ContentCallback () |
Silentium. | |
![]() | |
virtual void | OnFailure ()=0 |
An error has occurred and the connection has been closed. | |
virtual void | OnReceiveData (const char *data, size_t length)=0 |
We're receiving data. | |
virtual | ~HTTPCallback () |
Silentium. |
Socket handler for the content server connection.
Definition at line 69 of file network_content.h.
|
protected |
Handle the opening of the file before downloading.
Definition at line 504 of file network_content.cpp.
References curFile, curInfo, DeleteWindowById(), ContentInfo::filesize, GetFullFilename(), ContentInfo::IsValid(), ShowErrorMessage(), WC_NETWORK_STATUS_WINDOW, WL_ERROR, and WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD.
Referenced by OnReceiveData().
|
inline |
Get the begin of the content inf iterator.
Definition at line 136 of file network_content.h.
References SmallVector< T, S >::Begin(), and infos.
Referenced by NetworkContentListWindow::BuildContentList(), and NetworkContentListWindow::DrawDetails().
void ClientNetworkContentSocketHandler::CheckDependencyState | ( | ContentInfo * | ci | ) |
Check the dependencies (recursively) of this content info.
ci | the content info to check the dependencies of |
Definition at line 948 of file network_content.cpp.
References ContentInfo::ALREADY_HERE, ContentInfo::AUTOSELECTED, SmallVector< T, S >::Begin(), SmallVector< T, S >::Clear(), ContentInfo::dependencies, ContentInfo::dependency_count, DownloadContentInfo(), SmallVector< T, S >::End(), GetContent(), ContentInfo::id, ContentInfo::IsSelected(), ReverseLookupDependency(), ReverseLookupTreeDependency(), ContentInfo::SELECTED, ContentInfo::state, Unselect(), and ContentInfo::UNSELECTED.
Referenced by Select(), SelectAll(), SelectUpgrade(), and Unselect().
void ClientNetworkContentSocketHandler::Clear | ( | ) |
Clear all downloaded content information.
Definition at line 1038 of file network_content.cpp.
References SmallVector< T, S >::Begin(), SmallVector< T, S >::Clear(), SmallVector< T, S >::End(), infos, and requested.
Referenced by ShowNetworkContentListWindow().
|
protected |
Download information of a given Content ID if not already tried.
cid | the ID to try |
Definition at line 799 of file network_content.cpp.
References SmallVector< T, S >::Append(), SmallVector< T, S >::Contains(), RequestContentList(), and requested.
Referenced by CheckDependencyState().
void ClientNetworkContentSocketHandler::DownloadSelectedContent | ( | uint & | files, |
uint & | bytes, | ||
bool | fallback = false |
||
) |
Actually begin downloading the content we selected.
[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 297 of file network_content.cpp.
References _settings_client, ContentInfo::ALREADY_HERE, SmallVector< T, S >::Append(), SmallVector< T, S >::Begin(), DownloadSelectedContentFallback(), DownloadSelectedContentHTTP(), SmallVector< T, S >::End(), ContentInfo::filesize, ContentInfo::id, infos, ContentInfo::IsSelected(), SmallVector< T, S >::Length(), ClientSettings::network, NetworkSettings::no_http_content_downloads, and ContentInfo::state.
Referenced by BaseNetworkContentDownloadStatusWindow::BaseNetworkContentDownloadStatusWindow().
|
protected |
Initiate downloading the content over the fallback protocol.
content | The content to download. |
Definition at line 354 of file network_content.cpp.
References SmallVector< T, S >::Begin(), Connect(), SmallVector< T, S >::Length(), min(), PACKET_CONTENT_CLIENT_CONTENT, SEND_MTU, Packet::Send_uint16(), Packet::Send_uint32(), and NetworkTCPSocketHandler::SendPacket().
Referenced by DownloadSelectedContent().
|
protected |
Initiate downloading the content over HTTP.
content | The content to download. |
Definition at line 326 of file network_content.cpp.
References SmallVector< T, S >::Begin(), SmallVector< T, S >::End(), http_response_index, lastof, SmallVector< T, S >::Length(), NETWORK_CONTENT_MIRROR_HOST, NETWORK_CONTENT_MIRROR_PORT, NETWORK_CONTENT_MIRROR_URL, and seprintf().
Referenced by DownloadSelectedContent().
|
inline |
Get the end of the content inf iterator.
Definition at line 140 of file network_content.h.
References SmallVector< T, S >::End(), and infos.
Referenced by NetworkContentListWindow::BuildContentList(), and NetworkContentListWindow::DrawDetails().
|
inline |
Get the nth position of the content inf iterator.
Definition at line 138 of file network_content.h.
References SmallVector< T, S >::Get(), and infos.
|
protected |
Get the content info based on a ContentID.
cid | the ContentID to search for |
Definition at line 814 of file network_content.cpp.
References SmallVector< T, S >::Begin(), SmallVector< T, S >::End(), ContentInfo::id, and infos.
Referenced by CheckDependencyState(), Select(), and Unselect().
|
inline |
Get the number of content items we know locally.
Definition at line 134 of file network_content.h.
References infos, and SmallVector< T, S >::Length().
|
protected |
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.
Definition at line 577 of file network_content.cpp.
References AfterDownload(), SmallVector< T, S >::Append(), BeforeDownload(), SmallVector< T, S >::Begin(), Connect(), curFile, curInfo, ContentInfo::filename, ContentInfo::filesize, http_response, http_response_index, ContentInfo::id, lastof, SmallVector< T, S >::Length(), strecpy(), and ContentInfo::type.
void ClientNetworkContentSocketHandler::RequestContentList | ( | ContentType | type | ) |
Request the content list for the given type.
type | The content type to request the list for. |
Definition at line 186 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(), Packet::Send_uint8(), and NetworkTCPSocketHandler::SendPacket().
Referenced by DownloadContentInfo(), and ShowNetworkContentListWindow().
void ClientNetworkContentSocketHandler::RequestContentList | ( | uint | count, |
const ContentID * | content_ids | ||
) |
Request the content list for a given number of content IDs.
count | The number of IDs to request. |
content_ids | The unique identifiers of the content to request information about. |
Definition at line 216 of file network_content.cpp.
References Connect(), min(), PACKET_CONTENT_CLIENT_INFO_ID, SEND_MTU, Packet::Send_uint16(), Packet::Send_uint32(), and NetworkTCPSocketHandler::SendPacket().
void ClientNetworkContentSocketHandler::RequestContentList | ( | ContentVector * | cv, |
bool | send_md5sum = true |
||
) |
Request the content list for a list of content.
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 246 of file network_content.cpp.
References SmallVector< T, S >::Append(), SmallVector< T, S >::Begin(), Connect(), SmallVector< T, S >::End(), infos, SmallVector< T, S >::Length(), ContentInfo::md5sum, PACKET_CONTENT_CLIENT_INFO_EXTID, PACKET_CONTENT_CLIENT_INFO_EXTID_MD5, SEND_MTU, NetworkTCPSocketHandler::SendPacket(), ContentInfo::type, and ContentInfo::unique_id.
void ClientNetworkContentSocketHandler::ReverseLookupDependency | ( | ConstContentVector & | parents, |
const ContentInfo * | child | ||
) | const |
Reverse lookup the dependencies of (direct) parents over a given child.
parents | list to store all parents in (is not cleared) |
child | the child to search the parents' dependencies for |
Definition at line 906 of file network_content.cpp.
References SmallVector< T, S >::Append(), SmallVector< T, S >::Begin(), ContentInfo::dependencies, ContentInfo::dependency_count, SmallVector< T, S >::End(), ContentInfo::id, and infos.
Referenced by CheckDependencyState(), and ReverseLookupTreeDependency().
void ClientNetworkContentSocketHandler::ReverseLookupTreeDependency | ( | ConstContentVector & | tree, |
const ContentInfo * | child | ||
) | const |
Reverse lookup the dependencies of all parents over a given child.
tree | list to store all parents in (is not cleared) |
child | the child to search the parents' dependencies for |
Definition at line 926 of file network_content.cpp.
References SmallVector< T, S >::Append(), SmallVector< T, S >::Begin(), SmallVector< T, S >::End(), SmallVector< T, S >::Include(), SmallVector< T, S >::Length(), and ReverseLookupDependency().
Referenced by CheckDependencyState(), and NetworkContentListWindow::DrawDetails().
void ClientNetworkContentSocketHandler::Select | ( | ContentID | cid | ) |
Select a specific content id.
cid | the content ID to select |
Definition at line 828 of file network_content.cpp.
References CheckDependencyState(), GetContent(), ContentInfo::SELECTED, ContentInfo::state, and ContentInfo::UNSELECTED.
Referenced by ToggleSelectedState().
void ClientNetworkContentSocketHandler::Unselect | ( | ContentID | cid | ) |
Unselect a specific content id.
cid | the content ID to deselect |
Definition at line 841 of file network_content.cpp.
References CheckDependencyState(), GetContent(), ContentInfo::IsSelected(), ContentInfo::state, and ContentInfo::UNSELECTED.
Referenced by CheckDependencyState(), and ToggleSelectedState().
void ClientNetworkContentSocketHandler::UnselectAll | ( | ) |
Unselect everything that we've not downloaded so far.
Definition at line 875 of file network_content.cpp.
References ContentInfo::ALREADY_HERE, SmallVector< T, S >::Begin(), SmallVector< T, S >::End(), infos, ContentInfo::IsSelected(), ContentInfo::state, and ContentInfo::UNSELECTED.
|
static |
The idle timeout; when to close the connection because it's idle.
Definition at line 107 of file network_content.h.
Referenced by SendReceive().