Exemplo n.º 1
0
void* TigerThread::thread_funcation(void *arg)
{
    ODSocket *pSocket = ODSocket::sharedODSocket();
    pSocket->Init();
    pSocket->Create(AF_INET, SOCK_STREAM,0);
    
    /*
     /////////////////////////////////////////////
     char ipaddr[20];
     cSocket.DnsParse("www.baidu.com", ipaddr);
     //192.168.22.154
     //aliyun-027.btcsky.com
     /////////////////////////////////////////////--DNS解析 www->ip
     */
    
    if (pSocket->Connect("58.83.190.220", 6621)) {
        char recvBuf[1024] = "\0";
        // std::string testmsg= |login|[email protected],e10adc3949ba59abbe56e057f20f883e,68:A8:6D:10:66:DC|$\r\n
        std::string testmsg= pSocket->msg_To_Sever;
        pSocket->Send(testmsg.data(),testmsg.length(),0);
        pSocket->Recv(recvBuf,1024,0);
        std::string rec_msg=std::string(recvBuf);
        printf("%s",rec_msg.c_str());
    }
    else
    {
        CCLog("服务器连接失败");
    }
    pSocket->Close();
    pSocket->Clean();
    return NULL;

}
Exemplo n.º 2
0
// 负责发送数据,发送成功后修改接受线程的状态为recv,让接受线程开始接受请求返回包(心跳+数据包都是)
static void* sendTask(void* data)
{
	while (true)
	{
		//如果接收线程当前还在收上一个请求包的响应则不再发送新的请求
		if(g_RecvThreadStat == RecvThreadStat_Recv)
		{
			lfSleep(100);
			continue;
		}

		//如果需要心跳,则先心跳。否则才从队列中取请求包
		int time_gap = time(0) - g_LastHeartBeatTime;
		RequestMessage* msg = NULL;
		if(time_gap >= HeartBeatTimeGap && g_HeartBeatMsg != NULL)
		{
			//更新时间
			g_LastHeartBeatTime = time(0);

			msg = new RequestMessage(g_HeartBeatMsg);
		}
		else
		{
			// 加锁
			pthread_mutex_lock(&requestMsgQueueMutex);
			if (requestMsgQueue.empty())
			{
				// 解锁
				pthread_mutex_unlock(&requestMsgQueueMutex);
			}
			else
			{
				msg = requestMsgQueue.front();
				requestMsgQueue.pop();

				// 解锁
				pthread_mutex_unlock(&requestMsgQueueMutex);
			}
		}

		//当前没有需要发送的请求
		if(msg == NULL)
		{
			lfSleep(100);
			continue;
		}

		//发送
		{
			//判断当前网络是否连上
			if(g_NetworkStat == NetworkStat_NotConnect)
			{
				CCLog("%s:%d --> g_NetworkStat == NetworkStat_NotConnect post send error",__FUNCTION__,__LINE__);

				// 通知发送数据出错
				GlobalNetworkMsgDispacher::getMsgDispacher()->addPakage(new GlobalNetPackage(Type_Send_Err,NULL,NULL));

				delete msg;

				continue;
			}

			CCLog("sendTask : send data .....");
			int l = cSocket.Send(msg->getData(),msg->getDataLength(),0);
			//
			if ( l != msg->getDataLength())
			{
				CCLog("sendTask : send data error! length = %d, sended length = %d",msg->getDataLength(),l);

				// 通知发送数据出错
				GlobalNetworkMsgDispacher::getMsgDispacher()->addPakage(new GlobalNetPackage(Type_Send_Err,NULL,NULL));

				delete msg;

				//出问题了
				closeConnection();

				continue;
			}
			else
			{
				delete msg;

				//发送成功,修改recv线程的标志
				g_RecvThreadStat = RecvThreadStat_Recv;

				CCLog("sendTask : send data success");
			}
		}
	}
	return 0;
}
//发送数据线程
void netSocketManger::sendThread()
{

    //bool connect = cSocket.Connect(kServerIP,kServerPort);
    bool connect = cSocket.Connect(netSocketManger::sharednetSocketManger()->server.IP.c_str(),netSocketManger::sharednetSocketManger()->server.port);
   // cSocket.Send("bbb",strlen("bbb")+1,0);

    if (connect) {
        isConnect = true;
        SocketData *errorData = newSocketData();
        errorData->eventType = CONNECT_SUCCEED;
        m_mainQueue.Push(errorData);
        //创建接收线程
        m_mutexx.lock();
        netSocketManger::sharednetSocketManger()->createReciveThread();
        m_mutexx.unlock();
        m_sendEvent->Lock();
        while (true) {
            while (!m_sendQueue.IsEmpty()) {
                SocketData *data = m_sendQueue.Pop();
                uLong comprLen = data->bodyLen;
                const char *compr = data->sendData;
                
                T_MSGHEAD_T msgHead;
                
                msgHead.cmd = (unsigned short)data->module.cmd;
                msgHead.com = 0;
                msgHead.enc = 0;
                msgHead.eno = 0;
                msgHead.idx = (uInt)data->sn;
                msgHead.len = (unsigned short)comprLen;
                msgHead.tea = 0;//s_tea;
                
                unsigned char *sendData = (unsigned char*)malloc(comprLen);
                unsigned int pos = 0;
                
                memcpy(&sendData[pos], compr, comprLen);//body
                pos += comprLen;
                
                int ret = cSocket.Send((char*)sendData,pos,0);
                log("发送:%s",compr);
                if (ret <= 0) {
                    m_sendEvent->Unlock();
                    free(sendData);
                    SocketData *errorData = newSocketData();
                    errorData->eventType = DISCONNECT;
                    m_mainQueue.Push(errorData);
                    return;
                }

                free(data->sendData);
                free(data);
                free(sendData);

                log("-----发送数据长度len:%d------",msgHead.len);
                log("-----------");
                
            }
            
            m_sendEvent->Wait();
        }
        m_sendEvent->Unlock();
    }else {
        isConnect = false;
        SocketData *errorData = newSocketData();
        errorData->eventType = CONNECT_FAIL;
        m_mainQueue.Push(errorData);
    }
}
Exemplo n.º 4
0
void MapScene::serverW()
{
    ODSocket cSocket;
    mysocketW.Init();
    mysocketW.Create(AF_INET,SOCK_STREAM,0);
    mysocketW.Bind(portW);
    mysocketW.Listen();
    char ipClient[64];
    while(!mysocketW.Accept(cSocket,ipClient))
    {
#ifdef WIN32
        Sleep(1000);
#else
        usleep(1000 * 1000);
#endif
    }
    char buf[BLOCK_SIZE];
    sprintf(buf,"%1d",id);
    int aid  = -1;
    int state = 0;
    while(1)
    {
        if (!start)
            break;
        for(int i = 0 ; i < MAX; ++i)
        {
            if (avatar[i]->getsprite()->getIsVisible())
            {
                aid = i;
                break;
            }
        }
        state = 0;
        if(avatar[aid]->isFrozen)
        {
            state = 1;
        }
        sprintf(buf+1,"%1d",state);
        sprintf(buf+2,"%2d",avatarLife);
        CCPoint herop = (player->getsprite())->getPosition();
        int dy = herop.y;
        int dx = herop.x;
        int angle = (int)player->pAngle;
        sprintf(buf+4, "%4d", angle);
        sprintf(buf+8, "%4d", dx);
        sprintf(buf+12, "%4d", dy);
        int len = 16;
        for (int i = 0; i <= 2; ++i)
            for (int j = 0; j < bullets[i]->capacity(); ++j)
            {
                GameBullet * mybullet = (GameBullet *)(bullets[i]->objectAtIndex(j));
                if (mybullet->getsprite()->getIsVisible())
                {
                    CCPoint p = (mybullet->getsprite()->getPosition());
                    sprintf(buf+len, "%1d", (int)i);
                    len += 1;
                    sprintf(buf+len, "%2d", (int)j);
                    len += 2;
                    sprintf(buf+len, "%4d", (int)mybullet->vAngle);
                    len += 4;
                    sprintf(buf+len, "%4d", (int)p.x);
                    len += 4;
                    sprintf(buf+len, "%4d", (int)p.y);
                    len += 4;
                }
            }
        buf[len] = '9';
        if (cSocket.Send(buf, BLOCK_SIZE ,0)== -1)
            break;
#ifdef WIN32
        Sleep(30);
#else
        usleep(30 * 1000);
#endif
    }
    mysocketW.Close();
    mysocketW.Clean();
}
Exemplo n.º 5
0
void MapScene::clientW()
{
    ODSocket cSocketW;
    cSocketW.Init();
    cSocketW.Create(AF_INET,SOCK_STREAM,0);
    while(!cSocketW.Connect(ip.c_str(),portR))
    {
#ifdef WIN32
        Sleep(1000);
#else
        usleep(1000 * 1000);
#endif
    }
    ; //因为是client 所以用的是portR;
    char buf[BLOCK_SIZE];
    sprintf(buf,"%1d",id);
    int aid  = -1;
    int state = 0;

    while(1)
    {
        if (!start)
            break;
        for(int i = 0 ; i < MAX; ++i)
        {
            if (avatar[i]->getsprite()->getIsVisible())
            {
                aid = i;
                break;
            }
        }
        state = 0;
        if(avatar[aid]->isFrozen)
        {
            state = 1;
        }
        sprintf(buf+1,"%1d",state);
        sprintf(buf+2,"%2d",avatarLife);
        CCPoint herop = (player->getsprite())->getPosition();
        int dy = herop.y;
        int dx = herop.x;
        int angle = (int)player->pAngle;
        sprintf(buf+4, "%4d", angle);
        sprintf(buf+8, "%4d", dx);
        sprintf(buf+12, "%4d", dy);
        int len = 16;
        for (int i = 0; i <= 2; ++i)
            for (int j = 0; j < bullets[i]->capacity(); ++j)
            {
                GameBullet * mybullet = (GameBullet *)(bullets[i]->objectAtIndex(j));
                if (mybullet->getsprite()->getIsVisible())
                {
                    CCPoint p = (mybullet->getsprite()->getPosition());
                    sprintf(buf+len, "%1d", (int)i);
                    len += 1;
                    sprintf(buf+len, "%2d", (int)j);
                    len += 2;
                    sprintf(buf+len, "%4d", (int)mybullet->vAngle);
                    len += 4;
                    sprintf(buf+len, "%4d", (int)p.x);
                    len += 4;
                    sprintf(buf+len, "%4d", (int)p.y);
                    len += 4;
                }
            }
        buf[len] = '9';
        //if (demo > 3 || temp != 0)
        //{
        if (cSocketW.Send(buf, BLOCK_SIZE ,0)== -1)
            break;

        //	}
#ifdef WIN32
        Sleep(30);
#else
        usleep(30 * 1000);
#endif
    }
    cSocketW.Close();
    cSocketW.Clean();
}