コード例 #1
0
ファイル: log_io.cpp プロジェクト: rauls/iReporter
long LogRead( void *refNum, char *filename, char *databuffer, long ReadCount )
{
	if ( refNum ){
#ifndef DEF_MAC
		if ( IsURL( filename )  )
			return NetRead( (void*)refNum, databuffer, ReadCount );
		else 
		if ( IsPKZIP( filename ) )
			return UnzipGetData( (void*)refNum, (unsigned char *)databuffer, (size_t)ReadCount );
		else 
#ifdef _BZLIB_H
		if ( IsBZIP( filename ) )	// please let the mac do this one day... for completeness sake and less #ifdefs
			return BZ2_bzread( (void*)refNum, databuffer, ReadCount );
		else
#endif
#endif
			return gzread( (gzFile)refNum, databuffer, ReadCount );
	}
	return 0;
}
コード例 #2
0
ファイル: minernet.c プロジェクト: ellierye/Bee2Miner
// TODO: add input buffer
int main(int argc, char *argv[]) { 
  int q;
  int ppc_fd = 0; 
  int  num_q = 4;
  miner_t miner[4];
  int start_port = 8087;

  unsigned int nonce;
  unsigned char bufferin[64];  // 512 bits
  unsigned char bufferout[4];  // 32 bits

  // Initialize HW interface and reset
  ppc_fd = InitQuadInterface();

  if (argc == 2){
    num_q = atoi(argv[1]);
    if (num_q <= 0 || num_q > 4){
      printf("Num Quads must be 1-4\n");
      print_usage(argv);
      exit(1);
    }
  }
  
  for (q = 0; q < num_q; q++){
    SetQuad(q, ppc_fd);
    ResetRW(ppc_fd);
  }

  // Initialize miners: numQ consecutive ports
  for (q = 0; q < num_q; ++q) {
    NetGetConnection(start_port + q,
		     &miner[q].connection);
    miner[q].state = STATE_IDLE;
  }

  while (1) {
    // feed / consume FPGA data
    // Note: several potential sources of lockups here.
    for (q = 0; q < num_q; ++q) {
      SetQuad(q, ppc_fd);
      if (miner[q].state == STATE_WORKING) {
	// Check for available data and read if available.
	if (ReadByteNB(&bufferout[0], ppc_fd)) {
	  ReadByte(&bufferout[1], ppc_fd);
	  ReadByte(&bufferout[2], ppc_fd);
	  ReadByte(&bufferout[3], ppc_fd);
	  // Send
	  NetWrite(&miner[q].connection, bufferout, 4);
	  miner[q].state = STATE_IDLE;
	  // Debug
	  nonce = BufferToNonce(bufferout);
	  printf("Miner(%d) TX nonce: %d  (0x%08x)\n", q, nonce, nonce); 
	}
      } 
      //else if (miner[q].state == STATE_READY) {
      // Just always do this.
      // RX net data and forward to FPGA
      if (NetRead(&miner[q].connection, bufferin, 64) == 64) {
	WriteBuffer(bufferin, 64, ppc_fd);
	miner[q].state = STATE_WORKING;
	printf("Miner(%d) RX data\n", q); 
      }
    } // quad iterator
    
    // TODO: Check sideband for commands.
    
  } // while (1)
  
  // Close connections
  for (q = 0; q < num_q; ++q) {
    NetClose(&miner[q].connection);
  }

  return 0;
} 
コード例 #3
0
ファイル: postproc.cpp プロジェクト: rauls/iReporter
long FtpFileDownload( void *fs, char *url, char *dest, char *fullpath, long length )
{
	long	dataread, datatotal=0;

	if ( fs )
	{
		void	*fp;
		FILE	*ff;
		char	*buffer=NULL;

		if ( fs && !IsStopped() )
		{
			if ( !(ff = fopen( dest, "wb+" )) )
			{
				StatusSetf( "Cannot open local file %s for writing.", dest );
				return -2;
			}
	
			fp = (void*)FtpOpen( fs, fullpath, 'R' );
			if ( fp ) 
			{
				if ( !length )
					length = FtpFileGetSize( url );

				StatusSetID( IDS_DOWNLOADING, fullpath );

				buffer = (char*)malloc( 1024*4 );

				if ( length && buffer )
				{
					long writesize;

					dataread = 1;
					while( dataread>0 && buffer && !IsStopped() )
					{
						//sprintf( msg, "Downloading %s...%d KB (%.2f%%)", fullpath, (datatotal)/1024, (100*((datatotal)/(float)length)) );
						StatusSetID( IDS_DOWNLOADINGKB, fullpath, (datatotal)/1024, (100*((datatotal)/(float)length))  );
	
						dataread = NetRead( fp, buffer, 1024*1 );
						datatotal+= dataread;
						if ( dataread ){
							writesize = fwrite( buffer, 1, dataread, ff );
							if ( writesize == 0 ){
								dataread = 0;
								datatotal = -2;
							}
						}
					}
					free( buffer );
				}
				FtpClose( fp );
			} else 
			{
				const char *msg = NetworkErr( NULL );
				if ( !msg ) msg = "Maybe path does not exist?";
				ErrorMsg( "Cannot open ftp file %s\nbecause ...\n%s", fullpath, msg ); 
				datatotal = -1;
			}
			fclose( ff );
		}

		if ( IsStopped() )
		{
			StatusSetID( IDS_DOWNLOADSTOP );
			remove( dest );
			datatotal = -1;
		}

	}
	return datatotal;
}
コード例 #4
0
ファイル: wiiload.c プロジェクト: fagensden/postloader-1
static bool WiiLoad (s32 connection)
	{
	char wiiloadVersion[2];
	unsigned char header[9];
	size_t buffsize;

	wiiload.status = WIILOAD_RECEIVING;
			
	printopt ("WiiLoad begin");
	
	NetRead(connection, header, 8, 250);

	if (memcmp(header, "HAXX", 4) != 0) // unsupported protocol
		{
		printopt ("unsupported protocol");
		return false;
		}
		
	LWP_SetThreadPriority (networkthread, PRIO_RCV);

	wiiloadVersion[0] = header[4];
	wiiloadVersion[1] = header[5];

	NetRead(connection, (u8 *) &buffsize, 4, 250);

	if (header[4] > 0 || header[5] > 4)
		{
		printopt ("compressed file !");
		NetRead(connection, (u8*) &uncfilesize, 4, 250); // Compressed protocol, read another 4 bytes
		}
	
	/*
	buff = (u8 *) malloc(buffsize);
	if (!buff)
		{
		printopt ("Not enough memory.");
		return false;
		}
	*/
	
	printopt ("Receiving file (%s)...", incommingIP);

	u32 done = 0;
	u32 blocksize = 4096;
	
	u8 *buff = malloc (blocksize);

	int retries = 10;
	int result;
	int isZip = 0;
	
	FILE *f;
	char path[300];
	sprintf (path, "%s/%s", tpath, WIILOAD_TMPFILE);
	f = fopen (path, "wb");

	do
		{
		if (blocksize > buffsize - done)
			blocksize = buffsize - done;

		result = net_read(connection, buff, blocksize);
		if (result <= 0)
			{
			--retries;
			usleep (100000); // lets wait 10 msec
			}
		else
			{
			if (f) fwrite (buff, 1, result, f);
			
			retries = 10;
			
			if (done == 0 && (buff[0] == 'P' && buff[1] == 'K' && buff[2] == 0x03 && buff[3] == 0x04))
				{
				Debug ("This is a ZIP file");
				isZip = 1;
				}
				
			done += result;
			}

		if (retries == 0)
			{
			free (buff);
			buff = 0;
			buffsize = 0;
			if (f) fclose(f);
			printopt ("Transfer failed.");
			return false;
			}

		//printopt ("%d of %d bytes (r %d)", done, buffsize, retries);
		printopt ("!%d%c", (done * 100) / buffsize, 37);
		//fflush(stdout);
		} 
	while (done < buffsize);
	
	if (f) fclose(f);
	
	if (done != buffsize)
		{
		wiiload.status = WIILOAD_IDLE;
		//free (buff);
		//buffsize = 0;
		//buff = 0;
		printopt("Filesize doesn't match.");
		return false;
		}

	// These are the arguments....
	char temp[1024];
	int ret = NetRead(connection, (u8 *) temp, 1023, 250);
	temp[ret] = 0;
	
	//Debug_hexdump (temp, ret);
	
	if (ret > 2 && temp[ret - 1] == '\0' && temp[ret - 2] == '\0') // Check if it is really an arg
		{
		wiiload.args = malloc (ret);
		if (wiiload.args)
			{
			memcpy (wiiload.args, temp, ret);

			// convert arguments in ; separated items
			int i;
			for (i = 0; i < ret; i++)
				{
				if (wiiload.args[i] == '\0' && wiiload.args[i+1] != '\0')
					wiiload.args[i] = ';';
				}

			wiiload.argl = ret;
			}
		}

	temp[ret] = 0;

	printopt("Filename %s (%d)", temp, ret);
	strcpy (wiiload.filename, temp);
	
	wiiload.status = WIILOAD_IDLE;
	UncompressData (wiiloadVersion, isZip);
	
	free (buff);
	
	LWP_SetThreadPriority (networkthread, PRIO_IDLE);
		
	return true;
	}