Implement queue system to display peers broadcasting
This commit is contained in:
@@ -39,6 +39,7 @@ void destroy();
|
||||
int select_device(SDL_KeyboardEvent* key_event, int max_index);
|
||||
void application_state_to_string(application_state state, char* string);
|
||||
|
||||
void* broadcast_t(void* args);
|
||||
void* peer_discovery_t(void* args);
|
||||
|
||||
SDL_Window* window = NULL;
|
||||
@@ -59,8 +60,8 @@ int main(int argc, char* argv[])
|
||||
char device_selected_fmt[] = "Device selected: %i";
|
||||
|
||||
state state;
|
||||
//state.application_state = SOCKET_LISTEN;
|
||||
state.application_state = SELECTING_DEVICE;
|
||||
state.application_state = SOCKET_LISTEN;
|
||||
//state.application_state = SELECTING_DEVICE;
|
||||
if (!audio_init(&state.audio)) return 1;
|
||||
state.device_index = -1;
|
||||
state.update_ui = 0;
|
||||
@@ -90,6 +91,9 @@ int main(int argc, char* argv[])
|
||||
pthread_t peer_discovery_thread;
|
||||
pthread_create(&peer_discovery_thread, NULL, peer_discovery_t, &state);
|
||||
|
||||
pthread_t broadcast_thread;
|
||||
pthread_create(&broadcast_thread, NULL, broadcast_t, &state);
|
||||
|
||||
SDL_Event event;
|
||||
while (1) {
|
||||
sem_wait(&state.sem);
|
||||
@@ -116,6 +120,19 @@ int main(int argc, char* argv[])
|
||||
application_state_to_string(state.application_state, char_buffer);
|
||||
text_texture_load(&state_text_texture, char_buffer);
|
||||
log_message(LOG_INFO, "Update UI State: %s", char_buffer);
|
||||
|
||||
int i = 0;
|
||||
struct sockaddr_in* peer_addr;
|
||||
while (net_get_peers(&peer_addr)) {
|
||||
if (i >= socket_frame.len) break;;
|
||||
char* ip = inet_ntoa(peer_addr->sin_addr);
|
||||
int port = ntohs(peer_addr->sin_port);
|
||||
sprintf(char_buffer, "%s:%i", ip, port);
|
||||
text_texture_load(&socket_frame.textures[i], char_buffer);
|
||||
i++;
|
||||
}
|
||||
for (; i < socket_frame.len; i++) text_texture_load(&socket_frame.textures[i], "");
|
||||
|
||||
state.update_ui = 0;
|
||||
}
|
||||
|
||||
@@ -168,6 +185,7 @@ int handle_input(SDL_Event* event, state* state)
|
||||
break;
|
||||
case SDLK_2:
|
||||
log_message(LOG_INFO, "Socket button 2 pressed");
|
||||
net_print_peers();
|
||||
break;
|
||||
}
|
||||
break;
|
||||
@@ -357,13 +375,22 @@ void application_state_to_string(application_state state, char* string)
|
||||
}
|
||||
}
|
||||
|
||||
void* broadcast_t(void* args)
|
||||
{
|
||||
while (1) {
|
||||
net_broadcast();
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
void* peer_discovery_t(void* args)
|
||||
{
|
||||
state* state = args;
|
||||
while (1) {
|
||||
int added_client = net_listen_peers();
|
||||
int pruned_client = net_prune_peers();
|
||||
sem_wait(&state->sem);
|
||||
net_listen_clients();
|
||||
state->update_ui = added_client || pruned_client;
|
||||
sem_post(&state->sem);
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user