Example #1
0
static int GeckoRead(int connection, u8 *buf, u32 len, u32 tout) // timeout in msec
	{
	u32 read = 0;
	s32 ret = 0;
	u32 t;
	
	t = ticks_to_millisecs(gettime()) + tout;

	while (read < len)
		{
		ret = usb_recvbuffer_safe_ex(connection,  buf + read, len - read, 500);
		
		if (ret > 0)
			{
			t = ticks_to_millisecs(gettime()) + tout;
			read += ret;
			}
		else
			usleep (1000);
			
		if (ticks_to_millisecs(gettime()) > t)
			break;
		}
	
	return read;
	}
Example #2
0
static int NetRead(int connection, u8 *buf, u32 len, u32 tout) // timeout in msec
	{
	u32 read = 0;
	s32 ret = 0;
	u32 t;
	
	t = ticks_to_millisecs(gettime()) + tout;

	while (read < len)
		{
		ret = net_read(connection, buf + read, len - read);

		if (ret <= 0)
			usleep (10 * 1000);
		else
			{
			read += ret;
			}
			
		if (ticks_to_millisecs(gettime()) > t)
			break;
		}

	return read;
	}
Example #3
0
u32 get_msec(bool reset)
{
	static u32 startms = 0;
	
	if (reset) startms = ticks_to_millisecs(gettime());

	return ticks_to_millisecs(gettime()) - startms;
}
Example #4
0
/**
 * This function reads all the data from a connection into a buffer which it returns.
 * It will return an empty buffer if something doesn't go as planned
 *
 * @param s32 connection The connection identifier to suck the response out of
 * @return block A 'block' struct (see http.h) in which the buffer is located
 */
static struct block read_message(s32 connection, struct block buffer, bool (*f)(void *, int, int), void *ud)
{
	static char tmpHdr[512];
	bool hdr = false, fail = true;
	u32 fileSize = 0, step = 0, offset = 0;
	s64 t = gettime();

	//The offset variable always points to the first byte of memory that is free in the buffer
	while (true)
	{
 		if(ticks_to_millisecs(diff_ticks(t, gettime())) > TCP_TIMEOUT || buffer.size <= offset)
			break;

		//Fill the buffer with a new batch of bytes from the connection,
		//starting from where we left of in the buffer till the end of the buffer
		u32 len = buffer.size - offset;
		s32 bytes_read = net_read(connection, buffer.data + offset, len > HTTP_BUFFER_GROWTH ? HTTP_BUFFER_GROWTH : len);
		//Anything below 0 is an error in the connection
		if(bytes_read > 0)
		{
			t = gettime ();

			offset += bytes_read;
			// Not enough memory
			if(buffer.size <= offset) return emptyblock;
			if(!hdr && offset >= sizeof tmpHdr)
			{
				hdr = true;
				memcpy(tmpHdr, buffer.data, sizeof tmpHdr - 1);
				tmpHdr[sizeof tmpHdr - 1] = 0;
				const char *p = strstr(tmpHdr, "Content-Length:");
				if(p != 0)
				{
					p += sizeof "Content-Length:";
					fileSize = strtol(p, 0, 10);
				}
			}
			if(step * HTTP_BUFFER_GROWTH < offset)
			{
				++step;
				if(f != 0)
				{
					if((fileSize != 0 && !f(ud, fileSize, offset <= fileSize ? offset : fileSize)) ||
						(fileSize == 0 && !f(ud, buffer.size, offset)))
						return emptyblock;
				}
			}
			fail = false;
		}
		else
		{
			if(bytes_read < 0) fail = true;
			break; // Need to translate the error messages here instead of just breaking.
		}
	}
	if(fail) return emptyblock;
	//At the end of above loop offset should be precisely the amount of bytes that were read from the connection
	buffer.size = offset;
	return buffer;
}
Example #5
0
// rulez nyan scene
int main(int argc, char **argv)
{
	bool nyaning = true;

	// self explain
	nyan();
	MP3Player_PlayBuffer(nyannyannyan_mp3, nyannyannyan_mp3_size, NULL);

	VIDEO_SetBlack(FALSE);
	while(nyaning)
	{
		u64 cticks = ticks_to_millisecs(gettime());
		// Loop the sound if ended
		if(!MP3Player_IsPlaying()) MP3Player_PlayBuffer(nyannyannyan_mp3, nyannyannyan_mp3_size, NULL);
		//Check wiimote input
		WPAD_ScanPads();
		u32 pressed = WPAD_ButtonsDown(0);
		if (pressed & WPAD_BUTTON_HOME) nyaning = false;
		// blackscreen until 3,8s like the youtube video (not extreme precison ;))
		if(cticks < startTime+3900) continue;
		// bkg frame counter (tick each 100ms)
		if(cticks > bkgTimeCounter+100)
		{
			bkgTimeCounter = cticks;
			currentBkgStep = (currentBkgStep+1) % BKG_STEP;
		}
		// nyan frame counter (tick each 60ms)
		if(cticks > nyanTimeCounter+60)
		{
			nyanTimeCounter = cticks;
			currentNyanStep = (currentNyanStep+1) % NYAN_STEP;
		}
		// Set the 2d matrix
		guMtxIdentity(GXmodelView2D);
		GX_LoadPosMtxImm(GXmodelView2D, GX_PNMTX0);

		// nyan
		f32 move =  delta % BKG_SIZE;
		f32 x = -move;
		while(x < rmode->fbWidth)
		{
			drawBkgSprite(x, currentBkgStep);
			x += BKG_SIZE;
		}

		// nyan nyan
		drawNyan(-5, 240 - ((NYAN_HEIGHT*8)/2), currentNyanStep);

		// Copy & switch fb
		GX_DrawDone();
		GX_CopyDisp(xfb[wichFb], GX_TRUE);
		VIDEO_SetNextFramebuffer(xfb[wichFb]);
		VIDEO_Flush();
		VIDEO_WaitVSync();
		wichFb ^= 1;
		delta += 8;
	}

	return 0;
}
Example #6
0
static int tcp_readln(const s32 s, char *buf, const u16 max_length)
{
	s32 res = -1;
	s32 ret;
	u64 start_time = gettime();
	u16 c = 0;

	while (c < max_length)
	{
		if (ticks_to_millisecs(diff_ticks(start_time, gettime())) > HTTP_TIMEOUT)
			break;

		ret = net_read(s, &buf[c], 1);

		if (ret == -EAGAIN)
		{
			usleep(20 * 1000);
			continue;
		}

		if (ret <= 0)
			break;

		if (c > 0 && buf[c - 1] == '\r' && buf[c] == '\n')
		{
			res = 0;
			buf[c-1] = 0;
			break;
		}
		c++;
		start_time = gettime();
		usleep(100);
	}
	return res;
}
Example #7
0
u32 diff_msec(long long start,long long end)
{
	u64 diff;

	diff = diff_ticks(start,end);
	return ticks_to_millisecs(diff);
}
Example #8
0
//Private functions
void updateTime()
{
	f32 current_time = (f32)(ticks_to_millisecs(gettime()));
	if(last_frame) frameTime = current_time - last_frame;
	else frameTime = 0;
	last_frame = current_time;
	frameTime *= 0.001f;
	gameTime = frameTime * timeSpeed;
}
Example #9
0
/**
 * This function reads all the data from a connection into a buffer.
 *
 * @param s32 connection The connection identifier to suck the response out of
 * @return bool True if data downloaded succesfully.
 */
