ReplicaNet and RNLobby  1
Classes | Public Types | Public Member Functions | Protected Attributes | List of all members
CodeServer Class Reference

#include <CodeServer.h>

Inheritance diagram for CodeServer:
Inheritance graph
[legend]
Collaboration diagram for CodeServer:
Collaboration graph
[legend]

Classes

class  CodeEntry
 

Public Types

enum  Reply {
  kReply_ValidKey = 0, kReply_UnspecifiedError, kReply_InvalidKey, kReply_KeyInUse,
  kReply_Timeout, kReply_CorruptReply
}
 
enum  { kDefaultPort = 4003 }
 

Public Member Functions

bool ReadCodeFile (const char *filename, const bool duplicateCodeCheck=false)
 
bool AddCode (const char *code, const bool duplicateCodeCheck=false, const int userSuppliedReply=0)
 
bool RemoveCode (const char *code=0)
 
bool Start (const XPAddress &address=XPAddress(kDefaultPort))
 
bool Stop (bool hardware=false)
 
void SetEncryptionKey (const void *data, const int length)
 
void SetCodeTimeout (const float time=(60.0f *30.0f))
 
float GetCodeTimeout (void) const
 
virtual void CallbackDataReceived (void *data, int &length, const int maxLength, XPAddress &address)
 
virtual void CallbackVersion (const int version)
 
virtual void CallbackKeyData (char *keyData, const int maxLength, int &nOnce1, int &nOnce2, bool &updateDatabase)
 
virtual void CallbackQueryReply (Reply &reply, int &userSuppliedReply, int &nOnce1, int &nOnce2)
 
virtual void CallbackDataSend (void *data, int &length, const int maxLength, XPAddress &address)
 
virtual void CallbackQueryReplyTime (const float seconds)
 
int GetNumRequests (void) const
 Returns the total number of codes queries.
 
int GetNumFailed (void) const
 Returns the total number of codes queries that were returned as kReply_InvalidKey.
 

Protected Attributes

RegistryManagerMap
< std::string, CodeEntry,
CodeEntry::ThisLess
mCodes
 
MutexClass mCodesMutex
 

Detailed Description

This code server uses a UDP socket to respond to queries for the validity of codes. The codes are stored in a file with one code per line. Use the RNLobby::CodeClient class to make queries to this server.

Member Function Documentation

bool AddCode ( const char *  code,
const bool  duplicateCodeCheck = false,
const int  userSuppliedReply = 0 
)

Adds a code to the database in memory. Each code must be no longer than 255 characters. This function may be used when the code server has been started.

Parameters
codeThe code to add.
duplicateCodeCheckIf this is true then the code is checked to make sure it is not a duplicate code already stored in the database. The default is false, which disables the duplicate code check and makes adding codes slightly faster.
userSuppliedReplyThis is returned from CodeClient::GetUserSuppliedReply(). This is useful if the key server wishes to communicate that the key has billing problems, been suspended etc. To update userReply for an existing key you may enable duplicateCodeCheck or use RemoveCode() and AddCode().
Returns
Success returns true, failure returns false.
virtual void CallbackDataReceived ( void *  data,
int &  length,
const int  maxLength,
XPAddress address 
)
inlinevirtual

This callback may be used to alter data after it has been received and before the data is processed.

Parameters
dataThe pointer to the data.
lengthThe length of the data. If the output length changes then this variable can be used to pass the value back by reference. A returned length of zero discards the packet.
maxLengthThe maximum length of the data buffer. Parameter length must not be greater than this value.
addressThe network address for this data packet. The contents of the address may be changed if required.
virtual void CallbackDataSend ( void *  data,
int &  length,
const int  maxLength,
XPAddress address 
)
inlinevirtual

This callback may be used to alter any data in a reply, just before it gets sent.

Parameters
dataThe pointer to the data.
lengthThe length of the data. If the output length changes then this variable can be used to pass the value back by reference. A returned length of zero discards the packet.
maxLengthThe maximum length of the data buffer. Parameter length must not be greater than this value.
addressThe network address for this data packet. The contents of the address may be changed if required.
virtual void CallbackKeyData ( char *  keyData,
const int  maxLength,
int &  nOnce1,
int &  nOnce2,
bool &  updateDatabase 
)
inlinevirtual

This callback may be used to alter the actual key queried before it is matched with the database.

Parameters
keyDataThe pointer to a null terminated char string.
maxLengthThe maximum length of the data buffer, including the null termination character.
nOnce1The first nonce.
nOnce2The second nonce.
updateDatabaseThis defaults to true, meaning that the query will update the database with the results of the search. If this is changed to be false the query will only be read only. A read only query does not require correct nonce values to be able to return kReply_ValidKey.
virtual void CallbackQueryReply ( Reply &  reply,
int &  userSuppliedReply,
int &  nOnce1,
int &  nOnce2 
)
inlinevirtual

This callback may be used to alter the CodeServer::Reply.

Parameters
replyThe reply from the key query.
userSuppliedReplyThe user supplied reply.
nOnce1The first reply nonce.
nOnce2The second reply nonce.
virtual void CallbackQueryReplyTime ( const float  seconds)
inlinevirtual

This callback reports how long an query and reply took starting from when packet data was received until the packet data is sent.

Parameters
secondsThe time in seconds.
virtual void CallbackVersion ( const int  version)
inlinevirtual

This callback may be used to ascertain the version of the request made.

Parameters
versionThe version of the request.
float GetCodeTimeout ( void  ) const

Gets the value configured by SetCodeTimeout()

Returns
The value configured by SetCodeTimeout()
bool ReadCodeFile ( const char *  filename,
const bool  duplicateCodeCheck = false 
)

Reads codes from a file and stores the codes in a database in memory. Each code must be in a separate line. This function may be used when the code server has been started. Each call to this function will add codes to the database held in memory. Each code must be no longer than 255 characters.

Parameters
filenameThe filename to read.
duplicateCodeCheckIf this is true then each code is checked to make sure it is not a duplicate code already stored in the database. The default is false, which disables the duplicate code check and makes adding codes slightly faster.
Returns
Success returns true, failure returns false.
bool RemoveCode ( const char *  code = 0)

Removes codes from the database in memory. This function may be used when the code server has been started.

Parameters
codeIf the code is supplied the code is removed from the database in memory. If there is no code supplied then all codes are removed from the database in memory.
Returns
Success returns true, failure returns false.
void SetCodeTimeout ( const float  time = (60.0f *30.0f))

This sets the time period that a code will be flagged as being used and kReply_KeyInUse will be returned from CodeClient::GetReply(). After this time period has elapsed a request to validate the same code will be allowed.

Parameters
timeThe time in seconds. The default value is (60.0f * 30.0f) which is 30 minutes.
void SetEncryptionKey ( const void *  data,
const int  length 
)

Sets the encryption key to be used by all queries for this server. The same key must be used with any RNLobby::CodeClient querying this server. The default encryption key is the same for RNLobby::CodeServer and RNLobby::CodeClient.

Parameters
dataThe data to use as a seed for the key. This can be NULL to use the default key.
lengthThe length of the data to use for creating the key.
bool Start ( const XPAddress address = XPAddress(kDefaultPort))

Starts the code server thread and processes code queries. If not already started the network hardware is started.

Returns
Success returns true, failure returns false.
bool Stop ( bool  hardware = false)

Stops the code server thread and stops processing code queries.

Parameters
hardwareIf true this stops the network hardware. The default is false.
Returns
Success returns true, failure returns false.