Ejemplo n.º 1
0
bool CSocketEx::RecvDataFromServer(char* &pData, DWORD& dwlength)
{
	pData = NULL;	dwlength = 0;

	DWORD nSize = 1024 * 100;
	char *p = new char[nSize];
	_packageheader* pheader = (_packageheader*)p;

	bool finish = false;
	int total = 0;
	while (!finish)
	{
		FD_SET fdset;
		timeval tv = {10, 0};
		FD_ZERO(&fdset);
		FD_SET(m_sock, &fdset);
		int ret = select(0, &fdset, NULL, NULL, &tv);
		if (ret <= 0)
		{
			if (ret == 0)
				continue;
			CloseConnect();
			delete []p;
			return false;
		}

		int len = recv(m_sock, &p[total], nSize-total, 0);
		if (len<=0)	
		{
			CloseConnect();
			delete []p;
			return false;
		}
		total += len;
		if (total >= sizeof(_packageheader))
		{
			if (nSize < pheader->Length)
			{				
				char* temp = new char[pheader->Length];
				memcpy(temp, p, total);
				nSize = pheader->Length;
				delete []p;
				p = temp;
				pheader = (_packageheader*)temp;				
			}

			if (total == (int)pheader->Length)
				finish = true;
		}
	}
	pData = p;
	dwlength = total; 
	return true;
}
Ejemplo n.º 2
0
void CUdpClient::handle_read(unsigned char * buf,const boost::system::error_code& error,size_t bytes_transferred)
{
	if(!error)
	{
		RecordFrameData(buf,bytes_transferred,true);

		//调用CProtocol模块处理recvbuf中bytes_transferred字节的报文
		if (protocol_)
		{
			protocol_->RecvProcess(buf,bytes_transferred);
		}

		//再继续读数据
		ReadFromConnect(buf);
	}
	else
	{
		AddStatusLogWithSynT("UdpClient通道读数据过程中发生错误\n");
	
		CloseConnect();
		UninitConnect();
		InitConnect();
		OpenConnect();
	}
}
Ejemplo n.º 3
0
void* NetEngine::ConnectWorker( NetConnect *pConnect )
{
	m_pNetServer->OnConnect( pConnect->m_host );
	/*
		监听连接
		※必须等OnConnect业务完成,才可以开始监听连接上的IO事件
		否则,可能业务层尚未完成连接初始化工作,就收到OnMsg通知,
		导致业务层不知道该如何处理消息
		
		※尚未加入监听,pConnect对象不存在并发线程访问
		如果OnConnect业务中,没有关闭连接,才能加入监听

		※如果不检查m_bConnect,则MonitorConnect有可能成功,导致OnData有机会触发。
		因为CloseConnect方法只是设置了关闭连接的标志,并将NetConnect从连接列表删除,
		并没有真的关闭socket。
		这是为了保证socket句柄在NetServer::OnClose业务完成前,不被系统重复使用,

		真正关闭是在NetEngine::CloseWorker()里是另外一个线程了。
		所以如果OnConnect业务中调用了关闭,但在CloseWorker线程执行前,
		在这里仍然有可能先被执行,监听成功,而这个监听是不希望发生的
	*/
	if ( pConnect->m_bConnect )
	{
		if ( !MonitorConnect(pConnect) )
		{
			CloseConnect(pConnect->GetSocket()->GetSocket());
		}
	}
	pConnect->Release();//业务层使用完毕,释放共享对象
	
	return 0;
}
Ejemplo n.º 4
0
//关闭一个连接
void NetEngine::CloseConnect( SOCKET sock )
{
	AutoLock lock( &m_connectsMutex );
	ConnectList::iterator itNetConnect = m_connectList.find( sock );
	if ( itNetConnect == m_connectList.end() ) return;//底层已经主动断开
	CloseConnect( itNetConnect );
}
Ejemplo n.º 5
0
//心跳线程
void NetEngine::HeartMonitor()
{
	if ( 0 >= m_nHeartTime ) return;//无心跳机制
	//////////////////////////////////////////////////////////////////////////
	//关闭无心跳的连接
	ConnectList::iterator it;
	NetConnect *pConnect;
	time_t tCurTime = 0;
	tCurTime = time( NULL );
	time_t tLastHeart;
	AutoLock lock( &m_connectsMutex );
	for ( it = m_connectList.begin();  it != m_connectList.end(); )
	{
		pConnect = it->second;
		if ( pConnect->m_host.IsServer() ) //服务连接,不检查心跳
		{
			it++;
			continue;
		}
		//检查心跳
		tLastHeart = pConnect->GetLastHeart();
		if ( tCurTime < tLastHeart || tCurTime - tLastHeart < m_nHeartTime )//有心跳
		{
			it++;
			continue;
		}
		//无心跳/连接已断开,强制断开连接
		CloseConnect( it );
		it = m_connectList.begin();
	}
	lock.Unlock();
}
Ejemplo n.º 6
0
/** 
 * 处理接收到网络消息
 *
 * @param state 要处理的网络状态
 * @param msg 如果处于连接状态,这里将是接收到的网络消息
 */
