Exemple #1
0
qboolean NET_GetPacket(struct qw_connection *qwc, enum netsrc sock, struct sizebuf_s *message, struct netaddr *from)
{
	int ret;
	struct SysSocket *net_socket;

	if (NET_GetLoopPacket(qwc, sock, message, from))
		return true;

	net_socket = qwc->netdata->sockets[sock];
	if (net_socket == 0)
		return false;

	ret = Sys_Net_Receive(qwc->netdata->sysnetdata, net_socket, message->data, message->maxsize, from);

	if (ret <= 0)
	{
		if (ret < 0)
			Com_Printf("%s: Network error.\n", __func__);

		return false;
	}

	message->cursize = ret;

	return true;
}
static void QTVR_Thread(void *arg)
{
	struct qtvr *qtvr;
	int r;
	char buf[128];
	struct netaddr serveraddr;
	struct netaddr addr;

	struct SysNetData *netdata;
	struct SysSocket *socket;

	qtvr = arg;

	netdata = Sys_Net_Init();

	if (netdata == NULL)
	{
		Com_Printf("QTVR_Thread: Sys_Net_Init failed.\n");
	}
	else
	{
		if (!NET_StringToAdr(netdata, qtvr->server, &serveraddr))
		{
			Com_Printf("QTVR_Thread: Unable to parse/resolve hoststring.\n");
		}
		else
		{
			socket = Sys_Net_CreateSocket(netdata, serveraddr.type);

			if (socket == NULL)
			{
				Com_Printf("QTVR_Create: could not create socket.\n");
			}
			else
			{
				Sys_Net_Send(netdata, socket, qtvr->request, strlen(qtvr->request), &serveraddr);

				while (!qtvr->quit)
				{
					Sys_Net_Wait(netdata, socket, 50000);

					if ((r = Sys_Net_Receive(netdata, socket, buf, sizeof(buf) - 1, &addr)) > 0)
					{
						buf[r] = 0;

						Sys_Thread_LockMutex(qtvr->mutex);
						qtvr->retval = strdup(buf);
						if (qtvr->retval == NULL)
							qtvr->status = QTVR_ERROR;
						else
							qtvr->status = QTVR_REPLY_RECIEVED;
						Sys_Thread_UnlockMutex(qtvr->mutex);

						break;
					}
				}

				Sys_Net_DeleteSocket(netdata, socket);
			}
		}

		Sys_Net_Shutdown(netdata);
	}

	if (qtvr->status != QTVR_REPLY_RECIEVED)
		qtvr->status = QTVR_ERROR;
}
unsigned int ServerScanner_DoStuffInternal(struct ServerScanner *serverscanner)
{
	struct qwserverpriv *qwserver;
	unsigned int i;
	unsigned long long curtime;
	unsigned int timeout;
	int r;
	unsigned char buf[8192];
	struct netaddr addr;

	if (serverscanner->status == SSS_ERROR || serverscanner->status == SSS_IDLE)
		return 50000;

	if (!serverscanner->initialstuffdone)
	{
		if (!ServerScanner_Thread_Init(serverscanner))
		{
			serverscanner->status = SSS_ERROR;
			return 50000;
		}

		ServerScanner_Thread_LookUpMasters(serverscanner);
		ServerScanner_Thread_OpenSockets(serverscanner);

		if (!serverscanner->numvalidmasterservers)
		{
			serverscanner->status = SSS_ERROR;
			return 50000;
		}

		serverscanner->starttime = Sys_IntTime();

		ServerScanner_Thread_QueryMasters(serverscanner);

		serverscanner->initialstuffdone = 1;
	}

	for(i=1;i<NA_NUMTYPES;i++)
	{
		if (!serverscanner->sockets[i])
			continue;

		while((r = Sys_Net_Receive(serverscanner->netdata, serverscanner->sockets[i], buf, sizeof(buf), &addr)) > 0)
		{
			ServerScanner_Thread_HandlePacket(serverscanner, buf, r, &addr);
		}
	}

	Sys_Thread_LockMutex(serverscanner->mutex);

	ServerScanner_Thread_CheckTimeout(serverscanner);

	if (serverscanner->status == SSS_SCANNING)
	{
		if (serverscanner->qwserversscanwaiting == 0 && serverscanner->qwserversscaninprogress == 0 && Sys_IntTime() > serverscanner->starttime + 2000000)
		{
			serverscanner->status = SSS_PINGING;
		}
	}

	if (serverscanner->status == SSS_PINGING)
	{
		if (serverscanner->qwserverspingwaiting == 0 && serverscanner->qwserverspinginprogress == 0)
		{
			if (serverscanner->qwserversscanwaiting)
				serverscanner->status = SSS_SCANNING;
			else
				serverscanner->status = SSS_IDLE;
		}
	}

	curtime = Sys_IntTime();

#warning Needs to schedule servers for scanning
	while (serverscanner->status == SSS_SCANNING && serverscanner->numqwserversscaninprogress < MAXCONCURRENTSCANS && serverscanner->qwserversscanwaiting)
	{
		qwserver = serverscanner->qwserversscanwaiting;
		ServerScanner_Thread_SendQWRequest(serverscanner, qwserver);
		serverscanner->qwserversscanwaiting = qwserver->nextscanwaiting;
		qwserver->nextscanwaiting = 0;
	}
			
	while (serverscanner->status == SSS_PINGING && serverscanner->numqwserverspinginprogress < MAXCONCURRENTPINGS && serverscanner->qwserverspingwaiting && serverscanner->lastpingtime + PINGINTERVAL <= curtime)
	{
		qwserver = serverscanner->qwserverspingwaiting;
		ServerScanner_Thread_SendQWPingRequest(serverscanner, qwserver);
		serverscanner->qwserverspingwaiting = qwserver->nextpingwaiting;
		qwserver->nextpingwaiting = 0;
	}

	timeout = 50000;
	if (serverscanner->status == SSS_SCANNING)
	{
		qwserver = serverscanner->qwserversscaninprogress;
		while(qwserver)
		{
			if (qwserver->packetsendtime + QWSERVERTIMEOUT >= curtime && qwserver->packetsendtime + QWSERVERTIMEOUT - curtime < timeout)
			{
				timeout = qwserver->packetsendtime + QWSERVERTIMEOUT - curtime;
			}
		
			qwserver = qwserver->nextscaninprogress;
		}
	}
	else if (serverscanner->status == SSS_PINGING)
	{
		qwserver = serverscanner->qwserverspinginprogress;
		while(qwserver)
		{
			if (qwserver->packetsendtime + QWSERVERTIMEOUT >= curtime && qwserver->packetsendtime + QWSERVERTIMEOUT - curtime < timeout)
			{
				timeout = qwserver->packetsendtime + QWSERVERTIMEOUT - curtime;
			}
	
			qwserver = qwserver->nextpinginprogress;
		}

		if (serverscanner->qwserverspingwaiting && serverscanner->lastpingtime + PINGINTERVAL >= curtime)
		{
			if (serverscanner->lastpingtime + PINGINTERVAL - curtime < timeout)
			{
				timeout = serverscanner->lastpingtime + PINGINTERVAL - curtime;
			}
		}
	}

	Sys_Thread_UnlockMutex(serverscanner->mutex);

	return timeout;
}