From bbf07cc6e233b34251f38960c7f339d85e916be5 Mon Sep 17 00:00:00 2001 From: Peter Slattery Date: Thu, 8 Apr 2021 14:33:37 -0400 Subject: [PATCH] Converting to non-blocking socket calls --- .../platform_win32/win32_foldhaus_socket.h | 38 +++++++++++++------ src/app/ss_blumen_lumen/blumen_lumen.cpp | 16 +++++--- .../ss_blumen_lumen/blumen_lumen_settings.h | 2 +- src/app/ss_blumen_lumen/message_queue.cpp | 16 +++++++- 4 files changed, 52 insertions(+), 20 deletions(-) diff --git a/src/app/platform_win32/win32_foldhaus_socket.h b/src/app/platform_win32/win32_foldhaus_socket.h index eb4ffa5..1b449bd 100644 --- a/src/app/platform_win32/win32_foldhaus_socket.h +++ b/src/app/platform_win32/win32_foldhaus_socket.h @@ -156,7 +156,7 @@ Win32ConnectSocket(platform_socket_manager* Manager, platform_socket* Socket) // If iMode == 0, blocking is enabled // if iMode != 0, non-blocking mode is enabled - u_long iMode = 1; + u_long iMode = 0; Error = ioctlsocket(SocketHandle, FIONBIO, &iMode); if (Error != NO_ERROR) { @@ -169,6 +169,7 @@ Win32ConnectSocket(platform_socket_manager* Manager, platform_socket* Socket) u32 Status = WSAGetLastError(); if (Status == WSAEWOULDBLOCK) { + // Non-blocking sockets #if 0 TIMEVAL Timeout = { 0, 500 }; fd_set SocketSet = {}; @@ -244,9 +245,12 @@ Win32SocketPeek(platform_socket_manager* Manager, platform_socket* Socket) char Temp[4]; u32 TempSize = 4; - OutputDebugString("Pre Peek"); - s32 BytesQueued = recv(*Win32Sock, Temp, TempSize, Flags); - OutputDebugString("Post Peek"); + //OutputDebugString("Pre Peek..."); + //s32 BytesQueued = recv(*Win32Sock, Temp, TempSize, Flags); + u_long BytesQueued = 0; + ioctlsocket(*Win32Sock, FIONREAD, &BytesQueued); + //OutputDebugString("Post Peek\n"); + if (BytesQueued != SOCKET_ERROR) { Result = (u32)BytesQueued; @@ -257,6 +261,14 @@ Win32SocketPeek(platform_socket_manager* Manager, platform_socket* Socket) switch (Error) { case WSAEWOULDBLOCK: + { + // NOTE(PS): This case covers non-blocking sockets + // if we peek and there's nothing there, it returns + // this error code. MSDN says its a non-fatal error + // and the operation should be retried later + Result = 0; + } break; + case WSAENOTCONN: case WSAECONNRESET: case WSAECONNABORTED: @@ -267,7 +279,7 @@ Win32SocketPeek(platform_socket_manager* Manager, platform_socket* Socket) InvalidDefaultCase; } } - return Result; + return (s32)Result; } internal gs_data @@ -315,12 +327,20 @@ Win32SocketSend(platform_socket_manager* Manager, platform_socket* Socket, u32 A s32 LengthSent = sendto(*Win32Sock, (char*)Data.Memory, Data.Size, Flags, (sockaddr*)&SockAddress, sizeof(sockaddr_in)); - OutputDebugString("Attempting To Send Network Data: "); + //OutputDebugString("Attempting To Send Network Data: "); if (LengthSent == SOCKET_ERROR) { s32 Error = WSAGetLastError(); switch (Error) { + case WSAEWOULDBLOCK: + { + // NOTE(PS): This covers non-blocking sockets + // In this case the message should be tried again + LengthSent = 0; + //OutputDebugString("Not sent, buffered\n"); + }break; + case WSAECONNABORTED: case WSAENETUNREACH: case WSAECONNRESET: @@ -329,16 +349,12 @@ Win32SocketSend(platform_socket_manager* Manager, platform_socket* Socket, u32 A if (CloseSocket(Manager, Socket)) { Error = 0; + OutputDebugString("Error\n"); } }break; InvalidDefaultCase; } - - if (Error) - { - OutputDebugString("Error\n"); - } } else { diff --git a/src/app/ss_blumen_lumen/blumen_lumen.cpp b/src/app/ss_blumen_lumen/blumen_lumen.cpp index ae89252..e807938 100644 --- a/src/app/ss_blumen_lumen/blumen_lumen.cpp +++ b/src/app/ss_blumen_lumen/blumen_lumen.cpp @@ -160,18 +160,22 @@ BlumenLumen_MicListenJob(gs_thread_context* Ctx, u8* UserData) while (MessageQueue_CanRead(*Data->OutgoingMsgQueue)) { - Msg = MessageQueue_Read(Data->OutgoingMsgQueue); + Msg = MessageQueue_Peek(Data->OutgoingMsgQueue); u32 Address = WeathermanIPV4; u32 Port = WeathermanPort; s32 Flags = 0; - SocketSend(Data->SocketManager, ListenSocket, Address, Port, Msg, Flags); + s32 LengthSent = SocketSend(Data->SocketManager, ListenSocket, Address, Port, Msg, Flags); + if (LengthSent != 0) + { + // if we actually sent the message, THEN we pull it off the + // message queue + MessageQueue_Read(Data->OutgoingMsgQueue); + } else { + break; + } } - - Assert(!MessageQueue_CanRead(*Data->OutgoingMsgQueue)); } - - MessageQueue_Clear(Data->OutgoingMsgQueue); } CloseSocket(Data->SocketManager, ListenSocket); diff --git a/src/app/ss_blumen_lumen/blumen_lumen_settings.h b/src/app/ss_blumen_lumen/blumen_lumen_settings.h index 5ad23dc..3fbefc3 100644 --- a/src/app/ss_blumen_lumen/blumen_lumen_settings.h +++ b/src/app/ss_blumen_lumen/blumen_lumen_settings.h @@ -33,7 +33,7 @@ gs_const_string VoicePatternFolder = ConstString("data/blumen_animations/audio_r // NOTE: There is no need to modify the MotorOpenTimesCount variable - // it is a compile time constant that gets calculated automatically global time_range MotorOpenTimes[] = { - { 08, 45, 17, 45 }, + { 12, 45, 17, 45 }, { 19, 00, 23, 00 }, }; global u32 MotorOpenTimesCount = CArrayLength(MotorOpenTimes); // do not edit diff --git a/src/app/ss_blumen_lumen/message_queue.cpp b/src/app/ss_blumen_lumen/message_queue.cpp index bd783a5..ba974de 100644 --- a/src/app/ss_blumen_lumen/message_queue.cpp +++ b/src/app/ss_blumen_lumen/message_queue.cpp @@ -44,7 +44,7 @@ MessageQueue_CanRead(blumen_network_msg_queue Queue) } internal gs_data -MessageQueue_Read(blumen_network_msg_queue* Queue) +MessageQueue_Peek(blumen_network_msg_queue* Queue) { gs_data Result = {}; u32 ReadIndex = Queue->ReadHead; @@ -53,7 +53,19 @@ MessageQueue_Read(blumen_network_msg_queue* Queue) ReadIndex = 0; } Result = Queue->Buffers[ReadIndex]; - Queue->ReadHead = ReadIndex; + return Result; +} + +internal gs_data +MessageQueue_Read(blumen_network_msg_queue* Queue) +{ + gs_data Result = {}; + u32 ReadIndex = Queue->ReadHead++; + if (ReadIndex >= BLUMEN_MESSAGE_QUEUE_COUNT) + { + Queue->ReadHead = 0; + } + Result = Queue->Buffers[ReadIndex]; return Result; }