ArNetworking: ArClientBase Class Reference

- Main Page
- Classes
- Related Pages

- Alphabetical List
- Class List
- Class Hierarchy
- Class Members

# ArClientBase Class Reference

`#include <ArClientBase.h>`

Inheritance diagram for ArClientBase:

List of all members.

## Detailed Description

The base client class.

You need to connect a client to a server using blockingConnect() with the address for the server. Then you should add handlers to the client to receive information from the server (with addHandler(), request(), and requestOnce()). Then call run() or runAsync() (or loopOnce() if you really know what you're doing). You can also add a callback that will get called every cycle of the client.

User and password information may be required by the server. For details on that see ArServerBase. The most important thing is the modification to blockingConnect(), it can now take a user and password argument and upon failure you'll want to check wasRejected() to determine whether the client couldn't find the server or if the user and password were rejected. Another relevant function is setServerKey() to set the key (string of text) we need to connect to the server.

This class should be thread safe... The only thing you cannot do is call 'addHandler' or 'remHandler' from within a handler for that specific piece of data.

## Public Types
enum | { CLIENT_KEY_LENGTH = 16 }
enum | ClientState { STATE_NO_CONNECTION, STATE_FAILED_CONNECTION, STATE_OPENED_SOCKET, STATE_EXCHANGED_INTROS, STATE_REJECTED, STATE_WAITING_LIST, STATE_CONNECTED, STATE_LOST_CONNECTION }
| The state of the connection. More...
enum | NonBlockingConnectReturn { NON_BLOCKING_CONTINUE, NON_BLOCKING_CONNECTED, NON_BLOCKING_FAILED }
## Public Member Functions
void | addCycleCallback (ArFunctor *functor)
| Adds a functor to call every cycle.
void | addDisconnectOnErrorCB (ArFunctor *functor, ArListPos::Pos position=ArListPos::LAST)
| Adds a call for when a disconnection has occured because of error.
bool | addHandler (const char *name, ArFunctor1< ArNetPacket * > *functor)
| Adds a functor for some particular data.
void | addServerShutdownCB (ArFunctor *functor, ArListPos::Pos position=ArListPos::LAST)
| Adds a call for when the server shuts down.
| ArClientBase ()
| Constructor.
bool | blockingConnect (const char *host, int port, bool print=true, const char *user=NULL, const char *password=NULL, const char *openOnIP=NULL)
| Connect to a server.
bool | dataExists (const char *name)
| Sees if this data exists.
bool | disconnect (void)
| Disconnect from a server.
void | disconnectSoon (void)
| Disconnect from a server next time through the loop.
unsigned int | findCommandFromName (const char *name)
| Returns the command number from the name.
void | finishNonBlockingDisconnect ()
| Finishes the process of disconnecting from the server.
void | getClientKey (unsigned char key[CLIENT_KEY_LENGTH])
| Gets a (probably) unique key that can be used to identify the client (after connection).
int | getConnectTimeoutTime (void)
| Gets the time allowed for connection.
const std::map< unsigned int, ArClientData * > * | getDataMap (void)
| Internal function get get the data map.
bool | getDebugLogging (void)
| Returns if we're doing debug logging or not.
const char * | getHost (void)
| Gets the name of the host we tried to connect to.
ArTime | getLastPacketReceived (void)
| Gets the last time a packet was received.
virtual const char * | getLogPrefix () const
| Returns the entire string used to prefix log entries for this client base.
const char * | getName (unsigned int command, bool internalCall=false)
| Gets the name of the data a command is.
const char * | getName (ArNetPacket *packet, bool internalCall=false)
| Gets the name of the data a packet is for.
bool | getReceivedArgRetList (void)
| Gets if we received the list of arguments and.
bool | getReceivedDataList (void)
| Gets if we received the list of commands.
bool | getReceivedGroupAndFlagsList (void)
| Gets if we received the list of commands.
int | getRejected (void)
| Tells us the reason the connection was rejected (see ArServerCommands for details).
const char * | getRejectedString (void)
| Tells us the reason the connection was rejected (see ArServerCommands for details).
virtual const char * | getRobotName () const
| Returns the optional name of the connected robot for debugging purposes.
ClientState | getState (void)
| Gets the state of the client.
in_addr * | getTcpAddr (void)
| Internal function to get the socket (no one should need this).
ArSocket * | getTcpSocket (void)
| Internal function to get the tcp socket.
ArSocket * | getUdpSocket (void)
| Internal function to get the udp socket.
bool | internalBlockingConnect (const char *host, int port, bool print, const char *user, const char *password, ArSocket *tcpSocket, const char *openOnIP=NULL)
| Internal function that'll do the connection (mainly for switch connections).
NonBlockingConnectReturn | internalNonBlockingConnectContinue (void)
| Internal function that'll start the non blocking connection (mainly for switch connections).
NonBlockingConnectReturn | internalNonBlockingConnectStart (const char *host, int port, bool print, const char *user, const char *password, ArSocket *tcpSocket, const char *openOnIP=NULL)
| Internal function that'll start the non blocking connection (mainly for switch connections).
bool | isConnected (void)
virtual bool | isStopped ()
| Returns whether the client has been stopped (regardless of whether its in its own thread).
bool | isTcpOnlyFromServer (void)
| Returns whether we only get TCP data from the server not UDP.
bool | isTcpOnlyToServer (void)
| Returns whether we only send TCP data to the server.
void | logDataList (void)
| Print out or data with descriptions.
void | logTracking (bool terse)
| Logs the tracking information (packet and byte counts).
void | loopOnce (void)
| Run the loop once.
void | processPacket (ArNetPacket *packet, bool tcp)
| Process the packet wherever it came from.
void | processPacketUdp (ArNetPacket *packet, struct sockaddr_in *sin)
| Process a packet from udp (just hands off to processPacket).
void | remCycleCallback (ArFunctor *functor)
| Removes a functor called every cycle.
void | remDisconnectOnErrorCB (ArFunctor *functor)
| Removes a call for when a disconnection has occured because of error.
bool | remHandler (const char *name, ArFunctor1< ArNetPacket * > *functor)
| Removes a functor for some particular data by name.
void | remServerShutdownCB (ArFunctor *functor)
| Removes a call for when the server shuts down.
bool | request (const char *name, long mSec, ArNetPacket *packet=NULL)
| Request some data every mSec milliseconds.
bool | requestByCommand (unsigned int command, long mSec, ArNetPacket *packet=NULL)
| Request some data every mSec milliseconds by command not name.
bool | requestOnce (const char *name, ArNetPacket *packet=NULL, bool quiet=false)
| Request some data (or send a command) just once.
bool | requestOnceByCommand (unsigned int command, ArNetPacket *packet=NULL)
| Request some data (or send a command) just once by command not name.
bool | requestOnceByCommandUdp (unsigned int command, ArNetPacket *packet=NULL)
| Request some data (or send a command) just once by command not name.
bool | requestOnceUdp (const char *name, ArNetPacket *packet=NULL, bool quiet=false)
| Request some data (or send a command) just once by UDP.
bool | requestOnceWithString (const char *name, const char *str)
| Request some data (or send a command) just once with a string as argument.
bool | requestStop (const char *name)
| Don't want this data anymore.
bool | requestStopByCommand (unsigned int command)
| Don't want this data anymore, by command not name.
void | resetTracking (void)
| Clears the tracking information (resets counters).
virtual void | run (void)
| Runs the client in this thread.
virtual void | runAsync (void)
| Runs the client in its own thread.
virtual void * | runThread (void *arg)
bool | sendPacketTcp (ArNetPacket *packet)
| Send a packet over TCP.
bool | sendPacketUdp (ArNetPacket *packet)
| Send a packet over UDP (unless client only wants tcp then sends over tcp).
void | setBackupTimeout (double timeoutInMins)
| Sets the backup timeout.
void | setConnectTimeoutTime (int sec)
| Sets the time to allow for connection (default 3).
virtual void | setRobotName (const char *name, bool debugLogging=false, int robotId=0)
| Sets the optional name of the connected robot for debugging purposes.
void | setServerKey (const char *serverKey, bool print=true)
| Sets the 'key' needed to connect to the server.
void | setTcpOnlyFromServer (void)
| Sets it so we only get TCP data from the server not UDP.
void | setTcpOnlyToServer (void)
| Sets it so we only send TCP data to the server.
bool | startNonBlockingDisconnect ()
| Starts the process of disconnecting from the server.
virtual void | stopRunning (void)
| Stops the thread and sets the stopped flag.
bool | wasRejected (void)
virtual | ~ArClientBase ()
| Destructor.
## Protected Types
enum | NonBlockingConnectState { NON_BLOCKING_STATE_NONE, NON_BLOCKING_STATE_TCP, NON_BLOCKING_STATE_UDP }
## Protected Member Functions
void | buildList (ArNetPacket *packet)
void | clear (void)
bool | internalConnect (const char *host, int port, bool obsolete, const char *openOnIP)
void | internalStartUdp (void)
void | internalSwitchState (ClientState state)
bool | setupPacket (ArNetPacket *packet)
void | trackPacketReceived (ArNetPacket *packet, bool tcp)
void | trackPacketSent (ArNetPacket *packet, bool tcp)
## Protected Attributes
long | myAuthKey
double | myBackupTimeout
ArMutex | myCallbackMutex
unsigned char | myClientKey [16]
ArMutex | myClientMutex
ArMutex | myCycleCallbackMutex
std::list< ArFunctor * > | myCycleCallbacks
ArMutex | myDataMutex
bool | myDebugLogging
std::list< ArFunctor * > | myDisconnectOnErrorCBList
bool | myDisconnectSoon
std::string | myHost
std::map< unsigned int, ArClientData * > | myIntDataMap
long | myIntroKey
bool | myIsRunningAsync
bool | myIsStartedDisconnect
bool | myIsStopped
ArTime | myLastPacketReceived
std::string | myLogPrefix
| Optional prefix to be inserted at the start of log messages.
ArMutex | myMapsMutex
std::map< std::string, unsigned int > | myNameIntMap
NonBlockingConnectState | myNonBlockingConnectState
ArMutex | myPacketTrackingMutex
std::string | myPassword
ArFunctor2C< ArClientBase, ArNetPacket *, bool > | myProcessPacketCB
ArFunctor2C< ArClientBase, ArNetPacket *, struct sockaddr_in * > | myProcessPacketUdpCB
bool | myQuiet
bool | myReceivedArgRetList
bool | myReceivedDataList
bool | myReceivedGroupAndFlagsList
int | myRejected
char | myRejectedString [32000]
std::string | myRobotName
| Optional robot name for logging.
std::string | myServerKey
unsigned int | myServerReportedUdpPort
unsigned int | myServerSentUdpPort
std::list< ArFunctor * > | myServerShutdownCBList
ArTime | myStartedConnection
ArTime | myStartedShutdown
ArTime | myStartedUdpConnection
ClientState | myState
ArMutex | myStateMutex
ArTime | myStateStarted
bool | myTcpOnlyFrom
bool | myTcpOnlyTo
ArNetPacketReceiverTcp | myTcpReceiver
ArNetPacketSenderTcp | myTcpSender
ArSocket | myTcpSocket
int | myTimeoutTime
std::map< unsigned int, Tracker * > | myTrackingReceivedMap
std::map< unsigned int, Tracker * > | myTrackingSentMap
ArTime | myTrackingStarted
bool | myUdpConfirmedFrom
bool | myUdpConfirmedTo
unsigned int | myUdpPort
ArNetPacketReceiverUdp | myUdpReceiver
sockaddr_in | myUdpSin
bool | myUdpSinValid
ArSocket | myUdpSocket
std::string | myUser
ArLog::LogLevel | myVerboseLogLevel

## Member Enumeration Documentation

enum ArClientBase::ClientState

The state of the connection.

Enumerator:

  STATE_NO_CONNECTION | The client has not connected.
  STATE_FAILED_CONNECTION | The client tried to connect and failed.
  STATE_OPENED_SOCKET | Client opened socket, waiting for intro from srvr.
  STATE_EXCHANGED_INTROS | Client and server have exchanged introductions.
  STATE_REJECTED | Client was rejected by server.
  STATE_WAITING_LIST | Client was connected to server, waiting for list.
  STATE_CONNECTED | Client is connected to server.
  STATE_LOST_CONNECTION | Client lost connection to server.

enum ArClientBase::NonBlockingConnectReturn

Enumerator:

  NON_BLOCKING_CONTINUE | Client didn't connect or fail to connect yet.
  NON_BLOCKING_CONNECTED | Client connected.
  NON_BLOCKING_FAILED | Client couldn't connect.

## Member Function Documentation

bool ArClientBase::disconnect | ( | void |  | ) |

Disconnect from a server.

This method blocks while the connection is being shutdown.

void ArClientBase::disconnectSoon | ( | void |  | ) |

Disconnect from a server next time through the loop.

This method does not block while the connection is being shutdown, and the disconnect only happens the next time the client base is in its loop

void ArClientBase::finishNonBlockingDisconnect | ( |  | ) |

Finishes the process of disconnecting from the server.

This method should be used in conjunction with startNonBlockingDisconnect in threads that should not block (such as the main GUI thread).

bool ArClientBase::isConnected | ( | void |  | ) | `[inline]`

Returns:
  true if we're connected to a server

bool ArClientBase::startNonBlockingDisconnect | ( |  | ) |

Starts the process of disconnecting from the server.

This method should be called in threads that should not block (such as the main GUI thread). The call should be followed by a delay of one second, followed by a call to finishNonBlockingDisconnect().

bool ArClientBase::wasRejected | ( | void |  | ) | `[inline]`

Returns:
  true if a server connection attempt failed because the server rejected the username or password, false if the connection failed for another reason, or the username/password were accepted.

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

- ArClientBase.h