2012-04-19 14:35:52 +08:00
|
|
|
#include "CurlTest.h"
|
|
|
|
#include "stdio.h"
|
|
|
|
#include "stdlib.h"
|
|
|
|
#include "curl/curl.h"
|
|
|
|
|
|
|
|
CurlTest::CurlTest()
|
|
|
|
{
|
2014-04-09 23:31:05 +08:00
|
|
|
auto label = Label::createWithTTF("Curl Test", "fonts/arial.ttf", 28);
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(label, 0);
|
2014-08-28 11:41:18 +08:00
|
|
|
label->setPosition(VisibleRect::center().x, VisibleRect::top().y-50);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-10-23 16:14:03 +08:00
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
|
|
|
listener->onTouchesEnded = CC_CALLBACK_2(CurlTest::onTouchesEnded, this);
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// create a label to display the tip string
|
2014-04-09 23:31:05 +08:00
|
|
|
_label = Label::createWithTTF("Touch the screen to connect", "fonts/arial.ttf", 22);
|
2013-06-15 14:03:30 +08:00
|
|
|
_label->setPosition(VisibleRect::center());
|
|
|
|
addChild(_label, 0);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_label->retain();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2015-01-05 15:55:40 +08:00
|
|
|
struct MemoryStruct {
|
|
|
|
char *memory;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
|
|
|
|
{
|
|
|
|
size_t realsize = size * nmemb;
|
|
|
|
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
|
|
|
|
|
|
|
|
mem->memory = (char*)realloc(mem->memory, mem->size + realsize + 1);
|
|
|
|
if(mem->memory == NULL) {
|
|
|
|
/* out of memory! */
|
|
|
|
printf("not enough memory (realloc returned NULL)\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&(mem->memory[mem->size]), contents, realsize);
|
|
|
|
mem->size += realsize;
|
|
|
|
mem->memory[mem->size] = 0;
|
|
|
|
|
|
|
|
return realsize;
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// the test code is
|
|
|
|
// http://curl.haxx.se/mail/lib-2009-12/0071.html
|
2013-09-03 18:22:03 +08:00
|
|
|
void CurlTest::onTouchesEnded(const std::vector<Touch*>& touches, Event *event)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CURL *curl;
|
|
|
|
CURLcode res;
|
|
|
|
char buffer[10];
|
|
|
|
|
2015-01-05 15:55:40 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
curl = curl_easy_init();
|
|
|
|
if (curl)
|
|
|
|
{
|
2015-01-05 15:55:40 +08:00
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, "http://www.baidu.com");
|
|
|
|
//code from http://curl.haxx.se/libcurl/c/getinmemory.html
|
|
|
|
//If we don't provide a write function for curl, it will recieve error code 23 on windows.
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
res = curl_easy_perform(curl);
|
|
|
|
/* always cleanup */
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
if (res == 0)
|
|
|
|
{
|
2014-07-03 16:08:34 +08:00
|
|
|
_label->setString("Connect successfully!");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprintf(buffer,"code: %i",res);
|
2013-06-15 14:03:30 +08:00
|
|
|
_label->setString(buffer);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_label->setString("no curl");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CurlTest::~CurlTest()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_label->release();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CurlTestScene::runThisTest()
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto layer = new (std::nothrow) CurlTest();
|
2013-07-23 08:25:44 +08:00
|
|
|
addChild(layer);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(this);
|
2013-07-23 08:25:44 +08:00
|
|
|
layer->release();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|