Compare commits
No commits in common. "9114ea6691f8442b9ccddeb17274a3d735dcdd63" and "ebc1a4e1ac745041a1bf1842c303491bfbdc3607" have entirely different histories.
9114ea6691
...
ebc1a4e1ac
34
main.c
34
main.c
@ -4,20 +4,16 @@
|
||||
|
||||
#include "texture.h"
|
||||
#include "audio.h"
|
||||
#include "net.h"
|
||||
#include "log.h"
|
||||
|
||||
#define SCREEN_WIDTH 640
|
||||
#define SCREEN_HEIGHT 480
|
||||
|
||||
#define PORT 55555
|
||||
|
||||
typedef enum {
|
||||
SELECTING_DEVICE,
|
||||
RECORDING,
|
||||
RECORDED,
|
||||
PLAYBACK,
|
||||
SOCKET,
|
||||
} application_state;
|
||||
|
||||
typedef struct {
|
||||
@ -49,7 +45,7 @@ int main(int argc, char* argv[])
|
||||
char default_fmt[] = "Device selected: %i";
|
||||
|
||||
state state;
|
||||
state.application_state = SOCKET;
|
||||
state.application_state = SELECTING_DEVICE;
|
||||
if (!audio_init(&state.audio)) return 1;
|
||||
state.device_index = -1;
|
||||
state.update_ui = 0;
|
||||
@ -147,7 +143,7 @@ int handle_input(SDL_Event* event, state* state)
|
||||
if (state->device_index == -1 || !state->audio.recording_device_id) break;
|
||||
audio_buffer_reset(&state->audio);
|
||||
state->audio.recording_buffer_position = 0;
|
||||
audio_device_pause(&state->audio, 1, 0);
|
||||
audio_playback_pause(&state->audio, 1, 0);
|
||||
state->application_state = RECORDING;
|
||||
state->update_ui = 1;
|
||||
break;
|
||||
@ -160,7 +156,7 @@ int handle_input(SDL_Event* event, state* state)
|
||||
if (device_index_new == -1) break;
|
||||
if (device_index_new == state->device_index) break;
|
||||
state->device_index = device_index_new;
|
||||
audio_device_close(&state->audio, 1);
|
||||
SDL_CloseAudioDevice(state->audio.recording_device_id);
|
||||
audio_recording_init(&state->audio, state->device_index);
|
||||
state->update_ui = 1;
|
||||
|
||||
@ -170,7 +166,7 @@ int handle_input(SDL_Event* event, state* state)
|
||||
switch (keysym) {
|
||||
case SDLK_q:
|
||||
case SDLK_ESCAPE:
|
||||
audio_device_pause(&state->audio, 1, 1);
|
||||
audio_playback_pause(&state->audio, 1, 1);
|
||||
state->application_state = SELECTING_DEVICE;
|
||||
state->update_ui = 1;
|
||||
break;
|
||||
@ -184,7 +180,7 @@ int handle_input(SDL_Event* event, state* state)
|
||||
if (!state->audio.playback_device_id)
|
||||
audio_playback_init(&state->audio);
|
||||
state->audio.recording_buffer_position = 0;
|
||||
audio_device_pause(&state->audio, 0, 0);
|
||||
audio_playback_pause(&state->audio, 0, 0);
|
||||
state->application_state = PLAYBACK;
|
||||
state->update_ui = 1;
|
||||
break;
|
||||
@ -200,24 +196,13 @@ int handle_input(SDL_Event* event, state* state)
|
||||
switch (keysym) {
|
||||
case SDLK_q:
|
||||
case SDLK_ESCAPE:
|
||||
audio_device_pause(&state->audio, 0, 1);
|
||||
audio_playback_pause(&state->audio, 0, 1);
|
||||
state->application_state = RECORDED;
|
||||
state->update_ui = 1;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case SOCKET:
|
||||
switch (keysym) {
|
||||
case SDLK_1:
|
||||
log_message(LOG_INFO, "Socket button 1 pressed");
|
||||
net_broadcast();
|
||||
break;
|
||||
case SDLK_2:
|
||||
log_message(LOG_INFO, "Socket button 2 pressed");
|
||||
net_listen_clients();
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@ -246,8 +231,6 @@ void handle_task(state* state)
|
||||
}
|
||||
break;
|
||||
|
||||
case SOCKET:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -278,8 +261,6 @@ int init()
|
||||
return 0;
|
||||
}
|
||||
|
||||
net_init(PORT);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -330,8 +311,5 @@ void application_state_to_string(application_state state, char* string)
|
||||
case PLAYBACK:
|
||||
sprintf(string, "Playing [ESC]: CANCEL");
|
||||
break;
|
||||
case SOCKET:
|
||||
sprintf(string, "Socket");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
153
net.c
153
net.c
@ -1,153 +0,0 @@
|
||||
#include "net.h"
|
||||
#include "log.h"
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
struct sockaddr_in addr;
|
||||
int socket;
|
||||
socklen_t addr_len;
|
||||
} net_info;
|
||||
|
||||
static net_info broadcast_info;
|
||||
static net_info listen_info;
|
||||
static net_info peer_info;
|
||||
static int PORT;
|
||||
|
||||
static int init = 0;
|
||||
|
||||
int net_init(unsigned int port)
|
||||
{
|
||||
PORT = port;
|
||||
// broadcast socket init
|
||||
broadcast_info.socket = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (broadcast_info.socket < 0) {
|
||||
log_message(LOG_ERROR, "Error creating broadcast socket");
|
||||
close(broadcast_info.socket);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int broadcast_enable = 1;
|
||||
if (setsockopt(broadcast_info.socket, SOL_SOCKET, SO_BROADCAST, &broadcast_enable, sizeof(broadcast_enable)) < 0) return 0;
|
||||
|
||||
memset(&broadcast_info.addr, 0, sizeof(broadcast_info.addr));
|
||||
broadcast_info.addr.sin_family = AF_INET;
|
||||
broadcast_info.addr.sin_addr.s_addr = inet_addr("255.255.255.255");
|
||||
broadcast_info.addr.sin_port = htons(port);
|
||||
|
||||
broadcast_info.addr_len = sizeof(broadcast_info.addr);
|
||||
|
||||
// listen socket init
|
||||
listen_info.socket = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (listen_info.socket < 0) {
|
||||
log_message(LOG_ERROR, "Error creating listen socket", strerror(errno));
|
||||
close(listen_info.socket);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int reuse_enable = 1;
|
||||
if (setsockopt(listen_info.socket, SOL_SOCKET, SO_REUSEADDR, &reuse_enable, sizeof(reuse_enable)) < 0) return 0;
|
||||
|
||||
memset(&listen_info.addr, 0, sizeof(listen_info.addr));
|
||||
listen_info.addr.sin_family = AF_INET;
|
||||
listen_info.addr.sin_addr.s_addr = INADDR_BROADCAST;
|
||||
listen_info.addr.sin_port = htons(port);
|
||||
|
||||
listen_info.addr_len =sizeof(listen_info.addr);
|
||||
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 100000;
|
||||
setsockopt(listen_info.socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
||||
|
||||
if (bind(listen_info.socket, (const struct sockaddr*)&listen_info.addr, sizeof(listen_info.addr)) < 0) {
|
||||
log_message(LOG_ERROR, "Socket bind failed: %s", strerror(errno));
|
||||
close(listen_info.socket);
|
||||
return 0;
|
||||
}
|
||||
|
||||
peer_info.socket = -1;
|
||||
|
||||
init = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int net_broadcast()
|
||||
{
|
||||
if (!init) return 0;;
|
||||
const char* message = "broadcast message";
|
||||
int retv = sendto(broadcast_info.socket, message,
|
||||
strlen(message), 0,
|
||||
(struct sockaddr*)&broadcast_info.addr,
|
||||
broadcast_info.addr_len);
|
||||
if (retv < 0) {
|
||||
log_message(LOG_ERROR, "Failed to send broadcast message: %s", strerror(errno));
|
||||
log_message(LOG_ERROR, "retv: %i", retv);
|
||||
return 0;
|
||||
}
|
||||
log_message(LOG_INFO, "Broadcast message sent");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int net_listen_clients()
|
||||
{
|
||||
if (!init) return 0;
|
||||
|
||||
if (peer_info.socket >= 0) {
|
||||
log_message(LOG_WARNING, "Peer socket already created");
|
||||
log_message(LOG_INFO,
|
||||
"Peer socket address:\n%s:%d",
|
||||
inet_ntoa(peer_info.addr.sin_addr),
|
||||
ntohs(peer_info.addr.sin_port));
|
||||
close(peer_info.socket);
|
||||
peer_info.socket = -1;
|
||||
//return 1;
|
||||
}
|
||||
|
||||
peer_info.socket = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (peer_info.socket < 0) {
|
||||
log_message(LOG_ERROR, "Error creating listen socket", strerror(errno));
|
||||
close(peer_info.socket);
|
||||
return 0;
|
||||
}
|
||||
|
||||
char buffer[64];
|
||||
|
||||
memset(&peer_info.addr, 0, sizeof(peer_info.addr));
|
||||
peer_info.addr.sin_family = AF_INET;
|
||||
peer_info.addr.sin_addr.s_addr = INADDR_ANY;
|
||||
peer_info.addr.sin_port = htons(PORT);
|
||||
|
||||
peer_info.addr_len = sizeof(peer_info.addr);
|
||||
|
||||
int retv = recvfrom(listen_info.socket, buffer, sizeof(buffer), 0,
|
||||
(struct sockaddr*)&peer_info.addr,
|
||||
&peer_info.addr_len);
|
||||
|
||||
if (retv < 0) {
|
||||
log_message(LOG_ERROR, "Failed to receive broadcast message: %s", strerror(errno));
|
||||
close(peer_info.socket);
|
||||
peer_info.socket = -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
buffer[retv] = '\0';
|
||||
log_message(LOG_INFO,
|
||||
"Broadcast message received from:\n%s:%d",
|
||||
inet_ntoa(peer_info.addr.sin_addr),
|
||||
ntohs(peer_info.addr.sin_port));
|
||||
log_message(LOG_INFO, "%s", buffer);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int net_send()
|
||||
{
|
||||
if (!init) return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int net_receive()
|
||||
{
|
||||
if (!init) return 0;
|
||||
return 1;
|
||||
}
|
Loading…
Reference in New Issue
Block a user