27#include "../testResource.h"
30#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
33#include <sys/socket.h>
48ConsoleTests::ConsoleTests()
75 _console = Director::getInstance()->getConsole();
76 static Console::Command commands[] = {
77 {
"hello",
"This is just a user generated command", [](
int fd,
const std::string& args) {
78 const char msg[] =
"how are you?\nArguments passed: ";
79 send(fd, msg,
sizeof(msg),0);
80 send(fd, args.c_str(), args.length(),0);
98 return "Console Custom Commands";
103 return "telnet localhost 5678";
115 std::srand ((
unsigned)time(
nullptr));
116 int id = rand()%100000;
118 sprintf(buf,
"%d",
id);
138 Data srcFileData = FileUtils::getInstance()->getDataFromFile(
s_pathGrossini);
139 if (srcFileData.isNull())
141 CCLOGERROR(
"ConsoleUploadFile: could not open file %s",
s_pathGrossini);
146 struct addrinfo hints;
147 struct addrinfo *result, *rp;
152 memset(&hints, 0,
sizeof(
struct addrinfo));
153 hints.ai_family = AF_UNSPEC;
154 hints.ai_socktype = SOCK_STREAM;
156 hints.ai_protocol = 0;
158#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
160 WSAStartup(MAKEWORD(2, 2),&wsaData);
163 std::string nodeName;
164 if (Director::getInstance()->getConsole()->isIpv6Server())
167 nodeName =
"localhost";
169 s = getaddrinfo(nodeName.c_str(),
"5678", &hints, &result);
172 CCLOG(
"ConsoleUploadFile: getaddrinfo error");
181 for (rp = result; rp !=
nullptr; rp = rp->ai_next) {
182 sfd = socket(rp->ai_family, rp->ai_socktype,
187 if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
190#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
198 CCLOG(
"ConsoleUploadFile: could not connect!");
202 freeaddrinfo(result);
204 std::string tmp =
"upload";
207 tmp += targetFileName;
211 strcpy(cmd, tmp.c_str());
212 send(sfd,cmd,strlen(cmd),0);
215 auto readBuffer = [&offset](
char* buf,
size_t bytes,
const Data& data) -> ssize_t {
216 if (offset >= data.getSize())
218 ssize_t actualReadBytes = (offset + bytes) > data.getSize() ? (data.getSize() - offset) : bytes;
219 if (actualReadBytes > 0)
221 memcpy(buf, data.getBytes() + offset, actualReadBytes);
222 offset += actualReadBytes;
224 return actualReadBytes;
229 char buffer[3], *out;
231 in = (
unsigned char *)buffer;
233 ssize_t ret = readBuffer(buffer, 3, srcFileData);
236 int len = base64Encode(in, (
unsigned int)ret, &out);
237 send(sfd, out, len, 0);
242 log(
"Reach the end, total send: %d bytes", (
int)offset);
256 std::this_thread::sleep_for(std::chrono::seconds(1));
258#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
268 return "Console UploadFile";
273 auto sharedFileUtils = FileUtils::getInstance();
275 std::string writablePath = sharedFileUtils->getWritablePath();
#define ADD_TEST_CASE(__className__)
virtual std::string title() const override
virtual std::string title() const override
virtual ~ConsoleCustomCommand()
virtual void onEnter() override
cocos2d::Console * _console
virtual std::string subtitle() const override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual ~ConsoleUploadFile()
std::string _targetFileName
virtual void onEnter() override
static const char s_pathGrossini[]