Пример #1
0
int main(int argc, const char * argv[]) {
    httpserver_t server;
    
    httpserver_init(&server, uv_default_loop());
    
    if (httpserver_bindipv4(&server, "0.0.0.0", 8080) != 0) {
        fprintf(stderr, "Error: in httpserver_bindipv4(): %s\n",
                httpserver_lasterror(&server));
        exit(1);
    }
    
    char wwwroot[1024];
    _getcwd(wwwroot, sizeof wwwroot);
    strcat(wwwroot, "/public");
    printf("Set wwwroot to %s\n", wwwroot);
    if (httpserver_listen(&server, wwwroot) != 0) {
        fprintf(stderr, "Error: in httpserver_listen(): %s\n",
                httpserver_lasterror(&server));
        exit(1);
    }
    
    puts("Server listening at 0.0.0.0:8080");
    
    httpserver_loop(&server);
    
    httpserver_shutdown(&server);
    
    return 0;
}
Пример #2
0
bool runloop_ctl(enum runloop_ctl_state state, void *data)
{

   switch (state)
   {
      case RUNLOOP_CTL_SHADER_DIR_DEINIT:
         shader_dir_free(&runloop_shader_dir);
         break;
      case RUNLOOP_CTL_SHADER_DIR_INIT:
         return shader_dir_init(&runloop_shader_dir);
      case RUNLOOP_CTL_SYSTEM_INFO_INIT:
         core_get_system_info(&runloop_system.info);

         if (!runloop_system.info.library_name)
            runloop_system.info.library_name = msg_hash_to_str(MSG_UNKNOWN);
         if (!runloop_system.info.library_version)
            runloop_system.info.library_version = "v0";

         video_driver_set_title_buf();

         strlcpy(runloop_system.valid_extensions,
               runloop_system.info.valid_extensions ?
               runloop_system.info.valid_extensions : DEFAULT_EXT,
               sizeof(runloop_system.valid_extensions));
         break;
      case RUNLOOP_CTL_GET_CORE_OPTION_SIZE:
         {
            unsigned *idx = (unsigned*)data;
            if (!idx)
               return false;
            *idx = core_option_manager_size(runloop_core_options);
         }
         break;
      case RUNLOOP_CTL_HAS_CORE_OPTIONS:
         return runloop_core_options;
      case RUNLOOP_CTL_CORE_OPTIONS_LIST_GET:
         {
            core_option_manager_t **coreopts = (core_option_manager_t**)data;
            if (!coreopts)
               return false;
            *coreopts = runloop_core_options;
         }
         break;
      case RUNLOOP_CTL_SYSTEM_INFO_GET:
         {
            rarch_system_info_t **system = (rarch_system_info_t**)data;
            if (!system)
               return false;
            *system = &runloop_system;
         }
         break;
      case RUNLOOP_CTL_SYSTEM_INFO_FREE:

         /* No longer valid. */
         if (runloop_system.subsystem.data)
            free(runloop_system.subsystem.data);
         runloop_system.subsystem.data = NULL;
         runloop_system.subsystem.size = 0;
         
         if (runloop_system.ports.data)
            free(runloop_system.ports.data);
         runloop_system.ports.data = NULL;
         runloop_system.ports.size = 0;
         
         if (runloop_system.mmaps.descriptors)
            free((void *)runloop_system.mmaps.descriptors);
         runloop_system.mmaps.descriptors     = NULL;
         runloop_system.mmaps.num_descriptors = 0;
         
         runloop_key_event          = NULL;
         runloop_frontend_key_event = NULL;

         audio_driver_unset_callback();
         memset(&runloop_system, 0, sizeof(rarch_system_info_t));
         break;
      case RUNLOOP_CTL_SET_FRAME_TIME_LAST:
         runloop_frame_time_last_enable = true;
         break;
      case RUNLOOP_CTL_UNSET_FRAME_TIME_LAST:
         if (!runloop_ctl(RUNLOOP_CTL_IS_FRAME_TIME_LAST, NULL))
            return false;
         runloop_frame_time_last        = 0;
         runloop_frame_time_last_enable = false;
         break;
      case RUNLOOP_CTL_SET_OVERRIDES_ACTIVE:
         runloop_overrides_active = true;
         break;
      case RUNLOOP_CTL_UNSET_OVERRIDES_ACTIVE:
         runloop_overrides_active = false; 
         break;
      case RUNLOOP_CTL_IS_OVERRIDES_ACTIVE:
         return runloop_overrides_active;
      case RUNLOOP_CTL_SET_GAME_OPTIONS_ACTIVE:
         runloop_game_options_active = true;
         break;
      case RUNLOOP_CTL_UNSET_GAME_OPTIONS_ACTIVE:
         runloop_game_options_active = false;
         break;
      case RUNLOOP_CTL_IS_GAME_OPTIONS_ACTIVE:
         return runloop_game_options_active;
      case RUNLOOP_CTL_IS_FRAME_TIME_LAST:
         return runloop_frame_time_last_enable;
      case RUNLOOP_CTL_SET_FRAME_LIMIT:
         runloop_set_frame_limit = true;
         break;
      case RUNLOOP_CTL_UNSET_FRAME_LIMIT:
         runloop_set_frame_limit = false;
         break;
      case RUNLOOP_CTL_SHOULD_SET_FRAME_LIMIT:
         return runloop_set_frame_limit;
      case RUNLOOP_CTL_GET_PERFCNT:
         {
            bool **perfcnt = (bool**)data;
            if (!perfcnt)
               return false;
            *perfcnt = &runloop_perfcnt_enable;
         }
         break;
      case RUNLOOP_CTL_SET_PERFCNT_ENABLE:
         runloop_perfcnt_enable = true;
         break;
      case RUNLOOP_CTL_UNSET_PERFCNT_ENABLE:
         runloop_perfcnt_enable = false;
         break;
      case RUNLOOP_CTL_IS_PERFCNT_ENABLE:
         return runloop_perfcnt_enable;
      case RUNLOOP_CTL_SET_NONBLOCK_FORCED:
         runloop_force_nonblock = true;
         break;
      case RUNLOOP_CTL_UNSET_NONBLOCK_FORCED:
         runloop_force_nonblock = false;
         break;
      case RUNLOOP_CTL_IS_NONBLOCK_FORCED:
         return runloop_force_nonblock;
      case RUNLOOP_CTL_SET_FRAME_TIME:
         {
            const struct retro_frame_time_callback *info =
               (const struct retro_frame_time_callback*)data;
#ifdef HAVE_NETPLAY
            global_t *global = global_get_ptr();

            /* retro_run() will be called in very strange and
             * mysterious ways, have to disable it. */
            if (global->netplay.enable)
               return false;
#endif
            runloop_frame_time = *info;
         }
         break;
      case RUNLOOP_CTL_GET_WINDOWED_SCALE:
         {
            unsigned **scale = (unsigned**)data;
            if (!scale)
               return false;
            *scale       = (unsigned*)&runloop_pending_windowed_scale;
         }
         break;
      case RUNLOOP_CTL_SET_WINDOWED_SCALE:
         {
            unsigned *idx = (unsigned*)data;
            if (!idx)
               return false;
            runloop_pending_windowed_scale = *idx;
         }
         break;
      case RUNLOOP_CTL_SET_LIBRETRO_PATH:
         {
            const char *fullpath = (const char*)data;
            if (!fullpath)
               return false;
            config_set_active_core_path(fullpath);
         }
         break;
      case RUNLOOP_CTL_CLEAR_CONTENT_PATH:
         *runloop_fullpath = '\0';
         break;
      case RUNLOOP_CTL_GET_CONTENT_PATH:
         {
            char **fullpath = (char**)data;
            if (!fullpath)
               return false;
            *fullpath       = (char*)runloop_fullpath;
         }
         break;
      case RUNLOOP_CTL_SET_CONTENT_PATH:
         {
            const char *fullpath = (const char*)data;
            if (!fullpath)
               return false;
            strlcpy(runloop_fullpath, fullpath, sizeof(runloop_fullpath));
         }
         break;
      case RUNLOOP_CTL_CLEAR_DEFAULT_SHADER_PRESET:
         *runloop_default_shader_preset = '\0';
         break;
      case RUNLOOP_CTL_GET_DEFAULT_SHADER_PRESET:
         {
            char **preset = (char**)data;
            if (!preset)
               return false;
            *preset       = (char*)runloop_default_shader_preset;
         }
         break;
      case RUNLOOP_CTL_SET_DEFAULT_SHADER_PRESET:
         {
            const char *preset = (const char*)data;
            if (!preset)
               return false;
            strlcpy(runloop_default_shader_preset, preset,
               sizeof(runloop_default_shader_preset));
         }
         break;
      case RUNLOOP_CTL_FRAME_TIME_FREE:
         memset(&runloop_frame_time, 0, sizeof(struct retro_frame_time_callback));
         runloop_frame_time_last           = 0;
         runloop_max_frames                = 0;
         break;
      case RUNLOOP_CTL_STATE_FREE:
         runloop_perfcnt_enable            = false;
         runloop_idle                      = false;
         runloop_paused                    = false;
         runloop_slowmotion                = false;
         runloop_frame_time_last_enable    = false;
         runloop_set_frame_limit           = false;
         runloop_overrides_active          = false;
         runloop_ctl(RUNLOOP_CTL_FRAME_TIME_FREE, NULL);
         break;
      case RUNLOOP_CTL_GLOBAL_FREE:
         {
            global_t *global = NULL;
            command_event(CMD_EVENT_TEMPORARY_CONTENT_DEINIT, NULL);
            command_event(CMD_EVENT_SUBSYSTEM_FULLPATHS_DEINIT, NULL);
            command_event(CMD_EVENT_RECORD_DEINIT, NULL);
            command_event(CMD_EVENT_LOG_FILE_DEINIT, NULL);

            rarch_ctl(RARCH_CTL_UNSET_BLOCK_CONFIG_READ, NULL);
            runloop_ctl(RUNLOOP_CTL_CLEAR_CONTENT_PATH,  NULL);
            runloop_overrides_active   = false;

            core_unset_input_descriptors();

            global = global_get_ptr();
            memset(global, 0, sizeof(struct global));
            config_free_state();
         }
         break;
      case RUNLOOP_CTL_CLEAR_STATE:
         driver_ctl(RARCH_DRIVER_CTL_DEINIT,  NULL);
         runloop_ctl(RUNLOOP_CTL_STATE_FREE,  NULL);
         runloop_ctl(RUNLOOP_CTL_GLOBAL_FREE, NULL);
         break;
      case RUNLOOP_CTL_SET_MAX_FRAMES:
         {
            unsigned *ptr = (unsigned*)data;
            if (!ptr)
               return false;
            runloop_max_frames = *ptr;
         }
         break;
      case RUNLOOP_CTL_IS_IDLE:
         return runloop_idle;
      case RUNLOOP_CTL_SET_IDLE:
         {
            bool *ptr = (bool*)data;
            if (!ptr)
               return false;
            runloop_idle = *ptr;
         }
         break;
      case RUNLOOP_CTL_IS_SLOWMOTION:
         return runloop_slowmotion;
      case RUNLOOP_CTL_SET_SLOWMOTION:
         {
            bool *ptr = (bool*)data;
            if (!ptr)
               return false;
            runloop_slowmotion = *ptr;
         }
         break;
      case RUNLOOP_CTL_SET_PAUSED:
         {
            bool *ptr = (bool*)data;
            if (!ptr)
               return false;
            runloop_paused = *ptr;
         }
         break;
      case RUNLOOP_CTL_IS_PAUSED:
         return runloop_paused;
      case RUNLOOP_CTL_MSG_QUEUE_PULL:
         runloop_msg_queue_lock();
         {
            const char **ret = (const char**)data;
            if (!ret)
               return false;
            *ret = msg_queue_pull(runloop_msg_queue);
         }
         runloop_msg_queue_unlock();
         break;
      case RUNLOOP_CTL_MSG_QUEUE_FREE:
#ifdef HAVE_THREADS
         slock_free(_runloop_msg_queue_lock);
         _runloop_msg_queue_lock = NULL;
#endif
         break;
      case RUNLOOP_CTL_MSG_QUEUE_CLEAR:
         msg_queue_clear(runloop_msg_queue);
         break;
      case RUNLOOP_CTL_MSG_QUEUE_DEINIT:
         if (!runloop_msg_queue)
            return true;

         runloop_msg_queue_lock();

         msg_queue_free(runloop_msg_queue);

         runloop_msg_queue_unlock();
         runloop_ctl(RUNLOOP_CTL_MSG_QUEUE_FREE, NULL);

         runloop_msg_queue = NULL;
         break;
      case RUNLOOP_CTL_MSG_QUEUE_INIT:
         runloop_ctl(RUNLOOP_CTL_MSG_QUEUE_DEINIT, NULL);
         runloop_msg_queue = msg_queue_new(8);
         retro_assert(runloop_msg_queue);

#ifdef HAVE_THREADS
         _runloop_msg_queue_lock = slock_new();
         retro_assert(_runloop_msg_queue_lock);
#endif
         break;
      case RUNLOOP_CTL_TASK_INIT:
         {
#ifdef HAVE_THREADS
            settings_t *settings = config_get_ptr();
            bool threaded_enable = settings->threaded_data_runloop_enable;
#else
            bool threaded_enable = false;
#endif
            task_queue_ctl(TASK_QUEUE_CTL_DEINIT, NULL);
            task_queue_ctl(TASK_QUEUE_CTL_INIT, &threaded_enable);
         }
         break;
      case RUNLOOP_CTL_SET_CORE_SHUTDOWN:
         runloop_core_shutdown_initiated = true;
         break;
      case RUNLOOP_CTL_UNSET_CORE_SHUTDOWN:
         runloop_core_shutdown_initiated = false;
         break;
      case RUNLOOP_CTL_IS_CORE_SHUTDOWN:
         return runloop_core_shutdown_initiated;
      case RUNLOOP_CTL_SET_SHUTDOWN:
         runloop_shutdown_initiated = true;
         break;
      case RUNLOOP_CTL_UNSET_SHUTDOWN:
         runloop_shutdown_initiated = false;
         break;
      case RUNLOOP_CTL_IS_SHUTDOWN:
         return runloop_shutdown_initiated;
      case RUNLOOP_CTL_SET_EXEC:
         runloop_exec = true;
         break;
      case RUNLOOP_CTL_UNSET_EXEC:
         runloop_exec = false;
         break;
      case RUNLOOP_CTL_IS_EXEC:
         return runloop_exec;
      case RUNLOOP_CTL_DATA_DEINIT:
         task_queue_ctl(TASK_QUEUE_CTL_DEINIT, NULL);
         break;
      case RUNLOOP_CTL_IS_CORE_OPTION_UPDATED:
         if (!runloop_core_options)
            return false;
         return  core_option_manager_updated(runloop_core_options);
      case RUNLOOP_CTL_CORE_OPTION_PREV:
         {
            unsigned *idx = (unsigned*)data;
            if (!idx)
               return false;
            core_option_manager_prev(runloop_core_options, *idx);
            if (ui_companion_is_on_foreground())
               ui_companion_driver_notify_refresh();
         }
         break;
      case RUNLOOP_CTL_CORE_OPTION_NEXT:
         {
            unsigned *idx = (unsigned*)data;
            if (!idx)
               return false;
            core_option_manager_next(runloop_core_options, *idx);
            if (ui_companion_is_on_foreground())
               ui_companion_driver_notify_refresh();
         }
         break;
      case RUNLOOP_CTL_CORE_OPTIONS_GET:
         {
            struct retro_variable *var = (struct retro_variable*)data;

            if (!runloop_core_options || !var)
               return false;

            RARCH_LOG("Environ GET_VARIABLE %s:\n", var->key);
            core_option_manager_get(runloop_core_options, var);
            RARCH_LOG("\t%s\n", var->value ? var->value : 
                  msg_hash_to_str(MENU_ENUM_LABEL_VALUE_NOT_AVAILABLE));
         }
         break;
      case RUNLOOP_CTL_CORE_OPTIONS_INIT:
         {
            char *game_options_path           = NULL;
            bool ret                          = false;
            char buf[PATH_MAX_LENGTH]         = {0};
            global_t *global                  = global_get_ptr();
            settings_t *settings              = config_get_ptr();
            const char *options_path          = settings->path.core_options;
            const struct retro_variable *vars = 
               (const struct retro_variable*)data;

            if (string_is_empty(options_path) 
                  && !string_is_empty(global->path.config))
            {
               fill_pathname_resolve_relative(buf, global->path.config,
                     file_path_str(FILE_PATH_CORE_OPTIONS_CONFIG), sizeof(buf));
               options_path = buf;
            }


            if (settings->game_specific_options)
               ret = rarch_game_specific_options(&game_options_path);

            if(ret)
            {
               runloop_ctl(RUNLOOP_CTL_SET_GAME_OPTIONS_ACTIVE, NULL);
               runloop_core_options = 
                  core_option_manager_new(game_options_path, vars);
               free(game_options_path);
            }
            else
            {
               runloop_ctl(RUNLOOP_CTL_UNSET_GAME_OPTIONS_ACTIVE, NULL);
               runloop_core_options = 
                  core_option_manager_new(options_path, vars);
            }

         }
         break;
      case RUNLOOP_CTL_CORE_OPTIONS_FREE:
         if (runloop_core_options)
            core_option_manager_free(runloop_core_options);
         runloop_core_options          = NULL;
         break;
      case RUNLOOP_CTL_CORE_OPTIONS_DEINIT:
         {
            global_t *global                  = global_get_ptr();
            if (!runloop_core_options)
               return false;

            /* check if game options file was just created and flush
               to that file instead */
            if(global && !string_is_empty(global->path.core_options_path))
            {
               core_option_manager_flush_game_specific(runloop_core_options,
                     global->path.core_options_path);
               global->path.core_options_path[0] = '\0';
            }
            else
               core_option_manager_flush(runloop_core_options);

            if (runloop_ctl(RUNLOOP_CTL_IS_GAME_OPTIONS_ACTIVE, NULL))
               runloop_ctl(RUNLOOP_CTL_UNSET_GAME_OPTIONS_ACTIVE, NULL);

            runloop_ctl(RUNLOOP_CTL_CORE_OPTIONS_FREE, NULL);
         }
         break;
      case RUNLOOP_CTL_KEY_EVENT_GET:
         {
            retro_keyboard_event_t **key_event = 
               (retro_keyboard_event_t**)data;
            if (!key_event)
               return false;
            *key_event = &runloop_key_event;
         }
         break;
      case RUNLOOP_CTL_FRONTEND_KEY_EVENT_GET:
         {
            retro_keyboard_event_t **key_event = 
               (retro_keyboard_event_t**)data;
            if (!key_event)
               return false;
            *key_event = &runloop_frontend_key_event;
         }
         break;
      case RUNLOOP_CTL_HTTPSERVER_INIT:
#if defined(HAVE_HTTPSERVER) && defined(HAVE_ZLIB)
         httpserver_init(8888);
#endif
         break;
      case RUNLOOP_CTL_HTTPSERVER_DESTROY:
#if defined(HAVE_HTTPSERVER) && defined(HAVE_ZLIB)
         httpserver_destroy();
#endif
         break;
      case RUNLOOP_CTL_NONE:
      default:
         break;
   }

   return true;
}
Пример #3
0
int main(int argc, char *argv[])
{
	if (argc != 3) {
		printf("Usage: %s <dfe_ip> <netmask>\n", argv[0]);
		return 1;
	}

	uint16_t Nsockets = 1;
	const int port = 80;
	struct in_addr dfe_ip;
	inet_aton(argv[1], &dfe_ip);
	struct in_addr netmask;
	inet_aton(argv[2], &netmask);

	// initialization files for crcIndex table, generated by init_code
	char fileCrcIndex1[] = "./results/romCrcIndex1_init.html";
	char fileCrcIndex2[] = "./results/romCrcIndex2_init.html";

	// LMEM initialization file location, generated by init_code
	char fileLmem[] = "./results/lmem_generated_file.html";

	uint64_t *arrCrc1;
	uint64_t *arrCrc2;
	long Lcrc;

	FILE *fpCrc1 = fopen(fileCrcIndex1, "rb");
	FILE *fpCrc2 = fopen(fileCrcIndex2, "rb");
	FILE *fpLmem = fopen(fileLmem, "rb");

	if (!(fpCrc1 && fpCrc2 && fpLmem)) {
		printf("Error with file\n");
		exit(0);
	}

	// obtain file size
	fseek(fpCrc1, 0, SEEK_END);
	Lcrc = ftell(fpCrc1);
	rewind(fpCrc1);

	fillRomCrcIndex(fpCrc1, &arrCrc1, Lcrc);
	fillRomCrcIndex(fpCrc2, &arrCrc2, Lcrc);

	printf("Preparing for init() and max_load()\n");
	max_file_t *maxfile = httpserver_init();
	max_engine_t * engine = max_load(maxfile, "*");
	printf("Done\n");

	max_actions_t *actions = max_actions_init(maxfile, NULL);

	int romDepthCrc = Lcrc / 8;
	for (uint32_t i = 0; i < romDepthCrc; i++) {
		max_set_mem_uint64t(actions, "CrcIndexTable", "romCrcIndex1", i, arrCrc1[i]);
		max_set_mem_uint64t(actions, "CrcIndexTable", "romCrcIndex2", i, arrCrc2[i]);
	}

	max_run(engine, actions);
	max_actions_free(actions);

	long L;
	size_t result;
	uint64_t* arrLmem;

	// obtain file size
	fseek(fpLmem, 0, SEEK_END);
	L = ftell(fpLmem);
	rewind(fpLmem);

	double diff = ceil(L / 8.0) - L / 8.0; // NULL character padding

	if (diff != 0) {
		L = (int) ceil(L / 8.0) * 8;
	}

	// allocate memory to contain the whole file
	size_t Nelem = sizeof(uint64_t) * (L / 8);
	arrLmem = (uint64_t*) malloc(Nelem);

	result = fread(arrLmem, 1, L, fpLmem); 

	int romDepth = L / 8;

	int burstLengthInBytes = max_get_burst_size(maxfile, "cmd_tolmem");
	inline int max(int a, int b) {
		return a > b ? a : b;
	}
	;

	const int size = romDepth;
	int sizeBytes = size * sizeof(uint64_t);
	uint64_t *inData; 


	printf("Writing to DFE memory.\n");
	inData = arrLmem;


	writeDataToLMem(inData, size, sizeBytes, burstLengthInBytes, engine, maxfile);
		printf("Done\n");



	max_ip_config(engine, MAX_NET_CONNECTION_QSFP_BOT_10G_PORT1, &dfe_ip, &netmask);

	//all sockets MUST be created before first call to max_tcp_connect or max_tcp_listen
	max_tcp_socket_t *(dfe_socket[Nsockets]);
	uint16_t socketNumber[Nsockets];

	for (int i = 0; i < Nsockets; i++) {
		//dfe_socket[i] = max_tcp_create_socket(engine, "tcp_ISCA_QSFP_BOT_10G_PORT1");
		dfe_socket[i] = max_tcp_create_socket_with_number(engine, "tcp_ISCA_QSFP_BOT_10G_PORT1", i);
		socketNumber[i] = max_tcp_get_socket_number(dfe_socket[i]);
		printf("Socket %d was assigned socket number %u\n", i, socketNumber[i]);
	}

	for (int i = 0; i < Nsockets; i++) {
		max_tcp_listen(dfe_socket[i], port + i);
		max_tcp_await_state(dfe_socket[i], MAX_TCP_STATE_LISTEN, NULL);
	}

	printf("CPU code: Total %u socket(s), listening on the port(s) %u-%u\n\n", Nsockets, port, port + Nsockets - 1);



	void *read_ptr;
	uint8_t *read_buffer;
	max_llstream_t *read_llstream;
	uint64_t *byteNumber;
	printf("CPU code: Setting up 'toCpuByteNumber' stream.\n");
	int Nslots_byteNumber = 512;
	size_t tCBN_buffer_size = Nslots_byteNumber * 16;
	posix_memalign((void *) &read_buffer, 4096, tCBN_buffer_size);
	read_llstream = max_llstream_setup(engine, "toCpuFileSizeBytes", Nslots_byteNumber, 16, read_buffer);




	uint8_t *read_buffer_socket;
	max_llstream_t *read_llstream_socket;
	printf("CPU code: Setting up 'toCpuSocketNumber' stream.\n");
	int Nslots_socketNumber = 512;
	size_t tCSB_buffer_size = Nslots_socketNumber * 16;
	posix_memalign((void *) &read_buffer_socket, 4096, tCSB_buffer_size);
	read_llstream_socket = max_llstream_setup(engine, "toCpuSocketNumber", Nslots_socketNumber, 16, read_buffer_socket);
	void *read_ptr_socket_slot;


	uint16_t ti = 10;
	while(ti > 0)
	{
		printf("CPU code: time=%u, waiting file size and socket numbers stream data to be sent to CPU\n", ti);
		usleep(1000*1000*1);
		ti--;
	}

	//while(1);


	uint64_t num_rx_bytes;
	uint64_t num_tx_bytes;
	uint8_t session_id;
	while (1) {

		//part 1: first wait to receive LengthBytes number

		printf("CPU code: PART 1 - waiting to receive LengthBytes number\n");

		int FoundByteNumber = 0;
		ti=0;
		while (FoundByteNumber != 1) //first wait to receive LengthBytes number
		{
			usleep(1000*1000*1);

			for (int i = 0; i < Nsockets; i++) {
				max_tcp_get_num_bytes_received(dfe_socket[i], &num_rx_bytes);
				max_tcp_get_num_bytes_transmitted(dfe_socket[i], &num_tx_bytes, &session_id);
				printf("CPU code: waiting, time=%u, port=%u, socket=%i, max_tcp_get_num_bytes_received=%llu, max_tcp_get_num_bytes_transmitted=%llu\n", ti, port + i, i, (long long unsigned int) num_rx_bytes, (long long unsigned int) num_tx_bytes);
			}
			ti++;

			uint8_t ii = max_llstream_read(read_llstream, 1, &read_ptr);
			if (ii) {
				byteNumber = (uint64_t*) read_ptr;
				printf("CPU code: number of slots found to contain new data=%u,  fileSizeBytes=%u\n", ii, (unsigned int) *byteNumber);
				max_llstream_read_discard(read_llstream, 1);
				FoundByteNumber = 1;
			}
		}

		//part 2: receive total number of data transfered

		printf("CPU code: PART 2 - receive socket number\n");
		while (max_llstream_read(read_llstream_socket, 1, &read_ptr_socket_slot) == 0)
			;

		
		uint16_t socket_returned = (uint16_t) *((uint16_t*) read_ptr_socket_slot); //event->socketID;
		unsigned int fileBytes = (unsigned int) *byteNumber;

		printf("CPU code: fileBytes=%u, socket_returned=%u\n", fileBytes, socket_returned);

		ti = 0;
		while (1) {
			{
				for (int i = 0; i < Nsockets; i++)
				{
					max_tcp_get_num_bytes_received(dfe_socket[i], &num_rx_bytes);
					max_tcp_get_num_bytes_transmitted(dfe_socket[i], &num_tx_bytes, &session_id);
					printf("CPU code: time=%i, port=%u, socket=%i, max_tcp_get_num_bytes_received=%llu, max_tcp_get_num_bytes_transmitted=%llu\n", ti, port + i, i, (long long unsigned int) num_rx_bytes, (long long unsigned int) num_tx_bytes);
				}
				ti++;
				printf("\n");

				max_tcp_get_num_bytes_transmitted(dfe_socket[socket_returned], &num_tx_bytes, &session_id);
				printf("CPU code: fileSizeBytes=%u, socketReturned=%u, num_tx_bytes=%llu\n", fileBytes, socket_returned, (long long unsigned int) num_tx_bytes);

			}

			//usleep(1000*100);
			//printf("CPU code: While LOOP, socket_returned=%u, fileBytes=%u, num_tx_bytes(max_tcp_get_num_bytes_transmitted)=%llu\n", socket_returned, fileBytes, (long long unsigned int) num_tx_bytes);
			if (num_tx_bytes == fileBytes) {
				//usleep(1000*1000*3);
				printf("CPU code: MATCH num_tx_bytes==fileBytes, socket_returned=%u, fileBytes=%u, num_tx_bytes(max_tcp_get_num_bytes_transmitted)=%llu\n", socket_returned, fileBytes, (long long unsigned int) num_tx_bytes);
				printf("CPU code: Closing socket=%u\n", socket_returned);
				max_tcp_close(dfe_socket[socket_returned]);
					//max_tcp_close_mode_t close_mode=MAX_TCP_CLOSE_ABORT_RESET;
					//max_tcp_close_advanced(dfe_socket[socket_returned],close_mode);

				printf("CPU code: Waiting for MAX_TCP_STATE_CLOSED\n");
				max_tcp_await_state(dfe_socket[socket_returned], MAX_TCP_STATE_CLOSED, NULL);

				printf("CPU code: Set LISTEN state\n");
				max_tcp_listen(dfe_socket[socket_returned], port);

				printf("CPU code: Waiting for MAX_TCP_STATE_LISTEN\n");
				max_tcp_await_state(dfe_socket[socket_returned], MAX_TCP_STATE_LISTEN, NULL);

				printf("CPU code: Again opened socket=%u\n", socket_returned);

				printf("\nCPU code: State of rx/tx after socket closing\n");


				break;
			}

			usleep(1000*1000*1);
		}
	}

	for (int i = 0; i < Nsockets; i++) {
		max_tcp_close(dfe_socket[i]);
		printf("max_tcp_close(dfe_socket[i])");
	}

	max_unload(engine);
	printf("max_unload(engine)");
	max_file_free(maxfile);
	printf("max_file_free(maxfile)");

	printf("The end\n");

	return 0;

}