//-------------------------------------------------------------------------------------------------
	void TextureMemory::Initialize()
	{
		// Todo: texture palette can probably have more slots, and other palette memory types can also expand. For now just 1 for testing texture palettes
		// Todo: If VRAM_E is mapped to texture or extended palette, it occupies more than 1 slot. Make this possible
		// Todo: Implement palette support. Only direct colored textures for now
		//PaletteMemory.Initialize(this);
		//PaletteMemory.AddSlot(BankE | BankF | BankG);

		if (IsMain())
		{
			InitializeSlots(4);

			Slots[0]->AddSupportedMapping(BankA, VRAM_A_TEXTURE_SLOT0);
			Slots[0]->AddSupportedMapping(BankB, VRAM_B_TEXTURE_SLOT0);
			Slots[0]->AddSupportedMapping(BankC, VRAM_C_TEXTURE_SLOT0);
			Slots[0]->AddSupportedMapping(BankD, VRAM_D_TEXTURE_SLOT0);

			Slots[1]->AddSupportedMapping(BankA, VRAM_A_TEXTURE_SLOT1);
			Slots[1]->AddSupportedMapping(BankB, VRAM_B_TEXTURE_SLOT1);
			Slots[1]->AddSupportedMapping(BankC, VRAM_C_TEXTURE_SLOT1);
			Slots[1]->AddSupportedMapping(BankD, VRAM_D_TEXTURE_SLOT1);

			Slots[2]->AddSupportedMapping(BankA, VRAM_A_TEXTURE_SLOT2);
			Slots[2]->AddSupportedMapping(BankB, VRAM_B_TEXTURE_SLOT2);
			Slots[2]->AddSupportedMapping(BankC, VRAM_C_TEXTURE_SLOT2);
			Slots[2]->AddSupportedMapping(BankD, VRAM_D_TEXTURE_SLOT2);

			Slots[3]->AddSupportedMapping(BankA, VRAM_A_TEXTURE_SLOT3);
			Slots[3]->AddSupportedMapping(BankB, VRAM_B_TEXTURE_SLOT3);
			Slots[3]->AddSupportedMapping(BankC, VRAM_C_TEXTURE_SLOT3);
			Slots[3]->AddSupportedMapping(BankD, VRAM_D_TEXTURE_SLOT3);

			// Todo: hack, only works if BankA is mapped to TextureMemory
			nextAvailableAddress = VRAM_A;
		}
	}
