OpenTTD
Public Member Functions | Static Public Member Functions | Data Fields | Private Member Functions | Private Attributes
NetworkHTTPSocketHandler Class Reference

Base socket handler for HTTP traffic. More...

#include <tcp_http.h>

Inheritance diagram for NetworkHTTPSocketHandler:
NetworkSocketHandler

Public Member Functions

bool IsConnected () const
 Whether this socket is currently bound to a socket. More...
 
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. More...
 
 NetworkHTTPSocketHandler (SOCKET sock, HTTPCallback *callback, const char *host, const char *url, const char *data, int depth)
 Start the querying. More...
 
 ~NetworkHTTPSocketHandler ()
 Free whatever needs to be freed. More...
 
- Public Member Functions inherited from NetworkSocketHandler
 NetworkSocketHandler ()
 Create a new unbound socket.
 
virtual ~NetworkSocketHandler ()
 Close the socket when destructing the socket handler.
 
virtual void Close ()
 Really close the socket.
 
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 Member Functions

static int Connect (char *uri, HTTPCallback *callback, const char *data=NULL, int depth=0)
 Connect to the given URI. More...
 
static void HTTPReceive ()
 Do the receiving for all HTTP connections.
 

Data Fields

SOCKET sock
 The socket currently connected to.
 

Private Member Functions

int HandleHeader ()
 Handle the header of a HTTP reply. More...
 
int Receive ()
 Handle receiving of HTTP data. More...
 

Private Attributes

char recv_buffer [4096]
 Partially received message.
 
int recv_pos
 Current position in buffer.
 
int recv_length
 Length of the data still retrieving.
 
HTTPCallbackcallback
 The callback to call for the incoming data.
 
const char * data
 The (POST) data we might want to forward (to a redirect).
 
int redirect_depth
 The depth of the redirection.
 

Detailed Description

Base socket handler for HTTP traffic.

Definition at line 42 of file tcp_http.h.

Constructor & Destructor Documentation

◆ NetworkHTTPSocketHandler()

NetworkHTTPSocketHandler::NetworkHTTPSocketHandler ( SOCKET  s,
HTTPCallback callback,
const char *  host,
const char *  url,
const char *  data,
int  depth 
)

Start the querying.

Parameters
sthe socket of this connection
callbackthe callback for HTTP retrieval
hostthe hostname of the server to connect to
urlthe url at the server
datathe data to send
depththe depth (redirect recursion) of the queries

Definition at line 37 of file tcp_http.cpp.

◆ ~NetworkHTTPSocketHandler()

NetworkHTTPSocketHandler::~NetworkHTTPSocketHandler ( )

Free whatever needs to be freed.

Definition at line 72 of file tcp_http.cpp.

References CloseConnection(), data, free(), and sock.

Member Function Documentation

◆ CloseConnection()

NetworkRecvStatus NetworkHTTPSocketHandler::CloseConnection ( bool  error = true)
virtual

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.

Parameters
errorWhether we quit under an error condition or not.
Returns
new status of the connection.

Reimplemented from NetworkSocketHandler.

Definition at line 81 of file tcp_http.cpp.

References NetworkSocketHandler::CloseConnection(), and NETWORK_RECV_STATUS_OKAY.

Referenced by HTTPReceive(), and ~NetworkHTTPSocketHandler().

◆ Connect()

int NetworkHTTPSocketHandler::Connect ( char *  uri,
HTTPCallback callback,
const char *  data = NULL,
int  depth = 0 
)
static

Connect to the given URI.

Parameters
urithe URI to connect to.
callbackthe callback to send data back on.
datathe data we want to send (as POST).
depththe recursion/redirect depth.

Definition at line 197 of file tcp_http.cpp.

References ParseConnectionString(), and return_error.

◆ HandleHeader()

int NetworkHTTPSocketHandler::HandleHeader ( )
private

Handle the header of a HTTP reply.

Returns
amount of data to continue downloading. > 0: we need to download N bytes. = 0: we're being redirected. < 0: an error occurred. Downloading failed.
Note
if an error occurred the header might not be in its original state. No effort is undertaken to bring the header in its original state.

Definition at line 110 of file tcp_http.cpp.

Referenced by Receive().

◆ IsConnected()

bool NetworkHTTPSocketHandler::IsConnected ( ) const
inline

Whether this socket is currently bound to a socket.

Returns
true when the socket is bound, false otherwise

Definition at line 60 of file tcp_http.h.

References error().

◆ Receive()

int NetworkHTTPSocketHandler::Receive ( )
private

Handle receiving of HTTP data.

Returns
state of the receival of HTTP data. > 0: we need more cycles for downloading = 0: we are done downloading < 0: we have hit an error

Definition at line 232 of file tcp_http.cpp.

References callback, DEBUG, END_OF_HEADER, HandleHeader(), lengthof, min(), HTTPCallback::OnReceiveData(), recv_buffer, recv_length, recv_pos, and sock.

Referenced by HTTPReceive().


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