void * YHSocketSendThread::Execute()
{
	bool loop = true;
	
	while(loop)
	{
		try
		{
			m_manager->m_condition.LockMutEx();
			
			// 如果没有消息等待发送, 则等待, 添加消息后, 该线程将被唤醒
			while (m_manager->m_queue->count() == 0)
			{
				m_manager->m_condition.Wait();
				
				if (m_destroy)
				{
					m_manager->m_condition.UnlockMutEx();
					return NULL;
				}
			}
			
			YHByteArray * bytes = (YHByteArray *)m_manager->m_queue->objectAtIndex(0);
			bytes->retain();
			m_manager->m_queue->removeObjectAtIndex(0);
			
			m_manager->m_condition.UnlockMutEx();
			
			// 阻塞, 将消息发送出去
			m_manager->m_connectManager->connectedSocket()->SendData(bytes->getBuffer(), bytes->readableBytes());
			bytes->release();
			
			if (m_destroy)
			{
				loop = false;
			}
		}
		catch (SocketException e)
		{
			m_manager->m_connectManager->close();
			
			m_manager->m_condition.LockMutEx();
			m_manager->m_queue->removeAllObjects();
			m_manager->m_condition.UnlockMutEx();
			
			CCLOG("%s", (const char *)e);
			loop = false;
		}
		catch (ThreadException e)
		{
			CCLOG("%s", (const char *)e);
		}
	}
	
	return NULL;
}
YHByteArray * YHHttpClient::recvData()
{
	YHByteArray * bytes = NULL;
	if (m_queue->count() != 0)
	{
		bytes = (YHByteArray *)m_queue->objectAtIndex(0);
		bytes->retain();
		m_queue->removeObjectAtIndex(0);
	}
	
	return bytes;
}
void * YHSocketRecvThread::Execute()
{
	bool loop = true;
	while (loop)
	{
		try
		{
			// 接收包头
			YHByteArray header;
			header.init(4);
			m_manager->m_connectManager->connectedSocket()->RecvData(header.getBuffer(), header.getCapacity(), MSG_WAITALL);
			
			uint32 size = header.getUnsignedInt(0);
			
			// 接收实际内容
			YHByteArray * content = new YHByteArray();
			content->init(size);
			m_manager->m_connectManager->connectedSocket()->RecvData(content->getBuffer(), size, MSG_WAITALL);
			content->setWriterIndex(size);
			
			// 将接收到的数据添加进队列
			m_manager->m_mutex.Lock();
			m_manager->m_queue->addObject(content);
			content->release();
			m_manager->m_mutex.Unlock();
		}
		catch (SocketException e)
		{
			m_destroy = true;
			
			m_manager->m_connectManager->close();
			
			m_manager->m_mutex.Lock();
			m_manager->m_queue->removeAllObjects();
			m_manager->m_mutex.Unlock();
			
			CCLOG("%s", (const char *)e);
		}
		catch (ThreadException e)
		{
			CCLOG("%s", (const char *)e);
		}
		
		if (m_destroy)
		{
			loop = false;
		}
	}
	
	return NULL;
}
CCObject * YHDataManagerImp::loadFile(const std::string & fullpath)
{
    string suffix = pathExtensionWithString(fullpath);
    asciiToLower(suffix);
    
    // 装载对应的对象
    CCObject * obj = NULL;
    if (suffix.compare("plist-array") == 0)
    {
        ValueVector vv = CCFileUtils::getInstance()->getValueVectorFromFile(fullpath);
        CCArray * arr = new CCArray();
        arr->initWithCapacity((ssize_t)vv.size());
        array_Value(arr, vv);
        obj = arr;
    }
    else if (suffix.compare("plist-dictionary") == 0)
    {
        obj = CCDictionary::createWithContentsOfFileThreadSafe(fullpath.c_str());
    }
    else if (suffix.compare("png") == 0 || suffix.compare("jpg") == 0 || suffix.compare("jpeg") == 0
             || suffix.compare("tif") == 0 || suffix.compare("tiff") == 0 || suffix.compare("webp") == 0)
    {
        Image * image = new Image();
        image->initWithImageFile(fullpath);
        obj = image;
    }
    else
    {
        FILE * pFile = fopen(fullpath.c_str(), "r");
        if (pFile != NULL)
        {
            // 获得文件大小
            fseek(pFile, 0, SEEK_END);
            uint32 size = ftell(pFile);
            fseek(pFile, 0, SEEK_SET);
            
            YHByteArray * bytes = new YHByteArray();
            bytes->init(size);
            fread(bytes->getBuffer(), size, 1, pFile);
            obj = bytes;
            
            fclose(pFile);
        }
    }
    
    return obj;
}
YHByteArray * YHSocketRecvManager::recv()
{
	if (m_mutex.TryLock())
	{
		YHByteArray * bytes = NULL;
		if (m_queue->count() != 0)
		{
			bytes = (YHByteArray *)m_queue->objectAtIndex(0);
			bytes->retain();
			m_queue->removeObjectAtIndex(0);
		}
		m_mutex.Unlock();
		
		return bytes;
	}
	
	return NULL;
}
void YHHttpClient::onResponseCallBack(cocos2d::extension::CCHttpClient * client,
									  cocos2d::extension::CCHttpResponse * response)
{
	std::vector<char> * rawData = response->getResponseData();
	if (rawData->size() != 0)	// 这里之所以不用 isSuccess() 是因为又是接收到数据, 但是也报错, 所以按是否接收到数据判断是否接收成功
	{
		std::vector<char>::iterator beg = rawData->begin();
		std::vector<char>::iterator end = rawData->end();
		YHByteArray * bytes = new YHByteArray();
		bytes->init(rawData->size());
		for (; beg != end; ++beg)
		{
			bytes->writeChar(*beg);
		}
		m_queue->addObject(bytes);
		bytes->release();
	}
	else
	{
		CCLOG("Http error: %s", response->getErrorBuffer());
	}
}
Пример #7
0
void testByteArray()
{
	YHByteArray * bytes = new YHByteArray();
	bytes->init();
	bytes->setOrder(kByteOrder_LittleEndian);
	
	YHByteArray * copied = new YHByteArray();
	copied->init();
	
	CCLOG("capacity: %d", bytes->getCapacity());
	
	char buf[] = "123456";
	bytes->writeBytes(buf, strlen(buf) + 1);
	bytes->writeBoolean(true);
	bytes->writeChar('a');
	bytes->writeUnsignedChar('b');
	bytes->writeShort(-23);
	bytes->writeUnsignedShort(-23);
	bytes->writeInt(-23);
	bytes->writeUnsignedInt(-23);
	bytes->writeFloat(100.001f);
	bytes->writeDouble(100.004);
	
	bytes->discardReadBytes();
	CCLOG("readableBytes: %d", bytes->readableBytes());
	CCLOG("writeableBytes: %d", bytes->writeableBytes());
	
	char tmp[256] = {0, };
//	bytes->readBytes(tmp, strlen(buf) + 1);
	bytes->readBytes(copied, strlen(buf) + 1);
	CCLOG("readBytes: %s", copied->getBuffer());
	CCLOG("readBoolean: %s", bytes->readBoolean() ? "true" : "false");
	CCLOG("readChar: %c", bytes->readChar());
	CCLOG("readUnsignedChar: %c", bytes->readUnsignedChar());
	CCLOG("readShort: %d", bytes->readShort());
	CCLOG("readUnsignedShor: %d", bytes->readUnsignedShort());
	CCLOG("readInt: %d", bytes->readInt());
	CCLOG("readUnsignedInt: %u", bytes->readUnsignedInt());
	CCLOG("readFloat: %f", bytes->readFloat());
	CCLOG("readDouble: %f", bytes->readDouble());
	
	bytes->discardReadBytes();
	CCLOG("readableBytes: %d", bytes->readableBytes());
	CCLOG("writeableBytes: %d", bytes->writeableBytes());
	
	CCLOG("--------------------------------");
	
	unsigned int index = 100;
	bytes->setBytes(index, buf, strlen(buf) + 1, 0); index += strlen(buf) + 1;
	bytes->setBoolean(index++, false);
	bytes->setChar(index++, 'x');
	bytes->setUnsignedChar(index++, 'y');
	bytes->setShort(index, -32); index += 2;
	bytes->setUnsignedShort(index, -32); index += 2;
	bytes->setInt(index, -32); index += 4;
	bytes->setUnsignedInt(index, -32); index += 4;
	bytes->setFloat(index, 123.123f); index += 4;
	bytes->setDouble(index, 321.321); index += 8;
	
	index = 100;
	char buf3[256] = {0, };
	bytes->getBytes(index, buf3, strlen(buf) + 1, 50);
	bytes->getBytes(index, copied, strlen(buf) + 1, 150);
	index += strlen(buf) + 1;
	CCLOG("getBytes: %s", buf3 + 50);
	CCLOG("getBytes: %s", copied->getBuffer() + 150);
	CCLOG("getBoolean: %s", bytes->getBoolean(index) ? "true" : "false"); index++;
	CCLOG("getChar: %c", bytes->getChar(index)); index++;
	CCLOG("getUnsignedChar: %c", bytes->getUnsignedChar(index)); index++;
	CCLOG("getShort: %d", bytes->getShort(index)); index += 2;
	CCLOG("getUnsignedShort: %d", bytes->getUnsignedShort(index)); index += 2;
	CCLOG("getInt: %d", bytes->getInt(index)); index += 4;
	CCLOG("getUnsignedInt: %u", bytes->getUnsignedInt(index)); index += 4;
	CCLOG("getFloat: %f", bytes->getFloat(index)); index += 4;
	CCLOG("getDouble: %f", bytes->getDouble(index)); index += 8;
	
	CCLOG("---------------------------------");
	
	index = 0;
	char buf4[256] = "654321";
	YHCommandParam * param0 = new YHCommandParam(bytes, index, strlen(buf4) + 1);
	param0->writeBytes(buf4, strlen(buf4) + 1);
	param0->readBytes(buf3, strlen(buf) + 1);
	CCLOG("readBytes: %s", buf3);
	
	YHCommandParam * param1 = new YHCommandParam(bytes, param0->getLoc() + param0->getLen(), 1);
	param1->writeBoolean(true);
	CCLOG("readBoolean: %s", param1->readBoolean() ? "true" : "false");
	
	YHCommandParam * param2 = new YHCommandParam(bytes, param1->getLoc() + param1->getLen(), 1);
	param2->writeChar('q');
	CCLOG("readChar: %c", param2->readChar());
	
	YHCommandParam * param3 = new YHCommandParam(bytes, param2->getLoc() + param2->getLen(), 1);
	param3->writeUnsignedChar('z');
	CCLOG("readUnsignedChar: %c", param3->readUnsignedChar());
	
	YHCommandParam * param4 = new YHCommandParam(bytes, param3->getLoc() + param3->getLen(), 2);
	param4->writeShort(0xffff);
	CCLOG("readShort: %d", param4->readShort());
	
	YHCommandParam * param5 = new YHCommandParam(bytes, param4->getLoc() + param4->getLen(), 2);
	param5->writeUnsignedShort(0xffff);
	CCLOG("readUnsignedShort: %d", param5->readUnsignedShort());
	
	YHCommandParam * param6 = new YHCommandParam(bytes, param5->getLoc() + param5->getLen(), 4);
	param6->writeInt(0xffffffff);
	CCLOG("readInt: %d", param6->readInt());
	
	YHCommandParam * param7 = new YHCommandParam(bytes, param6->getLoc() + param6->getLen(), 4);
	param7->writeUnsignedInt(0xffffffff);
	CCLOG("readUnsignedInt: %u", param7->readUnsignedInt());
	
	YHCommandParam * param8 = new YHCommandParam(bytes, param7->getLoc() + param7->getLen(), 4);
	param8->writeFloat(1234.1234f);
	CCLOG("readFloat: %f", param8->readFloat());
	
	YHCommandParam * param9 = new YHCommandParam(bytes, param8->getLoc() + param8->getLen(), 8);
	param9->writeDouble(123456.123456);
	CCLOG("readDouble: %f", param9->readDouble());
	
	CC_SAFE_RELEASE_NULL(bytes);
	CC_SAFE_RELEASE_NULL(param0);
	CC_SAFE_RELEASE_NULL(param1);
	CC_SAFE_RELEASE_NULL(param2);
	CC_SAFE_RELEASE_NULL(param3);
	CC_SAFE_RELEASE_NULL(param4);
	CC_SAFE_RELEASE_NULL(param5);
	CC_SAFE_RELEASE_NULL(param6);
	CC_SAFE_RELEASE_NULL(param7);
	CC_SAFE_RELEASE_NULL(param8);
	CC_SAFE_RELEASE_NULL(param9);
	
	CCLOG("---------------------------------");
	
	YHGameLogicPackage * package = new YHGameLogicPackage(1, 100);
	char buf5[] = "0123456789";
	package->writeBytes(buf5, sizeof(buf5));
	package->writeBoolean(false);
	package->writeChar('g');
	package->writeUnsignedChar('h');
	package->writeShort(1);
	package->writeUnsignedShort(-1);
	package->writeInt(1);
	package->writeUnsignedInt(-1);
	package->writeFloat(123456.654321f);
	package->writeDouble(123456789.987654321);
	
	YHByteArray * sendBytes = package->getSendBytes();
	CCLOG("getSendBytes: %u", sendBytes->readUnsignedInt());
	
	YHGameLogicPackage * package2 = new YHGameLogicPackage(sendBytes);
	CCLOG("moduleID: %d, functionID: %d", package2->getModuleID(), package2->getFunctionsID());
	
	YHByteArray * tmpBytes = new YHByteArray();
	package2->readBytes(tmpBytes);
	CCLOG("readBytes: %s", tmpBytes->getBuffer());
	CCLOG("readBoolean: %s", package2->readBoolean() ? "true" : "false");
	CCLOG("readChar: %c", package2->readChar());
	CCLOG("readUnsignedChar: %c", package2->readUnsignedChar());
	CCLOG("readShort: %d", package2->readShort());
	CCLOG("readUnsignedShort: %d", package2->readUnsignedShort());
	CCLOG("readInt: %d", package2->readInt());
	CCLOG("readUnsignedInt: %u", package2->readUnsignedInt());
	CCLOG("readFloat: %f", package2->readFloat());
	CCLOG("readDouble: %f", package2->readDouble());
	
	CC_SAFE_RELEASE_NULL(package);
	CC_SAFE_RELEASE_NULL(package2);
	CC_SAFE_RELEASE_NULL(tmpBytes);
}
Пример #8
0
CCObject * YHDataManagerImp::loadFile(const std::string & fullpath)
{
    string suffix = pathExtensionWithString(fullpath);
    asciiToLower(suffix);
    
    // 装载对应的对象
    CCObject * obj = NULL;
    if (suffix.compare("plist-array") == 0)
    {
        ValueVector vv = CCFileUtils::getInstance()->getValueVectorFromFile(fullpath);
        CCArray * arr = new CCArray();
        arr->initWithCapacity((ssize_t)vv.size());
        array_Value(arr, vv);
        obj = arr;
    }
    else if (suffix.compare("plist-dictionary") == 0)
    {
        ValueMap vm = FileUtils::getInstance()->getValueMapFromFile(fullpath);
        CCDictionary * dic = new CCDictionary();
        dic->init();
        dictionary_Value(dic, vm);
        obj = dic;
    }
    else if (suffix.compare("png") == 0 || suffix.compare("jpg") == 0 || suffix.compare("jpeg") == 0
             || suffix.compare("tif") == 0 || suffix.compare("tiff") == 0 || suffix.compare("webp") == 0)
    {
        Image * image = new Image();
        image->initWithImageFile(fullpath);
        obj = image;
    }
    else if (suffix.compare("plist") == 0)
    {
		ValueMap vm = CCFileUtils::getInstance()->getValueMapFromFile(fullpath);
		if (vm.size())
		{
			CCDictionary * dic = new CCDictionary();
			dic->init();
			dictionary_Value(dic, vm);
			obj = dic;
		}
		else
		{
			ValueVector vv = CCFileUtils::getInstance()->getValueVectorFromFile(fullpath);
			if (vv.size())
			{
				CCArray * arr = new CCArray();
				arr->initWithCapacity((ssize_t)vv.size());
				array_Value(arr, vv);
				obj = arr;
			}
			else
			{
				CCASSERT(false, "没有找到适合的解析文件方式。");
			}
		}
    }
    else
    {
        std::string data = FileUtils::getInstance()->getStringFromFile(fullpath);
        YHByteArray * bytes = new YHByteArray();
        bytes->init(data.length() + 1);
        bytes->writeBytes((char *)data.c_str(), data.length() + 1);
        obj = bytes;
    }
    
    return obj;
}