bool tcp_readData(const s32 connection, u8 **buffer, const u32 length)
{
	u8 *p;
	u32 left, block, received;
	s64 t;
	s32 res;

	p = *buffer;
	left = length;
	received = 0;

	t = gettime ();
	while (left)
	{
		if (ticks_to_millisecs (diff_ticks (t, gettime ())) > TCP_BLOCK_RECV_TIMEOUT)
			break;

		// Update the progress bar
		if(displayProgressWindow)
		{
			ShowProgress(received, length);
			if(ProgressCanceled())
			{
				ProgressStop();
				break;
			}
		}

		// Get next block size
		block = left;
		if (block > TCP_BLOCK_SIZE)
			block = TCP_BLOCK_SIZE;

		if(http_port == 443)
			res = ssl_read (connection, p, block);
		else
			res = net_read (connection, p, block);

		if ((res == 0) || (res == -EAGAIN))
		{
			usleep (20 * 1000);
			continue;
		}

		if (res < 0) break;

		received += res;
		left -= res;
		p += res;

		// update timing after each downloaded block
		t = gettime ();
	}

	return left == 0;
}
Example #10
0
s32 tcp_connect (char *host, const u16 port) {
	struct hostent *hp;
	struct sockaddr_in sa;
	s32 s, res;
	s64 t;

	hp = net_gethostbyname (host);
	if (!hp || !(hp->h_addrtype == PF_INET)) {
		printDebugMsg(NORMAL_DEBUG_MESSAGE,"net_gethostbyname failed: %d\n", errno);
		return errno;
	}

	s = tcp_socket ();
	if (s < 0)
		return s;

	memset (&sa, 0, sizeof (struct sockaddr_in));
	sa.sin_family= PF_INET;
	sa.sin_len = sizeof (struct sockaddr_in);
	sa.sin_port= htons (port);
	memcpy ((char *) &sa.sin_addr, hp->h_addr_list[0], hp->h_length);

	t = gettime ();
	while (true) {
		if (ticks_to_millisecs (diff_ticks (t, gettime ())) >
				TCP_CONNECT_TIMEOUT) {
			printDebugMsg(NORMAL_DEBUG_MESSAGE,"tcp_connect timeout\n");
			net_close (s);

			return -ETIMEDOUT;
		}

		res = net_connect (s, (struct sockaddr *) &sa,
							sizeof (struct sockaddr_in));

		if (res < 0) {
			if (res == -EISCONN)
				break;

			if (res == -EINPROGRESS || res == -EALREADY) {
				usleep (20 * 1000);

				continue;
			}

			printDebugMsg(NORMAL_DEBUG_MESSAGE,"net_connect failed: %d\n", res);
			net_close (s);

			return res;
		}

		break;
	}

	return s;
}
Example #11
0
void UpdateNintendont(void) {
	int selected = 0;
	u64 delay = ticks_to_millisecs(gettime()) + 500;
	while(true) {
		ClearScreen();
		PrintInfo();
		PrintFormat(DEFAULT_SIZE, BLACK, MENU_POS_X + 50, MENU_POS_Y + 20*5, "Download Nintendont");
		PrintFormat(DEFAULT_SIZE, BLACK, MENU_POS_X + 50, MENU_POS_Y + 20*6, "Download titles.txt");
		PrintFormat(DEFAULT_SIZE, BLACK, MENU_POS_X + 50, MENU_POS_Y + 20*7, "Download controllers.zip");
		PrintFormat(DEFAULT_SIZE, BLACK, MENU_POS_X + 50, MENU_POS_Y + 20*8, "Return to Settings");
		PrintFormat(DEFAULT_SIZE, BLACK, MENU_POS_X + 35, MENU_POS_Y + 20*(5+selected), ARROW_RIGHT);
		GRRLIB_Render();
		FPAD_Update();
		if (delay > ticks_to_millisecs(gettime())) continue;
		if (FPAD_Start(1)) {
			ClearScreen();
			PrintFormat(DEFAULT_SIZE, BLACK, 212, 232, "Returning to loader...");
			ExitToLoader(0);
		}
		if (FPAD_OK(1)) {
			if (selected <= DOWNLOAD_CONTROLLERS)
				Download(selected);
			else
				break;
		}
		if (FPAD_Down(1)) {
			delay = ticks_to_millisecs(gettime()) + 150;
			selected++;
			if (selected > 3) selected = 0;
		}
		if (FPAD_Up(1)) {
			delay = ticks_to_millisecs(gettime()) + 150;
			selected--;
			if (selected < 0) selected = 3;
		}
		if (FPAD_Cancel(1)) {
			break;
		}
	}
	ClearScreen();
	return;
}
Example #12
0
bool fsop_CopyFolder (char *source, char *target, fsopCallback vc)
	{
	fsop.breakop = 0;
	fsop.multy.startms = ticks_to_millisecs(gettime());
	fsop.multy.bytes = 0;
	fsop.multy.size = fsop_GetFolderBytes (source, vc);
	
	Debug ("fsop_CopyFolder");
	Debug ("fsop.multy.startms = %u", fsop.multy.startms);
	Debug ("fsop.multy.bytes = %llu", fsop.multy.bytes);
	Debug ("fsop.multy.size = %llu (%u Mb)", fsop.multy.size, (u32)((fsop.multy.size/1000)/1000));
	
	return doCopyFolder (source, target, vc);
	}