void CMole2dGameFrameDlg::OnProcessNetMessage(ConnectState state,CMolMessageIn &msg)
{
	switch(state)
	{
	case NOCONNECT:
		{
			AfxGetMainWnd()->PostMessage(IDD_SHOW_CONN_TIP,0,0);	

			UpdateState pUpdateState = CMole2dGameClientDlg::getSingleton().GetCurUpdateState();
			if(pUpdateState == UPDATESTATE_MAINFRAME)
			{
				MMessageBox("中心服务器连接失败,请检查本地网络设置。","登陆出错!",MB_OK);
				AfxGetMainWnd()->SendMessage(IDM_GAMEFRAME_MESSAGE,IDD_MESSAGE_GAME_CENTER_LOGIN_FAIL,0);	
			}
			else
			{
				MMessageBox("游戏服务器连接失败,请检查本地网络设置。","登陆出错!",MB_OK);
				AfxGetMainWnd()->SendMessage(IDM_GAMEFRAME_MESSAGE,IDD_MESSAGE_GAME_LOGIN_FAIL,0);
			}

			CloseConnect();
		}
		break;
	case CONNECTTING:
		{

		}
		break;
	case CONNECTED:
		{
			AfxGetMainWnd()->SendMessage(IDD_SHOW_CONN_TIP,0,0);

			UpdateState pUpdateState = CMole2dGameClientDlg::getSingleton().GetCurUpdateState();
			if(pUpdateState == UPDATESTATE_MAINFRAME)
			{
				// 连接成功后,开始验证用户的合法性
				CMolMessageOut out(IDD_MESSAGE_CENTER_LOGIN);
				out.writeString(m_loginName);
				out.writeString(GetStringMD5(m_loginPW));
				Send(out);
			}
			else 
			{
				// 连接成功后,开始验证用户的合法性
				CMolMessageOut out(IDD_MESSAGE_GAME_LOGIN);
				out.writeString(m_loginName);
				out.writeString(GetStringMD5(m_loginPW));
				Send(out);
			}
		}
		break;
	case MESPROCESS:
		{
			OnProcessNetMes(msg);
		}
		break;
	default:
		break;
	}
}
Ejemplo n.º 7
0
int GetQuotaData()
{
    GetConnect();
    memset(client_request, 0, REQUESTSIZE);
    strcat(client_request, "GET /v5.0/me/skydrive/quota?access_token=");
    strcat(client_request, api_access_token);
    strcat(client_request, " HTTP/1.1\r\n");
    strcat(client_request, "HOST: apis.live.net\r\n\r\n");
 //   printf("%s\n",client_request);

    if((SSL_write(ssl, client_request, strlen(client_request))) == -1)
    {
        printf("send fail.\n");
        return -1;
    }
  //  printf("sned success!\n");

    memset(service_respones, 0, REQUESTSIZE);
    if((state = SSL_read(ssl, service_respones, REQUESTSIZE)) == -1)
    {
        printf("recv faile.\n");
        return -1;
    }
    else if(state == 0)
    {
        printf("No available data.\n");
  //      return -1;
    }
  //  printf("recv success!\n");
    GetQuotaDataFromRespones();

    CloseConnect();
    return 0;
}
Ejemplo n.º 8
0
bool CSocketEx::SendCmdToServer(int cmd, void* pData   , DWORD dwSize  )
{
	int total = sizeof(_packageheader) + dwSize + 5;
	_packageheader *p = (_packageheader*)new char[total];
	memset(p, 0, sizeof(_packageheader));
	p->StartFlag = 0x5E7D;
	p->Cmd = cmd;
	p->Length = total;
	if (pData != NULL && dwSize )
	{
		CPackageHelper outpackage((char*)p);
		outpackage.pushString((char*)pData, dwSize);
	}

	int left = total;

	while (left)
	{
		int len = send(m_sock, (char*) &p[total-left], left, 0);
		if (len <=0)
		{
			CloseConnect();
			delete []p;			
			return false;
		}
		left -= len;
	}
	delete []p;		
	return true;
}
Ejemplo n.º 9
0
int download(char *service_name)
{
    char *location,*location_tmp;

    GetConnect();
    memset(client_request, 0, REQUESTSIZE);
    strcat(client_request, "GET /v5.0/");
    strcat(client_request, service_name);
    strcat(client_request, "/content?download=true&access_token=");
    strcat(client_request, api_access_token);
    strcat(client_request, " HTTP/1.1\r\n");
    strcat(client_request, "HOST: apis.live.net\r\n\r\n");
  //  printf("%s\n",client_request);

    if((SSL_write(ssl, client_request, strlen(client_request))) == -1)
    {
        printf("send fail.\n");
        return -1;
    }
//    printf("sned success!\n");

    memset(service_respones, 0, REQUESTSIZE);
    if((state = SSL_read(ssl, service_respones, REQUESTSIZE)) == -1)
    {
        printf("recv faile.\n");
        return -1;
    }
    else if(state == 0)
    {
        printf("No available data.\n");
  //      return -1;
    }
//    printf("recv success!\n");
//    printf("%s\n\n",service_respones);
    for(location = service_respones; *location != '\0'; location++)
    {
        if(*location == '\n')
        {
            location++;
            if(strncmp(location, "Location", 8) == 0)
                break;
        }
    }
    location+=10;
    for(location_tmp = location; *location_tmp != '\r'; location_tmp++);
    for(; *location_tmp !='?'; location_tmp--);
    *location_tmp = '\0';
    location-=strlen("wget --no-check-certificate ");
    strncpy(location, "wget --no-check-certificate ",strlen("wget --no-check-certificate "));
   // printf("%s",location);
    system(location);

    CloseConnect();
    FreeFileData(FileMes);
    GetFileData();
    return 0;
}
Ejemplo n.º 10
0
CUdpClient::~CUdpClient(void)
{
	if (getConnectAlive())
	{
		CloseConnect();
	}

	UninitConnect();
}
Ejemplo n.º 11
0
int upload(char *client_name,char *service_name)
{
    int file_fd;
    char file_data[REQUESTSIZE - NAMESIZE];
    int buf_size;

    GetConnect();
    file_fd = open(client_name, O_RDONLY);
    buf_size = read(file_fd, file_data, REQUESTSIZE - NAMESIZE);
    sprintf(tem_buf, "%d", buf_size);

    memset(client_request, 0, REQUESTSIZE);
    strcat(client_request, "PUT /v5.0/me/skydrive/files/");
    strcat(client_request, service_name);
    strcat(client_request, "?access_token=");
    strcat(client_request, api_access_token);
    strcat(client_request, " HTTP/1.1\r\n");
    strcat(client_request, "HOST: apis.live.net\r\n");
 //   strcat(client_request, "Content-Type:application/octet-stream\r\n");
    strcat(client_request, "Content-Length:");
    strcat(client_request, tem_buf);
    strcat(client_request, "\r\n\r\n");
    strcat(client_request, file_data);
  //  printf("\n\n%s\n\n\n",client_request);

    if((SSL_write(ssl, client_request, strlen(client_request))) == -1)
    {
        printf("send fail.\n");
        return -1;
    }
 //   printf("sned success!\n");
 //   printf("%s\n", client_request);

    memset(service_respones, 0, REQUESTSIZE);
    if((state = SSL_read(ssl, service_respones, REQUESTSIZE)) == -1)
    {
        printf("recv faile.\n");
        return -1;
    }
    else if(state == 0)
    {
        printf("No available data.\n");
  //      return -1;
    }
 //   printf("recv success!\n");
 //   printf("%s\n", service_respones);

    close(file_fd);
    CloseConnect();

    FreeFileData(FileMes);
    GetFileData();
    return 0;
}
//-------------------------------------------------------------------------
// 连接错误消息
void FKNetworkSocket::OnConnectError( int nErrorCode, const char* strErrorMsg )
{
	CloseConnect();

	if ( m_pOwner == NULL )
		return;
	FKNetworkSystem* p = dynamic_cast<FKNetworkSystem*>( m_pOwner );
	if( p == NULL )
		return;

	p->PushConnectErrorMsg( m_unSocketID, nErrorCode, strErrorMsg );
}
Ejemplo n.º 13
0
int move_copy(char *operation,char *path1, char *path2)
{
    GetConnect();

    memset(client_request_date, 0, REQUESTSIZE);
    strcat(client_request_date, "destination=");
    strcat(client_request_date, path2);

    sprintf(tem_buf ,"%d" ,strlen(client_request_date));

    memset(client_request, 0, REQUESTSIZE);
    strcat(client_request, operation);
    strcat(client_request, " /v5.0/");
    strcat(client_request, path1);
    strcat(client_request, " HTTP/1.1\r\n");
    strcat(client_request, "HOST: apis.live.net\r\n");
    strcat(client_request, "Authorization: Bearer ");
    strcat(client_request, api_access_token);
    strcat(client_request, "\r\n");
    strcat(client_request, "Content-type:application/x-www-form-urlencoded\r\n");
    strcat(client_request, "Content-Length:");
    strcat(client_request, tem_buf);
    strcat(client_request, "\r\n\r\n");
    strcat(client_request, client_request_date);
 //   printf("%s\n",client_request);

    if((SSL_write(ssl, client_request, strlen(client_request))) == -1)
    {
        printf("send fail.\n");
        return -1;
    }
 //   printf("sned success!\n");

    memset(service_respones, 0, REQUESTSIZE);
    if((state = SSL_read(ssl, service_respones, REQUESTSIZE)) == -1)
    {
        printf("recv faile.\n");
        return -1;
    }
    else if(state == 0)
    {
        printf("No available data.\n");
  //      return -1;
    }
 //   printf("recv success!\n");
  //  printf("%s\n",service_respones);

    CloseConnect();
    FreeFileData(FileMes);
    GetFileData();
    return 0;
}
Ejemplo n.º 14
0
CSocketEx::~CSocketEx(void)
{
	SetEvent(m_hExit);
	if(m_hThread)
	{
		WaitForSingleObject(m_hThread,INFINITE);
		CloseHandle(m_hThread);
		m_hThread = NULL;
	}
	CloseConnect();
	CloseHandle(m_hExit);
	WSACleanup();
}
Ejemplo n.º 15
0
int createfolder(const char *name)
{
    GetConnect();

    memset(client_request_date, 0, REQUESTSIZE);
  //  strcat(client_request_date, "type=folder");
    strcat(client_request_date, "name=");
    strcat(client_request_date, name);

    sprintf(tem_buf ,"%d" ,strlen(client_request_date));

    memset(client_request, 0, REQUESTSIZE);
    strcat(client_request, "POST /v5.0/me/skydrive");
    strcat(client_request, " HTTP/1.1\r\n");
    strcat(client_request, "HOST: apis.live.net\r\n");
    strcat(client_request, "Authorization: Bearer ");
    strcat(client_request, api_access_token);
    strcat(client_request, "\r\n");
    strcat(client_request, "Content-type:application/x-www-form-urlencoded\r\n");
    strcat(client_request, "Content-Length:");
    strcat(client_request, tem_buf);
    strcat(client_request, "\r\n\r\n");
    strcat(client_request, client_request_date);
//    printf("%s\n",client_request);

    if((SSL_write(ssl, client_request, strlen(client_request))) == -1)
    {
        printf("send fail.\n");
        return -1;
    }
 //   printf("sned success!\n");

    memset(service_respones, 0, REQUESTSIZE);
    if((state = SSL_read(ssl, service_respones, REQUESTSIZE)) == -1)
    {
        printf("recv faile.\n");
        return -1;
    }
    else if(state == 0)
    {
        printf("No available data.\n");
  //      return -1;
    }
//    printf("recv success!\n");
//    printf("%s\n",service_respones);

    CloseConnect();
    FreeFileData(FileMes);
    GetFileData();
    return 0;
}
Ejemplo n.º 16
0
void			AuthServerListener::ProcessCloseClients()
{
    vector<SOCKET> delSocket;
	for(int i=0; i<m_Users.Count(); i++)
	{
		if(m_Users.At(i)->GetIsClosed())
		{
			delSocket.push_back(m_Users.At(i)->GetClientSocket());
		}
	}
	for(DWORD i=0; i<delSocket.size(); i++)
	{
		CloseConnect(delSocket[i]);
	}
}
Ejemplo n.º 17
0
void CMole2dGameFrameDlg::OnClose()
{
	if(!CloseMyGameFrame()) return;
	//else
	//{
	//	EnableWindow(FALSE);
	//	pSwitchUserDlg.ShowWindow(SW_SHOW);
	//}

	Reset();
	ShowWindow(SW_HIDE);
	CloseConnect();
	CMole2dGameClientDlg::getSingleton().RefreshMyselfInfo();
	CMole2dGameClientDlg::getSingleton().ShowMainFrame();

	//CMolBaseFrame::OnClose();
}
Ejemplo n.º 18
0
bool CNetBase::ConncetToServer( int iConnectID, const char *pIPAddress, int iPort)
{
	if( m_pConnectHandles[iConnectID].m_bConnect )
	{
		CloseConnect( iConnectID );
	}
	m_pConnectHandles[iConnectID].m_Socket = socket(AF_INET,SOCK_STREAM,0);
	if( m_pConnectHandles[iConnectID].m_Socket == INVALID_SOCKET )
	{
		return false;
	}
	m_pConnectHandles[iConnectID].m_dwPort = iPort;
	memcpy( m_pConnectHandles[iConnectID].m_strIP, pIPAddress, 16 );

	SOCKADDR_IN kAddr;
	kAddr.sin_addr.S_un.S_addr = inet_addr(m_pConnectHandles[iConnectID].m_strIP);
	kAddr.sin_family = AF_INET;
	kAddr.sin_port = htons( (u_short)m_pConnectHandles[iConnectID].m_dwPort);

	if( connect( m_pConnectHandles[iConnectID].m_Socket, (SOCKADDR*)&kAddr, sizeof(SOCKADDR) ) )
	{
		return false;
	}
	unsigned   long   ul   =   1;   
	int ret   =   ioctlsocket(m_pConnectHandles[iConnectID].m_Socket,   FIONBIO,   (unsigned   long*)&ul);   
	if(ret==SOCKET_ERROR)   
	{   
		::closesocket   (m_pConnectHandles[iConnectID].m_Socket);   
		return false;   
	}

	m_pConnectHandles[iConnectID].m_bConnect = true;
	m_pConnectHandles[iConnectID].m_iEndPtr = 0;
	m_pConnectHandles[iConnectID].m_iStartPtr = 0;
	m_pConnectHandles[iConnectID].m_pRecvFunc = m_pRecvFunc;
	m_pConnectHandles[iConnectID].m_SendLen = 0;
	m_pConnectHandles[iConnectID].m_bIsClosed = false;

	m_pConnectHandles[iConnectID].m_dwThreadHandle = CreateThread( NULL,0,(LPTHREAD_START_ROUTINE)NetWorkThread,&m_pConnectHandles[iConnectID],0,0 );

	return true;
}
Ejemplo n.º 19
0
int deletefile(char *path)
{
    GetConnect();

    memset(client_request, 0, REQUESTSIZE);
    strcat(client_request, "DELETE /v5.0/");
    strcat(client_request, path);
    strcat(client_request, "?access_token=");
    strcat(client_request, api_access_token);
    strcat(client_request, " HTTP/1.1\r\n");
    strcat(client_request, "HOST: apis.live.net\r\n\r\n");
 //   printf("%s\n",client_request);

    if((SSL_write(ssl, client_request, strlen(client_request))) == -1)
    {
        printf("send fail.\n");
        return -1;
    }
 //   printf("sned success!\n");

    memset(service_respones, 0, REQUESTSIZE);
    if((state = SSL_read(ssl, service_respones, REQUESTSIZE)) == -1)
    {
        printf("recv faile.\n");
        return -1;
    }
    else if(state == 0)
    {
        printf("No available data.\n");
  //      return -1;
    }
 //   printf("recv success!\n");
 //   printf("%s\n",service_respones);

    CloseConnect();
    FreeFileData(FileMes);
    GetFileData();
    return 0;
}
Ejemplo n.º 20
0
bool STNetEngine::OnConnect( SOCKET sock, bool isConnectServer )
{
	AtomAdd(&g_c, 1);
	STNetConnect *pConnect = new (m_pConnectPool->Alloc())STNetConnect(sock, isConnectServer, m_pNetMonitor, this, m_pConnectPool);
	if ( NULL == pConnect ) 
	{
		closesocket(sock);
		return false;
	}
	//加入管理列表
	pConnect->RefreshHeart();
	AtomAdd(&pConnect->m_useCount, 1);//被m_connectList访问
	AutoLock lock( &m_connectsMutex );
	pair<ConnectList::iterator, bool> ret = m_connectList.insert( ConnectList::value_type(pConnect->GetSocket()->GetSocket(),pConnect) );
	lock.Unlock();
	//执行业务
	STNetHost accessHost = pConnect->m_host;//被引擎访问,局部变量离开时,析构函数自动释放访问
	m_pNetServer->OnConnect( pConnect->m_host );
	/*
		监听连接
		必须等OnConnect完成,才可以开始监听连接上的IO事件
		否则,可能业务层尚未完成连接初始化工作,就收到OnMsg通知,
		导致业务层不知道该如何处理消息
	 */
	bool bMonitor = true;
	if ( !m_pNetMonitor->AddMonitor(sock) ) return false;
#ifdef WIN32
	bMonitor = m_pNetMonitor->AddRecv( 
		sock, 
		(char*)(pConnect->PrepareBuffer(BUFBLOCK_SIZE)), 
		BUFBLOCK_SIZE );
#else
	bMonitor = m_pNetMonitor->AddIO( sock, true, false );
#endif
	if ( !bMonitor ) CloseConnect(pConnect->GetSocket()->GetSocket());
	return true;
}
Ejemplo n.º 21
0
int GetFolderInfo(struct FileData* Folder)
{
    GetConnect();
    memset(client_request, 0, REQUESTSIZE);
    strcat(client_request, "GET /v5.0/");
    strcat(client_request, Folder->id);
    strcat(client_request, "/files?access_token=");
    strcat(client_request, api_access_token);
    strcat(client_request, " HTTP/1.1\r\n");
    strcat(client_request, "HOST: apis.live.net\r\n\r\n");
 //   printf("%s\n",client_request);

    if((SSL_write(ssl, client_request, strlen(client_request))) == -1)
    {
        printf("send fail.\n");
        return -1;
    }
 //   printf("sned success!\n");

    memset(service_respones, 0, REQUESTSIZE);
    if((state = SSL_read(ssl, service_respones, REQUESTSIZE)) == -1)
    {
        printf("recv faile.\n");
        return -1;
    }
    else if(state == 0)
    {
        printf("No available data.\n");
  //      return -1;
    }
 //   printf("recv success!\n");
 //   printf("%s\n",service_respones);
    GetFolderInfoFromRespones(Folder);

    CloseConnect();
    return 0;
}
Ejemplo n.º 22
0
//心跳线程
void STNetEngine::HeartMonitor()
{
	if ( 0 >= m_nHeartTime ) return;
	//////////////////////////////////////////////////////////////////////////
	//关闭无心跳的连接
	ConnectList::iterator it;
	STNetConnect *pConnect;
	time_t tCurTime = 0;
	/*	
		创建一个临时的释放列表保存要释放的对象,等遍历结束1次性导入等待释放对象列表
		避免在循环中因为重复的为等待释放列表的访问而加锁解所
	 */
	tCurTime = time( NULL );
	time_t tLastHeart;
	AutoLock lock( &m_connectsMutex );
	for ( it = m_connectList.begin(); it != m_connectList.end(); )//心跳时间<=0无心跳机制,或遍历完成
	{
		pConnect = it->second;
		if ( pConnect->m_host.IsServer() ) //服务连接 ,不检查心跳
		{
			it++;
			continue;
		}
		//检查心跳
		tLastHeart = pConnect->GetLastHeart();
		if ( tCurTime < tLastHeart || tCurTime - tLastHeart < m_nHeartTime )//有心跳
		{
			it++;
			continue;
		}
		//无心跳/连接已断开,强制断开连接,加入释放列表
		CloseConnect( it );
		it = m_connectList.begin();
	}
	lock.Unlock();
}
Ejemplo n.º 23
0
/** 
 * 用于处理接收到的事件
 *
 * @param event 要处理的本地游戏事件
 *
 * @return 如果事件处理成功返回真,否则返回假
 */
