axmol/tests/test-cpp/Classes/ConsoleTest/ConsoleTest.cpp

199 lines
4.6 KiB
C++
Raw Normal View History

2013-12-04 10:46:54 +08:00
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
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.
****************************************************************************/
#include "ConsoleTest.h"
#include "../testResource.h"
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
2013-12-05 09:38:11 +08:00
#include <unistd.h>
2014-02-24 12:01:04 +08:00
#include <sys/socket.h>
#else
#include <io.h>
#endif
2013-12-05 09:38:11 +08:00
2013-12-04 10:46:54 +08:00
//------------------------------------------------------------------
//
// EaseSpriteDemo
//
//------------------------------------------------------------------
static int sceneIdx = -1;
static std::function<Layer*()> createFunctions[] =
{
CL(ConsoleTCP),
2013-12-05 09:38:11 +08:00
CL(ConsoleCustomCommand),
2013-12-04 10:46:54 +08:00
};
#define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0]))
Layer* nextConsoleTest()
{
sceneIdx++;
sceneIdx = sceneIdx % MAX_LAYER;
auto layer = (createFunctions[sceneIdx])();
return layer;
}
Layer* backConsoleTest()
{
sceneIdx--;
int total = MAX_LAYER;
if( sceneIdx < 0 )
sceneIdx += total;
auto layer = (createFunctions[sceneIdx])();
return layer;
}
Layer* restartConsoleTest()
{
auto layer = (createFunctions[sceneIdx])();
return layer;
}
BaseTestConsole::BaseTestConsole()
{
}
BaseTestConsole::~BaseTestConsole(void)
{
}
std::string BaseTestConsole::title() const
2013-12-04 10:46:54 +08:00
{
return "No title";
}
void BaseTestConsole::onEnter()
{
BaseTest::onEnter();
}
void BaseTestConsole::restartCallback(Ref* sender)
2013-12-04 10:46:54 +08:00
{
auto s = new ConsoleTestScene();
s->addChild(restartConsoleTest());
Director::getInstance()->replaceScene(s);
s->release();
}
void BaseTestConsole::nextCallback(Ref* sender)
2013-12-04 10:46:54 +08:00
{
auto s = new ConsoleTestScene();
s->addChild( nextConsoleTest() );
Director::getInstance()->replaceScene(s);
s->release();
}
void BaseTestConsole::backCallback(Ref* sender)
2013-12-04 10:46:54 +08:00
{
auto s = new ConsoleTestScene();
s->addChild( backConsoleTest() );
Director::getInstance()->replaceScene(s);
s->release();
}
void ConsoleTestScene::runThisTest()
{
auto layer = nextConsoleTest();
addChild(layer);
Director::getInstance()->replaceScene(this);
}
//------------------------------------------------------------------
//
// ConsoleTCP
//
//------------------------------------------------------------------
ConsoleTCP::ConsoleTCP()
{
_console = Director::getInstance()->getConsole();
2013-12-04 10:46:54 +08:00
}
ConsoleTCP::~ConsoleTCP()
{
}
void ConsoleTCP::onEnter()
{
BaseTestConsole::onEnter();
_console->listenOnTCP(5678);
}
std::string ConsoleTCP::title() const
2013-12-04 10:46:54 +08:00
{
return "Console TCP";
}
std::string ConsoleTCP::subtitle() const
{
return "telnet localhost 5678";
}
2013-12-04 10:46:54 +08:00
//------------------------------------------------------------------
//
2013-12-05 09:38:11 +08:00
// ConsoleCustomCommand
2013-12-04 10:46:54 +08:00
//
//------------------------------------------------------------------
2013-12-05 09:38:11 +08:00
ConsoleCustomCommand::ConsoleCustomCommand()
2013-12-04 10:46:54 +08:00
{
2013-12-05 09:38:11 +08:00
static struct Console::Command commands[] = {
{"hello", "This is just a user generated command", [](int fd, const std::string& args) {
const char msg[] = "how are you?\nArguments passed: ";
2014-02-24 12:01:04 +08:00
send(fd, msg, sizeof(msg),0);
send(fd, args.c_str(), args.length(),0);
send(fd, "\n",1,0);
2013-12-05 09:38:11 +08:00
}},
};
2014-02-13 11:10:23 +08:00
_console->addCommand(commands[0]);
2013-12-04 10:46:54 +08:00
}
2013-12-05 09:38:11 +08:00
ConsoleCustomCommand::~ConsoleCustomCommand()
2013-12-04 10:46:54 +08:00
{
}
2013-12-05 09:38:11 +08:00
void ConsoleCustomCommand::onEnter()
2013-12-04 10:46:54 +08:00
{
BaseTestConsole::onEnter();
2013-12-05 09:38:11 +08:00
_console->listenOnTCP(5678);
2013-12-04 10:46:54 +08:00
}
std::string ConsoleCustomCommand::title() const
2013-12-04 10:46:54 +08:00
{
2013-12-05 09:38:11 +08:00
return "Console Custom Commands";
2013-12-04 10:46:54 +08:00
}
std::string ConsoleCustomCommand::subtitle() const
{
return "telnet localhost 5678";
}