Leviathan  0.8.0.0
Leviathan game engine
Leviathan::CommandHandler Class Reference

Handles all commands sent by the players on the server. More...

#include <CommandHandler.h>

Public Member Functions

DLLEXPORT CommandHandler (NetworkServerInterface *owneraccess)
 Constructs a CommandHandler for use by a single server network interface. More...
 
virtual DLLEXPORT ~CommandHandler ()
 
virtual DLLEXPORT void QueueCommand (const std::string &command, CommandSender *issuer)
 Queues a command to be executed. More...
 
virtual DLLEXPORT void UpdateStatus ()
 Call this periodically to perform cleanup tasks. More...
 
virtual DLLEXPORT void RemoveMe (CommandSender *object)
 This should be called by CommandSender subclasses when they are no longer available to remove their commands from the queue. More...
 
virtual DLLEXPORT bool IsSenderStillValid (CommandSender *checkthis)
 Called by an actual command callback to make sure that the sender is still active. More...
 
virtual DLLEXPORT void SenderNoLongerRequired (CommandSender *checkthis)
 Called by a command handler when a CommandSender is no longer needed. More...
 
virtual DLLEXPORT bool RegisterCustomCommandHandler (std::shared_ptr< CustomCommandHandler > handler)
 Registers a new custom command handler. More...
 
virtual DLLEXPORT bool IsThisDefaultCommand (const std::string &firstword) const
 Returns true when the default command handling function can process this. More...
 

Static Public Member Functions

static DLLEXPORT CommandHandlerGet ()
 

Protected Member Functions

void _LetGoOfAll ()
 Lets go of all the CommandSenders. More...
 
void _AddSender (CommandSender *object)
 Adds a CommandSender to the list of active ones. More...
 

Protected Attributes

std::list< CommandSender * > SendersInUse
 Holds all the command dispatchers who are currently executing commands. More...
 
NetworkServerInterfaceOwner
 Pointer to the owning interface for fetching various other things. More...
 
std::vector< std::shared_ptr< CustomCommandHandler > > CustomHandlers
 All the custom command providers. More...
 

Static Protected Attributes

static CommandHandlerStaticaccess
 A static access member for command executing functions. More...
 

Detailed Description

Handles all commands sent by the players on the server.

Definition at line 77 of file CommandHandler.h.

Constructor & Destructor Documentation

◆ CommandHandler()

DLLEXPORT Leviathan::CommandHandler::CommandHandler ( NetworkServerInterface owneraccess)

Constructs a CommandHandler for use by a single server network interface.

Definition at line 39 of file CommandHandler.cpp.

39  :
40  Owner(owneraccess)
41 {
42  Staticaccess = this;
43 }
static CommandHandler * Staticaccess
A static access member for command executing functions.
NetworkServerInterface * Owner
Pointer to the owning interface for fetching various other things.

◆ ~CommandHandler()

DLLEXPORT Leviathan::CommandHandler::~CommandHandler ( )
virtual

Definition at line 45 of file CommandHandler.cpp.

45  {
46 
47  Staticaccess = NULL;
48 
49  CustomHandlers.clear();
50 
51  // All the pointers have to be removed before deleting this //
52  _LetGoOfAll();
53 }
void _LetGoOfAll()
Lets go of all the CommandSenders.
static CommandHandler * Staticaccess
A static access member for command executing functions.
std::vector< std::shared_ptr< CustomCommandHandler > > CustomHandlers
All the custom command providers.

Member Function Documentation

◆ _AddSender()

void Leviathan::CommandHandler::_AddSender ( CommandSender object)
protected

Adds a CommandSender to the list of active ones.

Definition at line 178 of file CommandHandler.cpp.

178  {
179 
180  // Notify the object //
181  object->StartOwnership(this);
182 
183  // Add to the list //
184  SendersInUse.push_back(object);
185 }
std::list< CommandSender * > SendersInUse
Holds all the command dispatchers who are currently executing commands.

◆ _LetGoOfAll()

