PuzzleSDK
ConsoleTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2013 cocos2d-x.org
3 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
4
5 http://www.cocos2d-x.org
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24 ****************************************************************************/
25
26#include "ConsoleTest.h"
27#include "../testResource.h"
28#include <stdio.h>
29#include <stdlib.h>
30#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
31#include <unistd.h>
32#include <sys/types.h>
33#include <sys/socket.h>
34#include <netdb.h>
35#else
36#include <io.h>
37#include <WS2tcpip.h>
38#endif
39
41
42//------------------------------------------------------------------
43//
44// ConsoleTest
45//
46//------------------------------------------------------------------
47
48ConsoleTests::ConsoleTests()
49{
52}
53
55{
56}
57
59{
60}
61
62std::string BaseTestConsole::title() const
63{
64 return "No title";
65}
66
67//------------------------------------------------------------------
68//
69// ConsoleCustomCommand
70//
71//------------------------------------------------------------------
72
74{
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);
81 send(fd, "\n",1,0);
82 }},
83 };
84 _console->addCommand(commands[0]);
85}
86
88{
89}
90
92{
94}
95
96std::string ConsoleCustomCommand::title() const
97{
98 return "Console Custom Commands";
99}
100
102{
103 return "telnet localhost 5678";
104}
105
106
107//------------------------------------------------------------------
108//
109// ConsoleUploadFile
110//
111//------------------------------------------------------------------
112
114{
115 std::srand ((unsigned)time(nullptr));
116 int id = rand()%100000;
117 char buf[32];
118 sprintf(buf, "%d", id);
119 _targetFileName = std::string("grossini") + buf + ".png";
120
121 std::thread t = std::thread( &ConsoleUploadFile::uploadFile, this);
122 t.detach();
123}
124
126{
128
129}
130
132{
133
134}
135
137{
138 Data srcFileData = FileUtils::getInstance()->getDataFromFile(s_pathGrossini);
139 if (srcFileData.isNull())
140 {
141 CCLOGERROR("ConsoleUploadFile: could not open file %s", s_pathGrossini);
142 }
143
144 std::string targetFileName = _targetFileName;
145
146 struct addrinfo hints;
147 struct addrinfo *result, *rp;
148 int sfd, s;
149
150 /* Obtain address(es) matching host/port */
151
152 memset(&hints, 0, sizeof(struct addrinfo));
153 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
154 hints.ai_socktype = SOCK_STREAM; /* stream socket */
155 hints.ai_flags = 0;
156 hints.ai_protocol = 0; /* Any protocol */
157
158#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
159 WSADATA wsaData;
160 WSAStartup(MAKEWORD(2, 2),&wsaData);
161#endif
162
163 std::string nodeName;
164 if (Director::getInstance()->getConsole()->isIpv6Server())
165 nodeName = "::1";
166 else
167 nodeName = "localhost";
168
169 s = getaddrinfo(nodeName.c_str(), "5678", &hints, &result);
170 if (s != 0)
171 {
172 CCLOG("ConsoleUploadFile: getaddrinfo error");
173 return;
174 }
175
176 /* getaddrinfo() returns a list of address structures.
177 Try each address until we successfully connect(2).
178 If socket(2) (or connect(2)) fails, we (close the socket
179 and) try the next address. */
180
181 for (rp = result; rp != nullptr; rp = rp->ai_next) {
182 sfd = socket(rp->ai_family, rp->ai_socktype,
183 rp->ai_protocol);
184 if (sfd == -1)
185 continue;
186
187 if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
188 break; /* Success */
189
190#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
191 closesocket(sfd);
192#else
193 close(sfd);
194#endif
195 }
196
197 if (rp == nullptr) { /* No address succeeded */
198 CCLOG("ConsoleUploadFile: could not connect!");
199 return;
200 }
201
202 freeaddrinfo(result); /* No longer needed */
203
204 std::string tmp = "upload";
205
206 tmp += " ";
207 tmp += targetFileName;
208 tmp += " ";
209 char cmd[512] = {0};
210
211 strcpy(cmd, tmp.c_str());
212 send(sfd,cmd,strlen(cmd),0);
213
214 size_t offset = 0;
215 auto readBuffer = [&offset](char* buf, size_t bytes, const Data& data) -> ssize_t {
216 if (offset >= data.getSize())
217 return 0;
218 ssize_t actualReadBytes = (offset + bytes) > data.getSize() ? (data.getSize() - offset) : bytes;
219 if (actualReadBytes > 0)
220 {
221 memcpy(buf, data.getBytes() + offset, actualReadBytes);
222 offset += actualReadBytes;
223 }
224 return actualReadBytes;
225 };
226
227 while(true)
228 {
229 char buffer[3], *out;
230 unsigned char *in;
231 in = (unsigned char *)buffer;
232 // copy the file into the buffer:
233 ssize_t ret = readBuffer(buffer, 3, srcFileData);
234 if (ret > 0)
235 {
236 int len = base64Encode(in, (unsigned int)ret, &out);
237 send(sfd, out, len, 0);
238 free(out);
239 if(ret < 3)
240 {
241 //eof
242 log("Reach the end, total send: %d bytes", (int)offset);
243 break;
244 }
245 }
246 else
247 {
248 //read error
249 break;
250 }
251 }
252 char l = '\n';
253 send(sfd, &l, 1, 0);
254
255 // Sleep 1s to wait server to receive all data.
256 std::this_thread::sleep_for(std::chrono::seconds(1));
257
258#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
259 closesocket(sfd);
260 WSACleanup();
261#else
262 close(sfd);
263#endif
264}
265
266std::string ConsoleUploadFile::title() const
267{
268 return "Console UploadFile";
269}
270
272{
273 auto sharedFileUtils = FileUtils::getInstance();
274
275 std::string writablePath = sharedFileUtils->getWritablePath();
276
277 return "file uploaded to:" + writablePath + _targetFileName;
278}
279
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
USING_NS_CC
Definition: ConsoleTest.cpp:40
virtual std::string title() const override
Definition: ConsoleTest.cpp:62
virtual std::string title() const override
Definition: ConsoleTest.cpp:96
virtual ~ConsoleCustomCommand()
Definition: ConsoleTest.cpp:87
virtual void onEnter() override
Definition: ConsoleTest.cpp:91
cocos2d::Console * _console
Definition: ConsoleTest.h:56
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
Definition: ConsoleTest.h:79
virtual void onEnter() override
Definition: BaseTest.cpp:430
static const char s_pathGrossini[]
Definition: testResource.h:28