diff --git a/src/app/blumen_lumen.cpp b/src/app/blumen_lumen.cpp index d2e5a5c..f58e410 100644 --- a/src/app/blumen_lumen.cpp +++ b/src/app/blumen_lumen.cpp @@ -21,38 +21,41 @@ BlumenLumen_MicListenJob(gs_thread_context* Ctx, u8* UserData) u32 WeathermanIPV4 = (u32)UpackB4(WeathermanIPAddr); u32 WeathermanPort = 20185; - while (true) + while (*Data->Running) { #if 1 - // TODO(pjs): Removing this block for now - nothing is wrong with it except that SocketPeek is still blocking for some reason - if (SocketPeek(Data->SocketManager, Data->ListenSocket)) + if (SocketQueryStatus(Data->SocketManager, Data->ListenSocket)) { - // TODO(pjs): Make this a peek operation - Msg = SocketRecieve(Data->SocketManager, Data->ListenSocket, Ctx->Transient); - if (Msg.Size > 0) + // TODO(pjs): Removing this block for now - nothing is wrong with it except that SocketPeek is still blocking for some reason + if (SocketPeek(Data->SocketManager, Data->ListenSocket)) { - Data->MicPacketBuffer->Values[Data->MicPacketBuffer->WriteHead++] = Msg; - if (Data->MicPacketBuffer->WriteHead >= PACKETS_MAX) + // TODO(pjs): Make this a peek operation + Msg = SocketRecieve(Data->SocketManager, Data->ListenSocket, Ctx->Transient); + if (Msg.Size > 0) { - Data->MicPacketBuffer->WriteHead = 0; + Data->MicPacketBuffer->Values[Data->MicPacketBuffer->WriteHead++] = Msg; + if (Data->MicPacketBuffer->WriteHead >= PACKETS_MAX) + { + Data->MicPacketBuffer->WriteHead = 0; + } } } - } #endif - - while (Data->OutgoingMsgQueue->ReadHead != Data->OutgoingMsgQueue->WriteHead) - { - u32 ReadIndex = Data->OutgoingMsgQueue->ReadHead++; - if (Data->OutgoingMsgQueue->ReadHead >= BLUMEN_MESSAGE_QUEUE_COUNT) - { - Data->OutgoingMsgQueue->ReadHead = 0; - } - Msg = Data->OutgoingMsgQueue->Buffers[ReadIndex]; - u32 Address = WeathermanIPV4; - u32 Port = WeathermanPort; - s32 Flags = 0; - SocketSend(Data->SocketManager, Data->ListenSocket, Address, Port, Msg, Flags); + while (Data->OutgoingMsgQueue->ReadHead != Data->OutgoingMsgQueue->WriteHead) + { + u32 ReadIndex = Data->OutgoingMsgQueue->ReadHead++; + if (Data->OutgoingMsgQueue->ReadHead >= BLUMEN_MESSAGE_QUEUE_COUNT) + { + Data->OutgoingMsgQueue->ReadHead = 0; + } + + Msg = Data->OutgoingMsgQueue->Buffers[ReadIndex]; + u32 Address = WeathermanIPV4; + u32 Port = WeathermanPort; + s32 Flags = 0; + SocketSend(Data->SocketManager, Data->ListenSocket, Address, Port, Msg, Flags); + } } } @@ -112,6 +115,9 @@ BlumenLumen_CustomInit(app_state* State, context Context) Result = PushSizeToData(&State->Permanent, sizeof(blumen_lumen_state)); blumen_lumen_state* BLState = (blumen_lumen_state*)Result.Memory; + BLState->Running = true; + + BLState->MicListenJobData.Running = &BLState->Running; BLState->MicListenJobData.SocketManager = Context.SocketManager; BLState->MicListenJobData.MicPacketBuffer = &BLState->MicPacketBuffer; BLState->MicListenJobData.OutgoingMsgQueue = &BLState->OutgoingMsgQueue; @@ -208,18 +214,18 @@ BlumenLumen_CustomUpdate(gs_data UserData, app_state* State, context* Context) } } - if (MotorTimeElapsed > OpenClosePeriod) + if (MotorTimeElapsed > 60) { // NOTE(pjs): MotorTimeElapsed = 0; u8 Position = LastPosition; if (LastPosition == 2) { - LastPosition = ClosedValue; + LastPosition = 1; } else { - LastPosition = OpenValue; + LastPosition = 2; } if ((BLState->OutgoingMsgQueue.WriteHead >= BLState->OutgoingMsgQueue.ReadHead) || @@ -253,6 +259,12 @@ BlumenLumen_CustomUpdate(gs_data UserData, app_state* State, context* Context) } } +US_CUSTOM_CLEANUP(BlumenLumen_CustomCleanup) +{ + blumen_lumen_state* BLState = (blumen_lumen_state*)UserData.Memory; + BLState->Running = false; +} + internal user_space_desc BlumenLumen_UserSpaceCreate() { @@ -260,6 +272,7 @@ BlumenLumen_UserSpaceCreate() Result.LoadPatterns = BlumenLumen_LoadPatterns; Result.CustomInit = BlumenLumen_CustomInit; Result.CustomUpdate = BlumenLumen_CustomUpdate; + Result.CustomCleanup = BlumenLumen_CustomCleanup; return Result; } diff --git a/src/app/blumen_lumen.h b/src/app/blumen_lumen.h index 9e1b166..0ea9db5 100644 --- a/src/app/blumen_lumen.h +++ b/src/app/blumen_lumen.h @@ -35,6 +35,8 @@ typedef struct blumen_network_msg_queue // TODO(pjs): Refactor this -> blumen_network_job_state struct mic_listen_job_data { + bool* Running; + platform_socket_manager* SocketManager; packet_ringbuffer* MicPacketBuffer; platform_socket_handle_ ListenSocket; @@ -44,6 +46,8 @@ struct mic_listen_job_data struct blumen_lumen_state { + bool Running; + packet_ringbuffer MicPacketBuffer; blumen_network_msg_queue OutgoingMsgQueue; diff --git a/src/app/engine/user_space.cpp b/src/app/engine/user_space.cpp index 57ceb1c..752435a 100644 --- a/src/app/engine/user_space.cpp +++ b/src/app/engine/user_space.cpp @@ -32,5 +32,15 @@ US_CustomUpdate(user_space_desc* Desc, app_state* State, context* Context) } } +internal void +US_CustomCleanup(user_space_desc* Desc, app_state* State, context Context) +{ + if (Desc->CustomCleanup) + { + Desc->CustomCleanup(Desc->UserData, State, Context); + } +} + + #define USERSPACE_CPP #endif // USERSPACE_CPP \ No newline at end of file diff --git a/src/app/engine/user_space.h b/src/app/engine/user_space.h index 78b5318..8dc5f8a 100644 --- a/src/app/engine/user_space.h +++ b/src/app/engine/user_space.h @@ -14,11 +14,15 @@ typedef US_CUSTOM_INIT(us_custom_init_proc); #define US_CUSTOM_UPDATE(name) void name(gs_data UserData, app_state* State, context* Context) typedef US_CUSTOM_UPDATE(us_custom_update_proc); +#define US_CUSTOM_CLEANUP(name) void name(gs_data UserData, app_state* State, context Context) +typedef US_CUSTOM_CLEANUP(us_custom_cleanup_proc); + typedef struct user_space_desc { us_load_patterns_proc* LoadPatterns; us_custom_init_proc* CustomInit; us_custom_update_proc* CustomUpdate; + us_custom_cleanup_proc* CustomCleanup; gs_data UserData; } user_space_desc; diff --git a/src/app/foldhaus_app.cpp b/src/app/foldhaus_app.cpp index afb9fe2..fe40c48 100644 --- a/src/app/foldhaus_app.cpp +++ b/src/app/foldhaus_app.cpp @@ -113,6 +113,7 @@ UPDATE_AND_RENDER(UpdateAndRender) CLEANUP_APPLICATION(CleanupApplication) { app_state* State = (app_state*)Context.MemoryBase; + US_CustomCleanup(&State->UserSpaceDesc, State, Context); SACN_Cleanup(&State->SACN, Context); } diff --git a/src/app/platform_win32/win32_foldhaus.cpp b/src/app/platform_win32/win32_foldhaus.cpp index c055de0..96a0a57 100644 --- a/src/app/platform_win32/win32_foldhaus.cpp +++ b/src/app/platform_win32/win32_foldhaus.cpp @@ -497,7 +497,7 @@ WinMain ( *Context.ThreadManager = CreatePlatformThreadManager(Win32CreateThread, Win32KillThread); Context.SocketManager = PushStruct(&PlatformPermanent, platform_socket_manager); - *Context.SocketManager = CreatePlatformSocketManager(Win32CreateSocket, Win32CloseSocket, Win32SocketPeek, Win32SocketReceive, Win32SocketSend); + *Context.SocketManager = CreatePlatformSocketManager(Win32CreateSocket, Win32CloseSocket, Win32SocketQueryStatus, Win32SocketPeek, Win32SocketReceive, Win32SocketSend); win32_dll_refresh DLLRefresh = InitializeDLLHotReloading(DLLName, WorkingDLLName, DLLLockFileName); if (!ReloadAndLinkDLL(&DLLRefresh, &Context, &Win32WorkQueue.WorkQueue, true)) { return -1; } @@ -600,10 +600,10 @@ WinMain ( Context.CleanupApplication(Context); - Win32SocketSystem_Cleanup(); - Win32WorkQueue_Cleanup(); - Win32_TestCode_SocketReading_Cleanup(); + //Win32_TestCode_SocketReading_Cleanup(); + + Win32SocketSystem_Cleanup(); return 0; } diff --git a/src/app/platform_win32/win32_foldhaus_socket.h b/src/app/platform_win32/win32_foldhaus_socket.h index bd5b5cd..cbb4303 100644 --- a/src/app/platform_win32/win32_foldhaus_socket.h +++ b/src/app/platform_win32/win32_foldhaus_socket.h @@ -228,6 +228,14 @@ Win32CloseSocket(platform_socket* Socket) return true; } +internal bool +Win32SocketQueryStatus(platform_socket* Socket) +{ + SOCKET* Win32Sock = (SOCKET*)Socket->PlatformHandle; + bool Result = (*Win32Sock != INVALID_SOCKET); + return Result; +} + internal u32 Win32SocketPeek(platform_socket* Socket) { @@ -246,13 +254,14 @@ Win32SocketPeek(platform_socket* Socket) { // TODO(pjs): Error handling s32 Error = WSAGetLastError(); - if (Error == WSAEWOULDBLOCK) + switch (Error) { - // NOTE(pjs): - } - else - { - InvalidCodePath; + case WSAEWOULDBLOCK: + case WSAENOTCONN: + { + }break; + + InvalidDefaultCase; } } return Result; diff --git a/src/app/platform_win32/win32_foldhaus_work_queue.h b/src/app/platform_win32/win32_foldhaus_work_queue.h index 8fa0311..b3a2a36 100644 --- a/src/app/platform_win32/win32_foldhaus_work_queue.h +++ b/src/app/platform_win32/win32_foldhaus_work_queue.h @@ -236,9 +236,15 @@ Win32WorkQueue_Init(gs_memory_arena* Arena, u32 ThreadCount) internal void Win32WorkQueue_Cleanup() { + u32 Error = 0; for (u32 Thread = 0; Thread < Win32WorkQueue.ThreadCount; Thread++) { - TerminateThread(Win32WorkQueue.Threads[Thread].Handle, 0); + u32 Success = TerminateThread(Win32WorkQueue.Threads[Thread].Handle, 0); + if (!Success) + { + Error = GetLastError(); + InvalidCodePath; + } } } diff --git a/src/gs_libs/gs_types.cpp b/src/gs_libs/gs_types.cpp index f51a16d..814b47c 100644 --- a/src/gs_libs/gs_types.cpp +++ b/src/gs_libs/gs_types.cpp @@ -3359,6 +3359,11 @@ CLOSE_SOCKET(PlatformCloseSocket_Stub) return false; } +SOCKET_QUERY_STATUS(PlatformSocketQueryStatus_Stub) +{ + return false; +} + SOCKET_PEEK(PlatformSocketPeek_Stub) { return 0; @@ -3377,6 +3382,7 @@ SOCKET_SEND(PlatformSocketSend_Stub) internal platform_socket_manager CreatePlatformSocketManager(platform_create_socket* CreateSocketProc, platform_close_socket* CloseSocketProc, + platform_socket_query_status* SocketQueryStatusProc, platform_socket_peek* SocketPeekProc, platform_socket_receive* SocketRecieveProc, platform_socket_send* SocketSendProc) @@ -3384,6 +3390,7 @@ CreatePlatformSocketManager(platform_create_socket* CreateSocketProc, platform_socket_manager Result = {}; Result.CreateSocketProc = CreateSocketProc; Result.CloseSocketProc = CloseSocketProc; + Result.SocketQueryStatusProc = SocketQueryStatusProc; Result.SocketPeekProc = SocketPeekProc; Result.SocketRecieveProc = SocketRecieveProc; Result.SocketSendProc = SocketSendProc; @@ -3396,6 +3403,10 @@ CreatePlatformSocketManager(platform_create_socket* CreateSocketProc, { Result.CloseSocketProc = PlatformCloseSocket_Stub; } + if (!SocketQueryStatusProc) + { + Result.SocketQueryStatusProc = PlatformSocketQueryStatus_Stub; + } if (!SocketPeekProc) { Result.SocketPeekProc = PlatformSocketPeek_Stub; @@ -3464,6 +3475,20 @@ CloseSocket(platform_socket_manager* Manager, platform_socket_handle_ Handle) return Result; } +// NOTE(pjs): returns true if the socket is connected +// TODO(pjs): make this more descriptive? +internal bool +SocketQueryStatus(platform_socket_manager* Manager, platform_socket_handle_ SocketHandle) +{ + bool Result = false; + platform_socket* Socket = SocketManagerGetSocket(Manager, SocketHandle); + if (Socket) + { + Result = Manager->SocketQueryStatusProc(Socket); + } + return Result; +} + internal u32 SocketPeek(platform_socket_manager* Manager, platform_socket_handle_ SocketHandle) { diff --git a/src/gs_libs/gs_types.h b/src/gs_libs/gs_types.h index 04a64fb..ea9091d 100644 --- a/src/gs_libs/gs_types.h +++ b/src/gs_libs/gs_types.h @@ -1104,6 +1104,9 @@ typedef CREATE_SOCKET(platform_create_socket); #define CLOSE_SOCKET(name) bool name(platform_socket* Socket) typedef CLOSE_SOCKET(platform_close_socket); +#define SOCKET_QUERY_STATUS(name) bool name(platform_socket* Socket) +typedef SOCKET_QUERY_STATUS(platform_socket_query_status); + #define SOCKET_PEEK(name) u32 name(platform_socket* Socket) typedef SOCKET_PEEK(platform_socket_peek); @@ -1125,6 +1128,7 @@ typedef struct platform_socket_manager platform_create_socket* CreateSocketProc; platform_close_socket* CloseSocketProc; + platform_socket_query_status* SocketQueryStatusProc; platform_socket_peek* SocketPeekProc; platform_socket_receive* SocketRecieveProc; platform_socket_send* SocketSendProc;