PuzzleSDK
CurlTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos2d-x.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24
25#include "platform/CCPlatformConfig.h"
26#if (CC_TARGET_PLATFORM != CC_PLATFORM_MAC) && (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) && (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
27#include "CurlTest.h"
28#include "stdio.h"
29#include "stdlib.h"
30#include "curl/curl.h"
31
33
34CurlTests::CurlTests()
35{
37}
38
40{
41 auto label = Label::createWithTTF("Curl Test", "fonts/arial.ttf", 28);
42 addChild(label, 0);
43 label->setPosition(VisibleRect::center().x, VisibleRect::top().y-50);
44
45 auto listener = EventListenerTouchAllAtOnce::create();
46 listener->onTouchesEnded = CC_CALLBACK_2(CurlTest::onTouchesEnded, this);
47 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
48
49 // create a label to display the tip string
50 _label = Label::createWithTTF("Touch the screen to connect", "fonts/arial.ttf", 22);
51 _label->setPosition(VisibleRect::center());
52 addChild(_label, 0);
53
54 _label->retain();
55}
56
57struct MemoryStruct {
58 char *memory;
59 size_t size;
60};
61
62static size_t
63WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
64{
65 size_t realsize = size * nmemb;
66 struct MemoryStruct *mem = (struct MemoryStruct *)userp;
67
68 mem->memory = (char*)realloc(mem->memory, mem->size + realsize + 1);
69 if(mem->memory == NULL) {
70 /* out of memory! */
71 printf("not enough memory (realloc returned NULL)\n");
72 return 0;
73 }
74
75 memcpy(&(mem->memory[mem->size]), contents, realsize);
76 mem->size += realsize;
77 mem->memory[mem->size] = 0;
78
79 return realsize;
80}
81
82
83// the test code is
84// http://curl.haxx.se/mail/lib-2009-12/0071.html
85void CurlTest::onTouchesEnded(const std::vector<Touch*>& touches, Event *event)
86{
87 CURL *curl;
88 CURLcode res;
89 char buffer[10];
90
91 struct MemoryStruct chunk;
92
93 chunk.memory = (char*)malloc(1); /* will be grown as needed by the realloc above */
94 chunk.size = 0; /* no data at this point */
95
96 curl = curl_easy_init();
97 if (curl)
98 {
99 curl_easy_setopt(curl, CURLOPT_URL, "http://webtest.cocos2d-x.org/curltest");
100 //code from http://curl.haxx.se/libcurl/c/getinmemory.html
101 /* we pass our 'chunk' struct to the callback function */
102 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
103 //If we don't provide a write function for curl, it will receive error code 23 on windows.
104 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
105
106 res = curl_easy_perform(curl);
107 /* always cleanup */
108 curl_easy_cleanup(curl);
109 if (res == 0)
110 {
111 _label->setString(StringUtils::format("Connect successfully!\n%s", chunk.memory));
112 }
113 else
114 {
115 sprintf(buffer,"code: %i",res);
116 _label->setString(buffer);
117 }
118 }
119 else
120 {
121 _label->setString("no curl");
122 }
123}
124
126{
127 _label->release();
128}
129#endif
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
void onTouchesEnded(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
cocos2d::Label * _label
Definition: CurlTest.h:44
static cocos2d::Vec2 top()
Definition: VisibleRect.cpp:57
static cocos2d::Vec2 center()
Definition: VisibleRect.cpp:69