Example #13
0
bool tcp_read (const s32 s, u8 **buffer, const u32 length) {
	u8 *p;
	u32 step, left, block, received;
	s64 t;
	s32 res;

	step = 0;
	p = *buffer;
	left = length;
	received = 0;

	t = gettime ();
	while (left) {
		if (ticks_to_millisecs (diff_ticks (t, gettime ())) >
				TCP_BLOCK_RECV_TIMEOUT) {
			printDebugMsg(NORMAL_DEBUG_MESSAGE,"tcp_read timeout\n");

			break;
		}

		block = left;
		if (block > 2048)
			block = 2048;

		res = net_read (s, p, block);

		if ((res == 0) || (res == -EAGAIN)) {
			usleep (20 * 1000);

			continue;
		}

		if (res < 0) {
			printDebugMsg(NORMAL_DEBUG_MESSAGE,"net_read failed: %d\n", res);

			break;
		}

		received += res;
		left -= res;
		p += res;

		if ((received / TCP_BLOCK_SIZE) > step) {
			t = gettime ();
			step++;
		}
	}

	return left == 0;
}
Example #14
0
static u32 tcp_write(const s32 s, const u8 *buffer, const u32 length)
{
	const u8 *p;
	u32 left, block, sent, step=0;
	s64 t;
	s32 res;

	p = buffer;
	left = length;
	sent = 0;

	t = gettime();
	while (left)
	{
		if (ticks_to_millisecs(diff_ticks(t, gettime()))
				> TCP_BLOCK_SEND_TIMEOUT)
		{
			break;
		}

		block = left;
		if (block > TCP_SEND_SIZE)
			block = TCP_SEND_SIZE;

		res = net_write(s, p, block);

		if ((res == 0) || (res == -56))
		{
			usleep(20 * 1000);
			continue;
		}

		if (res < 0)
			break;

		sent += res;
		left -= res;
		p += res;
		usleep(100);

		if ((sent / TCP_BLOCK_SIZE) > step)
		{
			t = gettime ();
			step++;
		}
	}

	return left == 0;
}
Example #15
0
char * tcp_readln (const s32 s, const u16 max_length, const u64 start_time, const u32 timeout) {
	char *buf;
	u16 c;
	s32 res;
	char *ret;

	buf = (char *) malloc (max_length);

	c = 0;
	ret = NULL;
	while (true) {
		if (ticks_to_millisecs (diff_ticks (start_time, gettime ())) > timeout)
			break;

		res = net_read (s, &buf[c], 1);

		if ((res == 0) || (res == -EAGAIN)) {
			usleep (20 * 1000);

			continue;
		}

		if (res < 0) {
			printDebugMsg(NORMAL_DEBUG_MESSAGE,"tcp_readln failed: %d\n", res);

			break;
		}

		if ((c > 0) && (buf[c - 1] == '\r') && (buf[c] == '\n')) {
			if (c == 1) {
				ret = strdup ("");

				break;
			}

			ret = strndup (buf, c - 1);

			break;
		}

		c++;

		if (c == max_length)
			break;
	}

	free (buf);
	return ret;
}
Example #16
0
bool tcp_write (const s32 s, const u8 *buffer, const u32 length) {
	const u8 *p;
	u32 step, left, block, sent;
	s64 t;
	s32 res;

	step = 0;
	p = buffer;
	left = length;
	sent = 0;

	t = gettime ();
	while (left) {
		if (ticks_to_millisecs (diff_ticks (t, gettime ())) >
				TCP_BLOCK_SEND_TIMEOUT) {

			break;
		}

		block = left;
		if (block > 2048)
			block = 2048;

		if(http_port == 443)
			res = ssl_write (s, p, block);
		else
			res = net_write (s, p, block);

		if ((res == 0) || (res == -56)) {
			usleep (20 * 1000);
			continue;
		}

		if (res < 0) {
			break;
		}

		sent += res;
		left -= res;
		p += res;

		if ((sent / TCP_BLOCK_SIZE) > step) {
			t = gettime ();
			step++;
		}
	}

	return left == 0;
}
Example #17
0
bool tcp_write (const s32 s, const u8 *buffer, const u32 length) {
	const u8 *p;
	u32 step, left, block, sent;
	s64 t;
	s32 res;

	step = 0;
	p = buffer;
	left = length;
	sent = 0;

	t = gettime ();
	while (left) {
		if (ticks_to_millisecs (diff_ticks (t, gettime ())) >
				TCP_BLOCK_SEND_TIMEOUT) {

			printDebugMsg(NORMAL_DEBUG_MESSAGE,"tcp_write timeout\n");
			break;
		}

		block = left;
		if (block > 2048)
			block = 2048;

		res = net_write (s, p, block);

		if ((res == 0) || (res == -56)) {
			usleep (20 * 1000);
			continue;
		}

		if (res < 0) {
			printDebugMsg(NORMAL_DEBUG_MESSAGE,"net_write failed: %d\n", res);
			break;
		}

		sent += res;
		left -= res;
		p += res;

		if ((sent / TCP_BLOCK_SIZE) > step) {
			t = gettime ();
			step++;
		}
	}

	return left == 0;
}
Example #18
0
char * tcp_readln (const s32 s, const u16 max_length, const u64 start_time, const u16 timeout) {
	char *buf;
	u16 c;
	s32 res;
	char *ret;

	buf = (char *) memalign (32, max_length);

	c = 0;
	ret = NULL;
	while (true) {
		if (ticks_to_millisecs (diff_ticks (start_time, gettime ())) > timeout)
			break;

		if(http_port == 443)
			res = ssl_read (s, &buf[c], 1);
		else
			res = net_read (s, &buf[c], 1);

		if ((res == 0) || (res == -EAGAIN)) {
			usleep (20 * 1000);

			continue;
		}

		if (res < 0) break;

		if ((c > 0) && (buf[c - 1] == '\r') && (buf[c] == '\n')) {
			if (c == 1) {
				ret = strdup ("");

				break;
			}

			ret = strndup (buf, c - 1);

			break;
		}

		c++;

		if (c == max_length)
			break;
	}

	free (buf);
	return ret;
}
Example #19
0
static u32 tcp_read(const s32 s, u8 *buffer, const u32 length)
{
	char *p;
	u32 left, block, received, step=0;
	u64 t;
	s32 res;

	p = (char *)buffer;
	left = length;
	received = 0;

	t = gettime();
	while (left)
	{
		if (ticks_to_millisecs(diff_ticks(t, gettime()))
				> TCP_BLOCK_RECV_TIMEOUT)
		{
			break;
		}

		block = left;
		if (block > TCP_RECV_SIZE)
			block = TCP_RECV_SIZE;

		res = net_read(s, p, block);

		if (res == -EAGAIN)
		{
			usleep(20 * 1000);
			continue;
		}

		if(res<=0) break; 

		received += res;
		left -= res;
		p += res;
		usleep(1000);

		if ((received / TCP_BLOCK_SIZE) > step)
		{
			t = gettime ();
			step++;
		}
	}
	return received;
}
Example #20
0
int		Sys_Milliseconds (void) {
	static int		base;
	static qboolean	initialized = qfalse;
	u64 t;
	int ms;

	t = gettime();
	ms = ticks_to_millisecs(t);
	if (!initialized)
	{
		base = ms;
		initialized = qtrue;
	}
	sys_curtime = ms - base;

	return sys_curtime;
}
Example #21
0
void grlib_DrawIRCursor (void)
	{
	ir_t ir;
	static u8 alphadec = 255;
	static s16 alpha = 0; // Start with cursor hidden
	static u32 cursorActivity = 0;
	
	static u32 startms = 0;
	u32 ms = ticks_to_millisecs(gettime());
	
	WPAD_IR (0, &ir);
	
	if (ms > startms)
		{
		if (cursorActivity == grlibSettings.cursorActivity)
			{
			alpha -= alphadec;
			if (alpha < 0) alpha = 0;
			}
		else
			{
			alpha = 255;
			alphadec = 5;
			cursorActivity = grlibSettings.cursorActivity;
			}
		
		startms = ms+100;
		}
	
	if (ir.valid)
		{
		grlib_irPos.x = ir.x;
		grlib_irPos.y = ir.y;
		grlib_irPos.valid = 1;
		alpha = 255;
		}
	else
		{
		grlib_irPos.valid = 0;
		}

	GRRLIB_DrawImg( grlib_irPos.x, 
					grlib_irPos.y, 
					grlibSettings.pointer[0], 0, 1, 1, RGBA(255, 255, 255, alpha) ); 
	}
