SC2API
An API for AI for StarCraft II
Public Member Functions | Public Attributes | List of all members
sc2::Connection Class Reference

#include <sc2_connection.h>

Public Member Functions

bool Connect (const std::string &address, int port, bool verbose=true)
 
void Send (const SC2APIProtocol::Request *request)
 
bool Receive (SC2APIProtocol::Response *&response, unsigned int timeout_ms)
 
void PopResponse (SC2APIProtocol::Response *&response)
 
void SetTimeoutCallback (std::function< void()> callback)
 
void SetConnectionClosedCallback (std::function< void()> callback)
 
bool HasConnection () const
 
void Disconnect ()
 Disconnect from the game if currently connected.
 
bool PollResponse ()
 
void PushResponse (SC2APIProtocol::Response *&response)
 

Public Attributes

std::function< void()> timeout_callback_
 Timeout callback.
 
std::function< void()> connection_closed_callback_
 Timeout callback.
 
mg_connection * connection_
 A pointer to the civetweb connection object.
 

Detailed Description

This class acts as a wrapper around a websocket connection and queue responsible for both sending out and receiving protobuf messages.

Member Function Documentation

bool sc2::Connection::Connect ( const std::string &  address,
int  port,
bool  verbose = true 
)

Connects via websocket on a given address/port.

Parameters
addressThe address to connect to, will most commonly be used locally so 127.0.0.1.
portThe port to connect the, the default for s2api is 9168 unless specified otherwise in settings.
Returns
Returns true if the connection was successful and false otherwise.
bool sc2::Connection::HasConnection ( ) const

Whether or not the connection is valid.

Returns
true if the connection is valid, false otherwise.
bool sc2::Connection::PollResponse ( )

Polls the queue in a thread safe way to check if a message has been received.

Returns
true if there is a response in the queue, false otherwise.
void sc2::Connection::PopResponse ( SC2APIProtocol::Response *&  response)

PopResponse is called in the Receive function when a message has been received off of the civetweb thread. Alternatively you could poll for responses with PollResponse and consume the message manually with this function.

Parameters
responseThe response pointer to be filled out.
void sc2::Connection::PushResponse ( SC2APIProtocol::Response *&  response)

PushResponse is called by a civetweb thread when it receives a message off the socket. Pushing a response triggers a condition and enqueues a message. The condition will cause anyone currently blocking for a response (if Receive is called) to wake up and be able to consume that message.

Parameters
responseA pointer to the Response to queue.
bool sc2::Connection::Receive ( SC2APIProtocol::Response *&  response,
unsigned int  timeout_ms 
)

Receive will block until a message is received from its websocket connection. If a message is not received within the timeout it will set response to null and return false, it also calls a timeout callback that can be used if a user has any timeout logic.

Parameters
responseThe response pointer to be filled out. The max time, in milliseconds, the function will wait to receive a message.
Returns
Returns true if a message is received, false otherwise.
void sc2::Connection::Send ( const SC2APIProtocol::Request *  request)

Sends a request via the websocket connection. This function assumes Connect has been called and returned success. It will assert in debug if that's not the case and will early out in a build that doesn't have asserts built in. This function also allocates a byte buffer to accommodate the request, it frees that buffer before returning.

Parameters
requestA pointer to the Request object.
void sc2::Connection::SetTimeoutCallback ( std::function< void()>  callback)

An accessor function that a user can bind a timeout function to.

Parameters
callbackA functor or lambda that represents the callback.

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