Esempio n. 2
0
int main(int argc, char **argv)
{
	u_short server_port = 5500;
	u_short viewer_port = 5900;
  int max_slots = 50;
  char * dump_file = NULL;
	thread_t hServerThread;
	thread_t hViewerThread;

	/* Arguments */
	if (argc > 1) {
		for(int i = 1; i < argc; i++)
		{
			if( _stricmp( argv[i], "-server" ) == 0 ) {
				/* Requires argument */
				if( (i+i) == argc ) {
					usage( argv[0] );
					return 1;
				}
				server_port = atoi( argv[(i+1)] );
				if( argv[(i+1)][0] == '-' ) {
					usage( argv[0] );
					return 1;
				} else if( server_port == 0 ) {
					usage( argv[0] );
					return 1;
				} else if( server_port > 65535 ) {
					usage( argv[0] );
					return 1;
				}
				i++;
			} else if( _stricmp( argv[i], "-viewer" ) == 0 ) {
				/* Requires argument */
				if( (i+i) == argc ) {
					usage( argv[0] );
					return 1;
				}
				viewer_port = atoi( argv[(i+1)] );
				if( argv[(i+1)][0] == '-' ) {
					usage( argv[0] );
					return 1;
				} else if( viewer_port == 0 ) {
					usage( argv[0] );
					return 1;
				} else if( viewer_port > 65535 ) {
					usage( argv[0] );
					return 1;
				}
				i++;
			} else if ( _stricmp( argv[i], "-dump" ) == 0 ) {
        if( (i+i) == argc ) {
					usage( argv[0] );
					return 1;
				}
				dump_file = argv[(i+1)];
        i++; 
			} else if ( _stricmp( argv[i], "-loglevel" ) == 0 ) {
        if( (i+i) == argc ) {
					usage( argv[0] );
					return 1;
				}
				char level = ::get_log_level(argv[(i+1)]);
        if(level == -1) { usage( argv[0] ); return 1; }
        ::logger_level = level;
        i++; 
			} else if ( _stricmp( argv[i], "-iobuffer_size" ) == 0 ) {
        if( (i+i) == argc ) {
					usage( argv[0] );
					return 1;
				}
        int buffer_sz = atoi(argv[(i+1)]);
        if(buffer_sz <= 128) { usage( argv[0] ); return 1; }
        IOBUFFER_SIZE = buffer_sz;
        i++; 
			} else if ( _stricmp( argv[i], "-max_slots" ) == 0 ) {
        if( (i+i) == argc ) {
					usage( argv[0] );
					return 1;
				}
        int _max_slots = atoi(argv[(i+1)]);
        if(_max_slots < 1) { usage( argv[0] ); return 1; }
        max_slots = _max_slots;
        i++; 
      } 
      else {
				usage( argv[0] );
				return 1;
			}
		}
	}
	
#ifdef WIN32
	/* Winsock */
	if( WinsockInitialize() == 0 )
		return 1;
#endif

	/* Start */
	logp(ERROR, "VNC Repeater [Version %s]", VNCREPEATER_VERSION);
	log(INFO, "Copyright (C) 2010 Juan Pedro Gonzalez Gutierrez. Licensed under GPL v2.");
	log(INFO, "Copyright (C) 2013 XSoft Ltd. - Andrey Andreev - www.xsoftbg.com. Licensed under GPL v2.");
	log(INFO, "Get the latest version at http://code.google.com/p/vncrepeater/ or https://github.com/XSoftBG/repeater\n");

	/* Initialize some variables */
	notstopped = true;
	InitializeSlots(max_slots);

	/* Trap signal in order to exit cleanlly */
	signal(SIGINT, ExitRepeater);

  listener_thread_params * server_thread_params = (listener_thread_params *)malloc(sizeof(listener_thread_params));
	memset(server_thread_params, 0, sizeof(listener_thread_params));
	listener_thread_params * viewer_thread_params = (listener_thread_params *)malloc(sizeof(listener_thread_params));
	memset(viewer_thread_params, 0, sizeof(listener_thread_params));

	server_thread_params->port = server_port;
	viewer_thread_params->port = viewer_port;

	// Start multithreading...
	// Tying new threads ;)
	if (notstopped) {
		if ( thread_create(&hServerThread, NULL, server_listen, (LPVOID)server_thread_params) != 0 ) {
			log(FATAL, "Unable to create the thread to listen for servers.");
			notstopped = false;
		}
	}

	if (notstopped) {
		if ( thread_create(&hViewerThread, NULL, viewer_listen, (LPVOID)viewer_thread_params) != 0 ) {
			log(FATAL, "Unable to create the thread to listen for viewers.");
			notstopped = false;
		}
	}

  if (notstopped) {
    FILE *f = fopen( "pid.repeater", "w" );
    if(!f) perror("pid.repeater"), exit(1);
    fprintf(f, "%d\n", (int)getpid());
    fclose(f);
  } 
	// Main loop
	while (notstopped)
	{ 
		/* Clean slots: Free slots where the endpoint has disconnected */
		CleanupSlots();
    if (dump_file != NULL) {
      int dump_fd = ::open(dump_file, O_CREAT | O_TRUNC | O_WRONLY, 0644);
      std::string json = DumpSlots();
      if (!json.empty()) 
        write (dump_fd, json.c_str(), json.length() );	
      close (dump_fd);
    }
		/* Take a "nap" so CPU usage doesn't go up. */
#ifdef WIN32
		Sleep(5000000);
#else
		usleep(5000000);
#endif
	}

  log(ERROR, "\nExiting VNC Repeater...\n");
	notstopped = false;

	/* Free the repeater slots */
	FreeSlots();

	/* Close the sockets used for the listeners */
	socket_close( server_thread_params->sock );
	socket_close( viewer_thread_params->sock );
	
	/* Free allocated memory for the thread parameters */
	free( server_thread_params );
	free( viewer_thread_params );

	/* Make sure the threads have finalized */
	if (thread_cleanup(hServerThread, 30) != 0) log(ERROR, "The server listener thread doesn't seem to exit cleanlly.");
	if (thread_cleanup(hViewerThread, 30) != 0) log(ERROR, "The viewer listener thread doesn't seem to exit cleanlly.");

  FinalizeSlots(); 
#ifdef WIN32
  WinsockFinalize(); // Cleanup Winsock.
#endif
  return 0;
}