예제 #1
0
void CNetMsg::Init(void)
{
    if(!this->p_data)
        this->p_data = g_netmsgpool.Get(false);
    
    this->p_msg_type    = 0;
    this->p_data        = g_netmsgpool.Get(false);
    this->p_len         = 0;
    this->p_point       = 0;
}
예제 #2
0
void CNetMsg::Init(void)
{
    if(!this->MsgData)
        this->MsgData = g_netmsgpool.Get(false);
	
    this->MsgType       = 0;
    this->MsgData       = g_netmsgpool.Get(false);
    this->MsgLength     = 0;
    this->MsgPointer    = 0;
}
예제 #3
0
int main()  
{  
    CMemPool mp;  
    assert( mp.Initialize() );  
  
    for(int i = 0; i<100; i++)  
    {  
        TCHAR* psz = mp.GetStringBuffer(8192);  
        _stprintf_s(psz, 8192, TEXT("now i=%d/n"), i);  
        _tprintf(psz);  
    }  
  
    mp.Destroy();  
    return getchar();  
}  
예제 #4
0
CNetMsg::~CNetMsg()
{
    if(this->p_data)
        g_netmsgpool.Set(this->p_data);

    this->p_msg_type    = 0;
    this->p_data        = 0;
    this->p_len         = 0;
    this->p_point       = 0;
}
예제 #5
0
CNetMsg::~CNetMsg()
{
    if(this->MsgData)
        g_netmsgpool.Set(this->MsgData);

    this->MsgType       = 0;
    this->MsgData       = 0;
    this->MsgLength     = 0;
    this->MsgPointer    = 0;
}
예제 #6
0
//读取信息到队列
void read_func(void *arg)
{ 
	while(1)
    {
        Sleep(1);
		CAutoLock lock(&g_TReadLock);
		std::string rcv("");
		try
		{
			rcv = sClient->ReceiveBytes();
		}
		catch(...)
		{
			//Log("read_func : MsClient->ReceiveBytes() fialed.");
			continue;
		}
        if(strlen(rcv.c_str()) == 0)
        {
            continue;
        }
	#ifdef  DEBUG_MODE
		std::cout << "read_func : " << rcv << std::endl;
	#endif
		char *data_buff = NULL;
		data_buff = reinterpret_cast<char *>(MsgPool.Get());
		//已解决bug1:如果不对其内存memset清0,,内存的当前值就会覆盖上次的(memcpy),
		//可能导致会有"垃圾尾巴"(例如上次内容为:12345,这次的期望内容是:abc,
		//最后在内存的值会变为:abc45,忘注意,这个和内存池本来的实现有关。
		if(data_buff)
		{
			memset(data_buff, 0, DATA_BLOCK_LEN);
			memcpy(data_buff, rcv.c_str(), strlen(rcv.c_str()));
			msg_queue.Push(data_buff);
			//(&doublebuffer_queue)->Push(data_buff);
		}
		else
		{
			//Log("read_func : MsgPool.Get() fialed.");
		}
    }
}
예제 #7
0
//获取消息队列消息
void proc(void *arg)
{
    HWND hMain = (HWND)arg;
	USE_JSONCODER_DOC(document);
	while(1)
    {
        Sleep(1);
		CAutoLock lock(&g_TGetLock);
		char *msg = NULL;
		try
		{
			msg = reinterpret_cast<char *>(msg_queue.Pop());
			//msg = (&doublebuffer_queue)->Popup();
			if(!msg)
			{
				continue;
			}
		}
		catch(...){
			//Log("proc : msg_queue.Pop() fialed.");
		}

	#ifdef  DEBUG_MODE
		std::cout << msg << std::endl;
	#endif	
		//解码	
		LOAD_JSONCODER_STR(document, msg);
		JsonCoder jc_decoder;
		if(!jc_decoder.CheckPacket(document))
		{
			//cout << "error packet" << endl;
			PostMessage(hMain, WM_ERRORPCK, 0, 0);
			continue;
		}
		//获取消息
		uint32 cmd = jc_decoder.GetInt(JC_KEY::JC_MSGTYPE, document);
		//服务端发送过来的回应包
		if(cmd == MESSAGE_TYPE::cmdLoginRep)
		{
			//返回的客户列表
			std::string     user_list   =  jc_decoder.GetString(JC_KEY::JC_USERLISTREP, document);
			//上线提示
			std::string		onlineUser  =  jc_decoder.GetString(JC_KEY::JC_ONLINEREP, document);
			//发送上线提醒消息
			std::string *olUser = new std::string(onlineUser);
			PostMessage(hMain, WM_ONLINEWARN, (WPARAM)olUser, 0);
			
#ifdef DEBUG_MODE
			cout << "user_list" << user_list << endl;
#endif			
			//发送列表消息
			std::string *uList = new std::string(user_list);
			//上线的用户提示
			std::string *warn = new std::string(onlineUser);
			PostMessage(hMain, WM_UPDATELIST, (WPARAM)uList, (LPARAM)warn);
		}
		else if(cmd == MESSAGE_TYPE::cmdLogoutRep)
		{
			//客户端离线提示
			std::string		logoutUser  =  jc_decoder.GetString(JC_KEY::JC_LOGOUTUSER, document);
			//列表删除
			ClientMap::iterator ite = cmap.begin();
			ite = cmap.find(logoutUser);
			if(ite != cmap.end())
			{
				cmap.erase(ite);				
			}
			std::string *ss = new std::string(logoutUser);
			PostMessage(hMain, WM_DELUSERLIST, (WPARAM)ss, 0);
		}
		else if(cmd == MESSAGE_TYPE::cmdPublicMsgRep)
		{
			//发送者id
			std::string		user_identify  =  jc_decoder.GetString(JC_KEY::JC_IDENTIFY, document);
			//谁发送的
			std::string		sender  =  jc_decoder.GetString(JC_KEY::JC_SENDER, document);
			//发送了什么
			std::string		pubmsg  =  jc_decoder.GetString(JC_KEY::JC_PUBMSG, document);
			//发送时间
			std::string		pubtime  =  jc_decoder.GetString(JC_KEY::JC_PUBTIME, document);
			if(strcmp(user_identify.c_str(), g_Identify.c_str()) != 0)	
			{
				std::wstring tmp = PublicTool::string2wstring(sender);
				tmp.append(STR_OTHER_SAY);
				tmp.append(PublicTool::string2wstring(pubmsg));
				tmp.append(L"[");
				tmp.append(PublicTool::string2wstring(pubtime));
				tmp.append(L"]");
				std::wstring *ss = new std::wstring(tmp);
				PostMessage(hMain, WM_PUBMSG, (WPARAM)ss, 0);
			}
		}
		//内存归还到内存池
		MsgPool.Release(msg);
	}
}
예제 #8
0
파일: mempool.cpp 프로젝트: ezhangle/SMAK
void* CMemPool::Alloc(size_t iSize, size_t iHandle)
{
    if (!s_apMemPools.size())
    {
        CMemPool::AddPool(256 * sizeof(float), 0);	// 1kb
        s_iMemoryAllocated = 0;
    }

    void* pReturn = NULL;
    for (unsigned int i = 0; i < s_apMemPools.size(); i++)
    {
        CMemPool* pPool = s_apMemPools[i];

        if (pPool->m_iHandle != iHandle)
            continue;

        // Easy out, is there not enough room?
        if (pPool->m_iMemoryAllocated + sizeof(CMemChunk) + iSize > pPool->m_iMemPoolSize)
            continue;

        // Easy out, are there no elements yet?
        else if (!pPool->m_pAllocMap)
            pReturn = pPool->Reserve(pPool->m_pMemPool, iSize);

        // Easy out, is there room at the beginning?
        else if ((size_t)pPool->m_pAllocMap - (size_t)pPool->m_pMemPool >= iSize + sizeof(CMemChunk))
            pReturn = pPool->Reserve(pPool->m_pMemPool, iSize, NULL);

        // Easy out, is there room at the end?
        else if ((size_t)pPool->m_pAllocMapBack->m_pMem + pPool->m_pAllocMapBack->m_iSize + iSize + sizeof(CMemChunk) <= (size_t)pPool->m_pMemPool + pPool->m_iMemPoolSize)
            pReturn = pPool->Reserve((void*)((size_t)pPool->m_pAllocMapBack->m_pMem + pPool->m_pAllocMapBack->m_iSize), iSize, pPool->m_pAllocMapBack);

        else
        {
            // Find a free spot large enough.
            for (CMemChunk* pChunk = pPool->m_pAllocMap; pChunk; pChunk = pChunk->m_pNext)
            {
                if ((size_t)pChunk->m_pNext - ((size_t)pChunk->m_pMem + pChunk->m_iSize) >= iSize + sizeof(CMemChunk))
                {
                    // If there is enough space here to insert a new piece of memory, then we'll use it.
                    pReturn = pPool->Reserve((void*)((size_t)pChunk->m_pMem + pChunk->m_iSize), iSize, pChunk);
                    break;
                }
            }
        }

        // If a proper space was located in the above code, don't search any more pools.
        if (pReturn)
            break;
    }

    if (!pReturn)
    {
        // Create a new one same size as the old plus a little more, so effectively we have a little more than 2x as much memory now.
        CMemPool* pPool = CMemPool::AddPool(s_iMemoryAllocated+iSize+sizeof(CMemChunk), iHandle);
        pReturn = pPool->Reserve(pPool->m_pMemPool, iSize);
    }

    TAssertNoMsg(pReturn);

    s_iMemoryAllocated += sizeof(CMemChunk) + iSize;
    return pReturn;
}
예제 #9
0
void* PoolMalloc(size_t nSize) {
	return __mempool.Alloc(nSize);
}
예제 #10
0
void  PoolFree(void* p) { 
	return __mempool.Free(p);
}