bool CMole2dGameFrameDlg::OnProcessEvent(const CMolEvent& event)
{
	switch(event.mEventType)
	{
	case MOLET_GUI_EVENT:                               // GUI事件
		{
			switch(event.mGuiEvent.EventType)
			{
			case MOLGET_ELEMENT_SELECTED:               // 控件被选中
				{
					int eventid = event.mGuiEvent.Caller->GetID();

					if(eventid == MOLE2D_GUI_ROOM_LIST)
					{
						CMolGUIRoomList *pRooList = static_cast<CMolGUIRoomList*>(event.mGuiEvent.Caller);
						if(pRooList)
						{
							if(!pRooList->isQueueEnterRoom())
							{
								CMolGUIRoom *pRoom = pRooList->GetGameRoomSelected();
								if(pRoom == NULL) return CMolBaseFrame::OnProcessEvent(event);

								// 如果这桌还没有开始游戏,那么进入游戏桌
								if(pRoom->GetRoomState() != ROOMSTATE_GAMING &&
									pRooList->GetGameRoomChairSelected()->m_player == NULL)
								{
									Player *myselfInfo = GetMySelf();
									if(myselfInfo == NULL) return true;

									if(myselfInfo->GetMoney() < CMole2dGameClientDlg::getSingleton().GetCurrentGameServer()->lastMoney)
									{
										PostMessage(IDD_GAMEFRAM_MESSAGEBOX,3,0);
										return true;
									}

									CMolMessageOut out(IDD_MESSAGE_FRAME);
									out.writeShort(IDD_MESSAGE_ENTER_ROOM);
									out.writeShort(pRooList->GetGameRoomIndexSelected());
									out.writeShort(pRooList->GetGameChairIndexSelected());
									Send(out);
								}
								else if(pRoom->GetRoomState() == ROOMSTATE_GAMING &&
									pRooList->GetGameRoomChairSelected()->m_player != NULL)
								{
									// 旁观进入
									CMolMessageOut out(IDD_MESSAGE_FRAME);
									out.writeShort(IDD_MESSAGE_LOOKON_ENTER_ROOM);
									out.writeShort(pRooList->GetGameRoomIndexSelected());
									out.writeShort(pRooList->GetGameChairIndexSelected());
									Send(out);
								}
							}
							else
							{
								Player *myselfInfo = GetMySelf();
								if(myselfInfo == NULL) return true;

								if(myselfInfo->GetMoney() < CMole2dGameClientDlg::getSingleton().GetCurrentGameServer()->lastMoney)
								{
									PostMessage(IDD_GAMEFRAM_MESSAGEBOX,3,0);
									return true;
								}

								CMolMessageOut out(IDD_MESSAGE_FRAME);
								out.writeShort(IDD_MESSAGE_ENTER_ROOM);
								out.writeShort(-1);
								out.writeShort(-1);
								Send(out);
							}
						}
					}
				}
				break;
			case MOLGET_BUTTON_CLICKED:                 // 按钮点击事件
				{
					int eventid = event.mGuiEvent.Caller->GetID();

					if(eventid == MOLE2D_GUI_MIN_FRAME)
					{
						MinGameFrame();
					}
					else if(eventid == IDD_MAINFRAME_HOMEPAGE)
					{
						CMolGUIRoomList *pGameList = GetRoomList();
						if(pGameList == NULL) return false;

						pGameList->Home();
					}
					else if(eventid == IDD_MAINFRAME_LASTPAGE)
					{
						CMolGUIRoomList *pGameList = GetRoomList();
						if(pGameList == NULL) return false;

						pGameList->PageUp();
					}
					else if(eventid == IDD_MAINFRAME_NEXTPAGE)
					{
						CMolGUIRoomList *pGameList = GetRoomList();
						if(pGameList == NULL) return false;

						pGameList->PageDown();
					}
					else if(eventid == IDD_MAINFRAME_TAILPAGE)
					{
						CMolGUIRoomList *pGameList = GetRoomList();
						if(pGameList == NULL) return false;

						pGameList->End();
					}
					else if(eventid == IDD_MAINFRAME_AUTOJOIN)
					{
						CMolGUIRoomList *pGameList = GetRoomList();
						if(pGameList == NULL) return false;

						Player *mySelfPlayer = pGameList->IsExistPlayer(pGameList->GetMySelfID());
						if(mySelfPlayer == NULL) return false;

						pGameList->AutoJoin(mySelfPlayer->GetID());
					}
					else if(eventid == MOLE2D_GUI_CLOSE_CONTAINER)
					{
						if(CloseMyGameFrame())
						{
							Reset();
							ShowWindow(SW_HIDE);
							CloseConnect();

							CMole2dGameClientDlg::getSingleton().RefreshMyselfInfo();
							CMole2dGameClientDlg::getSingleton().ShowMainFrame();
							//EnableWindow(FALSE);
							//pSwitchUserDlg.ShowWindow(SW_SHOW);
							return false;
						}

						return true;
					}
				}
				break;

			default:
				break;
			}
		}
		break;
	default:
		break;
	}

	return CMolBaseFrame::OnProcessEvent(event);
}
Ejemplo n.º 24
0
/** 
 * 处理用户登录系统消息
 *
 * @param mes 接收到要处理的网络消息
 */
