Пример #1
0
void RemoteNetworkView::RemoveHTTPSocket(IEventDataPtr pEventData) {
	printf("Remove HTTP Socket %d\n", pEventData->VGetSocketId());
	NetSocket* netSocket = g_pSocketManager->FindSocket(pEventData->VGetSocketId());
	// say to delete socket AFTER packet treatment
	if (netSocket) {
	    netSocket->SetSocketDelete(4);
	}	
}
Пример #2
0
bool BaseSocketManager::Send(int sockId, std::shared_ptr<IPacket> packet) {
    NetSocket* sock = FindSocket(sockId);
    if (!sock)
        return false;

    sock->Send(packet);
    return true;
}
Пример #3
0
int BaseSocketManager::GetIpAddress(int sockId) {
    NetSocket* socket = FindSocket(sockId);
    if (socket) {
        return socket->GetIpAddress();
    } else {
        return 0;
    }
}
Пример #4
0
void* NetSocket::WorkThread(void *lpParam)
{
	NetSocket *pThis = (NetSocket*)lpParam;
	pThis->ConnectServer();
	while( pThis->m_bIsConnected )
	{
		pThis->NetLogic();
	}
	return NULL;
}
Пример #5
0
void ServerSocketManager::broadcastToAllClients(shared_ptr<BinaryPacket> packet)
{
	for (SocketList::iterator it = mSockList.begin(); it != mSockList.end(); ++it)
    {
		NetSocket *sock = *it;

		if ((dynamic_cast<RemoteSocket*>(sock)) != NULL)
		{
			sock->send(packet);
		}
	}
}
Пример #6
0
DWORD WINAPI NetSocket::WorkThread(LPVOID lparam)
{
	NetSocket *pThis = (NetSocket*)lparam;
	
	pThis->ConnectServer();

	while( pThis->m_bIsConnected )
	{
		pThis->NetLogic();
		Sleep( 2 );
	}

	return 0;
}
Пример #7
0
void NetServer::OnUpdateRecived()
{

	static size_t nSocketSize = 0;
	static int32 nMsgBodyLen = 0;
	static NetMsgHead* pMsg = NULL;

	nSocketSize = m_vecUsedSocket.size();

	for(size_t nIndex = 0; nIndex < nSocketSize; ++nIndex)
	{
		NetSocket* pSocket = m_vecUsedSocket[nIndex];
		if(!pSocket)
			continue;
		
		
		switch(pSocket->ReadMsg(&pMsg,nMsgBodyLen))
		{
			case MSG_READ_INVALID: // 断开socket,再设置为重新等待 
			{
				printf("[WARRING]:MSG_READ_INVALID \n");
				(m_pOnExit)(*pSocket);
				m_vecUsedSocket.erase(m_vecUsedSocket.begin() + nIndex);
				pSocket->Disconnect();
				pSocket->Clear();
				SetAccept(*pSocket);
				nIndex--;
				nSocketSize--;
			}
				break;
			case MSG_READ_OK: // 收到正常的数据请求 
			{
				(m_pOnMsg)(*pSocket, pMsg,nMsgBodyLen);
				pSocket->RemoveMsg(PACKAGE_HEADER_SIZE + nMsgBodyLen);
			}
				break;
			case MSG_READ_WAITTING:
			case MSG_READ_REVCING:
				break;
		}
	}
}
Пример #8
0
void MatchMaker_RequestConnection( char *_targetIp, int _targetPort, Directory *_myDetails )
{
#ifdef WAN_PLAY_ENABLED
    AppDebugOut( "Requesting connection to %s:%d via matchmaker\n", _targetIp, _targetPort );

    Directory request( _myDetails );
    request.SetName     ( NET_MATCHMAKER_MESSAGE );
    request.CreateData  ( NET_METASERVER_COMMAND, NET_MATCHMAKER_REQUEST_CONNECT );
    request.CreateData  ( NET_MATCHMAKER_TARGETIP, _targetIp );
    request.CreateData  ( NET_MATCHMAKER_TARGETPORT, _targetPort );

    AppAssert( s_matchMakerIp );

    NetSocket socket;   
    NetRetCode result = socket.Connect( s_matchMakerIp, s_matchMakerPort );
    AppAssert( result == NetOk );

    MetaServer_SendDirectory( &request, &socket );
#endif
}
Пример #9
0
void NetClient::SetConnect(NetSocket& rSocket)
{
	rSocket.async_connect(m_cServerAddr,boost::bind(&NetClient::HandleConnect,this,boost::asio::placeholders::error,&rSocket));
}
Пример #10
0
void GetLocalHostIP( char *str, int len )
{
	static bool retrievedAddress = false;
	static char localHostIP[16];

	if (!retrievedAddress) 
	{
		// Try to find the IP address of the default route (without using name lookup)
		NetSocket s;
	
		// It doesn't actually matter whether the host is reachable
		// Since UDP is a connectionless protocol, the only effect 
		// of connect is to see if there is a route to the specified
		// host.
		
		if (s.Connect( "80.175.29.66", 5000 ) == NetOk) 
		{
			NetIpAddress localAddress = s.GetLocalAddress();

			// On some systems (Win98) this can return 0.0.0.0 
			// which is not a valid IP to connect to.
			if (* (unsigned long *) (&localAddress.sin_addr) != 0) {
				IpAddressToStr( localAddress, localHostIP );
				retrievedAddress = true;
			}

			// It seems that on some systems this can return 0.0.0.0, strange, strange!
			if (strcmp( localHostIP, "0.0.0.0" ) != 0)
				retrievedAddress = true;
		}

		if (!retrievedAddress) {
			// Not connected to the internet (or 0.0.0.0 returned), revert to 
			// host name lookup method

			// Default fallback
			strcpy( localHostIP, "127.0.0.1" );

			// Get our host name
			char hostname[256];
			int result = gethostname( hostname, sizeof(hostname) );
			if (result >= 0) {
				struct hostent *lpHostEnt = gethostbyname( hostname );

				if (lpHostEnt) {
					NetIpAddress addr;
					memcpy( &addr.sin_addr, lpHostEnt->h_addr_list[0], sizeof(addr.sin_addr) );
					addr.sin_port = 0;

					IpAddressToStr( addr, localHostIP );
				}
			}
			
		}

		AppDebugOut("Local IP address is: %s\n", localHostIP );
		retrievedAddress = true;
	}

	strncpy( str, localHostIP, len );
	str[ len - 1 ] = '\0';
}
Пример #11
0
void BaseSocketManager::DoSelect(int pauseMicroSecs, int handleInput) {
    timeval tv;
    tv.tv_sec = 0;

    tv.tv_usec = pauseMicroSecs;

    fd_set inp_set, out_set, exc_set;
    int maxDesc;

    FD_ZERO(&inp_set);
    FD_ZERO(&out_set);
    FD_ZERO(&exc_set);

    maxDesc = 0;

    for (SocketList::iterator i = m_SockList.begin(); i != m_SockList.end(); ++i) {
        NetSocket* pSock = *i;
        if ((pSock->m_deleteFlag & 1) || pSock->m_sock == INVALID_SOCKET)
            continue;

        if (handleInput)
            FD_SET(pSock->m_sock, &inp_set);

        FD_SET(pSock->m_sock, &exc_set);

        if (pSock->VHasOutput())
            FD_SET(pSock->m_sock, &out_set);

        if ((int)pSock->m_sock > maxDesc)
            maxDesc = (int)pSock->m_sock;
    }

    int selRet = 0;

    selRet = select(maxDesc + 1, &inp_set, &out_set, &exc_set, &tv);
    if (selRet == SOCKET_ERROR) {
        GCC_ERROR("Error in DoSelect!");
        return;
    }

    if (selRet) {
        for (SocketList::iterator i = m_SockList.begin(); i != m_SockList.end(); ++i) {
            NetSocket* pSock = *i;

            if ((pSock->m_deleteFlag & 1) || pSock->m_sock == INVALID_SOCKET)
                continue;

            if (FD_ISSET(pSock->m_sock, &exc_set))
                pSock->HandleException();

            if (!(pSock->m_deleteFlag & 1) && FD_ISSET(pSock->m_sock, &out_set))
                pSock->VHandleOutput();

            if (handleInput && !(pSock->m_deleteFlag & 1) && FD_ISSET(pSock->m_sock, &inp_set)) {
                pSock->VHandleInput();
            }
        }
    }

    unsigned int timeNow = timeGetTime();

    SocketList::iterator i = m_SockList.begin();
    while (i != m_SockList.end()) {
        NetSocket* pSock = *i;
        if (pSock->m_timeOut && pSock->m_timeOut < timeNow) 
            pSock->VTimeOut();

        if (pSock->m_deleteFlag & 1) {
            switch (pSock->m_deleteFlag) {
                case 1:
                    g_pSocketManager->RemoveSocket(pSock);
                    i = m_SockList.begin();
                    break;

                case 3:
                    pSock->m_deleteFlag = 2;
                    if (pSock->m_sock != INVALID_SOCKET) {
                        closesocket(pSock->m_sock);
                        pSock->m_sock = INVALID_SOCKET;
                    }
                    break;
            }
        }
        i++;
    }
}
Пример #12
0
//
// BaseSocketManager::DoSelect					- Chapter 19, page 679
//
void BaseSocketManager::DoSelect(int pauseMicroSecs, bool handleInput) 
{
	timeval tv;
	tv.tv_sec = 0;
	tv.tv_usec = pauseMicroSecs;    // 100 microseconds is 0.1 milliseconds or .0001 seconds

	fd_set inp_set, out_set, exc_set;
	int maxdesc;

	FD_ZERO(&inp_set);
	FD_ZERO(&out_set);
	FD_ZERO(&exc_set);

	maxdesc = 0;

	// set everything up for the select
	for (SocketList::iterator i = m_SockList.begin(); i != m_SockList.end(); ++i)
	{
		NetSocket *pSock = *i;
		if ((pSock->m_deleteFlag&1) || pSock->m_sock == INVALID_SOCKET)
			continue;

		if (handleInput)
			FD_SET(pSock->m_sock, &inp_set);

		FD_SET(pSock->m_sock, &exc_set);

		if (pSock->VHasOutput())
			FD_SET(pSock->m_sock, &out_set);

		if ((int)pSock->m_sock > maxdesc)
			maxdesc = (int)pSock->m_sock;

	 }
  
	int selRet = 0;

	// do the select (duration passed in as tv, NULL to block until event)
	selRet = select(maxdesc+1, &inp_set, &out_set, &exc_set, &tv) ;
	if (selRet == SOCKET_ERROR)
	{
		PrintError();
		return;
	}

	// handle input, output, and exceptions

	if (selRet)
	{
		for (SocketList::iterator i = m_SockList.begin(); i != m_SockList.end(); ++i)
		{
			NetSocket *pSock = *i;

			if ((pSock->m_deleteFlag&1) || pSock->m_sock == INVALID_SOCKET)
				continue;

			if (FD_ISSET(pSock->m_sock, &exc_set))
			{
				pSock->HandleException();
			}

			if (!(pSock->m_deleteFlag&1) && FD_ISSET(pSock->m_sock, &out_set))
			{
				pSock->VHandleOutput();
			}

			if (   handleInput
				&& !(pSock->m_deleteFlag&1) && FD_ISSET(pSock->m_sock, &inp_set))
			{
				pSock->VHandleInput();
			}
		 }	
	}

	unsigned int timeNow = GLUFGetTimeMs();

	// handle deleting any sockets
	SocketList::iterator i = m_SockList.begin();
	while (i != m_SockList.end())
	{
		NetSocket *pSock = *i;
		if (pSock->m_timeOut) 
		{
			if (pSock->m_timeOut < timeNow)
			{
				pSock->VTimeOut();
			}
		}

		if (pSock->m_deleteFlag&1)
		{
			switch (pSock->m_deleteFlag) 
			{
			  case 1:
					g_pSocketManager->RemoveSocket(pSock);
					i = m_SockList.begin();
					break;
				case 3:
					pSock->m_deleteFlag = 2;
					if (pSock->m_sock != INVALID_SOCKET) 
					{
						closesocket(pSock->m_sock);
						pSock->m_sock = INVALID_SOCKET;
					}
					break;
			}
		}

		++i;

	 }
 }