Example #22
0
bool tcp_read (const s32 s, u8 **buffer, const u32 length) {
	u8 *p;
	u32 step, left, block, received;
	s64 t;
	s32 res;

	step = 0;
	p = *buffer;
	left = length;
	received = 0;

	t = gettime ();
	while (left) {
		if (ticks_to_millisecs (diff_ticks (t, gettime ())) > TCP_BLOCK_RECV_TIMEOUT) break;

		block = left;
		if (block > 2048)
			block = 2048;

		if(http_port == 443)
			res = ssl_read (s, p, block);
		else
			res = net_read (s, p, block);

		if ((res == 0) || (res == -EAGAIN)) {
			usleep (20 * 1000);

			continue;
		}

		if (res < 0) break;

		received += res;
		left -= res;
		p += res;
		
		if ((received / TCP_BLOCK_SIZE) > step) {
			t = gettime ();
			step++;
		}
	}

	return left == 0;
}
Example #23
0
// Write our message to the server
static s32 send_message(s32 server, char *msg)
{
	s32 bytes_transferred = 0, remaining = strlen(msg);
	s64 t = gettime();
	while (remaining)
	{
		if((bytes_transferred = net_write(server, msg, remaining > NET_BUFFER_SIZE ? NET_BUFFER_SIZE : remaining)) > 0)
		{
			remaining -= bytes_transferred;
			usleep (20 * 1000);
			t = gettime();
		}
		else if(bytes_transferred < 0) return bytes_transferred;

		if(ticks_to_millisecs(diff_ticks(t, gettime())) > TCP_TIMEOUT)
			break;
	}
	return 0;
}
Example #24
0
uint32 OSystem_Wii::getMillis() {
	return ticks_to_millisecs(diff_ticks(_startup_time, gettime()));
}
Example #25
0
static int
connect2Server_with_af(char *host, int port, int af,int verb) {
	int socket_server_fd;
	int err;
        socklen_t err_len;
	int ret,count = 0;
	fd_set set;
	struct timeval tv;
	union {
		struct sockaddr_in four;
#ifdef HAVE_AF_INET6
		struct sockaddr_in6 six;
#endif
	} server_address;
	size_t server_address_size;
	void *our_s_addr;	// Pointer to sin_addr or sin6_addr
	struct hostent *hp=NULL;
	char buf[255];

#if HAVE_WINSOCK2_H
	unsigned long val;
	int to;
#else
	struct timeval to;
#endif

#if HAVE_WINSOCK2_H && defined(HAVE_AF_INET6)
	// our winsock name resolution code can not handle IPv6
	if (af == AF_INET6) {
		mp_msg(MSGT_NETWORK, MSGL_WARN, "IPv6 not supported for winsock2\n");
		return TCP_ERROR_FATAL;
	}
#endif

	socket_server_fd = socket(af, SOCK_STREAM, 0);


	if( socket_server_fd==-1 ) {
//		mp_msg(MSGT_NETWORK,MSGL_ERR,"Failed to create %s socket:\n", af2String(af));
		return TCP_ERROR_FATAL;
	}

#if defined(SO_RCVTIMEO) && defined(SO_SNDTIMEO)
#if HAVE_WINSOCK2_H
	/* timeout in milliseconds */
	to = 10 * 1000;
#else
	to.tv_sec = 10;
	to.tv_usec = 0;
#endif
	setsockopt(socket_server_fd, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to));
	setsockopt(socket_server_fd, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof(to));
