Esempio n. 1
0
void Bot::sendChecking()
{
	string tmp=mybuddylist.head->buddyJID;
	JID buddyjid(tmp);
	Message checkmsg(Message::Chat,buddyjid,"C");
	client->send(checkmsg);
	BuddyNode* p=mybuddylist.head;
	while (p->next)
	{
		p=p->next;
		tmp=p->buddyJID;
		JID buddyjid(tmp);
		Message checkmsg(Message::Chat,buddyjid,"C");
		client->send(checkmsg);
	}
}
void update(void)
{	checkmsg();
	vd_update();
//	checkmsg();
//	if (audioworks) audio_update();
	framesdrawn++;
}
bool initnetwork(word port)
{	if (netinitialized)	
	{	msg("Networking Error","An attempt was made to initialize the networking module more than once");
	}
	int fromlen=sizeof(testfrom);

#ifdef PLATFORM_win32
	if (WSAStartup(0x202,&wsaData)) 
	{	reportNetError();
		WSACleanup();
		return false;
	}
#endif
	netinitialized = true;

	serverok = inittestserver(port);
	clientok = inittestclient(port);

	if (!clientok) serverok = false;

	if (clientok && serverok)
	{	// Receive initial test packet
		char buf[10];
		long retval = WSAEWOULDBLOCK;
		dword timeouttime = globalTimer + currentTimerHz * 2;
		while (retval==WSAEWOULDBLOCK && globalTimer<timeouttime)
		{	checkmsg();
			retval = recvfrom(testserversocket,buf,10,0,(struct sockaddr *)&testfrom,&fromlen);
			if (retval == SOCKET_ERROR || retval == 0 || txtcmp(buf,"test")!=0) 
			{	serverok = false;
				closesocket(testserversocket);
			}
		}
		if (globalTimer>=timeouttime)
		{	serverok = false;
			closesocket(testserversocket);
		}
	}

	if (serverok) closesocket(testserversocket);
	if (clientok) closesocket(testclientsocket);
	return true;
}
netclient::netclient(bool reliable,char *requeststring,char *host,word port,dword bandwidth,void (*_receiver)(netclient *client,byte packettype,byte *data,long size))
{	reliable = false;

	if (!clientok)
	{	connectionerror = net_tcpipna;
		return;
	}
	receiver = _receiver;
	netclientoem *oem = (netclientoem *)fcalloc(sizeof(netclientoem),"Network Client Connection");
	oemdata = oem;

	if (reliable) oem->socket_type = SOCK_STREAM;	// TCP
			else  oem->socket_type = SOCK_DGRAM;	// UDP

	// Attempt to detect if we should call gethostbyname() or gethostbyaddr()
	if (isalpha(host[0])) 
	{   // server address is a name
		oem->host = gethostbyname(host);
	}	else  
	{	// Convert nnn.nnn address to a usable one
		dword addr = inet_addr(host);
		oem->host = gethostbyaddr((char *)&addr,4,AF_INET);
	}
	if (oem->host == NULL ) 
	{	//con->add("Client: Cannot resolve address [%s]: Error %d\n",host,WSAGetLastError());
		connectionerror = net_cantresolveaddr;
		return;
	}

	oem->conn_socket = socket(AF_INET,oem->socket_type,0); // Open a socket
	if (oem->conn_socket <0 ) 
	{	//con->add("Client: Error Opening socket: Error %d\n",WSAGetLastError());
		connectionerror = net_opensocketerror;
		return;
	}

	u_long argp = 1;
	if (ioctlsocket(oem->conn_socket,FIONBIO,&argp))
	{	connectionerror = net_ioctlfailed;
		closesocket(oem->conn_socket);
		return;
	}

	//
	// Notice that nothing in this code is specific to whether we 
	// are using UDP or TCP.
	// We achieve this by using a simple trick.
	//    When connect() is called on a datagram socket, it does not 
	//    actually establish the connection as a stream (TCP) socket
	//    would. Instead, TCP/IP establishes the remote half of the
	//    ( LocalIPAddress, LocalPort, RemoteIP, RemotePort) mapping.
	//    This enables us to use send() and recv() on datagram sockets,
	//    instead of recvfrom() and sendto()

	//
	// Copy the resolved information into the sockaddr_in structure
	//
	sockaddr_in server;
	memfill(&server,0,sizeof(server));
	memcpy(&server.sin_addr,oem->host->h_addr,oem->host->h_length);
	server.sin_family = oem->host->h_addrtype;
	server.sin_port = htons(port);	// Port Number
//dword ip = *(dword *)oem->host->h_addr;
//con->add("server IP = %i.%i.%i.%i, Port %i",(ip)&0xff,(ip>>8)&0xff,(ip>>16)&0xff,(ip>>24)&0xff,port);

	long retval = WSAEWOULDBLOCK;
	while (retval==WSAEWOULDBLOCK)
	{	checkmsg();
		retval = connect(oem->conn_socket,(struct sockaddr*)&server,sizeof(server));
		if (retval == SOCKET_ERROR) 
		{	connectionerror = net_connectfailed;
			closesocket(oem->conn_socket);
			return;
		}
	}

	// Send initial request for a connection
	char *constring = buildstr("%s%8x",requeststring,bandwidth);
	retval = WSAEWOULDBLOCK;
	while (retval==WSAEWOULDBLOCK)
	{	checkmsg();
//con->add("Send '%s' on Socket %i",requeststring,oem->conn_socket);
//		retval = send(oem->conn_socket,requeststring,txtlen(requeststring)+1,0);
		retval = send(oem->conn_socket,constring,txtlen(constring)+1,0);
		if (retval == SOCKET_ERROR) 
		{	connectionerror = net_hostlogin;
			closesocket(oem->conn_socket);
			return;
		}
	}
		
	retval = WSAEWOULDBLOCK;
	dword timeouttime = globalTimer + currentTimerHz * 2;
	while ((globalTimer<timeouttime) && (retval == WSAEWOULDBLOCK))
	{	checkmsg();
		retval = recv(oem->conn_socket,(char *)&oem->Buffer[0],sizeof(oem->Buffer),0 );
		if (retval == SOCKET_ERROR) 
		{	retval = WSAEWOULDBLOCK;
			//connectionerror = net_hostresponse;
			//closesocket(oem->conn_socket);
			//return;
		}
		if (retval == 0) 
		{	retval = WSAEWOULDBLOCK;
		}
	}
	
	if (globalTimer>=timeouttime)
	{	connectionerror = net_timeout;
		closesocket(oem->conn_socket);
		return;
	}

	//con->add("Server says: %s\n",Buffer);
	long ofs = 0;
	char sep;
	char *token;
	token = gettoken((char *)oem->Buffer,&ofs,&sep);
	connectionerror = net_invalidresponse;
	if (txtcmp(token,"ServerFull")==0) connectionerror = net_serverfull;
	if (txtcmp(token,"ConnectionFailed")==0) connectionerror = net_servererror;
	if (txtcmp(token,"ConnectionGranted")==0) 
	{	port = (word)getuinttoken((char *)oem->Buffer,&ofs,&sep);
		oem->refnum = port;
		connectionerror = 0;
		return;
	}

//	con->add("%s",oem->Buffer);
//	con->add("Token = '%s', buffer = %s, length = %i",""/*token*/,oem->Buffer,retval);
//	con->add("Expected ConnectionGranted");
	closesocket(oem->conn_socket);	// ERROR ONLY! This line is not executed during normal execution
}