# 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 with the given socket and (server) address.
| ~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)
void | RequestContentList (uint count, const ContentID *content_ids)
void | RequestContentList (ContentVector *cv, bool send_md5sum=true)
void | DownloadSelectedContent (uint &files, uint &bytes, bool fallback=false)
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 it's 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.

## 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

## Protected Member Functions

| DECLARE_CONTENT_RECEIVE_COMMAND (PACKET_CONTENT_SERVER_INFO)
| 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).
| DECLARE_CONTENT_RECEIVE_COMMAND (PACKET_CONTENT_SERVER_CONTENT)
| 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.
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)
| Callback for when the connection has finished.
void | OnDisconnect ()
| Callback for when the connection got disconnected.
void | OnReceiveContentInfo (const ContentInfo *ci)
| We received a content info.
void | OnDownloadProgress (const ContentInfo *ci, uint bytes)
| We have progress in the download of a file.
void | OnDownloadComplete (ContentID cid)
| We have finished downloading a file.
void | OnFailure ()
| An error has occured and the connection has been closed.
void | OnReceiveData (const char *data, size_t length)
| We're receiving data.
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)
void | DownloadSelectedContentFallback (const ContentIDList &content)

## 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.

## Friends

class | NetworkContentConnecter

## Detailed Description

Socket handler for the content server connection.

Definition at line 67 of file network_content.h.

## Constructor & Destructor Documentation

ClientNetworkContentSocketHandler::ClientNetworkContentSocketHandler | ( |  | ) |

Create a socket handler with the given socket and (server) address.

Parameters:

  | s | the socket to communicate over
  | sin | the IP/port of the server

Definition at line 635 of file network_content.cpp.

## Member Function Documentation

bool ClientNetworkContentSocketHandler::BeforeDownload | ( |  | ) | `[protected]`

Handle the opening of the file before downloading.

Returns:
  false on any error.

Definition at line 445 of file network_content.cpp.

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

Referenced by OnReceiveData().

ConstContentIterator ClientNetworkContentSocketHandler::Begin | ( |  | ) | const`[inline]`

Get the begin of the content inf iterator.

Definition at line 134 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.

Parameters:

  | ci | the content info to check the dependencies of

Definition at line 883 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 972 of file network_content.cpp.

References SmallVector< T, S >::Begin(), SmallVector< T, S >::Clear(), SmallVector< T, S >::End(), infos, and requested.

Referenced by ShowNetworkContentListWindow().

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 737 of file network_content.cpp.

References SmallVector< T, S >::Append(), SmallVector< T, S >::Contains(), and requested.

Referenced by CheckDependencyState().

ConstContentIterator ClientNetworkContentSocketHandler::End | ( |  | ) | const`[inline]`

Get the end of the content inf iterator.

Definition at line 138 of file network_content.h.

References SmallVector< T, S >::End(), and infos.

Referenced by NetworkContentListWindow::BuildContentList(), and NetworkContentListWindow::DrawDetails().

ConstContentIterator ClientNetworkContentSocketHandler::Get | ( | uint32 | index | ) | const`[inline]`

Get the nth position of the content inf iterator.

Definition at line 136 of file network_content.h.

References SmallVector< T, S >::Get(), and infos.

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 NULL if not found

Definition at line 752 of file network_content.cpp.

References SmallVector< T, S >::Begin(), SmallVector< T, S >::End(), ContentInfo::id, and infos.

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

uint ClientNetworkContentSocketHandler::Length | ( |  | ) | const`[inline]`

Get the number of content items we know locally.

Definition at line 132 of file network_content.h.

References infos, and SmallVector< T, S >::Length().

Referenced by NetworkContentListWindow::OnKeyPress().

void ClientNetworkContentSocketHandler::OnConnect | ( | bool | success | ) | `[protected, virtual]`

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 982 of file network_content.cpp.

References SmallVector< T, S >::Begin(), callbacks, SmallVector< T, S >::End(), and ContentCallback::OnConnect().

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

void ClientNetworkContentSocketHandler::OnDownloadComplete | ( | ContentID | cid | ) | `[protected, virtual]`

We have finished downloading a file.

Parameters:

  | cid | the ContentID of the downloaded file

Reimplemented from ContentCallback.

Definition at line 1018 of file network_content.cpp.

References ContentInfo::ALREADY_HERE, SmallVector< T, S >::Begin(), callbacks, SmallVector< T, S >::End(), GetContent(), ContentCallback::OnDownloadComplete(), and ContentInfo::state.

Referenced by AfterDownload().

void ClientNetworkContentSocketHandler::OnDownloadProgress | ( | const ContentInfo * | ci,
|  | uint | bytes |
| ) |  |  | `[protected, virtual]`

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 1009 of file network_content.cpp.

References SmallVector< T, S >::Begin(), callbacks, SmallVector< T, S >::End(), and ContentCallback::OnDownloadProgress().

Referenced by OnReceiveData().

void ClientNetworkContentSocketHandler::OnFailure | ( |  | ) | `[protected, virtual]`

An error has occured and the connection has been closed.

Note:
  HTTP socket handler is closed/freed.

Implements HTTPCallback.

Definition at line 491 of file network_content.cpp.

References curFile, http_response, http_response_index, and SmallVector< T, S >::Reset().

Referenced by OnReceiveData().

void ClientNetworkContentSocketHandler::OnReceiveContentInfo | ( | const ContentInfo * | ci | ) | `[protected, virtual]`

We received a content info.

Parameters:

  | ci | the content info

Reimplemented from ContentCallback.

Definition at line 1000 of file network_content.cpp.

References SmallVector< T, S >::Begin(), callbacks, SmallVector< T, S >::End(), and ContentCallback::OnReceiveContentInfo().

void ClientNetworkContentSocketHandler::OnReceiveData | ( | const char * | data,
|  | size_t | length |
| ) |  |  | `[protected, virtual]`

We're receiving data.

Parameters:

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

Note:
  When NULL 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 506 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(), OnDownloadProgress(), OnFailure(), strecpy(), and ContentInfo::type.

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 844 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.

Parameters:

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

Definition at line 864 of file network_content.cpp.

References SmallVector< T, S >::Append(), SmallVector< T, S >::Begin(), SmallVector< T, S >::End(), SmallVector< T, S >::Include(), and ReverseLookupDependency().

Referenced by CheckDependencyState(), and NetworkContentListWindow::DrawDetails().

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

Select a specific content id.

Parameters:

  | cid | the content ID to select

Definition at line 766 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.

Parameters:

  | cid | the content ID to deselect

Definition at line 779 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 813 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.

Referenced by NetworkContentListWindow::OnClick().

## Field Documentation

const int ClientNetworkContentSocketHandler::IDLE_TIMEOUT = 60 * 1000`[static]`

The idle timeout; when to close the connection because it's idle.

Definition at line 105 of file network_content.h.

Referenced by SendReceive().

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

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