예제 #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;

}
예제 #2
0
void* SocketThread::start_thread(void *arg)   {  
	SocketThread* thred=(SocketThread*)arg;
	ODSocket cdSocket;
	cdSocket.Init();	
	bool isok=cdSocket.Create(AF_INET,SOCK_STREAM,0);	
//	bool iscon=cdSocket.Connect("127.0.0.1",8000);
    bool iscon=cdSocket.Connect("192.168.0.144",8000);
	if(iscon){
		thred->state=0;
		ResPonseThread::GetInstance()->start();//// 启动响应参数
		CCLOG("conection");
	}else{
		thred->state=1;
	}	
	thred->csocket=cdSocket;
	return NULL;                                                                                    
}
예제 #3
0
bool connectToServer()
{
	if (g_NetworkStat != NetworkStat_NotConnect)
		return true;

	if (cSocket.Init() == -1)
	{
		CCLog("\n==============================================================");
		CCLog("ODSocket: connectToServer Init error,errorCode = %d!", cSocket.GetError());
		CCLog("==============================================================\n");
		// 通知链接失败
		GlobalNetworkMsgDispacher::getMsgDispacher()->addPakage(new GlobalNetPackage(Type_Conncet_Err,NULL,NULL));
		return false;
	}
	if (cSocket.Create(AF_INET,SOCK_STREAM,0) == false)
	{
		CCLog("\n==============================================================");
		CCLog("ODSocket: connectToServer Create error,errorCode = %d!",cSocket.GetError());
		CCLog("==============================================================\n");

		// 通知链接失败
		GlobalNetworkMsgDispacher::getMsgDispacher()->addPakage(new GlobalNetPackage(Type_Conncet_Err,NULL,NULL));
		return false;
	}
	if (cSocket.Connect(NetWorkThreadProc::getProc()->getCurrentIp(),NetWorkThreadProc::getProc()->getCurrentPort()) == false)
	{
		CCLog("\n==========================================================================");
		CCLog("ODSocket: connectToServer[%s:%d] Connect error,errorCode = %d!",NetWorkThreadProc::getProc()->getCurrentIp(),NetWorkThreadProc::getProc()->getCurrentPort(),cSocket.GetError());
		CCLog("============================================================================\n");

		closeConnection();

		// 通知链失败
		GlobalNetworkMsgDispacher::getMsgDispacher()->addPakage(new GlobalNetPackage(Type_Conncet_Err,NULL,NULL));

		return false;
	}
	CCLog("\n\n");
	CCLog("\n==============================================================");
	CCLog("ODSocket : connectToServer %s-%d Success!",NetWorkThreadProc::getProc()->getCurrentIp(),NetWorkThreadProc::getProc()->getCurrentPort());
	CCLog("==============================================================\n");
	CCLog("\n\n");
	//cSocket.setT

	//设置状态
	g_NetworkStat = NetworkStat_Connected;

	// 设置超时
	{
		cSocket.SetRecvTimeOut(RECV_TIME_OUT);
		cSocket.SetSendTimeOut(SEND_TIME_OUT);
	}

	// 这个在init时已经调用了,这里不需要了,而且WSAStartup应该是winSocket第一个调用的函数
	// 所以写在这里也是不对的
//#ifdef WIN32
//	WORD wVersionRequested;
//	WSADATA wsaData;
//	int err;
//
//	/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
//	wVersionRequested = MAKEWORD(2, 2);
//
//	err = WSAStartup(wVersionRequested, &wsaData);
//	if (err != 0) {
//		/* Tell the user that we could not find a usable */
//		/* Winsock DLL.                                  */
//		CCLog("WSAStartup failed");
//
//		closeConnection();
//
//		return false;
//	}
//#endif

	// 通知链接成功
	GlobalNetworkMsgDispacher::getMsgDispacher()->addPakage(new GlobalNetPackage(Type_Conncet_Suc,NULL,NULL));

	return true;
}
//发送数据线程
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);
    }
}
예제 #5
0
void MapScene::clientR()
{
    ODSocket cSocketR;
    cSocketR.Init();
    cSocketR.Create(AF_INET,SOCK_STREAM,0);
    while(!cSocketR.Connect(ip.c_str(),portW))
    {
#ifdef WIN32
        Sleep(1000);
#else
        usleep(1000 * 1000);
#endif
    } //因为是client 所以用的是portR;
    char buf[BLOCK_SIZE];
    for (;;)
    {
        if (!start)
            return ;

        int len = cSocketR.Recv(buf,BLOCK_SIZE ,0);
        if (len <= 0 )
        {
            heartBeat = false;
            break;
        }
        if (!status)
            break;
        int pAngle;
        int x;
        int y;
        int avatarId;
        int type = 0;
        int number;
        bool flag[MAX][4][20];
        int t_life = 0;
        int state = 0;
        avatarId = getIntFromStr(buf, 1);
        state = getIntFromStr(buf+1, 1);
        t_life = getIntFromStr(buf+2, 2);
        pAngle = getIntFromStr(buf+4, 4);
        x = getIntFromStr(buf+8, 4);
        y = getIntFromStr(buf+12, 4);
        avatar[avatarId]->pAngle = pAngle;
        avatar[avatarId]->getsprite()->setRotation(pAngle);
        avatar[avatarId]->getsprite()->setPosition(ccp(x,y));
        if (!avatar[avatarId]->getsprite()->getIsVisible() && status)
            avatar[avatarId]->getsprite()->setIsVisible(true);
        len = 16;
        if (state == 1 && !player->isFrozen)
        {
            player->isFrozen = true;
            schedule(schedule_selector(MapScene::freezeplayer), 0.1f);
            schedule(schedule_selector(MapScene::revertplayer), 2.0f);
        }
        if (t_life < player->life)
        {

            if (player->life == t_life + 5)
            {
                schedule(schedule_selector(MapScene::attackByBomb),0.1f);
            }
            reduceLife(t_life);
            player->life = t_life;
            schedule(schedule_selector(MapScene::syncLife), 0.1f);
        }
        for (int i = 0 ; i < MAX ; ++i)
            for (int j = 0 ; j <= 2; ++j)
                for (int k = 0 ; k < avatarBullets[i][j]->capacity(); ++k)
                    flag[i][j][k] = false;


        while(1)
        {
            type = getIntFromStr(buf+len, 1);
            if (type == 9)
                break;
            len += 1;
            number = getIntFromStr(buf+len, 2);
            len += 2;
            pAngle = getIntFromStr(buf+len, 4);
            len += 4;
            x = getIntFromStr(buf+len, 4);
            len += 4;
            y = getIntFromStr(buf+len, 4);
            len += 4;
            GameBullet * mybullet = (GameBullet *)(avatarBullets[avatarId][type]->objectAtIndex(number));
            mybullet->vAngle = pAngle;
            mybullet->getsprite()->setRotation(pAngle);
            mybullet->getsprite()->setPosition(ccp(x,y));
            if (!mybullet->getsprite()->getIsVisible())
                mybullet->getsprite()->setIsVisible(true);
            flag[avatarId][type][number] = true;
        }
        for (int i = 0 ; i < MAX ; ++i)
            for (int j = 0 ; j <= 2; ++j)
                for (int k = 0 ; k < avatarBullets[i][j]->capacity(); ++k)
                {
                    if (!flag[i][j][k])
                    {
                        ((GameBullet *)(avatarBullets[i][j]->objectAtIndex(k)))->getsprite()->setIsVisible(false);
                    }
                }
        if (t_life ==  0)
            break;
    }
    cSocketR.Close();
    cSocketR.Clean();
}
예제 #6
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();
}