#endif

	switch (af) {
		case AF_INET:  our_s_addr = (void *) &server_address.four.sin_addr; break;
#ifdef HAVE_AF_INET6
		case AF_INET6: our_s_addr = (void *) &server_address.six.sin6_addr; break;
#endif
		default:
			mp_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af);
			return TCP_ERROR_FATAL;
	}


	memset(&server_address, 0, sizeof(server_address));

#if HAVE_INET_PTON
	if (inet_pton(af, host, our_s_addr)!=1)
#elif HAVE_INET_ATON
	if (inet_aton(host, our_s_addr)!=1)
#elif HAVE_WINSOCK2_H
	if ( inet_addr(host)==INADDR_NONE )
#endif
	{
		if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,MSGTR_MPDEMUX_NW_ResolvingHostForAF, host, af2String(af));

#ifdef HAVE_GETHOSTBYNAME2
		hp=(struct hostent*)gethostbyname2( host, af );
#else
		hp=(struct hostent*)gethostbyname( host );
#endif
		if( hp==NULL ) {
			if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantResolv, af2String(af), host);
			return TCP_ERROR_FATAL;
		}

		memcpy( our_s_addr, (void*)hp->h_addr_list[0], hp->h_length );
	}
#if HAVE_WINSOCK2_H
	else {
		unsigned long addr = inet_addr(host);
		memcpy( our_s_addr, (void*)&addr, sizeof(addr) );
	}
#endif

	switch (af) {
		case AF_INET:
			server_address.four.sin_family=af;
			server_address.four.sin_port=htons(port);
			server_address_size = sizeof(server_address.four);
			break;
#ifdef HAVE_AF_INET6
		case AF_INET6:
			server_address.six.sin6_family=af;
			server_address.six.sin6_port=htons(port);
			server_address_size = sizeof(server_address.six);
			break;
#endif
		default:
			mp_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af);
			return TCP_ERROR_FATAL;
	}

#if HAVE_INET_PTON
	inet_ntop(af, our_s_addr, buf, 255);
#elif HAVE_INET_ATON || defined(HAVE_WINSOCK2_H)
	av_strlcpy( buf, inet_ntoa( *((struct in_addr*)our_s_addr) ), 255);
#endif
	if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,MSGTR_MPDEMUX_NW_ConnectingToServer, host, buf , port );

	// Turn the socket as non blocking so we can timeout on the connection
#if defined(GEKKO)
	// turn off Nagle
	u32 nodelay = 1;
	net_setsockopt(socket_server_fd,IPPROTO_TCP,TCP_NODELAY,&nodelay,sizeof(nodelay));

	// net_fcntl(socket_server_fd, F_SETFL, net_fcntl(socket_server_fd, F_GETFL, 0) | IOS_O_NONBLOCK);
	u64 t1,t2;
	t1=ticks_to_millisecs(gettime());
	do {
		ret = net_connect(socket_server_fd,(struct sockaddr*)&server_address,server_address_size);
		t2=ticks_to_millisecs(gettime());
		if(t2-t1 > 8000) break; // 8 secs to try to connect
		usleep(500);
	}while(ret != -EISCONN);
	if(ret != -EISCONN)
	{
		closesocket(socket_server_fd);
		return TCP_ERROR_PORT;
	}
	// net_fcntl(socket_server_fd, F_SETFL, net_fcntl(socket_server_fd, F_GETFL, 0) & ~IOS_O_NONBLOCK);
#elif !HAVE_WINSOCK2_H
	fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) | O_NONBLOCK );
#else
	val = 1;
	ioctlsocket( socket_server_fd, FIONBIO, &val );
#endif

