From 6f27291b3f6efc69f5923d0c351fa920a5fec8a4 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Wed, 23 Oct 2024 20:18:28 -0400
Subject: [PATCH] voipchat: Fixed strict-aliasing warnings (thanks, @sezero!).
Fixes #93.
---
examples/voipchat.c | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)
diff --git a/examples/voipchat.c b/examples/voipchat.c
index 49e7cb9..22af456 100644
--- a/examples/voipchat.c
+++ b/examples/voipchat.c
@@ -37,7 +37,7 @@ static SDL_AudioDeviceID audio_device = 0;
static SDL_AudioDeviceID capture_device = 0;
static SDL_AudioStream *capture_stream = NULL;
static const SDL_AudioSpec audio_spec = { SDL_AUDIO_S16LE, 1, 8000 };
-static Uint8 scratch_area[4096];
+static Uint64 scratch_area[512];
static Voice *FindVoiceByAddr(const SDLNet_Address *addr, const Uint16 port)
{
@@ -89,10 +89,11 @@ static void ClearOldVoices(const Uint64 now)
static const int extra = (int) (sizeof (Uint64) * 2);
static void SendClientAudioToServer(void)
{
- const int br = SDL_GetAudioStreamData(capture_stream, scratch_area + extra, max_datagram - extra);
+ const int br = SDL_GetAudioStreamData(capture_stream, scratch_area + (extra / sizeof(Uint64)), max_datagram - extra);
if (br > 0) {
- ((Uint64 *) scratch_area)[0] = SDL_Swap64LE(0); /* just being nice and leaving space in the buffer for the server to replace. */
- ((Uint64 *) scratch_area)[1] = SDL_Swap64LE(++next_idnum);
+ next_idnum++;
+ scratch_area[0] = SDL_Swap64LE(0); /* just being nice and leaving space in the buffer for the server to replace. */
+ scratch_area[1] = SDL_Swap64LE(next_idnum);
SDL_Log("CLIENT: Sending %d new bytes to server at %s:%d...", br + extra, SDLNet_GetAddressString(server_addr), (int) server_port);
SDLNet_SendDatagram(sock, server_addr, server_port, scratch_area, br + extra);
}
@@ -225,8 +226,9 @@ static void mainloop(void)
}
if (!last_send_ticks || ((now - last_send_ticks) > 5000)) { /* send a keepalive packet if we haven't transmitted for a bit. */
- ((Uint64 *) scratch_area)[0] = SDL_Swap64LE(0);
- ((Uint64 *) scratch_area)[1] = SDL_Swap64LE(++next_idnum);
+ next_idnum++;
+ scratch_area[0] = SDL_Swap64LE(0);
+ scratch_area[1] = SDL_Swap64LE(next_idnum);
SDL_Log("CLIENT: Sending %d keepalive bytes to server at %s:%d...", extra, SDLNet_GetAddressString(server_addr), (int) server_port);
SDLNet_SendDatagram(sock, server_addr, server_port, scratch_area, extra);
last_send_ticks = now;