void CMole2dGameFrameDlg::OnProcessUserLoginMes(CMolMessageIn &mes)
{
	switch(mes.readShort())
	{
	case IDD_MESSAGE_GAME_LOGIN_SUCESS:
		{
			CMolGUIRoomList *pGameList = GetRoomList();
			if(pGameList == NULL) return;

			if(pGameList->GetMySelfID() != -1)
			{
				unsigned int pPlayerId = mes.readLong();

				// 首先检查服务器中是否有这个玩家
				Player *pPlayer = pGameList->IsExistPlayer(pPlayerId);
				if(pPlayer == NULL)
				{
					pPlayer = new Player(pPlayerId);
					pPlayer->SetState((PlayerState)mes.readByte());
					pPlayer->SetType((PlayerType)mes.readByte());
					pPlayer->SetRoomId(mes.readShort());
					pPlayer->SetRoomIndex(mes.readShort());
					pPlayer->SetName(mes.readString());
					pPlayer->SetUserAvatar(mes.readString());
					pPlayer->SetLevel(mes.readShort());
					pPlayer->SetMoney(mes.readDouble());
					pPlayer->SetExperience(mes.readLong());			
					pPlayer->SetTotalBureau(mes.readShort());
					pPlayer->SetSuccessBureau(mes.readShort());
					pPlayer->SetFailBureau(mes.readShort());
					pPlayer->SetSuccessRate(mes.readFloat());
					pPlayer->SetRunawayrate(mes.readFloat());

					pGameList->AddGamePlayer(pPlayer);	
				}	

				if(pPlayerId == pGameList->GetMySelfID())
				{
					m_revPlayerCount = 0;

					// 获取在线玩家列表
					CMolMessageOut out(IDD_MESSAGE_FRAME);
					out.writeShort(IDD_MESSAGE_GET_ONLINE_PLAYERS);
					Send(out);

					AfxGetMainWnd()->SendMessage(IDD_SHOW_CONN_TIP,1,0);
				}
			}
			else
			{
				//// 先清空原来的数据
				//pGameList->Clear();

				unsigned int pPlayerId = mes.readLong();

				// 首先检查服务器中是否有这个玩家
				Player *pPlayer = pGameList->IsExistPlayer(pPlayerId);
				if(pPlayer == NULL)
				{
					pPlayer = new Player(pPlayerId);
					pPlayer->SetState((PlayerState)mes.readByte());
					pPlayer->SetType((PlayerType)mes.readByte());
					pPlayer->SetRoomId(mes.readShort());
					pPlayer->SetRoomIndex(mes.readShort());
					pPlayer->SetName(mes.readString());
					pPlayer->SetUserAvatar(mes.readString());
					pPlayer->SetLevel(mes.readShort());
					pPlayer->SetMoney(mes.readDouble());
					pPlayer->SetExperience(mes.readLong());			
					pPlayer->SetTotalBureau(mes.readShort());
					pPlayer->SetSuccessBureau(mes.readShort());
					pPlayer->SetFailBureau(mes.readShort());
					pPlayer->SetSuccessRate(mes.readFloat());
					pPlayer->SetRunawayrate(mes.readFloat());

					if(pGameList->GetMySelfID() == -1)
					{
						pGameList->SetMySelfID(pPlayer->GetID());
					}

					pGameList->AddGamePlayer(pPlayer);	
				}	

				m_revPlayerCount = 0;

				// 获取在线玩家列表
				CMolMessageOut out(IDD_MESSAGE_FRAME);
				out.writeShort(IDD_MESSAGE_GET_ONLINE_PLAYERS);
				Send(out);

				AfxGetMainWnd()->SendMessage(IDD_SHOW_CONN_TIP,1,0);
			}
		}
		break;
	case IDD_MESSAGE_GAME_LOGIN_FAIL:
		{
			MMessageBox("游戏服务器登陆失败,请检查您的账户和密码。","登陆出错!",MB_OK);
			AfxGetMainWnd()->SendMessage(IDM_GAMEFRAME_MESSAGE,IDD_MESSAGE_GAME_LOGIN_FAIL,0);
			CloseConnect();
		}
		break;
	case IDD_MESSAGE_GAME_LOGIN_FULL:
		{
			MMessageBox("游戏服务器已满,请选择其它游戏服务器进入。","登陆出错!",MB_OK);
			AfxGetMainWnd()->SendMessage(IDM_GAMEFRAME_MESSAGE,IDD_MESSAGE_GAME_LOGIN_FULL,0);
			CloseConnect();
		}
		break;
	case IDD_MESSAGE_GAME_LOGIN_EXIST:
		{
			MMessageBox("当前您已经在游戏服务器中,不能重复登陆。","登陆出错!",MB_OK);
			AfxGetMainWnd()->SendMessage(IDM_GAMEFRAME_MESSAGE,IDD_MESSAGE_GAME_LOGIN_EXIST,0);
			CloseConnect();
		}
		break;
	default:
		break;
	}
}
Ejemplo n.º 25
0
/** 
 * 处理用户登录中心服务器消息
 *
 * @param mes 接收到要处理的网络消息
 */
