PuzzleSDK
ConsoleUploadFile类 参考

#include <ConsoleTest.h>

+ 类 ConsoleUploadFile 继承关系图:
+ ConsoleUploadFile 的协作图:

Public 成员函数

 CREATE_FUNC (ConsoleUploadFile)
 
virtual void onEnter () override
 
virtual std::string title () const override
 
virtual std::string subtitle () const override
 
- Public 成员函数 继承自 BaseTestConsole
 BaseTestConsole ()
 
 ~BaseTestConsole ()
 
- Public 成员函数 继承自 TestCase
 TestCase ()
 
 ~TestCase ()
 
virtual Type getTestType () const
 
virtual float getDuration () const
 
virtual std::string getExpectedOutput () const
 
virtual std::string getActualOutput () const
 
virtual void restartTestCallback (cocos2d::Ref *sender)
 
virtual void nextTestCallback (cocos2d::Ref *sender)
 
virtual void priorTestCallback (cocos2d::Ref *sender)
 
virtual void onBackCallback (cocos2d::Ref *sender)
 
void setTestSuite (TestSuite *testSuite)
 
TestSuitegetTestSuite () const
 
float getRunTime () const
 
void setTestCaseName (const std::string &name)
 
std::string getTestCaseName () const
 
const cocos2d::Label * getSubtitleLable () const
 
const cocos2d::MenuItemImage * getRestartTestItem () const
 

Protected 成员函数

 ConsoleUploadFile ()
 
virtual ~ConsoleUploadFile ()
 
void uploadFile ()
 

Private 成员函数

 CC_DISALLOW_COPY_AND_ASSIGN (ConsoleUploadFile)
 

Private 属性

std::string _targetFileName
 

额外继承的成员函数

- Public 类型 继承自 TestCase
enum class  Type { ROBUSTNESS , UNIT , GRAPHICAL_STATIC , MANUAL }
 
- Public 属性 继承自 TestCase
CC_CONSTRUCTOR_ACCESS __pad0__: virtual bool init() override
 
- Protected 属性 继承自 TestCase
cocos2d::MenuItemImage * _priorTestItem
 
cocos2d::MenuItemImage * _restartTestItem
 
cocos2d::MenuItemImage * _nextTestItem
 
cocos2d::Label * _titleLabel
 
cocos2d::Label * _subtitleLabel
 

详细描述

在文件 ConsoleTest.h62 行定义.

构造及析构函数说明

◆ ConsoleUploadFile()

ConsoleUploadFile::ConsoleUploadFile ( )
protected

在文件 ConsoleTest.cpp113 行定义.

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}
std::string _targetFileName
Definition: ConsoleTest.h:79

引用了 _targetFileName , 以及 uploadFile().

+ 函数调用图:

◆ ~ConsoleUploadFile()

ConsoleUploadFile::~ConsoleUploadFile ( )
protectedvirtual

在文件 ConsoleTest.cpp131 行定义.

132{
133
134}

成员函数说明

◆ CC_DISALLOW_COPY_AND_ASSIGN()

ConsoleUploadFile::CC_DISALLOW_COPY_AND_ASSIGN ( ConsoleUploadFile  )
private

◆ CREATE_FUNC()

ConsoleUploadFile::CREATE_FUNC ( ConsoleUploadFile  )

◆ onEnter()

void ConsoleUploadFile::onEnter ( )
overridevirtual

重载 TestCase .

在文件 ConsoleTest.cpp125 行定义.

126{
128
129}
virtual void onEnter() override
Definition: BaseTest.cpp:430

引用了 TestCase::onEnter().

+ 函数调用图:

◆ subtitle()

std::string ConsoleUploadFile::subtitle ( ) const
overridevirtual

重载 TestCase .

在文件 ConsoleTest.cpp271 行定义.

272{
273 auto sharedFileUtils = FileUtils::getInstance();
274
275 std::string writablePath = sharedFileUtils->getWritablePath();
276
277 return "file uploaded to:" + writablePath + _targetFileName;
278}

引用了 _targetFileName.

◆ title()

std::string ConsoleUploadFile::title ( ) const
overridevirtual

重载 BaseTestConsole .

在文件 ConsoleTest.cpp266 行定义.

267{
268 return "Console UploadFile";
269}

◆ uploadFile()

void ConsoleUploadFile::uploadFile ( )
protected

在文件 ConsoleTest.cpp136 行定义.

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}
static const char s_pathGrossini[]
Definition: testResource.h:28

引用了 _targetFileName , 以及 s_pathGrossini.

被这些函数引用 ConsoleUploadFile().

+ 这是这个函数的调用关系图:

类成员变量说明

◆ _targetFileName

std::string ConsoleUploadFile::_targetFileName
private

在文件 ConsoleTest.h79 行定义.

被这些函数引用 ConsoleUploadFile(), subtitle() , 以及 uploadFile().


该类的文档由以下文件生成: