2013-12-05 08:26:21 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
2013-11-27 10:58:36 +08:00
|
|
|
|
2013-12-05 08:26:21 +08:00
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef __CCCONSOLE_H__
|
|
|
|
#define __CCCONSOLE_H__
|
|
|
|
|
|
|
|
#include <sys/select.h>
|
2013-11-27 10:58:36 +08:00
|
|
|
|
|
|
|
#include <thread>
|
2013-12-05 04:26:43 +08:00
|
|
|
#include <vector>
|
2013-12-05 08:26:21 +08:00
|
|
|
#include <functional>
|
2013-11-27 10:58:36 +08:00
|
|
|
|
|
|
|
#include "ccMacros.h"
|
|
|
|
#include "base/CCObject.h"
|
|
|
|
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-12-05 08:26:21 +08:00
|
|
|
/** Console is helper class that lets the developer control the game from TCP connection.
|
|
|
|
Console will spawn a new thread that will listen to a specified TCP port.
|
|
|
|
Console has a basic token parser. Each token is associated with an std::function<void(int)>.
|
|
|
|
If the std::function<> needs to use the cocos2d API, it needs to call
|
|
|
|
|
|
|
|
```
|
|
|
|
scheduler->performFunctionInCocosThread( ... );
|
|
|
|
```
|
|
|
|
*/
|
2013-11-27 10:58:36 +08:00
|
|
|
class CC_DLL Console : public Object
|
|
|
|
{
|
|
|
|
public:
|
2013-12-05 09:38:11 +08:00
|
|
|
|
|
|
|
struct Command {
|
|
|
|
const char *name;
|
|
|
|
const std::function<void(int)> callback;
|
|
|
|
};
|
|
|
|
|
2013-12-05 08:26:21 +08:00
|
|
|
/** creates a new instnace of the Console */
|
2013-12-04 10:46:54 +08:00
|
|
|
static Console* create();
|
|
|
|
|
2013-12-05 08:26:21 +08:00
|
|
|
/** starts listening to specifed TCP port */
|
2013-12-04 10:46:54 +08:00
|
|
|
bool listenOnTCP(int port);
|
2013-12-05 08:26:21 +08:00
|
|
|
|
|
|
|
/** starts listening to specifed file descriptor */
|
2013-12-04 10:46:54 +08:00
|
|
|
bool listenOnFileDescriptor(int fd);
|
2013-11-27 10:58:36 +08:00
|
|
|
|
2013-12-05 08:26:21 +08:00
|
|
|
/** cancels the Console. Cancel will be called at destruction time as well */
|
2013-11-27 10:58:36 +08:00
|
|
|
void cancel();
|
|
|
|
|
2013-12-05 09:38:11 +08:00
|
|
|
/** sets user tokens */
|
|
|
|
void setUserCommands( Command* commands, int numberOfCommands);
|
|
|
|
|
2013-11-27 10:58:36 +08:00
|
|
|
protected:
|
|
|
|
Console();
|
|
|
|
virtual ~Console();
|
|
|
|
|
|
|
|
void loop();
|
|
|
|
|
2013-12-05 08:26:21 +08:00
|
|
|
ssize_t readline(int fd);
|
2013-12-05 09:38:11 +08:00
|
|
|
bool parseCommand(int fd);
|
2013-12-05 08:26:21 +08:00
|
|
|
void sendPrompt(int fd);
|
|
|
|
void addClient();
|
|
|
|
|
|
|
|
// Add commands here
|
|
|
|
void commandHelp(int fd);
|
|
|
|
void commandExit(int fd);
|
2013-12-05 08:57:44 +08:00
|
|
|
void commandSceneGraph(int fd);
|
2013-11-27 10:58:36 +08:00
|
|
|
|
|
|
|
// file descriptor: socket, console, etc.
|
2013-12-04 10:46:54 +08:00
|
|
|
int _listenfd;
|
2013-12-05 04:26:43 +08:00
|
|
|
int _maxfd;
|
|
|
|
std::vector<int> _fds;
|
2013-11-27 10:58:36 +08:00
|
|
|
std::thread _thread;
|
2013-12-05 04:26:43 +08:00
|
|
|
|
2013-12-05 08:26:21 +08:00
|
|
|
fd_set _read_set;
|
|
|
|
|
2013-11-27 10:58:36 +08:00
|
|
|
bool _running;
|
|
|
|
bool _endThread;
|
|
|
|
|
2013-12-04 10:46:54 +08:00
|
|
|
char _buffer[512];
|
|
|
|
|
2013-12-05 09:38:11 +08:00
|
|
|
struct Command _commands[15], *_userCommands;
|
|
|
|
int _maxCommands, _maxUserCommands;
|
2013-12-05 08:26:21 +08:00
|
|
|
|
2013-11-27 10:58:36 +08:00
|
|
|
private:
|
|
|
|
CC_DISALLOW_COPY_AND_ASSIGN(Console);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
2013-12-05 08:26:21 +08:00
|
|
|
#endif /* defined(__CCCONSOLE_H__) */
|