void Leviathan::CommandHandler::_LetGoOfAll ( )
protected

Lets go of all the CommandSenders.

Definition at line 149 of file CommandHandler.cpp.

149  {
150 
151  auto end = SendersInUse.end();
152  for(auto iter = SendersInUse.begin(); iter != end; ++iter){
153 
154  (*iter)->EndOwnership(this);
155  }
156 
157  // Clear them all at once //
158  SendersInUse.clear();
159 }
std::list< CommandSender * > SendersInUse
Holds all the command dispatchers who are currently executing commands.

◆ Get()

DLLEXPORT CommandHandler * Leviathan::CommandHandler::Get ( )
static

Definition at line 57 of file CommandHandler.cpp.

57  {
58 
59  return Staticaccess;
60 }
static CommandHandler * Staticaccess
A static access member for command executing functions.

◆ IsSenderStillValid()

DLLEXPORT bool Leviathan::CommandHandler::IsSenderStillValid ( CommandSender checkthis)
virtual

Called by an actual command callback to make sure that the sender is still active.

Parameters
retlockWill contain a lock for the object so hold onto it while using the object

Definition at line 128 of file CommandHandler.cpp.

128  {
129 
130  // Check is it still in the list //
131  auto end = SendersInUse.end();
132  for(auto iter = SendersInUse.begin(); iter != end; ++iter){
133  if(*iter == checkthis){
134  // It is still there //
135  return true;
136  }
137  }
138 
139  // Not found //
140  return false;
141 }
std::list< CommandSender * > SendersInUse
Holds all the command dispatchers who are currently executing commands.

◆ IsThisDefaultCommand()

DLLEXPORT bool Leviathan::CommandHandler::IsThisDefaultCommand ( const std::string &  firstword) const
virtual

Returns true when the default command handling function can process this.

Definition at line 198 of file CommandHandler.cpp.

200 {
201  // Didn't match any of the strings, cannot be //
202  return false;
203 }

◆ QueueCommand()

DLLEXPORT void Leviathan::CommandHandler::QueueCommand ( const std::string &  command,
CommandSender issuer 
)
virtual

Queues a command to be executed.

Definition at line 62 of file CommandHandler.cpp.

64 {
65  // Get the first word //
66  StringIterator itr(std::make_unique<UTF8DataIterator>(command));
67 
68  // Only skip a single / if there is one //
69  if(itr.GetCharacter() == '/')
70  itr.MoveToNext();
71 
72  // Get the first word //
73  auto firstword = itr.GetNextCharacterSequence<std::string>(
75 
76  if(!firstword || firstword->empty())
77  return;
78 
79 
80 
81  // Find a command handler that provides handling for this command //
82 
83  // First check the default commands //
84  if(IsThisDefaultCommand(*firstword)){
85 
86  // Queue a default command execution //
87  DEBUG_BREAK;
88 
89  // Default will also need this //
90  _AddSender(issuer);
91  return;
92  }
93 
94  // Loop through and check the custom command handlers //
95  auto end = CustomHandlers.end();
96  for(auto iter = CustomHandlers.begin(); iter != end; ++iter){
97 
98  if((*iter)->CanHandleCommand(*firstword)){
99  // Queue the command handler //
101  *iter, std::make_shared<std::string>(command), issuer)));
102 
103 
104  // And take good care of the object while the command handler is waiting //
105  _AddSender(issuer);
106  return;
107  }
108  }
109 
110  // Couldn't find a handler for command //
111  issuer->SendPrivateMessage("Unknown command \""+*firstword+"\"");
112 }
DLLEXPORT void QueueTask(std::shared_ptr< QueuedTask > task)
Adds a task to the queue.
static DLLEXPORT ThreadingManager * Get()
virtual DLLEXPORT bool IsThisDefaultCommand(const std::string &firstword) const
Returns true when the default command handling function can process this.
Iterator class for getting parts of a string.
Encapsulates a function that can later be ran in a free thread.
Definition: QueuedTask.h:34
virtual DLLEXPORT void SendPrivateMessage(const std::string &message)
This should send a message to this entity for them to see what happened when they executed a command...
void RunCustomHandler(std::shared_ptr< CustomCommandHandler > handler, std::shared_ptr< std::string > command, CommandSender *sender)
Runs the thing.
std::vector< std::shared_ptr< CustomCommandHandler > > CustomHandlers
All the custom command providers.
void _AddSender(CommandSender *object)
Adds a CommandSender to the list of active ones.

