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"
|
2014-02-27 00:25:37 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-04-20 01:08:01 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) && (CC_TARGET_PLATFORM != CC_PLATFORM_WP8) && (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
|
2013-12-05 09:38:11 +08:00
|
|
|
#include <unistd.h>
|
2014-02-26 23:41:47 +08:00
|
|
|
#include <sys/types.h>
|
2014-02-24 12:01:04 +08:00
|
|
|
#include <sys/socket.h>
|
2014-02-26 23:41:47 +08:00
|
|
|
#include <netdb.h>
|
2013-12-05 16:12:04 +08:00
|
|
|
#else
|
|
|
|
#include <io.h>
|
2014-02-27 00:13:17 +08:00
|
|
|
#include <WS2tcpip.h>
|
2013-12-05 16:12:04 +08:00
|
|
|
#endif
|
2013-12-05 09:38:11 +08:00
|
|
|
|
2014-04-20 01:08:01 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_WP8
|
|
|
|
#include "CCWinRTUtils.h"
|
|
|
|
#include <sstream>
|
|
|
|
#endif
|
|
|
|
|
2013-12-04 10:46:54 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
2014-02-26 23:41:47 +08:00
|
|
|
// ConsoleTest
|
2013-12-04 10:46:54 +08:00
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
|
|
|
static int sceneIdx = -1;
|
|
|
|
|
|
|
|
static std::function<Layer*()> createFunctions[] =
|
|
|
|
{
|
2013-12-05 09:38:11 +08:00
|
|
|
CL(ConsoleCustomCommand),
|
2014-02-26 23:41:47 +08:00
|
|
|
CL(ConsoleUploadFile),
|
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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string BaseTestConsole::title() const
|
2013-12-04 10:46:54 +08:00
|
|
|
{
|
|
|
|
return "No title";
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseTestConsole::onEnter()
|
|
|
|
{
|
|
|
|
BaseTest::onEnter();
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void BaseTestConsole::restartCallback(Ref* sender)
|
2013-12-04 10:46:54 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto s = new (std::nothrow) ConsoleTestScene();
|
2013-12-04 10:46:54 +08:00
|
|
|
s->addChild(restartConsoleTest());
|
|
|
|
|
|
|
|
Director::getInstance()->replaceScene(s);
|
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void BaseTestConsole::nextCallback(Ref* sender)
|
2013-12-04 10:46:54 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto s = new (std::nothrow) ConsoleTestScene();
|
2013-12-04 10:46:54 +08:00
|
|
|
s->addChild( nextConsoleTest() );
|
|
|
|
Director::getInstance()->replaceScene(s);
|
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void BaseTestConsole::backCallback(Ref* sender)
|
2013-12-04 10:46:54 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto s = new (std::nothrow) ConsoleTestScene();
|
2013-12-04 10:46:54 +08:00
|
|
|
s->addChild( backConsoleTest() );
|
|
|
|
Director::getInstance()->replaceScene(s);
|
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleTestScene::runThisTest()
|
|
|
|
{
|
|
|
|
auto layer = nextConsoleTest();
|
|
|
|
addChild(layer);
|
|
|
|
|
|
|
|
Director::getInstance()->replaceScene(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
2014-02-26 23:41:47 +08:00
|
|
|
// ConsoleCustomCommand
|
2013-12-04 10:46:54 +08:00
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
2014-02-26 23:41:47 +08:00
|
|
|
ConsoleCustomCommand::ConsoleCustomCommand()
|
2013-12-04 10:46:54 +08:00
|
|
|
{
|
2014-01-11 09:11:14 +08:00
|
|
|
_console = Director::getInstance()->getConsole();
|
2014-02-26 23:41:47 +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: ";
|
|
|
|
send(fd, msg, sizeof(msg),0);
|
|
|
|
send(fd, args.c_str(), args.length(),0);
|
|
|
|
send(fd, "\n",1,0);
|
|
|
|
}},
|
|
|
|
};
|
|
|
|
_console->addCommand(commands[0]);
|
2014-04-20 01:08:01 +08:00
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "WP8 Device IP Addresses:" << std::endl;
|
|
|
|
ss << getDeviceIPAddresses();
|
|
|
|
|
|
|
|
auto origin = Director::getInstance()->getVisibleOrigin();
|
|
|
|
auto visibleSize = Director::getInstance()->getVisibleSize();
|
|
|
|
auto label = LabelTTF::create(ss.str(), "Arial", 12);
|
|
|
|
|
|
|
|
// position the label on the center of the screen
|
2014-08-28 11:41:18 +08:00
|
|
|
label->setPosition(origin.x + visibleSize.width/2,
|
|
|
|
origin.y + visibleSize.height/2 + (label->getContentSize().height/2));
|
2014-04-20 01:08:01 +08:00
|
|
|
|
|
|
|
// add the label as a child to this layer
|
|
|
|
this->addChild(label, 1);
|
|
|
|
#endif
|
2013-12-04 10:46:54 +08:00
|
|
|
}
|
|
|
|
|
2014-02-26 23:41:47 +08:00
|
|
|
ConsoleCustomCommand::~ConsoleCustomCommand()
|
2013-12-04 10:46:54 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-26 23:41:47 +08:00
|
|
|
void ConsoleCustomCommand::onEnter()
|
2013-12-04 10:46:54 +08:00
|
|
|
{
|
|
|
|
BaseTestConsole::onEnter();
|
|
|
|
}
|
|
|
|
|
2014-02-26 23:41:47 +08:00
|
|
|
std::string ConsoleCustomCommand::title() const
|
2013-12-04 10:46:54 +08:00
|
|
|
{
|
2014-02-26 23:41:47 +08:00
|
|
|
return "Console Custom Commands";
|
2013-12-04 10:46:54 +08:00
|
|
|
}
|
|
|
|
|
2014-02-26 23:41:47 +08:00
|
|
|
std::string ConsoleCustomCommand::subtitle() const
|
2013-12-18 10:02:11 +08:00
|
|
|
{
|
2014-04-20 01:08:01 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_WP8
|
|
|
|
return "telnet [ip address] 5678";
|
|
|
|
#else
|
2013-12-18 10:02:11 +08:00
|
|
|
return "telnet localhost 5678";
|
2014-04-20 01:08:01 +08:00
|
|
|
#endif
|
2013-12-18 10:02:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-04 10:46:54 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
2014-02-26 23:41:47 +08:00
|
|
|
// ConsoleUploadFile
|
2013-12-04 10:46:54 +08:00
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
2014-02-26 23:41:47 +08:00
|
|
|
ConsoleUploadFile::ConsoleUploadFile()
|
2013-12-04 10:46:54 +08:00
|
|
|
{
|
2014-04-09 22:53:59 +08:00
|
|
|
srand ((unsigned)time(nullptr));
|
2014-02-26 23:41:47 +08:00
|
|
|
int _id = rand()%100000;
|
2014-02-27 00:25:37 +08:00
|
|
|
char buf[32];
|
|
|
|
sprintf(buf, "%d", _id);
|
|
|
|
_target_file_name = std::string("grossini") + buf;
|
2014-02-26 23:41:47 +08:00
|
|
|
|
2014-04-10 18:31:28 +08:00
|
|
|
_src_file_path = FileUtils::getInstance()->fullPathForFilename(s_pathGrossini);
|
|
|
|
std::thread t = std::thread( &ConsoleUploadFile::uploadFile, this);
|
|
|
|
t.detach();
|
2013-12-04 10:46:54 +08:00
|
|
|
}
|
|
|
|
|
2014-02-26 23:41:47 +08:00
|
|
|
void ConsoleUploadFile::onEnter()
|
2013-12-04 10:46:54 +08:00
|
|
|
{
|
2014-02-26 23:41:47 +08:00
|
|
|
BaseTestConsole::onEnter();
|
|
|
|
|
2013-12-04 10:46:54 +08:00
|
|
|
}
|
|
|
|
|
2014-02-26 23:41:47 +08:00
|
|
|
ConsoleUploadFile::~ConsoleUploadFile()
|
2013-12-04 10:46:54 +08:00
|
|
|
{
|
2014-04-10 18:31:28 +08:00
|
|
|
|
2013-12-04 10:46:54 +08:00
|
|
|
}
|
2014-03-05 15:04:30 +08:00
|
|
|
|
2014-02-26 23:41:47 +08:00
|
|
|
void ConsoleUploadFile::uploadFile()
|
|
|
|
{
|
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo *result, *rp;
|
2014-03-05 15:04:30 +08:00
|
|
|
int sfd, s;
|
2013-12-04 10:46:54 +08:00
|
|
|
|
2014-02-26 23:41:47 +08:00
|
|
|
/* Obtain address(es) matching host/port */
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
hints.ai_family = AF_INET; /* Allow IPv4 or IPv6 */
|
|
|
|
hints.ai_socktype = SOCK_STREAM; /* stream socket */
|
|
|
|
hints.ai_flags = 0;
|
|
|
|
hints.ai_protocol = 0; /* Any protocol */
|
|
|
|
|
2015-02-28 02:38:18 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2014-02-26 23:41:47 +08:00
|
|
|
WSADATA wsaData;
|
2014-02-27 00:13:17 +08:00
|
|
|
WSAStartup(MAKEWORD(2, 2),&wsaData);
|
2014-02-26 23:41:47 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
s = getaddrinfo("localhost", "5678", &hints, &result);
|
2014-02-27 00:13:17 +08:00
|
|
|
if (s != 0)
|
|
|
|
{
|
|
|
|
CCLOG("ConsoleUploadFile: getaddrinfo error");
|
2014-02-26 23:41:47 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* getaddrinfo() returns a list of address structures.
|
|
|
|
Try each address until we successfully connect(2).
|
|
|
|
If socket(2) (or connect(2)) fails, we (close the socket
|
|
|
|
and) try the next address. */
|
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
for (rp = result; rp != nullptr; rp = rp->ai_next) {
|
2014-02-26 23:41:47 +08:00
|
|
|
sfd = socket(rp->ai_family, rp->ai_socktype,
|
|
|
|
rp->ai_protocol);
|
|
|
|
if (sfd == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
|
|
|
|
break; /* Success */
|
|
|
|
|
2015-02-28 02:38:18 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2014-02-26 23:41:47 +08:00
|
|
|
closesocket(sfd);
|
|
|
|
#else
|
|
|
|
close(sfd);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
if (rp == nullptr) { /* No address succeeded */
|
2014-02-26 23:41:47 +08:00
|
|
|
CCLOG("ConsoleUploadFile: could not connect!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(result); /* No longer needed */
|
|
|
|
|
|
|
|
|
|
|
|
FILE* fp = fopen(_src_file_path.c_str(), "rb");
|
|
|
|
if(!fp)
|
|
|
|
{
|
|
|
|
CCLOG("ConsoleUploadFile: could not open file %s", _src_file_path.c_str());
|
|
|
|
return;
|
|
|
|
}
|
2014-03-18 00:42:36 +08:00
|
|
|
|
2014-02-26 23:41:47 +08:00
|
|
|
std::string tmp = "upload";
|
|
|
|
|
|
|
|
tmp += " ";
|
|
|
|
tmp += _target_file_name;
|
|
|
|
tmp += " ";
|
|
|
|
char cmd[512];
|
|
|
|
|
|
|
|
strcpy(cmd, tmp.c_str());
|
|
|
|
send(sfd,cmd,strlen(cmd),0);
|
2014-03-18 00:42:36 +08:00
|
|
|
while(true)
|
2014-02-26 23:41:47 +08:00
|
|
|
{
|
2014-03-18 00:42:36 +08:00
|
|
|
char buffer[3], *out;
|
|
|
|
unsigned char *in;
|
|
|
|
in = (unsigned char *)buffer;
|
|
|
|
// copy the file into the buffer:
|
2014-03-20 16:24:55 +08:00
|
|
|
size_t ret = fread(buffer, 1, 3, fp);
|
2014-03-18 00:42:36 +08:00
|
|
|
if (ret > 0)
|
|
|
|
{
|
2014-03-20 16:24:55 +08:00
|
|
|
base64Encode(in, (unsigned int)ret, &out);
|
2014-03-18 00:42:36 +08:00
|
|
|
send(sfd, out, 4, 0);
|
|
|
|
free(out);
|
|
|
|
if(ret < 3)
|
|
|
|
{
|
|
|
|
//eof
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//read error
|
|
|
|
break;
|
|
|
|
}
|
2014-02-26 23:41:47 +08:00
|
|
|
}
|
2014-03-18 00:42:36 +08:00
|
|
|
char l = '\n';
|
|
|
|
send(sfd, &l, 1, 0);
|
2014-02-26 23:41:47 +08:00
|
|
|
// terminate
|
|
|
|
fclose (fp);
|
2014-03-18 00:42:36 +08:00
|
|
|
|
2014-12-24 10:45:29 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2014-02-26 23:41:47 +08:00
|
|
|
closesocket(sfd);
|
|
|
|
WSACleanup();
|
|
|
|
#else
|
|
|
|
close(sfd);
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ConsoleUploadFile::title() const
|
2013-12-04 10:46:54 +08:00
|
|
|
{
|
2014-02-26 23:41:47 +08:00
|
|
|
return "Console UploadFile";
|
2013-12-04 10:46:54 +08:00
|
|
|
}
|
2013-12-18 10:02:11 +08:00
|
|
|
|
2014-02-26 23:41:47 +08:00
|
|
|
std::string ConsoleUploadFile::subtitle() const
|
2013-12-18 10:02:11 +08:00
|
|
|
{
|
2014-02-26 23:41:47 +08:00
|
|
|
auto sharedFileUtils = FileUtils::getInstance();
|
|
|
|
|
|
|
|
std::string writablePath = sharedFileUtils->getWritablePath();
|
|
|
|
|
|
|
|
return "file uploaded to:" + writablePath + _target_file_name;
|
2013-12-18 10:02:11 +08:00
|
|
|
}
|
2014-02-26 23:41:47 +08:00
|
|
|
|