Merge pull request #4466 from dumganhar/iss2790-deprecate-ccarray

issue #2790: HttpClient and TouchesTest uses Vector<T> now.
This commit is contained in:
James Chen 2013-12-10 03:24:31 -08:00
commit 47541523e8
4 changed files with 38 additions and 71 deletions

View File

@ -32,7 +32,7 @@
#include "platform/CCFileUtils.h" #include "platform/CCFileUtils.h"
using namespace cocos2d; using namespace cocos2d;
namespace network { namespace network {
@ -42,7 +42,6 @@ static std::mutex s_responseQueueMutex;
static std::mutex s_SleepMutex; static std::mutex s_SleepMutex;
static std::condition_variable s_SleepCondition; static std::condition_variable s_SleepCondition;
static int s_asyncRequestCount = 0;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
typedef int int32_t; typedef int int32_t;
@ -50,12 +49,12 @@ typedef int int32_t;
static bool s_need_quit = false; static bool s_need_quit = false;
static Array* s_requestQueue = NULL; static Vector<HttpRequest*>* s_requestQueue = nullptr;
static Array* s_responseQueue = NULL; static Vector<HttpResponse*>* s_responseQueue = nullptr;
static HttpClient *s_pHttpClient = NULL; // pointer to singleton static HttpClient *s_pHttpClient = NULL; // pointer to singleton
static char s_errorBuffer[CURL_ERROR_SIZE]; static char s_errorBuffer[CURL_ERROR_SIZE] = {0};
typedef size_t (*write_callback)(void *ptr, size_t size, size_t nmemb, void *stream); typedef size_t (*write_callback)(void *ptr, size_t size, size_t nmemb, void *stream);
@ -96,10 +95,12 @@ static int processDeleteTask(HttpRequest *request, write_callback callback, void
// Worker thread // Worker thread
static void networkThread(void) void HttpClient::networkThread()
{ {
HttpRequest *request = NULL; HttpRequest *request = NULL;
auto scheduler = Director::getInstance()->getScheduler();
while (true) while (true)
{ {
if (s_need_quit) if (s_need_quit)
@ -114,10 +115,10 @@ static void networkThread(void)
//Get request task from queue //Get request task from queue
if (0 != s_requestQueue->count()) if (!s_requestQueue->empty())
{ {
request = dynamic_cast<HttpRequest*>(s_requestQueue->getObjectAtIndex(0)); request = s_requestQueue->at(0);
s_requestQueue->removeObjectAtIndex(0); s_requestQueue->remove(0);
} }
s_requestQueueMutex.unlock(); s_requestQueueMutex.unlock();
@ -202,26 +203,23 @@ static void networkThread(void)
// add response packet into queue // add response packet into queue
s_responseQueueMutex.lock(); s_responseQueueMutex.lock();
s_responseQueue->addObject(response); s_responseQueue->pushBack(response);
s_responseQueueMutex.unlock(); s_responseQueueMutex.unlock();
// resume dispatcher selector scheduler->performFunctionInCocosThread(CC_CALLBACK_0(HttpClient::dispatchResponseCallbacks, this));
Director::getInstance()->getScheduler()->resumeTarget(HttpClient::getInstance());
} }
// cleanup: if worker thread received quit signal, clean up un-completed request queue // cleanup: if worker thread received quit signal, clean up un-completed request queue
s_requestQueueMutex.lock(); s_requestQueueMutex.lock();
s_requestQueue->removeAllObjects(); s_requestQueue->clear();
s_requestQueueMutex.unlock(); s_requestQueueMutex.unlock();
s_asyncRequestCount -= s_requestQueue->count();
if (s_requestQueue != NULL) { if (s_requestQueue != nullptr) {
delete s_requestQueue;
s_requestQueue->release(); s_requestQueue = nullptr;
s_requestQueue = NULL; delete s_responseQueue;
s_responseQueue->release(); s_responseQueue = nullptr;
s_responseQueue = NULL;
} }
} }
@ -399,7 +397,6 @@ HttpClient* HttpClient::getInstance()
void HttpClient::destroyInstance() void HttpClient::destroyInstance()
{ {
CCASSERT(s_pHttpClient, ""); CCASSERT(s_pHttpClient, "");
Director::getInstance()->getScheduler()->unscheduleSelector(schedule_selector(HttpClient::dispatchResponseCallbacks), s_pHttpClient);
s_pHttpClient->release(); s_pHttpClient->release();
} }
@ -416,9 +413,6 @@ HttpClient::HttpClient()
: _timeoutForConnect(30) : _timeoutForConnect(30)
, _timeoutForRead(60) , _timeoutForRead(60)
{ {
Director::getInstance()->getScheduler()->scheduleSelector(
schedule_selector(HttpClient::dispatchResponseCallbacks), this, 0, false);
Director::getInstance()->getScheduler()->pauseTarget(this);
} }
HttpClient::~HttpClient() HttpClient::~HttpClient()
@ -439,14 +433,10 @@ bool HttpClient::lazyInitThreadSemphore()
return true; return true;
} else { } else {
s_requestQueue = new Array(); s_requestQueue = new Vector<HttpRequest*>();
s_requestQueue->init(); s_responseQueue = new Vector<HttpResponse*>();
s_responseQueue = new Array(); auto t = std::thread(CC_CALLBACK_0(HttpClient::networkThread, this));
s_responseQueue->init();
auto t = std::thread(&networkThread);
t.detach(); t.detach();
s_need_quit = false; s_need_quit = false;
@ -468,12 +458,10 @@ void HttpClient::send(HttpRequest* request)
return; return;
} }
++s_asyncRequestCount;
request->retain(); request->retain();
s_requestQueueMutex.lock(); s_requestQueueMutex.lock();
s_requestQueue->addObject(request); s_requestQueue->pushBack(request);
s_requestQueueMutex.unlock(); s_requestQueueMutex.unlock();
// Notify thread start to work // Notify thread start to work
@ -481,7 +469,7 @@ void HttpClient::send(HttpRequest* request)
} }
// Poll and notify main thread if responses exists in queue // Poll and notify main thread if responses exists in queue
void HttpClient::dispatchResponseCallbacks(float delta) void HttpClient::dispatchResponseCallbacks()
{ {
// log("CCHttpClient::dispatchResponseCallbacks is running"); // log("CCHttpClient::dispatchResponseCallbacks is running");
@ -489,18 +477,16 @@ void HttpClient::dispatchResponseCallbacks(float delta)
s_responseQueueMutex.lock(); s_responseQueueMutex.lock();
if (s_responseQueue->count()) if (!s_responseQueue->empty())
{ {
response = dynamic_cast<HttpResponse*>(s_responseQueue->getObjectAtIndex(0)); response = s_responseQueue->at(0);
s_responseQueue->removeObjectAtIndex(0); s_responseQueue->remove(0);
} }
s_responseQueueMutex.unlock(); s_responseQueueMutex.unlock();
if (response) if (response)
{ {
--s_asyncRequestCount;
HttpRequest *request = response->getHttpRequest(); HttpRequest *request = response->getHttpRequest();
Object *pTarget = request->getTarget(); Object *pTarget = request->getTarget();
SEL_HttpResponse pSelector = request->getSelector(); SEL_HttpResponse pSelector = request->getSelector();
@ -512,12 +498,6 @@ void HttpClient::dispatchResponseCallbacks(float delta)
response->release(); response->release();
} }
if (0 == s_asyncRequestCount)
{
Director::getInstance()->getScheduler()->pauseTarget(this);
}
} }
} }

