2013-12-05 08:26:21 +08:00
|
|
|
/****************************************************************************
|
2015-03-24 20:23:51 +08:00
|
|
|
Copyright (c) 2013-2015 Chukong Technologies Inc.
|
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__
|
2015-03-24 20:23:51 +08:00
|
|
|
/// @cond DO_NOT_SHOW
|
2013-12-05 08:26:21 +08:00
|
|
|
|
2014-01-04 14:40:22 +08:00
|
|
|
#if defined(_MSC_VER) || defined(__MINGW32__)
|
2013-12-05 16:09:38 +08:00
|
|
|
#include <BaseTsd.h>
|
2014-01-12 11:02:48 +08:00
|
|
|
#include <WinSock2.h>
|
2014-04-09 23:54:40 +08:00
|
|
|
|
|
|
|
#ifndef __SSIZE_T
|
|
|
|
#define __SSIZE_T
|
|
|
|
typedef SSIZE_T ssize_t;
|
|
|
|
#endif // __SSIZE_T
|
|
|
|
|
2013-12-05 16:09:38 +08:00
|
|
|
#else
|
2013-12-05 08:26:21 +08:00
|
|
|
#include <sys/select.h>
|
2013-12-05 16:09:38 +08:00
|
|
|
#endif
|
2013-11-27 10:58:36 +08:00
|
|
|
|
|
|
|
#include <thread>
|
2013-12-05 04:26:43 +08:00
|
|
|
#include <vector>
|
2014-02-14 14:54:26 +08:00
|
|
|
#include <map>
|
2013-12-05 08:26:21 +08:00
|
|
|
#include <functional>
|
2014-01-11 09:58:54 +08:00
|
|
|
#include <string>
|
|
|
|
#include <mutex>
|
2014-01-11 11:04:07 +08:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2014-07-03 23:42:06 +08:00
|
|
|
#include "base/CCRef.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccMacros.h"
|
2014-09-10 08:17:07 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
2013-11-27 10:58:36 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-01-11 11:04:07 +08:00
|
|
|
/// The max length of CCLog message.
|
|
|
|
static const int MAX_LOG_LENGTH = 16*1024;
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Output Debug message.
|
|
|
|
*/
|
|
|
|
void CC_DLL log(const char * format, ...) CC_FORMAT_PRINTF(1, 2);
|
|
|
|
|
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( ... );
|
|
|
|
```
|
|
|
|
*/
|
2014-03-22 21:11:10 +08:00
|
|
|
|
2014-01-11 09:11:14 +08:00
|
|
|
class CC_DLL Console
|
2014-07-03 23:42:06 +08:00
|
|
|
: public Ref
|
2013-11-27 10:58:36 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-12-05 09:38:11 +08:00
|
|
|
struct Command {
|
2014-04-08 18:20:06 +08:00
|
|
|
std::string name;
|
|
|
|
std::string help;
|
2014-02-09 04:46:44 +08:00
|
|
|
std::function<void(int, const std::string&)> callback;
|
2013-12-05 09:38:11 +08:00
|
|
|
};
|
|
|
|
|
2014-01-11 09:11:14 +08:00
|
|
|
/** Constructor */
|
|
|
|
Console();
|
|
|
|
|
|
|
|
/** Destructor */
|
|
|
|
virtual ~Console();
|
2013-12-04 10:46:54 +08:00
|
|
|
|
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
|
|
|
|
2014-01-11 09:11:14 +08:00
|
|
|
/** stops the Console. 'stop' will be called at destruction time as well */
|
|
|
|
void stop();
|
2013-11-27 10:58:36 +08:00
|
|
|
|
2014-02-13 10:40:57 +08:00
|
|
|
/** add custom command */
|
2014-02-14 14:54:26 +08:00
|
|
|
void addCommand(const Command& cmd);
|
2014-01-11 09:58:54 +08:00
|
|
|
/** log something in the console */
|
|
|
|
void log(const char *buf);
|
2014-12-24 02:04:42 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* set bind address
|
|
|
|
*
|
|
|
|
* @address : 127.0.0.1
|
|
|
|
*/
|
|
|
|
void setBindAddress(const std::string &address);
|
2014-02-24 12:01:04 +08:00
|
|
|
|
2013-11-27 10:58:36 +08:00
|
|
|
protected:
|
|
|
|
void loop();
|
2014-03-20 14:50:02 +08:00
|
|
|
ssize_t readline(int fd, char *buf, size_t maxlen);
|
|
|
|
ssize_t readBytes(int fd, char* buffer, size_t maxlen, bool* more);
|
2013-12-05 09:38:11 +08:00
|
|
|
bool parseCommand(int fd);
|
2014-02-13 10:40:57 +08:00
|
|
|
|
2013-12-05 08:26:21 +08:00
|
|
|
void addClient();
|
|
|
|
|
|
|
|
// Add commands here
|
2014-02-09 04:46:44 +08:00
|
|
|
void commandHelp(int fd, const std::string &args);
|
|
|
|
void commandExit(int fd, const std::string &args);
|
|
|
|
void commandSceneGraph(int fd, const std::string &args);
|
|
|
|
void commandFileUtils(int fd, const std::string &args);
|
|
|
|
void commandConfig(int fd, const std::string &args);
|
|
|
|
void commandTextures(int fd, const std::string &args);
|
|
|
|
void commandResolution(int fd, const std::string &args);
|
|
|
|
void commandProjection(int fd, const std::string &args);
|
2014-02-20 11:15:29 +08:00
|
|
|
void commandDirector(int fd, const std::string &args);
|
2014-02-25 11:47:53 +08:00
|
|
|
void commandTouch(int fd, const std::string &args);
|
2014-03-18 00:42:36 +08:00
|
|
|
void commandUpload(int fd);
|
2014-12-13 00:57:06 +08:00
|
|
|
void commandAllocator(int fd, const std::string &args);
|
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;
|
|
|
|
|
2014-02-14 14:54:26 +08:00
|
|
|
std::map<std::string, Command> _commands;
|
2014-01-11 09:58:54 +08:00
|
|
|
|
|
|
|
// strings generated by cocos2d sent to the remote console
|
|
|
|
bool _sendDebugStrings;
|
|
|
|
std::mutex _DebugStringsMutex;
|
|
|
|
std::vector<std::string> _DebugStrings;
|
|
|
|
|
2014-03-26 15:04:11 +08:00
|
|
|
intptr_t _touchId;
|
2014-12-24 02:04:42 +08:00
|
|
|
|
|
|
|
std::string _bindAddress;
|
2013-11-27 10:58:36 +08:00
|
|
|
private:
|
|
|
|
CC_DISALLOW_COPY_AND_ASSIGN(Console);
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
2015-03-24 20:23:51 +08:00
|
|
|
/// @endcond
|
2013-12-05 08:26:21 +08:00
|
|
|
#endif /* defined(__CCCONSOLE_H__) */
|