#if !defined(GEKKO)
	if( connect( socket_server_fd, (struct sockaddr*)&server_address, server_address_size )==-1 ) {
#if !HAVE_WINSOCK2_H
		if( errno!=EINPROGRESS ) {
#else
		if( (WSAGetLastError() != WSAEINPROGRESS) && (WSAGetLastError() != WSAEWOULDBLOCK) ) {
#endif
			if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantConnect2Server, af2String(af));
			closesocket(socket_server_fd);
			return TCP_ERROR_PORT;
		}
	}
	tv.tv_sec = 0;
	tv.tv_usec = 500000;
	FD_ZERO( &set );
	FD_SET( socket_server_fd, &set );
	// When the connection will be made, we will have a writeable fd
	while((ret = select(socket_server_fd+1, NULL, &set, NULL, &tv)) == 0) {
	      if(count > 30 || stream_check_interrupt(500)) {
		if(count > 30)
		  mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnTimeout);
		else
		  mp_msg(MSGT_NETWORK,MSGL_V,"Connection interrupted by user\n");
		return TCP_ERROR_TIMEOUT;
	      }
	      count++;
	      FD_ZERO( &set );
	      FD_SET( socket_server_fd, &set );
	      tv.tv_sec = 0;
	      tv.tv_usec = 500000;
	}
	if (ret < 0) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_SelectFailed);

	// Turn back the socket as blocking
#if !HAVE_WINSOCK2_H
	// fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) & ~O_NONBLOCK );
#else
	val = 0;
	ioctlsocket( socket_server_fd, FIONBIO, &val );
#endif
	// Check if there were any errors
	err_len = sizeof(int);
	ret =  getsockopt(socket_server_fd,SOL_SOCKET,SO_ERROR,&err,&err_len);
	if(ret < 0) {
		mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_GetSockOptFailed,strerror(errno));
		return TCP_ERROR_FATAL;
	}
	if(err > 0) {
		mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnectError,strerror(err));
		return TCP_ERROR_PORT;
	}
#endif

	return socket_server_fd;
}

// Connect to a server using a TCP connection
// return -2 for fatal error, like unable to resolve name, connection timeout...
// return -1 is unable to connect to a particular port


int
connect2Server(char *host, int  port, int verb) {
#ifdef HAVE_AF_INET6
	int r;
	int s = TCP_ERROR_FATAL;

	r = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET:AF_INET6,verb);
	if (r >= 0) return r;

	s = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET6:AF_INET,verb);
	if (s == TCP_ERROR_FATAL) return r;
	return s;
#else
	return connect2Server_with_af(host, port, AF_INET,verb);
#endif


}
Example #26
0
static u32 tcp_read(const s32 s, u8 *buffer, const u32 length, int chunked)
{
	char *p;
	u32 left, block, received, step=0;
	u64 t;
	s32 res;

	p = (char *)buffer;
	left = length;
	received = 0;

	char line[64];
	int chunksize;

	t = gettime();

	while (left || chunked)
	{
		if(chunked == 1){
			tcp_readln(s, line, 64);
			sscanf(line,"%X",&chunksize);
			if(chunksize == 0) break;
			left = chunksize + 2; //Read the \r\n
			chunked = 2;
			t = gettime();
		}

		if (ticks_to_millisecs(diff_ticks(t, gettime()))
				> TCP_BLOCK_RECV_TIMEOUT)
		{
			break;
		}

		block = left;
		if (block > TCP_RECV_SIZE)
			block = TCP_RECV_SIZE;

		res = readFunc(s, p, block);

		if (res == -EAGAIN)
		{
			usleep(20 * 1000);
			continue;
		}

		if(res<=0) break; 

		received += res;
		left -= res;
		if(left == 0 && chunked == 2){ chunked = 1; p -= 2; received -= 2; } //Overwrite \r\n
		p += res;
		usleep(1000);

		if ((received / TCP_BLOCK_SIZE) > step)
		{
			t = gettime ();
			step++;
		}
	}
	return received;
}
Example #27
0
bool fsop_CopyFile (char *source, char *target, fsopCallback vc)
	{
	int err = 0;
	fsop.breakop = 0;
	
	u8 *buff = NULL;
	u32 size;
	u32 bytes, rb,wb;
	u32 block = 32768;
	FILE *fs = NULL, *ft = NULL;
	u32 vcskip, ms;
	
	
	Debug ("fsop_CopyFile (%s, %s): Started", source, target);
	
	if (strstr (source, "usb:") && strstr (target, "usb:"))
		{
		Debug ("fsop_CopyFile: buffer size changed to %dKbyte", block / 1024);
		block = 1024*1048;
		}
	
	fs = fopen(source, "rb");
	if (!fs)
		{
		Debug ("fsop_CopyFile: Unable to open source file");
		return false;
		}

	ft = fopen(target, "wt");
	if (!ft)
		{
		fclose (fs);
		Debug ("fsop_CopyFile: Unable to open target file");
		return false;
		}
	
	//Get file size
	fseek ( fs, 0, SEEK_END);
	size = ftell(fs);

	fsop.size = size;
	
	if (size == 0)
		{
		fclose (fs);
		fclose (ft);
		Debug ("fsop_CopyFile: Warning file size 0");
		return true;
		}
		
	// Return to beginning....
	fseek( fs, 0, SEEK_SET);
	
	//buff = mem2_malloc (block);
	buff = memalign( 32, block);  
	if (buff == NULL) 
		{
		fclose (fs);
		Debug ("fsop_CopyFile: ERR Unable to allocate buffers");
		return false;
		}
	
	bytes = 0;
	vcskip = 0;
	do
		{
		rb = fread(buff, 1, block, fs );
		wb = fwrite(buff, 1, rb, ft );
		
		if (wb != wb) err = 1;
		if (rb == 0) err = 1;
		bytes += rb;
		
		fsop.multy.bytes += rb;
		fsop.bytes = bytes;
		
		ms = ticks_to_millisecs(gettime());
		if (ms > vcskip && vc) 
			{
			fsop.multy.elapsed = ms - fsop.multy.startms;
			vc();
			vcskip = ticks_to_millisecs(gettime()) + 200;
			}
			
		if (fsop.breakop) break;
		}
	while (bytes < size && err == 0);

	fclose (fs);
	fclose (ft);
	
	free (buff);
	//mem2_free (buff);
	
	Debug ("fsop_CopyFile: bytes %u, size %u, err %d, breakop %d", bytes, size, err, fsop.breakop);
	
	if (err) unlink (target);

	if (fsop.breakop || err) return false;
	
	return true;
	}