void CMole2dGameFrameDlg::OnProcessUserCenterMes(CMolMessageIn &mes)
{
	switch(mes.readShort())
	{
	case IDD_MESSAGE_CENTER_LOGIN_SUCESS:
		{
			CMolGUIRoomList *pGameList = GetRoomList();
			if(pGameList == NULL) return;

			//int MyPlayerId = pGameList->GetMySelfID();

			//if(MyPlayerId == -1)
			//{
				unsigned int pPlayerId = mes.readLong();

				// 首先检查服务器中是否有这个玩家
				Player *pPlayer = pGameList->IsExistPlayer(pPlayerId);
				if(pPlayer == NULL)
				{
					pPlayer = new Player(pPlayerId);
					//pPlayer->SetState((PlayerState)mes.readByte());
					//pPlayer->SetType((PlayerType)mes.readByte());
					//pPlayer->SetRoomId(mes.readShort());
					//pPlayer->SetRoomIndex(mes.readShort());
					pPlayer->SetName(mes.readString());
					pPlayer->SetUserAvatar(mes.readString());
					pPlayer->SetLevel(mes.readShort());
					pPlayer->SetMoney(mes.readDouble());
					pPlayer->SetExperience(mes.readLong());			
					pPlayer->SetTotalBureau(mes.readShort());
					pPlayer->SetSuccessBureau(mes.readShort());
					pPlayer->SetFailBureau(mes.readShort());
					pPlayer->SetSuccessRate(mes.readFloat());
					pPlayer->SetRunawayrate(mes.readFloat());

					pGameList->AddGamePlayer(pPlayer);	
				}	

				if(pGameList->GetMySelfID() == -1)
				{
					pGameList->SetMySelfID(pPlayer->GetID());

					CMole2dGameClientDlg::getSingleton().SetMySelfInfo(pPlayer);
				}

				CMole2dGameClientDlg::getSingleton().RefreshMyselfInfo();
			//}

			// 获取在线玩家列表
			CMolMessageOut out(IDD_MESSAGE_GET_GAMESERVER);
			Send(out);

			AfxGetMainWnd()->SendMessage(IDD_SHOW_CONN_TIP,4,0);			
		}
		break;
	case IDD_MESSAGE_CENTER_LOGIN_FAIL:
		{
			MMessageBox("中心服务器登陆失败,请检查您的账户和密码。","登陆出错!",MB_OK);
			AfxGetMainWnd()->SendMessage(IDM_GAMEFRAME_MESSAGE,IDD_MESSAGE_GAME_CENTER_LOGIN_FAIL,0);
			CloseConnect();
		}
		break;
	case IDD_MESSAGE_GET_GAMESERVER:
		{
			int GameServerCount = mes.readShort();

			for(int index = 0;index < GameServerCount;index++)
			{
				int GameId = mes.readLong();
				std::string ServerName = mes.readString();
				std::string ServerIp = mes.readString();
				int ServerPort = mes.readShort();
				int OnlinePlayerCount = mes.readShort();
				int lastMoney = mes.readDouble();

				CMole2dGameClientDlg::getSingleton().AddGameServer(GameServerInfo(GameId,ServerName,ServerIp,ServerPort,OnlinePlayerCount,lastMoney));
			}

			AfxGetMainWnd()->SendMessage(IDD_SHOW_CONN_TIP,0,0);

			// 登录成功后就关闭连接
			CloseConnect();

			// 如果用户选择了保存用户,就将用户名写入数据库中
			if(CMole2dGameClientDlg::getSingleton().IsRegisterPWD())
				WriteRegisterKey("MOLEUSERNAME",m_loginName);

			// 显示主窗口
			CMole2dGameClientDlg::getSingleton().ShowMainFrame();
		}
		break;
	default:
		break;
	}
}
//-------------------------------------------------------------------------
// 释放清理
void FKNetworkSocket::Release()
{
	CloseConnect();
}