View File

@ -99,8 +99,9 @@ private:
* @return bool * @return bool
*/ */
bool lazyInitThreadSemphore(); bool lazyInitThreadSemphore();
void networkThread();
/** Poll function called from main thread to dispatch callbacks when http requests finished **/ /** Poll function called from main thread to dispatch callbacks when http requests finished **/
void dispatchResponseCallbacks(float delta); void dispatchResponseCallbacks();
private: private:
int _timeoutForConnect; int _timeoutForConnect;

View File

@ -47,35 +47,28 @@ PongLayer::PongLayer()
auto paddleTexture = Director::getInstance()->getTextureCache()->addImage(s_Paddle); auto paddleTexture = Director::getInstance()->getTextureCache()->addImage(s_Paddle);
auto paddlesM = Array::createWithCapacity(4); Vector<Paddle*> paddlesM(4);
Paddle* paddle = Paddle::createWithTexture(paddleTexture); Paddle* paddle = Paddle::createWithTexture(paddleTexture);
paddle->setPosition( Point(VisibleRect::center().x, VisibleRect::bottom().y + 15) ); paddle->setPosition( Point(VisibleRect::center().x, VisibleRect::bottom().y + 15) );
paddlesM->addObject( paddle ); paddlesM.pushBack( paddle );
paddle = Paddle::createWithTexture( paddleTexture ); paddle = Paddle::createWithTexture( paddleTexture );
paddle->setPosition( Point(VisibleRect::center().x, VisibleRect::top().y - kStatusBarHeight - 15) ); paddle->setPosition( Point(VisibleRect::center().x, VisibleRect::top().y - kStatusBarHeight - 15) );
paddlesM->addObject( paddle ); paddlesM.pushBack( paddle );
paddle = Paddle::createWithTexture( paddleTexture ); paddle = Paddle::createWithTexture( paddleTexture );
paddle->setPosition( Point(VisibleRect::center().x, VisibleRect::bottom().y + 100) ); paddle->setPosition( Point(VisibleRect::center().x, VisibleRect::bottom().y + 100) );
paddlesM->addObject( paddle ); paddlesM.pushBack( paddle );
paddle = Paddle::createWithTexture( paddleTexture ); paddle = Paddle::createWithTexture( paddleTexture );
paddle->setPosition( Point(VisibleRect::center().x, VisibleRect::top().y - kStatusBarHeight - 100) ); paddle->setPosition( Point(VisibleRect::center().x, VisibleRect::top().y - kStatusBarHeight - 100) );
paddlesM->addObject( paddle ); paddlesM.pushBack( paddle );
_paddles = paddlesM->clone(); _paddles = paddlesM;
_paddles->retain();
Object* pObj = NULL; for (auto& paddle : _paddles)
CCARRAY_FOREACH(_paddles, pObj)
{ {
paddle = static_cast<Paddle*>(pObj);
if(!paddle)
break;
addChild(paddle); addChild(paddle);
} }
@ -85,7 +78,6 @@ PongLayer::PongLayer()
PongLayer::~PongLayer() PongLayer::~PongLayer()
{ {
_ball->release(); _ball->release();
_paddles->release();
} }
void PongLayer::resetAndScoreBallForPlayer(int player) void PongLayer::resetAndScoreBallForPlayer(int player)
@ -101,15 +93,8 @@ void PongLayer::doStep(float delta)
{ {
_ball->move(delta); _ball->move(delta);
Paddle* paddle = NULL; for (auto& paddle : _paddles)
Object* pObj = NULL;
CCARRAY_FOREACH(_paddles, pObj)
{ {
paddle = static_cast<Paddle*>(pObj);
if(!paddle)
break;
_ball->collideWithPaddle( paddle ); _ball->collideWithPaddle( paddle );
} }

View File

@ -15,11 +15,12 @@ public:
}; };
class Ball; class Ball;
class Paddle;
class PongLayer : public Layer class PongLayer : public Layer
{ {
private: private:
Ball* _ball; Ball* _ball;
Array* _paddles; Vector<Paddle*> _paddles;
Point _ballStartingVelocity; Point _ballStartingVelocity;
public: public:
PongLayer(); PongLayer();