Example #28
0
void REV_process(tMapQ3 * map)
{
	//Variables
	f32 preWait = 0, postWait = 0;
	static u8 firstFrame = 1;
	TRACKER * auxT;
	setUp3D();
	//Wait just before drawing (instead of after), this should enhance performance
	VIDEO_Flush();
	preWait = (f32)(ticks_to_millisecs(gettime()));
 	VIDEO_WaitVSync();
	postWait = (f32)(ticks_to_millisecs(gettime()));
	GPUWaitTime = 0.001f * (postWait - preWait);
	//Update physics
	updatePhysics();
	setBGColor(SC_BLUE);
	//Clasify objects into solid or transparent queues
	//This is done before everything else because this clasification is the same for every viewport
	clasify3D(mainRoot->rootNode);
	//Now we use the clasified queues to render shadows
	//if(mainRoot->shadowCaster)
		//mainRoot->shadowScene();
	//Render each Viewport into it's texture
	GX_SetBlendMode(GX_BM_BLEND, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_CLEAR);
	//GX_SetZMode (GX_TRUE, GX_LEQUAL, GX_TRUE);
	std::multimap<CAMERA*, TRender2Texture*>::iterator iter = mainRoot->m_Render2Textures.begin();
	for(;iter != mainRoot->m_Render2Textures.end(); ++iter)
	{
		(*iter).second->setForRender(perspective);
		(*iter).second->getCamera()->setForRender(view);
		//Before rendering the scene, render the skyBox
		GX_SetZMode (GX_FALSE, GX_LEQUAL, GX_TRUE);
		mainRoot->skyBox.render((*iter).second->getCamera());
		GX_SetZMode (GX_TRUE, GX_LEQUAL, GX_TRUE);
		//Now render the map
		//GX_LoadPosMtxImm(view, GX_PNMTX0);
		//if(map)
			//renderQ3Map(tTex->cam->getPos(), map);
		//Now render objects
		GX_SetCullMode(GX_CULL_NONE);
		auxT = solidQueue;
		while(auxT)
		{
			render(auxT->target, (*iter).second->getCamera()->getPos());
			auxT = auxT->next;
		}
		orderQueue((*iter).second->getCamera());
		auxT = transQueue;
		while(auxT)
		{
			render(auxT->target, (*iter).second->getCamera()->getPos());
			auxT = auxT->next;
		}
		//Copy the embeded frame buffer to the texture
		(*iter).second->copyTexture();
	}
	while(solidQueue)
	{
		auxT = solidQueue;
		solidQueue = solidQueue->next;
		free(auxT);
	}
	while(transQueue)
	{
		auxT = transQueue;
		transQueue = transQueue->next;
		free(auxT);
	}
	setBGColor(SC_WHITE);
	//2D System
	//GX_SetZMode (GX_FALSE, GX_LEQUAL, GX_TRUE);
	GX_SetCopyFilter(rMode->aa,rMode->sample_pattern,GX_TRUE,rMode->vfilter);
	GX_SetViewport(0,0, w, h,0,1);
	GX_SetScissor(0,0, w, h);
	guOrtho(perspective,0,h,0,w, 1.0,100.0);
	GX_LoadProjectionMtx(perspective, GX_ORTHOGRAPHIC);
	GX_SetCullMode(GX_CULL_NONE);
	setUp2D();
	parse2D(mainRoot->rootN2D);
	order2D();
	render2D();
	GX_DrawDone();
	
	GX_CopyDisp(frameBuffer[fb],GX_TRUE);
	VIDEO_SetNextFramebuffer(frameBuffer[fb]);
	
	//Set out black screen after first frame
	if(firstFrame)
	{
		firstFrame = 0;
		VIDEO_SetBlack(FALSE);
	}
	
	fb ^= 1;
}
Example #29
0
int grlib_GetUserInput (void)
	{
	u32  wbtn, gcbtn, cbtn;
	s8  gcX, gcY;
	int nX, nY;
	int cX, cY;
	
	static float g[MAXG];
	static int gidx = -1;
	static u32 gestureDisable = 0;
	
	u32 ms = ticks_to_millisecs(gettime());
	u32 mstout = ms+200;
		
	struct expansion_t e; //nunchuk
	
	if (gidx == -1)
		{
		memset (&g, 0, sizeof(g));
		gidx = 0;
		}
	
	WPAD_ScanPads();  // Scan the Wiimotes
	wbtn = WPAD_ButtonsDown(0);

	if (grlibSettings.usesGestures && ms > gestureDisable)
		{
		WPADData *wp = WPAD_Data (0);
		g[gidx] = wp->gforce.x;
		/*
		int gidx2 = gidx - 10;
		int gidx3 = gidx - 20;
		if (gidx2 < 0) gidx2 = (MAXG-1) - gidx2;
		if (gidx3 < 0) gidx3 = (MAXG-1) - gidx3;
		
		if (g[gidx2] < -2.0 && g[gidx] > -1.5 && g[gidx3] > -1.5)
			{
			memset (&g, 0, sizeof(g));
			return WPAD_BUTTON_MINUS;
			//Debug ("left");
			}
		if (g[gidx2] > 2.0 && g[gidx] < 1.5 && g[gidx3] < 1.5)
			{
			memset (&g, 0, sizeof(g));
			return WPAD_BUTTON_PLUS;
			//Debug ("right");
			}
		*/
		int i;
		float mean = 0;
		for (i = 0; i < MAXG; i++)
			mean+=g[i];
		mean /= (float) MAXG;
		
		// gprintf ("%.2f\n", mean);

		if (mean < -1.5)
			{
			memset (&g, 0, sizeof(g));
			gestureDisable = ms+1000;
			return WPAD_BUTTON_MINUS;
			}
		if (mean > 1.5)
			{
			memset (&g, 0, sizeof(g));
			gestureDisable = ms+1000;
			return WPAD_BUTTON_PLUS;
			}
		
		if (++gidx >= MAXG) gidx = 0;
		}
	
	//Debug ("wm = %.1f,%.1f,%.1f", wp->gforce.x,wp->gforce.y,wp->gforce.z);
	//Debug ("wm = %.1f,%.1f,%.1f", wp->orient.roll,wp->orient.pitch,wp->orient.yaw);
	
	
	WPAD_Expansion( 0, &e );
	
	if (e.type != WPAD_EXP_NUNCHUK)
		{
		nX = 0;
		nY = 0;
		}
	else
		{
		nX = e.nunchuk.js.pos.x - e.nunchuk.js.center.x;
		nY = e.nunchuk.js.pos.y - e.nunchuk.js.center.y;
		}

	if (e.type != WPAD_EXP_CLASSIC)
		{
		cX = 0;
		cY = 0;
		cbtn = 0;
		}
	else
		{
		cX = e.classic.ljs.pos.x - e.classic.ljs.center.x;
		cY = e.classic.ljs.pos.y - e.classic.ljs.center.y;
		cbtn = e.classic.btns;
		}
	
	PAD_ScanPads();
	gcX = PAD_StickX(0);
	gcY = PAD_StickY(0);
	gcbtn = PAD_ButtonsDown(0);
	
	// sticks
	if (abs (nX) > 10) {grlib_irPos.x += (nX / 16); grlibSettings.cursorActivity++;}
	if (abs (nY) > 10) {grlib_irPos.y -= (nY / 16); grlibSettings.cursorActivity++;}
	
	if (abs (gcX) > 10) {grlib_irPos.x += (gcX / 16); grlibSettings.cursorActivity++;}
	if (abs (gcY) > 10) {grlib_irPos.y -= (gcY / 16); grlibSettings.cursorActivity++;}
	
	if (abs (cX) > 10) {grlib_irPos.x += (cX / 4); grlibSettings.cursorActivity++;}
	if (abs (cY) > 10) {grlib_irPos.y -= (cY / 4); grlibSettings.cursorActivity++;}
	
	// Check limits
	if (grlib_irPos.x < 0) grlib_irPos.x = 0;
	if (grlib_irPos.x > 640) grlib_irPos.x = 640;
	
	if (grlib_irPos.y < 0) grlib_irPos.y = 0;
	if (grlib_irPos.y > 480) grlib_irPos.y = 480;
	
	// As usual wiimotes will have priority
	if (wbtn)
		{
		grlibSettings.buttonActivity ++;
		// Wait until button is released
		
		while (WPAD_ButtonsDown(0) && ticks_to_millisecs(gettime()) < mstout) WPAD_ScanPads();
		return wbtn;
		}

	// Then gc
	if (gcbtn)
		{
		grlibSettings.buttonActivity ++;
		
		// Wait until button is released
		
		while (PAD_ButtonsDown(0) && ticks_to_millisecs(gettime()) < mstout) PAD_ScanPads();
		
		// Convert to wiimote values
		if (gcbtn & PAD_TRIGGER_R) return WPAD_BUTTON_PLUS;
		if (gcbtn & PAD_TRIGGER_L) return WPAD_BUTTON_MINUS;

		if (gcbtn & PAD_BUTTON_A) return WPAD_BUTTON_A;
		if (gcbtn & PAD_BUTTON_B) return WPAD_BUTTON_B;
		if (gcbtn & PAD_BUTTON_X) return WPAD_BUTTON_1;
		if (gcbtn & PAD_BUTTON_Y) return WPAD_BUTTON_2;
		if (gcbtn & PAD_BUTTON_MENU) return WPAD_BUTTON_HOME;
		if (gcbtn & PAD_BUTTON_UP) return WPAD_BUTTON_UP;
		if (gcbtn & PAD_BUTTON_LEFT) return WPAD_BUTTON_LEFT;
		if (gcbtn & PAD_BUTTON_DOWN) return WPAD_BUTTON_DOWN;
		if (gcbtn & PAD_BUTTON_RIGHT) return WPAD_BUTTON_RIGHT;
		}
		
	// Classic
	if (cbtn)
		{
		grlibSettings.buttonActivity ++;
		
		while (e.classic.btns && ticks_to_millisecs(gettime()) < mstout)
			{
			WPAD_ScanPads();  // Scan the Wiimotes
			WPAD_Expansion( 0, &e );
			}
		
		// Convert to wiimote values
		if (cbtn & CLASSIC_CTRL_BUTTON_ZR) return WPAD_BUTTON_PLUS;
		if (cbtn & CLASSIC_CTRL_BUTTON_ZL) return WPAD_BUTTON_MINUS;
		
		if (cbtn & CLASSIC_CTRL_BUTTON_PLUS) return WPAD_BUTTON_PLUS;
		if (cbtn & CLASSIC_CTRL_BUTTON_MINUS) return WPAD_BUTTON_MINUS;

		if (cbtn & CLASSIC_CTRL_BUTTON_A) return WPAD_BUTTON_A;
		if (cbtn & CLASSIC_CTRL_BUTTON_B) return WPAD_BUTTON_B;
		if (cbtn & CLASSIC_CTRL_BUTTON_X) return WPAD_BUTTON_1;
		if (cbtn & CLASSIC_CTRL_BUTTON_Y) return WPAD_BUTTON_2;
		if (cbtn & CLASSIC_CTRL_BUTTON_HOME) return WPAD_BUTTON_HOME;
		}
	
	
	return 0;
	}
Example #30
0
/*
	Returns the time in miliseconds
*/
u32 GetTime(){
	return ticks_to_millisecs(gettime());
}