◆ RegisterCustomCommandHandler()

DLLEXPORT bool Leviathan::CommandHandler::RegisterCustomCommandHandler ( std::shared_ptr< CustomCommandHandler handler)
virtual

Registers a new custom command handler.

Parameters
handlerThe object to use for handling. The object will be owned by this and will be deleted when it is no longer used
See also
CustomCommandHandler

Definition at line 187 of file CommandHandler.cpp.

189 {
190  // Might be unnecessary to check this, but it's a way to return false sometimes //
191  if(!handler)
192  return false;
193 
194  CustomHandlers.push_back(handler);
195  return true;
196 }
std::vector< std::shared_ptr< CustomCommandHandler > > CustomHandlers
All the custom command providers.

◆ RemoveMe()

DLLEXPORT void Leviathan::CommandHandler::RemoveMe ( CommandSender object)
virtual

This should be called by CommandSender subclasses when they are no longer available to remove their commands from the queue.

Definition at line 114 of file CommandHandler.cpp.

114  {
115 
116  // Remove from the vector //
117  auto end = SendersInUse.end();
118  for(auto iter = SendersInUse.begin(); iter != end; ++iter){
119 
120  if(*iter == object){
121  // Remove the match //
122  SendersInUse.erase(iter);
123  return;
124  }
125  }
126 }
std::list< CommandSender * > SendersInUse
Holds all the command dispatchers who are currently executing commands.

◆ SenderNoLongerRequired()

DLLEXPORT void Leviathan::CommandHandler::SenderNoLongerRequired ( CommandSender checkthis)
virtual

Called by a command handler when a CommandSender is no longer needed.

Parameters
stillgotthisIs the lock received from IsSenderStillValid

Definition at line 161 of file CommandHandler.cpp.

161  {
162 
163  // Remove from the vector //
164  auto end = SendersInUse.end();
165  for(auto iter = SendersInUse.begin(); iter != end; ++iter){
166 
167  if(*iter == checkthis){
168  // Notify it //
169  (*iter)->EndOwnership(this);
170 
171  // Remove the match //
172  SendersInUse.erase(iter);
173  return;
174  }
175  }
176 }
std::list< CommandSender * > SendersInUse
Holds all the command dispatchers who are currently executing commands.

◆ UpdateStatus()

DLLEXPORT void Leviathan::CommandHandler::UpdateStatus ( )
virtual

Call this periodically to perform cleanup tasks.

and depending on actual implementation the command handling. The default implementation doesn't really need this, but it should still be called

Definition at line 144 of file CommandHandler.cpp.

144  {
145 
146 
147 }

Member Data Documentation

◆ CustomHandlers

std::vector<std::shared_ptr<CustomCommandHandler> > Leviathan::CommandHandler::CustomHandlers
protected

All the custom command providers.

Definition at line 149 of file CommandHandler.h.

◆ Owner

NetworkServerInterface* Leviathan::CommandHandler::Owner
protected

Pointer to the owning interface for fetching various other things.

Definition at line 145 of file CommandHandler.h.

◆ SendersInUse

std::list<CommandSender*> Leviathan::CommandHandler::SendersInUse
protected

Holds all the command dispatchers who are currently executing commands.

Definition at line 141 of file CommandHandler.h.

◆ Staticaccess

CommandHandler * Leviathan::CommandHandler::Staticaccess
staticprotected

A static access member for command executing functions.

Definition at line 153 of file CommandHandler.h.


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